Skip to content

Commit

Permalink
Added DBALv4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
wjzijderveld authored and frankdejonge committed Mar 26, 2024
1 parent e15d080 commit ed5df5d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"require": {
"eventsauce/eventsauce": "^3.0",
"eventsauce/backoff": "^1.0",
"doctrine/dbal": "^2.11|^3.1"
"doctrine/dbal": "^2.11|^3.1|^4.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\DriverManager;
use EventSauce\EventSourcing\AggregateRootId;
use EventSauce\MessageOutbox\TestTooling\DoctrineConnectionTrait;
use EventSauce\MessageRepository\TestTooling\MessageRepositoryTestCase;
use Ramsey\Uuid\Uuid;

Expand All @@ -15,6 +16,8 @@

abstract class DoctrineMessageRepositoryTestCase extends MessageRepositoryTestCase
{
use DoctrineConnectionTrait;

protected Connection $connection;

protected function setUp(): void
Expand All @@ -24,7 +27,7 @@ protected function setUp(): void
}

parent::setUp();
$connection = DriverManager::getConnection(['url' => $this->formatDsn()]);
$connection = DriverManager::getConnection($this->getConnectionParams());
$this->connection = $connection;
$this->truncateTable();
}
Expand Down
14 changes: 12 additions & 2 deletions src/DoctrineOutbox/DoctrineOutboxRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EventSauce\MessageOutbox\DoctrineOutbox;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use EventSauce\EventSourcing\Message;
Expand Down Expand Up @@ -69,7 +70,7 @@ public function markConsumed(Message ...$messages): void
$this->connection->executeQuery($sqlStatement, [
'ids' => $ids,
], [
'ids' => Connection::PARAM_INT_ARRAY,
'ids' => $this->intArrayType(),
]);
}

Expand All @@ -88,7 +89,7 @@ public function deleteMessages(Message ...$messages): void
$this->connection->executeQuery($sqlStatement, [
'ids' => $ids,
], [
'ids' => Connection::PARAM_INT_ARRAY,
'ids' => $this->intArrayType(),
]);
}

Expand Down Expand Up @@ -129,4 +130,13 @@ private function idFromMessage(Message $message): int

return (int) $id;
}

private function intArrayType(): mixed
{
if (class_exists(ArrayParameterType::class)) {
return ArrayParameterType::INTEGER;
}

return Connection::PARAM_INT_ARRAY;
}
}
20 changes: 12 additions & 8 deletions src/DoctrineOutbox/DoctrineTransactionalMessageRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use EventSauce\EventSourcing\MessageRepository;
use EventSauce\EventSourcing\Serialization\ConstructingMessageSerializer;
use EventSauce\MessageOutbox\OutboxRepository;
use EventSauce\MessageOutbox\TestTooling\DoctrineConnectionTrait;
use EventSauce\MessageOutbox\TestTooling\TransactionalMessageRepositoryTestCase;
use EventSauce\MessageRepository\DoctrineMessageRepository\DoctrineUuidV4MessageRepository;
use EventSauce\MessageRepository\DoctrineMessageRepository\DummyAggregateRootId;
Expand All @@ -18,6 +19,8 @@

class DoctrineTransactionalMessageRepositoryTest extends TransactionalMessageRepositoryTestCase
{
use DoctrineConnectionTrait;

private Connection $connection;

protected function setUp(): void
Expand All @@ -28,19 +31,20 @@ protected function setUp(): void

parent::setUp();

$host = getenv('EVENTSAUCE_TESTING_MYSQL_HOST') ?: '127.0.0.1';
$port = getenv('EVENTSAUCE_TESTING_MYSQL_PORT') ?: '3306';

$this->connection = DriverManager::getConnection(
[
'url' => "mysql://username:password@$host:$port/outbox_messages",
]
);
$this->connection = DriverManager::getConnection($this->getConnectionParams());

$this->connection->executeQuery('TRUNCATE TABLE '.$this->repositoryTable);
$this->connection->executeQuery('TRUNCATE TABLE '.$this->outboxTable);
}

protected function formatDsn(): string
{
$host = getenv('EVENTSAUCE_TESTING_MYSQL_HOST') ?: '127.0.0.1';
$port = getenv('EVENTSAUCE_TESTING_MYSQL_PORT') ?: '3306';

return "mysql://username:password@$host:$port/outbox_messages";
}

protected function messageRepository(): MessageRepository
{
return new DoctrineUuidV4MessageRepository(
Expand Down
25 changes: 25 additions & 0 deletions src/TestTooling/DoctrineConnectionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace EventSauce\MessageOutbox\TestTooling;

use Doctrine\DBAL\Tools\DsnParser;

trait DoctrineConnectionTrait
{
abstract function formatDsn(): string;

protected function getConnectionParams(): array
{
$dsn = $this->formatDsn();
if (class_exists(DsnParser::class)) {
if (str_starts_with($this->formatDsn(), 'mysql')) {
$parserParams = ['mysql' => 'pdo_mysql'];
} else {
$parserParams = ['pgsql' => 'pdo_pgsql'];
}
$parser = new DsnParser($parserParams);
return $parser->parse($dsn);
}
return ['url' => $dsn];
}
}

0 comments on commit ed5df5d

Please sign in to comment.