Skip to content

Commit

Permalink
Merge pull request #559 from patchlevel/add-dbal-tests
Browse files Browse the repository at this point in the history
Add more DoctrineDbalStore tests
  • Loading branch information
DanielBadura authored Mar 28, 2024
2 parents 1fd21cb + 300b010 commit 2eb51a6
Show file tree
Hide file tree
Showing 14 changed files with 978 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mutation-tests-diff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
dependency-versions: ${{ matrix.dependencies }}

- name: "Infection"
run: "vendor/bin/roave-infection-static-analysis-plugin --git-diff-lines --git-diff-base=origin/$GITHUB_BASE_REF --ignore-msi-with-no-mutations --only-covered --min-msi=80 --min-covered-msi=95"
run: "vendor/bin/roave-infection-static-analysis-plugin --threads=max --git-diff-lines --git-diff-base=origin/$GITHUB_BASE_REF --ignore-msi-with-no-mutations --only-covered --min-msi=80 --min-covered-msi=95"
2 changes: 1 addition & 1 deletion .github/workflows/mutation-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ jobs:
dependency-versions: ${{ matrix.dependencies }}

- name: "Infection"
run: "vendor/bin/roave-infection-static-analysis-plugin"
run: "vendor/bin/roave-infection-static-analysis-plugin --threads=max"
env:
STRYKER_DASHBOARD_API_KEY: ${{ secrets.STRYKER_DASHBOARD_API_KEY }}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ phpunit-unit: vendor ## run phpu

.PHONY: infection
infection: vendor ## run infection
vendor/bin/infection
php -d memory_limit=312M vendor/bin/roave-infection-static-analysis-plugin --threads=max

