forked from krowinski/php-mysql-replication
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigTest.php
More file actions
230 lines (200 loc) · 8.77 KB
/
ConfigTest.php
File metadata and controls
230 lines (200 loc) · 8.77 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/** @noinspection PhpUnhandledExceptionInspection */
declare(strict_types=1);
namespace MySQLReplication\Tests\Unit\Config;
use MySQLReplication\Config\Config;
use MySQLReplication\Config\ConfigBuilder;
use MySQLReplication\Config\ConfigException;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class ConfigTest extends TestCase
{
public function shouldMakeConfig(): void
{
$expected = [
'user' => 'foo',
'host' => '127.0.0.1',
'port' => 3308,
'password' => 'secret',
'charset' => 'utf8',
'gtid' => '9b1c8d18-2a76-11e5-a26b-000c2976f3f3:1-177592',
'slaveId' => 1,
'binLogFileName' => 'binfile1.bin',
'binLogPosition' => '999',
'eventsOnly' => [],
'eventsIgnore' => [],
'tablesOnly' => ['test_table'],
'databasesOnly' => ['test_database'],
'mariaDbGtid' => '123:123',
'tableCacheSize' => 777,
'custom' => [
[
'random' => 'data',
],
],
'heartbeatPeriod' => 69,
'slaveUuid' => '6c27ed6d-7ee1-11e3-be39-6c626d957cff',
];
$config = new Config(
$expected['user'],
$expected['host'],
$expected['port'],
$expected['password'],
$expected['charset'],
$expected['gtid'],
$expected['mariaDbGtid'],
$expected['slaveId'],
$expected['binLogFileName'],
$expected['binLogPosition'],
$expected['eventsOnly'],
$expected['eventsIgnore'],
$expected['tablesOnly'],
$expected['databasesOnly'],
$expected['tableCacheSize'],
$expected['custom'],
$expected['heartbeatPeriod'],
$expected['slaveUuid']
);
self::assertSame($expected['user'], $config->user);
self::assertSame($expected['host'], $config->host);
self::assertSame($expected['port'], $config->port);
self::assertSame($expected['password'], $config->password);
self::assertSame($expected['charset'], $config->charset);
self::assertSame($expected['gtid'], $config->gtid);
self::assertSame($expected['slaveId'], $config->slaveId);
self::assertSame($expected['binLogFileName'], $config->binLogFileName);
self::assertSame($expected['binLogPosition'], $config->binLogPosition);
self::assertSame($expected['eventsOnly'], $config->eventsOnly);
self::assertSame($expected['eventsIgnore'], $config->eventsIgnore);
self::assertSame($expected['tablesOnly'], $config->tablesOnly);
self::assertSame($expected['mariaDbGtid'], $config->mariaDbGtid);
self::assertSame($expected['tableCacheSize'], $config->tableCacheSize);
self::assertSame($expected['custom'], $config->custom);
self::assertSame($expected['heartbeatPeriod'], $config->heartbeatPeriod);
self::assertSame($expected['databasesOnly'], $config->databasesOnly);
self::assertSame($expected['slaveUuid'], $config->slaveUuid);
$config->validate();
}
public function testShouldCheckDataBasesOnly(): void
{
$config = (new ConfigBuilder())->withDatabasesOnly(['boo'])->build();
self::assertTrue($config->checkDataBasesOnly('foo'));
$config = (new ConfigBuilder())->withDatabasesOnly(['foo'])->build();
self::assertFalse($config->checkDataBasesOnly('foo'));
$config = (new ConfigBuilder())->withDatabasesOnly(['test'])->build();
self::assertFalse($config->checkDataBasesOnly('test'));
$config = (new ConfigBuilder())->withDatabasesOnly(['foo'])->build();
self::assertTrue($config->checkDataBasesOnly('bar'));
$config = (new ConfigBuilder())->withDatabasesRegex(['/^foo_.*/'])->build();
self::assertFalse($config->checkDataBasesOnly('foo_123'));
}
public function testShouldCheckDataBasesOnlyListAndRegex(): void
{
$config = (new ConfigBuilder())
->withDatabasesOnly(['foo'])
->withDatabasesRegex(['/^instance\d+$/'])
->build();
self::assertFalse($config->checkDataBasesOnly('foo'));
self::assertFalse($config->checkDataBasesOnly('instance1'));
self::assertFalse($config->checkDataBasesOnly('instance123'));
self::assertTrue($config->checkDataBasesOnly('bar'));
self::assertTrue($config->checkDataBasesOnly('instance'));
self::assertTrue($config->checkDataBasesOnly('instanceX'));
}
public function testShouldCheckTablesOnly(): void
{
$config = (new ConfigBuilder())->build();
self::assertFalse($config->checkTablesOnly('foo'));
$config = (new ConfigBuilder())->withTablesOnly(['foo'])->build();
self::assertFalse($config->checkTablesOnly('foo'));
$config = (new ConfigBuilder())->withTablesOnly(['test'])->build();
self::assertFalse($config->checkTablesOnly('test'));
$config = (new ConfigBuilder())->withTablesOnly(['foo'])->build();
self::assertTrue($config->checkTablesOnly('bar'));
$config = (new ConfigBuilder())->withTablesRegex(['/^foo_.*/'])->build();
self::assertFalse($config->checkTablesOnly('foo_123'));
}
public function testShouldCheckTablesOnlyListAndRegex(): void
{
$config = (new ConfigBuilder())
->withTablesOnly(['foo'])
->withTablesRegex(['/^instance\d+$/'])
->build();
self::assertFalse($config->checkTablesOnly('foo'));
self::assertFalse($config->checkTablesOnly('instance1'));
self::assertFalse($config->checkTablesOnly('instance123'));
self::assertTrue($config->checkTablesOnly('bar'));
self::assertTrue($config->checkTablesOnly('instance'));
self::assertTrue($config->checkTablesOnly('instanceX'));
}
public function testShouldCheckEvent(): void
{
$config = (new ConfigBuilder())->build();
self::assertTrue($config->checkEvent(1));
$config = (new ConfigBuilder())->withEventsOnly([2])->build();
self::assertTrue($config->checkEvent(2));
$config = (new ConfigBuilder())->withEventsOnly([3])->build();
self::assertFalse($config->checkEvent(4));
$config = (new ConfigBuilder())->withEventsIgnore([4])->build();
self::assertFalse($config->checkEvent(4));
}
public static function shouldCheckHeartbeatPeriodProvider(): array
{
return [[0], [0.0], [0.001], [4294967], [2]];
}
#[DataProvider('shouldCheckHeartbeatPeriodProvider')] public function testShouldCheckHeartbeatPeriod(
int|float $heartbeatPeriod
): void {
$config = (new ConfigBuilder())->withHeartbeatPeriod($heartbeatPeriod)
->build();
$config->validate();
self::assertEquals($heartbeatPeriod, $config->heartbeatPeriod);
}
public static function shouldValidateProvider(): array
{
return [
['host', 'aaa', ConfigException::IP_ERROR_MESSAGE, ConfigException::IP_ERROR_CODE],
['port', -1, ConfigException::PORT_ERROR_MESSAGE, ConfigException::PORT_ERROR_CODE],
['slaveId', -1, ConfigException::SLAVE_ID_ERROR_MESSAGE, ConfigException::SLAVE_ID_ERROR_CODE],
['gtid', '-1', ConfigException::GTID_ERROR_MESSAGE, ConfigException::GTID_ERROR_CODE],
[
'binLogPosition',
'-1',
ConfigException::BIN_LOG_FILE_POSITION_ERROR_MESSAGE,
ConfigException::BIN_LOG_FILE_POSITION_ERROR_CODE,
],
[
'tableCacheSize',
-1,
ConfigException::TABLE_CACHE_SIZE_ERROR_MESSAGE,
ConfigException::TABLE_CACHE_SIZE_ERROR_CODE,
],
[
'heartbeatPeriod',
4294968,
ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE,
ConfigException::HEARTBEAT_PERIOD_ERROR_CODE,
],
[
'heartbeatPeriod',
-1,
ConfigException::HEARTBEAT_PERIOD_ERROR_MESSAGE,
ConfigException::HEARTBEAT_PERIOD_ERROR_CODE,
],
];
}
#[DataProvider('shouldValidateProvider')]
public function testShouldValidate(
string $configKey,
mixed $configValue,
string $expectedMessage,
int $expectedCode
): void {
$this->expectException(ConfigException::class);
$this->expectExceptionMessage($expectedMessage);
$this->expectExceptionCode($expectedCode);
/** @var Config $config */
$config = (new ConfigBuilder())->{'with' . strtoupper($configKey)}($configValue)->build();
$config->validate();
}
}