-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathBaseCase.php
More file actions
139 lines (111 loc) · 4.69 KB
/
Copy pathBaseCase.php
File metadata and controls
139 lines (111 loc) · 4.69 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace MySQLReplication\Tests\Integration;
use Doctrine\DBAL\Connection;
use MySQLReplication\Config\ConfigBuilder;
use MySQLReplication\Definitions\ConstEventType;
use MySQLReplication\Event\DTO\EventDTO;
use MySQLReplication\Event\DTO\FormatDescriptionEventDTO;
use MySQLReplication\Event\DTO\QueryDTO;
use MySQLReplication\Event\DTO\RotateDTO;
use MySQLReplication\Event\DTO\TableMapDTO;
use MySQLReplication\MySQLReplicationFactory;
use MySQLReplication\Tools;
use PHPUnit\Framework\TestCase;
use RuntimeException;
abstract class BaseCase extends TestCase
{
protected ?MySQLReplicationFactory $mySQLReplicationFactory;
protected Connection $connection;
protected string $database = 'mysqlreplication_test';
protected ?EventDTO $currentEvent;
protected ConfigBuilder $configBuilder;
private TestEventSubscribers $testEventSubscribers;
protected function setUp(): void
{
parent::setUp();
$this->configBuilder = (new ConfigBuilder())
->withUser('root')
->withHost('0.0.0.0')
->withPassword('root')
->withPort((int)Tools::getFromEnv('MYSQL_PORT', 3306))
->withEventsIgnore($this->getIgnoredEvents());
$this->connect();
var_dump($this->mySQLReplicationFactory?->getServerInfo(), $this->configBuilder->build());
if ($this->mySQLReplicationFactory?->getServerInfo()->versionRevision >= 8 && $this->mySQLReplicationFactory?->getServerInfo()->isGeneric()) {
self::assertInstanceOf(RotateDTO::class, $this->getEvent());
}
self::assertInstanceOf(FormatDescriptionEventDTO::class, $this->getEvent());
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
}
protected function tearDown(): void
{
parent::tearDown();
$this->disconnect();
}
public function setEvent(EventDTO $eventDTO): void
{
$this->currentEvent = $eventDTO;
}
public function connect(): void
{
$this->mySQLReplicationFactory = new MySQLReplicationFactory($this->configBuilder->build());
$this->testEventSubscribers = new TestEventSubscribers($this);
$this->mySQLReplicationFactory->registerSubscriber($this->testEventSubscribers);
$connection = $this->mySQLReplicationFactory->getDbConnection();
if ($connection === null) {
throw new RuntimeException('Connection not initialized');
}
$this->connection = $connection;
$this->connection->executeStatement('SET SESSION time_zone = "UTC"');
$this->connection->executeStatement('DROP DATABASE IF EXISTS ' . $this->database);
$this->connection->executeStatement('CREATE DATABASE ' . $this->database);
$this->connection->executeStatement('USE ' . $this->database);
$this->connection->executeStatement('SET SESSION sql_mode = \'\';');
}
protected function getIgnoredEvents(): array
{
return [
ConstEventType::GTID_LOG_EVENT->value, // Generally in here
ConstEventType::ROWS_QUERY_LOG_EVENT->value, // Just debugging, there is a special test for it
];
}
protected function getEvent(): EventDTO
{
if ($this->mySQLReplicationFactory === null) {
throw new RuntimeException('MySQLReplicationFactory not initialized');
}
// events can be null lets us continue until we find event
$this->currentEvent = null;
while ($this->currentEvent === null) {
$this->mySQLReplicationFactory->consume();
}
/** @phpstan-ignore-next-line */
return $this->currentEvent;
}
protected function disconnect(): void
{
if ($this->mySQLReplicationFactory === null) {
return;
}
$this->mySQLReplicationFactory->unregisterSubscriber($this->testEventSubscribers);
$this->mySQLReplicationFactory = null;
}
protected function checkForVersion(float $version): bool
{
/** @phpstan-ignore-next-line */
return $this->mySQLReplicationFactory->getServerInfo()
->versionRevision < $version;
}
protected function createAndInsertValue(string $createQuery, string $insertQuery): EventDTO
{
$this->connection->executeStatement($createQuery);
$this->connection->executeStatement($insertQuery);
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
self::assertInstanceOf(QueryDTO::class, $this->getEvent());
self::assertInstanceOf(TableMapDTO::class, $this->getEvent());
return $this->getEvent();
}
}