Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Jul 26, 2024
1 parent 214cbdc commit d2f82b8
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions tests/Unit/Store/StreamDoctrineDbalStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Patchlevel\EventSourcing\Serializer\EventSerializer;
use Patchlevel\EventSourcing\Serializer\SerializedEvent;
use Patchlevel\EventSourcing\Store\Criteria\CriteriaBuilder;
use Patchlevel\EventSourcing\Store\InvalidStreamName;
use Patchlevel\EventSourcing\Store\MissingDataForStorage;
use Patchlevel\EventSourcing\Store\StreamDoctrineDbalStore;
use Patchlevel\EventSourcing\Store\StreamHeader;
Expand Down Expand Up @@ -253,6 +254,134 @@ public function testLoadWithIndex(): void
self::assertSame(null, $stream->position());
}

public function testLoadWithLike(): void
{
$connection = $this->prophesize(Connection::class);
$result = $this->prophesize(Result::class);
$result->iterateAssociative()->willReturn(new EmptyIterator());

$connection->executeQuery(
'SELECT * FROM event_store WHERE (stream LIKE :stream) AND (playhead > :playhead) AND (archived = :archived) ORDER BY id ASC',
[
'stream' => 'profile-%',
'playhead' => 0,
'archived' => false,
],
Argument::type('array'),
)->willReturn($result->reveal());

$abstractPlatform = $this->prophesize(AbstractPlatform::class);
$abstractPlatform->createSelectSQLBuilder()->shouldBeCalledOnce()->willReturn(new DefaultSelectSQLBuilder(
$abstractPlatform->reveal(),
'FOR UPDATE',
'SKIP LOCKED',
));

$connection->getDatabasePlatform()->willReturn($abstractPlatform->reveal());
$queryBuilder = new QueryBuilder($connection->reveal());
$connection->createQueryBuilder()->willReturn($queryBuilder);

$eventSerializer = $this->prophesize(EventSerializer::class);
$headersSerializer = $this->prophesize(HeadersSerializer::class);

$doctrineDbalStore = new StreamDoctrineDbalStore(
$connection->reveal(),
$eventSerializer->reveal(),
$headersSerializer->reveal(),
);

$stream = $doctrineDbalStore->load(
(new CriteriaBuilder())
->streamName('profile-*')
->fromPlayhead(0)
->archived(false)
->build(),
);

self::assertSame(null, $stream->index());
self::assertSame(null, $stream->position());
}

public function testLoadWithLikeAll(): void
{
$connection = $this->prophesize(Connection::class);
$result = $this->prophesize(Result::class);
$result->iterateAssociative()->willReturn(new EmptyIterator());

$connection->executeQuery(
'SELECT * FROM event_store WHERE (playhead > :playhead) AND (archived = :archived) ORDER BY id ASC',
[
'playhead' => 0,
'archived' => false,
],
Argument::type('array'),
)->willReturn($result->reveal());

$abstractPlatform = $this->prophesize(AbstractPlatform::class);
$abstractPlatform->createSelectSQLBuilder()->shouldBeCalledOnce()->willReturn(new DefaultSelectSQLBuilder(
$abstractPlatform->reveal(),
'FOR UPDATE',
'SKIP LOCKED',
));

$connection->getDatabasePlatform()->willReturn($abstractPlatform->reveal());
$queryBuilder = new QueryBuilder($connection->reveal());
$connection->createQueryBuilder()->willReturn($queryBuilder);

$eventSerializer = $this->prophesize(EventSerializer::class);
$headersSerializer = $this->prophesize(HeadersSerializer::class);

$doctrineDbalStore = new StreamDoctrineDbalStore(
$connection->reveal(),
$eventSerializer->reveal(),
$headersSerializer->reveal(),
);

$stream = $doctrineDbalStore->load(
(new CriteriaBuilder())
->streamName('*')
->fromPlayhead(0)
->archived(false)
->build(),
);

self::assertSame(null, $stream->index());
self::assertSame(null, $stream->position());
}

public function testLoadWithLikeInvalid(): void
{
$connection = $this->prophesize(Connection::class);

$abstractPlatform = $this->prophesize(AbstractPlatform::class);

$connection->getDatabasePlatform()->willReturn($abstractPlatform->reveal());
$queryBuilder = new QueryBuilder($connection->reveal());
$connection->createQueryBuilder()->willReturn($queryBuilder);

$eventSerializer = $this->prophesize(EventSerializer::class);
$headersSerializer = $this->prophesize(HeadersSerializer::class);

$doctrineDbalStore = new StreamDoctrineDbalStore(
$connection->reveal(),
$eventSerializer->reveal(),
$headersSerializer->reveal(),
);

$this->expectException(InvalidStreamName::class);

$stream = $doctrineDbalStore->load(
(new CriteriaBuilder())
->streamName('*-*')
->fromPlayhead(0)
->archived(false)
->build(),
);

self::assertSame(null, $stream->index());
self::assertSame(null, $stream->position());
}

public function testLoadWithOneEvent(): void
{
$connection = $this->prophesize(Connection::class);
Expand Down

0 comments on commit d2f82b8

Please # to comment.