-
Notifications
You must be signed in to change notification settings - Fork 1
/
DoctrineMessageRepositoryTestCase.php
63 lines (50 loc) · 1.78 KB
/
DoctrineMessageRepositoryTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace EventSauce\MessageRepository\DoctrineMessageRepository;
use Doctrine\DBAL\Connection;
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;
use function class_exists;
use function getenv;
use function str_starts_with;
abstract class DoctrineMessageRepositoryTestCase extends MessageRepositoryTestCase
{
use DoctrineConnectionTrait;
protected Connection $connection;
protected function setUp(): void
{
if (class_exists(ResultStatement::class)) {
$this->markTestSkipped('Doctrine v2 installed');
}
parent::setUp();
$connection = DriverManager::getConnection($this->getConnectionParams());
$this->connection = $connection;
$this->truncateTable();
}
protected function formatDsn(): string
{
$host = getenv('EVENTSAUCE_TESTING_MYSQL_HOST') ?: '127.0.0.1';
$port = getenv('EVENTSAUCE_TESTING_MYSQL_PORT') ?: '3306';
$dsn = "mysql://username:password@$host:$port/outbox_messages";
return $dsn;
}
protected function truncateTable(): void
{
if (str_starts_with($this->formatDsn(), 'mysql')) {
$this->connection->executeQuery('TRUNCATE TABLE ' . $this->tableName);
} else {
$this->connection->executeQuery('TRUNCATE TABLE ' . $this->tableName . ' RESTART IDENTITY CASCADE');
}
}
protected function aggregateRootId(): AggregateRootId
{
return DummyAggregateRootId::generate();
}
protected function eventId(): string
{
return Uuid::uuid4()->toString();
}
}