Skip to content

Commit

Permalink
Merge pull request #1 from Phauthentic/develop
Browse files Browse the repository at this point in the history
Fixing a bug in the InMemory store
  • Loading branch information
floriankraemer authored Jul 12, 2024
2 parents f5ebd73 + 268bce3 commit c44ff53
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
/vendor/
/bin/
/.phpunit.cache/
/.phpunit.cache/
6 changes: 4 additions & 2 deletions src/InMemoryEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
29 changes: 10 additions & 19 deletions tests/AbstractEventStoreTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
37 changes: 36 additions & 1 deletion tests/InMemoryEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class InMemoryEventStoreTest extends AbstractEventStoreTestCase
{
public function testStoreEvent()
public function testReplyFromPositionZero(): void
{
$eventStore = new InMemoryEventStore();

Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/PdoEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function createPdoEventStore(): PdoEventStore
);
}

public function testStoreEvent()
public function testReplyFromPositionZero(): void
{
$eventStore = $this->createPdoEventStore();
$domainEvents = $this->getEvents();
Expand Down

0 comments on commit c44ff53

Please sign in to comment.