diff --git a/src/AggregateRoots/FakeAggregateRoot.php b/src/AggregateRoots/FakeAggregateRoot.php index 801262c4..2c058233 100644 --- a/src/AggregateRoots/FakeAggregateRoot.php +++ b/src/AggregateRoots/FakeAggregateRoot.php @@ -56,7 +56,7 @@ public function then(callable $callback): self public function assertNothingRecorded(): self { - Assert::assertCount(0, $this->aggregateRoot->getRecordedEvents()); + Assert::assertCount(0, $this->getRecordedEventsAfterGiven()); return $this; } @@ -85,7 +85,7 @@ public function assertRecorded($expectedEvents): self public function assertNotRecorded($unexpectedEventClasses): self { - $actualEventClasses = array_map(fn (ShouldBeStored $event) => get_class($event), $this->aggregateRoot->getRecordedEvents()); + $actualEventClasses = array_map(fn (ShouldBeStored $event) => get_class($event), $this->getRecordedEventsAfterGiven()); $unexpectedEventClasses = Arr::wrap($unexpectedEventClasses); @@ -173,6 +173,11 @@ private function getRecordedEventsWithoutUuid(): array unset($metaData[MetaData::AGGREGATE_ROOT_VERSION]); return $eventWithoutUuid->setMetaData($metaData); - }, array_slice($this->aggregateRoot->getRecordedEvents(), $this->givenEventsCount)); + }, $this->getRecordedEventsAfterGiven()); + } + + private function getRecordedEventsAfterGiven(): array + { + return array_slice($this->aggregateRoot->getRecordedEvents(), $this->givenEventsCount); } } diff --git a/tests/FakeAggregateRootTest.php b/tests/FakeAggregateRootTest.php index abc980e5..55aac650 100644 --- a/tests/FakeAggregateRootTest.php +++ b/tests/FakeAggregateRootTest.php @@ -13,6 +13,33 @@ public function it_can_determine_that_no_events_were_recorded() DummyAggregateRoot::fake()->assertNothingRecorded(); } + /** @test */ + public function it_ignores_given_events_in_assert_nothing_recorded() + { + DummyAggregateRoot::fake() + ->given([new DummyEvent(123)]) + ->assertNothingRecorded(); + } + + /** @test */ + public function it_ignores_given_events_in_assert_recorded() + { + DummyAggregateRoot::fake() + ->given([new DummyEvent(1)]) + ->when(function (DummyAggregateRoot $dummyAggregateRoot) { + $dummyAggregateRoot->dummy(); + }) + ->assertRecorded([new DummyEvent(2)]); + } + + /** @test */ + public function it_ignores_given_events_in_assert_not_recorded() + { + DummyAggregateRoot::fake() + ->given([new DummyEvent(123)]) + ->assertNotRecorded(DummyEvent::class); + } + /** @test */ public function it_can_retrieve_an_aggregate_for_a_given_uuid() {