Skip to content

Commit

Permalink
Merge pull request #344 from itsmarsu/ignore-given-events-in-assert-n…
Browse files Browse the repository at this point in the history
…othing-recorded

Do not assert events given to FakeAggregateRoot as recorded
  • Loading branch information
freekmurze authored May 27, 2022
2 parents a052bdb + f89a061 commit 1e1f48d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/AggregateRoots/FakeAggregateRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
}
27 changes: 27 additions & 0 deletions tests/FakeAggregateRootTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 1e1f48d

Please sign in to comment.