From 268bce346cdd84d84788cfca6bdb734671134b13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Fri, 12 Jul 2024 22:17:33 +0200 Subject: [PATCH] Fixing a bug in the InMemory store --- .gitignore | 3 ++- src/InMemoryEventStore.php | 6 +++-- tests/AbstractEventStoreTestCase.php | 29 ++++++++-------------- tests/InMemoryEventStoreTest.php | 37 +++++++++++++++++++++++++++- tests/PdoEventStoreTest.php | 2 +- 5 files changed, 53 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index dc99a83..531d420 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea /vendor/ /bin/ -/.phpunit.cache/ \ No newline at end of file +/.phpunit.cache/ diff --git a/src/InMemoryEventStore.php b/src/InMemoryEventStore.php index 5f9f126..225c045 100644 --- a/src/InMemoryEventStore.php +++ b/src/InMemoryEventStore.php @@ -30,8 +30,10 @@ public function replyFromPosition(string $aggregateId, int $position = 0): Itera return new EmptyIterator(); } - foreach ($this->aggregates[$aggregateId] as $event) { - yield $event; + foreach ($this->aggregates[$aggregateId] as $storePosition => $event) { + if ($storePosition > $position) { + yield $event; + } } } } diff --git a/tests/AbstractEventStoreTestCase.php b/tests/AbstractEventStoreTestCase.php index 218cded..4ef84bb 100644 --- a/tests/AbstractEventStoreTestCase.php +++ b/tests/AbstractEventStoreTestCase.php @@ -15,33 +15,24 @@ */ class AbstractEventStoreTestCase extends TestCase { - public function getEvents(): array + public function getEvents($numberOfEvents = 2): array { $uuid = Uuid::uuid4()->toString(); - return [ - new Event( - stream: 'test-stream', + for ($i = 1; $i <= $numberOfEvents; $i++) { + $events[] = new Event( aggregateId: $uuid, - aggregateVersion: 1, + aggregateVersion: $i, event: 'created', payload: '', createdAt: DateTimeImmutable::createFromFormat( EventInterface::CREATED_AT_FORMAT, '2023-12-12 12:00:00 12351' - ) - ), - new Event( - stream: 'test-stream', - aggregateId: $uuid, - aggregateVersion: 2, - event: 'edited', - payload: '', - createdAt: DateTimeImmutable::createFromFormat( - EventInterface::CREATED_AT_FORMAT, - '2023-12-12 12:00:01 12351' - ) - ) - ]; + ), + stream: 'test-stream' + ); + } + + return $events; } } diff --git a/tests/InMemoryEventStoreTest.php b/tests/InMemoryEventStoreTest.php index c97fac3..c163236 100644 --- a/tests/InMemoryEventStoreTest.php +++ b/tests/InMemoryEventStoreTest.php @@ -9,7 +9,7 @@ class InMemoryEventStoreTest extends AbstractEventStoreTestCase { - public function testStoreEvent() + public function testReplyFromPositionZero(): void { $eventStore = new InMemoryEventStore(); @@ -25,4 +25,39 @@ public function testStoreEvent() $this->assertCount(2, $events); } + + public function testReplyFromPositionGreaterThanZero(): void + { + $eventStore = new InMemoryEventStore(); + + $domainEvents = $this->getEvents(4); + + $eventStore->storeEvent($domainEvents[0]); + $eventStore->storeEvent($domainEvents[1]); + $eventStore->storeEvent($domainEvents[2]); + $eventStore->storeEvent($domainEvents[3]); + + $events = []; + foreach ($eventStore->replyFromPosition($domainEvents[0]->getAggregateId(), 2) as $event) { + $events[] = $event; + } + + $this->assertCount(2, $events); + } + + public function testReplyFromPositionWithAHigherPositionThanExisting(): void + { + $eventStore = new InMemoryEventStore(); + + $domainEvents = $this->getEvents(4); + + $eventStore->storeEvent($domainEvents[0]); + + $events = []; + foreach ($eventStore->replyFromPosition($domainEvents[0]->getAggregateId(), 1000) as $event) { + $events[] = $event; + } + + $this->assertCount(0, $events); + } } diff --git a/tests/PdoEventStoreTest.php b/tests/PdoEventStoreTest.php index 9558cc5..bb027ee 100644 --- a/tests/PdoEventStoreTest.php +++ b/tests/PdoEventStoreTest.php @@ -46,7 +46,7 @@ protected function createPdoEventStore(): PdoEventStore ); } - public function testStoreEvent() + public function testReplyFromPositionZero(): void { $eventStore = $this->createPdoEventStore(); $domainEvents = $this->getEvents();