Skip to content

Commit

Permalink
Allow for AggregateRoot to record multiple events
Browse files Browse the repository at this point in the history
  • Loading branch information
simensen committed Sep 15, 2023
1 parent a4cc61a commit 8c187ad
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/Testing/GivenWhenThen/Scenario.php
Expand Up @@ -79,10 +79,10 @@ public function when(object $command): self
return $instance;
}

public function then(object $event): self
public function then(object ...$event): self
{
$instance = clone $this;
$instance->outcome = new ThenEvent($event);
$instance->outcome = count($event) > 1 ? new ThenEvents(...$event) : new ThenEvent($event[0]);

return $instance;
}
Expand Down Expand Up @@ -220,6 +220,35 @@ public function assert(): self
$this->assertObjectSurvivesSerializationRoundTrip($recordedEvent);
}

if ($instance->outcome instanceof ThenEvents) {
$expectedEvents = array_values($instance->outcome->events);

Assert::assertGreaterThan(
1,
count($expectedEvents),
'Should expect at least two events.'
);

Assert::assertCount(
count($expectedEvents),
$recordedEvents,
sprintf('Expected exactly %d events.', count($expectedEvents))
);

while (count($recordedEvents) > 0) {
$recordedEvent = array_shift($recordedEvents);
$expectedEvent = array_shift($expectedEvents);

if ($expectedEvent instanceof TestObject) {
$expectedEvent = $expectedEvent->build();
}

Assert::assertEquals($expectedEvent, $recordedEvent);

$this->assertObjectSurvivesSerializationRoundTrip($recordedEvent);
}
}

$this->assertObjectSurvivesSerializationRoundTrip($instance->when->command);

return $instance;
Expand Down
16 changes: 16 additions & 0 deletions src/Testing/GivenWhenThen/ThenEvents.php
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Dflydev\EventSauce\Support\Testing\GivenWhenThen;

final readonly class ThenEvents implements Then
{
/** @var array<object> */
public array $events;

public function __construct(object ...$events)
{
$this->events = $events;
}
}

0 comments on commit 8c187ad

Please sign in to comment.