From 571e39bc11eeba8f6c1e387fa070415f150ed030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sat, 13 Jul 2024 21:49:21 +0200 Subject: [PATCH] Refactoring the tests --- tests/AbstractEventStoreTestCase.php | 44 ++++++++++++++++++++++++- tests/InMemoryEventStoreTest.php | 43 ++++--------------------- tests/PdoEventStoreTest.php | 48 +++------------------------- tests/ReplyFromPositionQueryTest.php | 3 +- 4 files changed, 55 insertions(+), 83 deletions(-) diff --git a/tests/AbstractEventStoreTestCase.php b/tests/AbstractEventStoreTestCase.php index d88840c..e229538 100644 --- a/tests/AbstractEventStoreTestCase.php +++ b/tests/AbstractEventStoreTestCase.php @@ -8,14 +8,17 @@ use Phauthentic\EventStore\Event; use Phauthentic\EventStore\EventInterface; use Phauthentic\EventStore\EventStoreInterface; +use Phauthentic\EventStore\ReplyFromPositionQuery; use PHPUnit\Framework\TestCase; use Ramsey\Uuid\Uuid; /** * */ -class AbstractEventStoreTestCase extends TestCase +abstract class AbstractEventStoreTestCase extends TestCase { + protected ?EventStoreInterface $eventStore; + /** * @param $numberOfEvents * @return array @@ -58,4 +61,43 @@ public function storeNumberOfEvents( $eventStore->storeEvent($event); } } + + public function testReplyFromPositionZero(): void + { + $aggregateId = Uuid::uuid4()->toString(); + $this->storeNumberOfEvents($this->eventStore, $aggregateId, 2); + + $events = []; + foreach ($this->eventStore->replyFromPosition(new ReplyFromPositionQuery($aggregateId, 1)) as $event) { + $events[] = $event; + } + + $this->assertCount(2, $events); + } + + public function testReplyFromPositionGreaterThanZero(): void + { + $aggregateId = Uuid::uuid4()->toString(); + $this->storeNumberOfEvents($this->eventStore, $aggregateId, 4); + + $events = []; + foreach ($this->eventStore->replyFromPosition(new ReplyFromPositionQuery($aggregateId, 2)) as $event) { + $events[] = $event; + } + + $this->assertCount(3, $events); + } + + public function testReplyFromPositionWithAHigherPositionThanExisting(): void + { + $aggregateId = Uuid::uuid4()->toString(); + $this->storeNumberOfEvents($this->eventStore, $aggregateId, 5); + + $events = []; + foreach ($this->eventStore->replyFromPosition(new ReplyFromPositionQuery($aggregateId, 100000)) as $event) { + $events[] = $event; + } + + $this->assertCount(0, $events); + } } diff --git a/tests/InMemoryEventStoreTest.php b/tests/InMemoryEventStoreTest.php index 6f7ae24..88f72d2 100644 --- a/tests/InMemoryEventStoreTest.php +++ b/tests/InMemoryEventStoreTest.php @@ -4,48 +4,17 @@ namespace Phauthentic\EventStore\Tests; -use Phauthentic\EventStore\Event; use Phauthentic\EventStore\InMemoryEventStore; -use Phauthentic\EventStore\ReplyFromPositionQuery; +/** + * + */ class InMemoryEventStoreTest extends AbstractEventStoreTestCase { - public function testReplyFromPositionZero(): void + public function setUp(): void { - $eventStore = new InMemoryEventStore(); - $this->storeNumberOfEvents($eventStore, '123', 2); + $this->eventStore = new InMemoryEventStore(); - $events = []; - foreach ($eventStore->replyFromPosition(new ReplyFromPositionQuery('123', 1)) as $event) { - $events[] = $event; - } - - $this->assertCount(2, $events); - } - - public function testReplyFromPositionGreaterThanZero(): void - { - $eventStore = new InMemoryEventStore(); - $this->storeNumberOfEvents($eventStore, '123', 4); - - $events = []; - foreach ($eventStore->replyFromPosition(new ReplyFromPositionQuery('123', 2)) as $event) { - $events[] = $event; - } - - $this->assertCount(3, $events); - } - - public function testReplyFromPositionWithAHigherPositionThanExisting(): void - { - $eventStore = new InMemoryEventStore(); - $this->storeNumberOfEvents($eventStore, '123', 1); - - $events = []; - foreach ($eventStore->replyFromPosition(new ReplyFromPositionQuery('123', 10000)) as $event) { - $events[] = $event; - } - - $this->assertCount(0, $events); + parent::setUp(); } } diff --git a/tests/PdoEventStoreTest.php b/tests/PdoEventStoreTest.php index 9cb946e..839bd94 100644 --- a/tests/PdoEventStoreTest.php +++ b/tests/PdoEventStoreTest.php @@ -18,12 +18,14 @@ class PdoEventStoreTest extends AbstractEventStoreTestCase { public function setUp(): void { - parent::setUp(); - $pdo = $this->createPdo(); $query = file_get_contents('./resources/event_store.sql'); $pdo->query('use test'); $pdo->query($query); + + $this->eventStore = $this->createPdoEventStore(); + + parent::setUp(); } protected function createPdo(): PDO @@ -50,46 +52,4 @@ protected function createPdoEventStore(): PdoEventStore eventFactory: new EventFactory() ); } - - public function testReplyFromPositionZero(): void - { - $aggregateId = Uuid::uuid4()->toString(); - $eventStore = $this->createPdoEventStore(); - $this->storeNumberOfEvents($eventStore, $aggregateId, 2); - - $events = []; - foreach ($eventStore->replyFromPosition(new ReplyFromPositionQuery($aggregateId,)) as $event) { - $events[] = $event; - } - - $this->assertCount(2, $events); - } - - public function testReplyFromPositionGreaterThanZero(): void - { - $aggregateId = Uuid::uuid4()->toString(); - $eventStore = $this->createPdoEventStore(); - $this->storeNumberOfEvents($eventStore, $aggregateId, 4); - - $events = []; - foreach ($eventStore->replyFromPosition(new ReplyFromPositionQuery($aggregateId, 2)) as $event) { - $events[] = $event; - } - - $this->assertCount(3, $events); - } - - public function testReplyFromPositionWithAHigherPositionThanExisting(): void - { - $aggregateId = Uuid::uuid4()->toString(); - $eventStore = $this->createPdoEventStore(); - $this->storeNumberOfEvents($eventStore, $aggregateId, 5); - - $events = []; - foreach ($eventStore->replyFromPosition(new ReplyFromPositionQuery($aggregateId, 100000)) as $event) { - $events[] = $event; - } - - $this->assertCount(0, $events); - } } diff --git a/tests/ReplyFromPositionQueryTest.php b/tests/ReplyFromPositionQueryTest.php index 86b0545..0ef4b56 100644 --- a/tests/ReplyFromPositionQueryTest.php +++ b/tests/ReplyFromPositionQueryTest.php @@ -6,11 +6,12 @@ use Phauthentic\EventStore\Exception\EventStoreException; use Phauthentic\EventStore\ReplyFromPositionQuery; +use PHPUnit\Framework\TestCase; /** * */ -class ReplyFromPositionQueryTest extends AbstractEventStoreTestCase +class ReplyFromPositionQueryTest extends TestCase { public function testNegativePosition(): void {