Skip to content

Commit

Permalink
Merge pull request #479 from patchlevel/fix-dbal-mock
Browse files Browse the repository at this point in the history
Fix tests for dbal 3.8.x
  • Loading branch information
DavidBadura authored Feb 1, 2024
2 parents c002cf7 + c1b3e2f commit 170d703
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 20 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"doctrine/dbal": "^3.4.2",
"doctrine/dbal": "^3.8.0",
"psr/cache": "^2.0.0|^3.0.0",
"psr/clock": "^1.0",
"psr/log": "^2.0.0|^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 29 additions & 11 deletions tests/Unit/Store/MultiTableStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Query\SelectQuery;
use Doctrine\DBAL\SQL\Builder\SelectSQLBuilder;
use Doctrine\DBAL\Statement;
use Doctrine\DBAL\Types\Types;
use Patchlevel\EventSourcing\EventBus\Message;
Expand All @@ -34,8 +36,6 @@ final class MultiTableStoreTest extends TestCase

public function testLoadWithNoEvents(): void
{
$queryBuilder = new QueryBuilder($this->prophesize(Connection::class)->reveal());

$connection = $this->prophesize(Connection::class);
$connection->fetchAllAssociative(
'SELECT * FROM profile WHERE (aggregate_id = :id) AND (playhead > :playhead) AND (archived = :archived)',
Expand All @@ -48,26 +48,35 @@ public function testLoadWithNoEvents(): void
'archived' => Types::BOOLEAN,
],
)->willReturn([]);

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

$selectSqlBuilder->buildSQL(Argument::type(SelectQuery::class))
->willReturn('SELECT * FROM profile WHERE (aggregate_id = :id) AND (playhead > :playhead) AND (archived = :archived)');

$abstractPlatform->createSelectSQLBuilder()
->willReturn($selectSqlBuilder->reveal());

$connection->getDatabasePlatform()->willReturn($abstractPlatform->reveal());
$queryBuilder = new QueryBuilder($connection->reveal());
$connection->createQueryBuilder()->willReturn($queryBuilder);
$connection->getDatabasePlatform()->willReturn($this->prophesize(AbstractPlatform::class)->reveal());

$serializer = $this->prophesize(EventSerializer::class);

$singleTableStore = new MultiTableStore(
$multiTableStore = new MultiTableStore(
$connection->reveal(),
$serializer->reveal(),
new AggregateRootRegistry(['profile' => Profile::class]),
'eventstore',
);

$events = $singleTableStore->load(Profile::class, '1');
$events = $multiTableStore->load(Profile::class, '1');
self::assertCount(0, $events);
}

public function testLoadWithOneEvent(): void
{
$queryBuilder = new QueryBuilder($this->prophesize(Connection::class)->reveal());

$connection = $this->prophesize(Connection::class);
$connection->fetchAllAssociative(
'SELECT * FROM profile WHERE (aggregate_id = :id) AND (playhead > :playhead) AND (archived = :archived)',
Expand All @@ -92,25 +101,34 @@ public function testLoadWithOneEvent(): void
],
);

$connection->createQueryBuilder()->willReturn($queryBuilder);

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

$selectSqlBuilder->buildSQL(Argument::type(SelectQuery::class))
->willReturn('SELECT * FROM profile WHERE (aggregate_id = :id) AND (playhead > :playhead) AND (archived = :archived)');

$abstractPlatform->createSelectSQLBuilder()
->willReturn($selectSqlBuilder->reveal());

$abstractPlatform->getDateTimeTzFormatString()->willReturn('Y-m-d H:i:s');

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

$serializer = $this->prophesize(EventSerializer::class);
$serializer->deserialize(
new SerializedEvent('profile.created', '{"profileId": "1", "email": "s"}'),
)->willReturn(new ProfileCreated(ProfileId::fromString('1'), Email::fromString('s')));

$singleTableStore = new MultiTableStore(
$multiTableStore = new MultiTableStore(
$connection->reveal(),
$serializer->reveal(),
new AggregateRootRegistry(['profile' => Profile::class]),
'eventstore',
);

$messages = $singleTableStore->load(Profile::class, '1');
$messages = $multiTableStore->load(Profile::class, '1');
self::assertCount(1, $messages);

$message = $messages[0];
Expand Down
32 changes: 25 additions & 7 deletions tests/Unit/Store/SingleTableStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Query\SelectQuery;
use Doctrine\DBAL\SQL\Builder\SelectSQLBuilder;
use Doctrine\DBAL\Statement;
use Doctrine\DBAL\Types\Types;
use Patchlevel\EventSourcing\EventBus\Message;
Expand All @@ -34,8 +36,6 @@ final class SingleTableStoreTest extends TestCase

public function testLoadWithNoEvents(): void
{
$queryBuilder = new QueryBuilder($this->prophesize(Connection::class)->reveal());

$connection = $this->prophesize(Connection::class);
$connection->fetchAllAssociative(
'SELECT * FROM eventstore WHERE (aggregate = :aggregate) AND (aggregate_id = :id) AND (playhead > :playhead) AND (archived = :archived)',
Expand All @@ -49,8 +49,19 @@ public function testLoadWithNoEvents(): void
'archived' => Types::BOOLEAN,
],
)->willReturn([]);

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

$selectSqlBuilder->buildSQL(Argument::type(SelectQuery::class))
->willReturn('SELECT * FROM eventstore WHERE (aggregate = :aggregate) AND (aggregate_id = :id) AND (playhead > :playhead) AND (archived = :archived)');

$abstractPlatform->createSelectSQLBuilder()
->willReturn($selectSqlBuilder->reveal());

$connection->getDatabasePlatform()->willReturn($abstractPlatform->reveal());
$queryBuilder = new QueryBuilder($connection->reveal());
$connection->createQueryBuilder()->willReturn($queryBuilder);
$connection->getDatabasePlatform()->willReturn($this->prophesize(AbstractPlatform::class)->reveal());

$serializer = $this->prophesize(EventSerializer::class);

Expand All @@ -67,8 +78,6 @@ public function testLoadWithNoEvents(): void

public function testLoadWithOneEvent(): void
{
$queryBuilder = new QueryBuilder($this->prophesize(Connection::class)->reveal());

$connection = $this->prophesize(Connection::class);
$connection->fetchAllAssociative(
'SELECT * FROM eventstore WHERE (aggregate = :aggregate) AND (aggregate_id = :id) AND (playhead > :playhead) AND (archived = :archived)',
Expand All @@ -94,11 +103,20 @@ public function testLoadWithOneEvent(): void
],
);

$connection->createQueryBuilder()->willReturn($queryBuilder);

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

$selectSqlBuilder->buildSQL(Argument::type(SelectQuery::class))
->willReturn('SELECT * FROM eventstore WHERE (aggregate = :aggregate) AND (aggregate_id = :id) AND (playhead > :playhead) AND (archived = :archived)');

$abstractPlatform->createSelectSQLBuilder()
->willReturn($selectSqlBuilder->reveal());

$abstractPlatform->getDateTimeTzFormatString()->willReturn('Y-m-d H:i:s');

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

$serializer = $this->prophesize(EventSerializer::class);
$serializer->deserialize(
Expand Down

0 comments on commit 170d703

Please sign in to comment.