-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
TableMetadataStorageTest.php
333 lines (261 loc) · 12.2 KB
/
TableMetadataStorageTest.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
declare(strict_types=1);
namespace Doctrine\Migrations\Tests\Metadata\Storage;
use DateTime;
use DateTimeImmutable;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as SQLiteDriver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Types\IntegerType;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\Exception\MetadataStorageError;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorage;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\Version\AlphabeticalComparator;
use Doctrine\Migrations\Version\Direction;
use Doctrine\Migrations\Version\ExecutionResult;
use Doctrine\Migrations\Version\Version;
use PHPUnit\Framework\TestCase;
use function sprintf;
class TableMetadataStorageTest extends TestCase
{
/** @var Connection */
private $connection;
/** @var TableMetadataStorage */
private $storage;
/** @var TableMetadataStorageConfiguration */
private $config;
/** @var AbstractSchemaManager */
private $schemaManager;
private function getSqliteConnection(): Connection
{
$params = ['driver' => 'pdo_sqlite', 'memory' => true];
return DriverManager::getConnection($params);
}
public function setUp(): void
{
$this->connection = $this->getSqliteConnection();
$this->schemaManager = $this->connection->getSchemaManager();
$this->config = new TableMetadataStorageConfiguration();
$this->storage = new TableMetadataStorage($this->connection, new AlphabeticalComparator(), $this->config);
}
public function testDifferentTableNotUpdatedOnRead(): void
{
$this->expectException(MetadataStorageError::class);
$this->expectExceptionMessage('The metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.');
$table = new Table($this->config->getTableName());
$table->addColumn($this->config->getVersionColumnName(), 'string', ['notnull' => true, 'length' => 10]);
$table->setPrimaryKey([$this->config->getVersionColumnName()]);
$this->schemaManager->createTable($table);
$this->storage->getExecutedMigrations();
}
public function testTableNotCreatedOnReadButReadingWorks(): void
{
$executedMigrations = $this->storage->getExecutedMigrations();
self::assertSame([], $executedMigrations->getItems());
self::assertFalse($this->schemaManager->tablesExist([$this->config->getTableName()]));
}
public function testTableStructureUpdate(): void
{
$config = new TableMetadataStorageConfiguration();
$config->setTableName('a');
$config->setVersionColumnName('b');
$config->setVersionColumnLength(199);
$config->setExecutedAtColumnName('c');
$config->setExecutionTimeColumnName('d');
$table = new Table($config->getTableName());
$table->addColumn($config->getVersionColumnName(), 'string', ['notnull' => true, 'length' => 10]);
$table->setPrimaryKey([$config->getVersionColumnName()]);
$this->schemaManager->createTable($table);
$storage = new TableMetadataStorage($this->connection, new AlphabeticalComparator(), $config);
$storage->ensureInitialized();
$table = $this->schemaManager->listTableDetails($config->getTableName());
self::assertInstanceOf(StringType::class, $table->getColumn('b')->getType());
self::assertInstanceOf(DateTimeType::class, $table->getColumn('c')->getType());
self::assertInstanceOf(IntegerType::class, $table->getColumn('d')->getType());
}
public function testTableNotUpToDateTriggersExcepton(): void
{
$this->expectException(MetadataStorageError::class);
$this->expectExceptionMessage('The metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue.');
$config = new TableMetadataStorageConfiguration();
$config->setTableName('a');
$config->setVersionColumnName('b');
$config->setVersionColumnLength(199);
$config->setExecutedAtColumnName('c');
$config->setExecutionTimeColumnName('d');
$table = new Table($config->getTableName());
$table->addColumn($config->getVersionColumnName(), 'string', ['notnull' => true, 'length' => 10]);
$table->setPrimaryKey([$config->getVersionColumnName()]);
$this->schemaManager->createTable($table);
$storage = new TableMetadataStorage($this->connection, new AlphabeticalComparator(), $config);
$storage->getExecutedMigrations();
}
public function testTableStructure(): void
{
$config = new TableMetadataStorageConfiguration();
$config->setTableName('a');
$config->setVersionColumnName('b');
$config->setVersionColumnLength(199);
$config->setExecutedAtColumnName('c');
$config->setExecutionTimeColumnName('d');
$storage = new TableMetadataStorage($this->connection, new AlphabeticalComparator(), $config);
$storage->ensureInitialized();
$table = $this->schemaManager->listTableDetails($config->getTableName());
self::assertInstanceOf(StringType::class, $table->getColumn('b')->getType());
self::assertInstanceOf(DateTimeType::class, $table->getColumn('c')->getType());
self::assertInstanceOf(IntegerType::class, $table->getColumn('d')->getType());
}
public function testComplete(): void
{
$this->storage->ensureInitialized();
$result = new ExecutionResult(new Version('1230'), Direction::UP, new DateTimeImmutable('2010-01-05 10:30:21'));
$result->setTime(31.0);
$this->storage->complete($result);
$sql = sprintf(
'SELECT * FROM %s',
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName())
);
$rows = $this->connection->fetchAll($sql);
self::assertSame([
0 =>
[
'version' => '1230',
'executed_at' => '2010-01-05 10:30:21',
'execution_time' => '31000',
],
], $rows);
}
public function testCompleteWithFloatTime(): void
{
$this->storage->ensureInitialized();
$result = new ExecutionResult(new Version('1230'), Direction::UP, new DateTimeImmutable('2010-01-05 10:30:21'));
$result->setTime(31.49);
$this->storage->complete($result);
$sql = sprintf(
'SELECT * FROM %s',
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName())
);
$rows = $this->connection->fetchAll($sql);
self::assertSame([
0 =>
[
'version' => '1230',
'executed_at' => '2010-01-05 10:30:21',
'execution_time' => '31490',
],
], $rows);
}
public function testCompleteWillAlwaysCastTimeToInteger(): void
{
$config = new TableMetadataStorageConfiguration();
$executedAt = new DateTimeImmutable('2010-01-05 10:30:21');
$connection = $this->getMockBuilder(Connection::class)
->setConstructorArgs([
['pdo' => $this->getSqliteConnection()->getWrappedConnection()],
new SQLiteDriver(),
])
->onlyMethods(['insert'])
->getMock();
$connection
->expects(self::once())
->method('insert')
->willReturnCallback(static function ($table, $params, $types) use ($config, $executedAt): int {
self::assertSame($config->getTableName(), $table);
self::assertSame([
$config->getVersionColumnName() => '1230',
$config->getExecutedAtColumnName() => $executedAt,
$config->getExecutionTimeColumnName() => 31000,
], $params);
self::assertSame([
Types::STRING,
Types::DATETIME_MUTABLE,
Types::INTEGER,
], $types);
return 1;
});
$storage = new TableMetadataStorage($connection, new AlphabeticalComparator(), $config);
$storage->ensureInitialized();
$result = new ExecutionResult(new Version('1230'), Direction::UP, $executedAt);
$result->setTime(31.0);
$storage->complete($result);
}
public function testRead(): void
{
$this->storage->ensureInitialized();
$date = new DateTimeImmutable('2010-01-05 10:30:21');
$result1 = new ExecutionResult(new Version('1230'), Direction::UP, $date);
$result1->setTime(31.0);
$this->storage->complete($result1);
$result2 = new ExecutionResult(new Version('1231'), Direction::UP);
$this->storage->complete($result2);
$executedMigrations = $this->storage->getExecutedMigrations();
self::assertTrue($executedMigrations->hasMigration(new Version('1230')));
self::assertTrue($executedMigrations->hasMigration(new Version('1231')));
self::assertFalse($executedMigrations->hasMigration(new Version('1232')));
$m1 = $executedMigrations->getMigration($result1->getVersion());
self::assertEquals($result1->getVersion(), $m1->getVersion());
self::assertNotNull($m1->getExecutedAt());
self::assertSame($date->format(DateTime::ISO8601), $m1->getExecutedAt()->format(DateTime::ISO8601));
self::assertSame(31.0, $m1->getExecutionTime());
$m2 = $executedMigrations->getMigration($result2->getVersion());
self::assertEquals($result2->getVersion(), $m2->getVersion());
self::assertNull($m2->getExecutedAt());
self::assertNull($m2->getExecutionTime());
}
public function testReadIsSorted(): void
{
$this->storage->ensureInitialized();
$result = new ExecutionResult(new Version('9000'), Direction::UP);
$this->storage->complete($result);
$result = new ExecutionResult(new Version('1000'), Direction::UP);
$this->storage->complete($result);
$executedMigrations = $this->storage->getExecutedMigrations();
self::assertEquals(new Version('1000'), $executedMigrations->getItems()[0]->getVersion());
self::assertEquals(new Version('9000'), $executedMigrations->getItems()[1]->getVersion());
}
public function testCompleteDownRemovesTheRow(): void
{
$this->storage->ensureInitialized();
$result = new ExecutionResult(new Version('1230'), Direction::UP, new DateTimeImmutable('2010-01-05 10:30:21'));
$result->setTime(31.0);
$this->storage->complete($result);
$sql = sprintf(
'SELECT * FROM %s',
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName())
);
self::assertCount(1, $this->connection->fetchAll($sql));
$result = new ExecutionResult(new Version('1230'), Direction::DOWN, new DateTimeImmutable('2010-01-05 10:30:21'));
$result->setTime(31.0);
$this->storage->complete($result);
self::assertCount(0, $this->connection->fetchAll($sql));
}
public function testReset(): void
{
$this->storage->ensureInitialized();
$result = new ExecutionResult(new Version('1230'), Direction::UP, new DateTimeImmutable('2010-01-05 10:30:21'));
$result->setTime(31.0);
$this->storage->complete($result);
$sql = sprintf(
'SELECT * FROM %s',
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName())
);
self::assertCount(1, $this->connection->fetchAll($sql));
$this->storage->reset();
self::assertCount(0, $this->connection->fetchAll($sql));
}
public function testResetWithEmptySchema(): void
{
$this->storage->ensureInitialized();
$this->storage->reset();
$sql = sprintf(
'SELECT * FROM %s',
$this->connection->getDatabasePlatform()->quoteIdentifier($this->config->getTableName())
);
self::assertCount(0, $this->connection->fetchAll($sql));
}
}