.PHONY: deptrac
deptrac: vendor-tools ## run deptrac
Expand Down
15 changes: 15 additions & 0 deletions baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,21 @@
<code><![CDATA[ProfileVisited&ProfileCreated $event]]></code>
</ReservedWord>
</file>
<file src="tests/Unit/Store/DoctrineDbalStoreTest.php">
<DeprecatedMethod>
<code><![CDATA[addMethods]]></code>
</DeprecatedMethod>
<InternalMethod>
<code><![CDATA[new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')]]></code>
<code><![CDATA[new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')]]></code>
<code><![CDATA[new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')]]></code>
<code><![CDATA[new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')]]></code>
<code><![CDATA[new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')]]></code>
<code><![CDATA[new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')]]></code>
<code><![CDATA[new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')]]></code>
<code><![CDATA[new DefaultSelectSQLBuilder($abstractPlatform->reveal(), 'FOR UPDATE', 'SKIP LOCKED')]]></code>
</InternalMethod>
</file>
<file src="tests/Unit/Subscription/Engine/DefaultSubscriptionEngineTest.php">
<PossiblyUndefinedArrayOffset>
<code><![CDATA[$update1]]></code>
Expand Down
6 changes: 3 additions & 3 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"logs": {
"text": "infection.log",
"stryker": {
"report": "2.0.x"
"report": "3.0.x"
}
},
"mutators": {
"@default": true
},
"minMsi": 55,
"minCoveredMsi": 90,
"minMsi": 62,
"minCoveredMsi": 92,
"testFrameworkOptions": "--testsuite=unit"
}
47 changes: 25 additions & 22 deletions src/Store/DoctrineDbalStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,7 @@ function (Connection $connection) use ($messages): void {
continue;
}

$query = sprintf(
"INSERT INTO %s (%s) VALUES\n(%s)",
$this->storeTableName,
implode(', ', $columns),
implode("),\n(", $placeholders),
);

$connection->executeStatement($query, $parameters, $types);
$this->executeSave($columns, $placeholders, $parameters, $types, $connection);

$parameters = [];
$placeholders = [];
Expand All @@ -234,18 +227,7 @@ function (Connection $connection) use ($messages): void {
return;
}

$query = sprintf(
"INSERT INTO %s (%s) VALUES\n(%s)",
$this->storeTableName,
implode(', ', $columns),
implode("),\n(", $placeholders),
);

try {
$connection->executeStatement($query, $parameters, $types);
} catch (UniqueConstraintViolationException $e) {
throw new UniqueConstraintViolation($e);
}
$this->executeSave($columns, $placeholders, $parameters, $types, $connection);
},
);
}
Expand Down Expand Up @@ -288,8 +270,7 @@ public function configureSchema(Schema $schema, Connection $connection): void
$table = $schema->createTable($this->storeTableName);

$table->addColumn('id', Types::BIGINT)
->setAutoincrement(true)
->setNotnull(true);
->setAutoincrement(true);
$table->addColumn('aggregate', Types::STRING)
->setLength(255)
->setNotnull(true);
Expand Down Expand Up @@ -390,4 +371,26 @@ private function createTriggerFunctionName(): string

return sprintf('%1$s.notify_%2$s', $tableConfig[0], $tableConfig[1]);
}

/**
* @param array<string> $columns
* @param array<string> $placeholders
* @param list<mixed> $parameters
* @param array<0|positive-int, Type> $types
*/
private function executeSave(array $columns, array $placeholders, array $parameters, array $types, Connection $connection): void
{
$query = sprintf(
"INSERT INTO %s (%s) VALUES\n(%s)",
$this->storeTableName,
implode(', ', $columns),
implode("),\n(", $placeholders),
);

try {
$connection->executeStatement($query, $parameters, $types);
} catch (UniqueConstraintViolationException $e) {
throw new UniqueConstraintViolation($e);
}
}
}
2 changes: 1 addition & 1 deletion tests/Integration/BasicImplementation/Header/BazHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Integration\BasicImplementation\Header;
namespace Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Header;

use Patchlevel\EventSourcing\Attribute\Header;

Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/BasicImplementation/Header/FooHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Integration\BasicImplementation\Header;
namespace Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Header;

use Patchlevel\EventSourcing\Attribute\Header;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\MessageDecorator;

use Integration\BasicImplementation\Header\BazHeader;
use Integration\BasicImplementation\Header\FooHeader;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Repository\MessageDecorator\MessageDecorator;
use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Header\BazHeader;
use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Header\FooHeader;

final class FooMessageDecorator implements MessageDecorator
{
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Fixture/Header/BazHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Fixture\Header;

use Patchlevel\EventSourcing\Attribute\Header;

/** @psalm-immutable */
#[Header('baz')]
final class BazHeader
{
public function __construct(
public readonly string $data,
) {
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Fixture/Header/FooHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Fixture\Header;

use Patchlevel\EventSourcing\Attribute\Header;

/** @psalm-immutable */
#[Header('foo')]
final class FooHeader
{
public function __construct(
public readonly string $data,
) {
}
}
20 changes: 20 additions & 0 deletions tests/Unit/Fixture/ProfileEmailChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\Fixture;

use Patchlevel\EventSourcing\Attribute\Event;
use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer;

#[Event('profile_email_changed')]
final class ProfileEmailChanged
{
public function __construct(
#[IdNormalizer]
public ProfileId $profileId,
#[EmailNormalizer]
public Email $email,
) {
}
}
25 changes: 25 additions & 0 deletions tests/Unit/Store/ArrayStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,29 @@ public function testClose(): void
$stream->close();
$stream->index();
}

public function testPositionEmpty(): void
{
$stream = new ArrayStream([]);
$position = $stream->position();

self::assertNull($position);
}

public function testPosition(): void
{
$message = Message::create(
new ProfileCreated(
ProfileId::fromString('foo'),
Email::fromString('[email protected]'),
),
);

$messages = [$message];

$stream = new ArrayStream($messages);
$position = $stream->position();

self::assertSame(0, $position);
}
}
Loading

0 comments on commit 2eb51a6

Please sign in to comment.