Skip to content

Commit

Permalink
Refactoring the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankraemer committed Jul 13, 2024
1 parent 8b3f244 commit 571e39b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 83 deletions.
44 changes: 43 additions & 1 deletion tests/AbstractEventStoreTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
43 changes: 6 additions & 37 deletions tests/InMemoryEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
48 changes: 4 additions & 44 deletions tests/PdoEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion tests/ReplyFromPositionQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 571e39b

Please sign in to comment.