Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/EventSourcedAggregateRootRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace EventSauce\EventSourcing;

use Generator;
use Ramsey\Uuid\Uuid;
use Throwable;

use function assert;
Expand Down Expand Up @@ -98,7 +99,10 @@ public function persistEvents(AggregateRootId $aggregateRootId, int $aggregateRo
$messages = array_map(function (object $event) use ($metadata, &$aggregateRootVersion) {
return $this->decorator->decorate(new Message(
$event,
$metadata + [Header::AGGREGATE_ROOT_VERSION => ++$aggregateRootVersion]
$metadata + [
Header::AGGREGATE_ROOT_VERSION => ++$aggregateRootVersion,
Header::EVENT_ID => Uuid::uuid4()->toString(),
],
));
}, $events);

Expand Down
23 changes: 23 additions & 0 deletions src/EventSourcedAggregateRootRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use EventSauce\EventSourcing\TestUtilities\TestingAggregates\DummyAggregate;
use PHPUnit\Framework\TestCase;

use Ramsey\Uuid\Uuid;
use function iterator_to_array;

class EventSourcedAggregateRootRepositoryTest extends TestCase
Expand Down Expand Up @@ -56,4 +57,26 @@ public function aggregate_types_are_added_to_messages(): void
self::assertEquals($expectedAggregateRootType, $messages[1]->aggregateRootType());
self::assertEquals($expectedAggregateRootType, $messages[2]->aggregateRootType());
}

/**
* @test
*/
public function event_ids_are_added_to_messages(): void
{
$messageRepository = new InMemoryMessageRepository();
$repository = new EventSourcedAggregateRootRepository(DummyAggregate::class, $messageRepository);
/** @var DummyAggregate $aggregate */
$aggregateRootId = DummyAggregateRootId::generate();
$aggregate = $repository->retrieve($aggregateRootId);
$aggregate->increment();
$aggregate->increment();
$aggregate->increment();
$repository->persist($aggregate);

/** @var Message[] $messages */
$messages = iterator_to_array($messageRepository->retrieveAll($aggregateRootId));
self::assertTrue(Uuid::isValid($messages[0]->header(Header::EVENT_ID)));
self::assertTrue(Uuid::isValid($messages[1]->header(Header::EVENT_ID)));
self::assertTrue(Uuid::isValid($messages[2]->header(Header::EVENT_ID)));
}
}