forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMigrationRunnerTest.php
More file actions
493 lines (372 loc) · 15.2 KB
/
MigrationRunnerTest.php
File metadata and controls
493 lines (372 loc) · 15.2 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Database\Migrations;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\Database\MigrationRunner;
use CodeIgniter\Events\Events;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Config\Database;
use Config\Migrations;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\MigrationTestMigrations\Database\Migrations\Migration_another_migration;
use Tests\Support\MigrationTestMigrations\Database\Migrations\Migration_some_migration;
/**
* @internal
*/
#[Group('DatabaseLive')]
final class MigrationRunnerTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $refresh = true;
// Do not migrate automatically, because we test migrations.
protected $migrate = false;
// Use specific migration files for this test case.
protected $namespace = 'Tests\Support\MigrationTestMigrations';
private vfsStreamDirectory $root;
private Migrations $config;
protected function setUp(): void
{
$this->setUpMethods[] = 'setUpAddNamespace';
parent::setUp();
$this->root = vfsStream::setup('root');
$this->config = new Migrations();
$this->config->enabled = true;
}
protected function setUpAddNamespace(): void
{
service('autoloader')->addNamespace(
'Tests\Support\MigrationTestMigrations',
SUPPORTPATH . 'MigrationTestMigrations',
);
}
protected function tearDown(): void
{
parent::tearDown();
// To delete data with `$this->regressDatabase()`, set it true.
$this->migrate = true;
$this->regressDatabase();
}
public function testLoadsDefaultDatabaseWhenNoneSpecified(): void
{
$dbConfig = new Database();
$runner = new MigrationRunner($this->config);
$db = $this->getPrivateProperty($runner, 'db');
$this->assertInstanceOf(BaseConnection::class, $db);
$database = (
$dbConfig->tests['DBDriver'] === 'SQLite3'
&& $dbConfig->tests['database'] !== ':memory:'
? WRITEPATH : ''
) . $dbConfig->tests['database'];
$this->assertSame(
$database,
$this->getPrivateProperty($db, 'database'),
);
$this->assertSame($dbConfig->tests['DBDriver'], $this->getPrivateProperty($db, 'DBDriver'));
}
public function testGetCliMessages(): void
{
$runner = new MigrationRunner($this->config);
$messages = ['foo', 'bar'];
$this->setPrivateProperty($runner, 'cliMessages', $messages);
$this->assertSame($messages, $runner->getCliMessages());
}
public function testGetHistory(): void
{
$runner = new MigrationRunner($this->config);
$runner->ensureTable();
$expected = [
'id' => 4,
'version' => 'abc123',
'class' => 'changesomething',
'group' => 'default',
'namespace' => 'App',
'time' => time(),
'batch' => 1,
];
if ($this->db->DBDriver === 'SQLSRV') {
$this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->prefixTable('migrations') . ' ON');
}
$this->hasInDatabase('migrations', $expected);
$history = (array) $runner->getHistory()[0];
$history = array_map(static function ($value) {
if (is_numeric($value)) {
return (int) $value;
}
return $value;
}, $history);
$this->assertSame($expected, $history);
if ($this->db->DBDriver === 'SQLSRV') {
$this->db->simpleQuery('SET IDENTITY_INSERT ' . $this->db->escapeIdentifiers($this->db->schema) . '.' . $this->db->prefixTable('migrations') . ' OFF');
$db = $this->getPrivateProperty($runner, 'db');
$db->table('migrations')->delete(['id' => 4]);
}
}
public function testGetHistoryReturnsEmptyArrayWithNoResults(): void
{
$runner = new MigrationRunner($this->config);
$runner->ensureTable();
$this->assertSame([], $runner->getHistory());
}
public function testGetMigrationNumberAllDigits(): void
{
$runner = new MigrationRunner($this->config);
$method = self::getPrivateMethodInvoker($runner, 'getMigrationNumber');
$this->assertSame('20190806235100', $method('20190806235100_Foo'));
}
public function testGetMigrationNumberDashes(): void
{
$runner = new MigrationRunner($this->config);
$method = self::getPrivateMethodInvoker($runner, 'getMigrationNumber');
$this->assertSame('2019-08-06-235100', $method('2019-08-06-235100_Foo'));
}
public function testGetMigrationNumberUnderscores(): void
{
$runner = new MigrationRunner($this->config);
$method = self::getPrivateMethodInvoker($runner, 'getMigrationNumber');
$this->assertSame('2019_08_06_235100', $method('2019_08_06_235100_Foo'));
}
public function testGetMigrationNumberReturnsZeroIfNoneFound(): void
{
$runner = new MigrationRunner($this->config);
$method = self::getPrivateMethodInvoker($runner, 'getMigrationNumber');
$this->assertSame('0', $method('Foo'));
}
public function testGetMigrationNameDashes(): void
{
$runner = new MigrationRunner($this->config);
$method = self::getPrivateMethodInvoker($runner, 'getMigrationName');
$this->assertSame('Foo_bar', $method('2019-08-06-235100_Foo_bar'));
}
public function testGetMigrationNameUnderscores(): void
{
$runner = new MigrationRunner($this->config);
$method = self::getPrivateMethodInvoker($runner, 'getMigrationName');
$this->assertSame('Foo_bar', $method('2019_08_06_235100_Foo_bar'));
}
public function testSetSilentStoresValue(): void
{
$runner = new MigrationRunner($this->config);
$runner->setSilent(true);
$this->assertTrue($this->getPrivateProperty($runner, 'silent'));
$runner->setSilent(false);
$this->assertFalse($this->getPrivateProperty($runner, 'silent'));
}
public function testSetNameStoresValue(): void
{
$runner = new MigrationRunner($this->config);
$runner->setName('foo');
$this->assertSame('foo', $this->getPrivateProperty($runner, 'name'));
}
public function testSetGroupStoresValue(): void
{
$runner = new MigrationRunner($this->config);
$runner->setGroup('foo');
$this->assertSame('foo', $this->getPrivateProperty($runner, 'group'));
}
public function testSetNamespaceStoresValue(): void
{
$runner = new MigrationRunner($this->config);
$runner->setNamespace('foo');
$this->assertSame('foo', $this->getPrivateProperty($runner, 'namespace'));
}
public function testFindMigrationsReturnsEmptyArrayWithNoneFound(): void
{
$config = $this->config;
$runner = new MigrationRunner($config);
$this->assertSame([], $runner->findMigrations());
}
public function testFindMigrationsSuccessTimestamp(): void
{
$config = $this->config;
$runner = new MigrationRunner($config);
$runner = $runner->setNamespace('Tests\Support\MigrationTestMigrations');
$mig1 = (object) [
'version' => '2018-01-24-102301',
'name' => 'Some_migration',
'path' => realpath(TESTPATH . '_support/MigrationTestMigrations/Database/Migrations/2018-01-24-102301_Some_migration.php'),
'class' => Migration_some_migration::class,
'namespace' => 'Tests\Support\MigrationTestMigrations',
];
$mig1->uid = $runner->getObjectUid($mig1);
$mig2 = (object) [
'version' => '2018-01-24-102302',
'name' => 'Another_migration',
'path' => realpath(TESTPATH . '_support/MigrationTestMigrations/Database/Migrations/2018-01-24-102302_Another_migration.php'),
'class' => Migration_another_migration::class,
'namespace' => 'Tests\Support\MigrationTestMigrations',
'uid' => '20180124102302Tests\Support\MigrationTestMigrations\Database\Migrations\Migration_another_migration',
];
$mig1->uid = $runner->getObjectUid($mig1);
$migrations = $runner->findMigrations();
$this->assertCount(2, $migrations);
$this->assertSame((array) $mig1, (array) array_shift($migrations));
$this->assertSame((array) $mig2, (array) array_shift($migrations));
}
public function testMigrationThrowsDisabledException(): void
{
$this->expectException(ConfigException::class);
$this->expectExceptionMessage('Migrations have been loaded but are disabled or setup incorrectly.');
$config = $this->config;
$config->enabled = false;
$runner = new MigrationRunner($config);
$runner->setSilent(false);
$runner = $runner->setNamespace('Tests\Support\MigrationTestMigrations');
vfsStream::copyFromFileSystem(
TESTPATH . '_support/MigrationTestMigrations/Database/Migrations',
$this->root,
);
$this->expectException(ConfigException::class);
$this->expectExceptionMessage('Migrations have been loaded but are disabled or setup incorrectly.');
$runner->latest();
}
public function testVersionReturnsUpDownSuccess(): void
{
$forge = Database::forge();
$forge->dropTable('foo', true);
$config = $this->config;
$runner = new MigrationRunner($config);
$runner->setSilent(false);
$runner->clearHistory();
$runner = $runner->setNamespace('Tests\Support\MigrationTestMigrations');
$runner->latest();
$version = $runner->getBatchEnd($runner->getLastBatch());
$this->assertSame('2018-01-24-102302', $version);
$this->seeInDatabase('foo', ['key' => 'foobar']);
$runner->regress(0);
$version = $runner->getBatchEnd($runner->getLastBatch());
$this->assertSame('0', $version);
$this->assertFalse($this->db->tableExists('foo'));
}
public function testLatestSuccess(): void
{
$runner = new MigrationRunner($this->config);
$runner->setSilent(false)
->setNamespace('Tests\Support\MigrationTestMigrations')
->clearHistory();
$runner->latest();
$version = $runner->getBatchEnd($runner->getLastBatch());
$this->assertSame('2018-01-24-102302', $version);
$this->assertTrue(db_connect()->tableExists('foo'));
$this->seeInDatabase('migrations', [
'batch' => 1,
]);
}
public function testRegressSuccess(): void
{
$forge = Database::forge();
$forge->dropTable('foo', true);
$runner = new MigrationRunner($this->config);
$runner->setSilent(false)
->setNamespace('Tests\Support\MigrationTestMigrations')
->clearHistory();
$runner->latest();
$runner->regress();
$version = $runner->getBatchEnd($runner->getLastBatch());
$this->assertSame('0', $version);
$this->assertFalse(db_connect()->tableExists('foo'));
$history = $runner->getHistory();
$this->assertEmpty($history);
}
public function testLatestTriggersEvent(): void
{
$forge = Database::forge();
$forge->dropTable('foo', true);
$runner = new MigrationRunner($this->config);
$runner->setSilent(false)
->setNamespace('Tests\Support\MigrationTestMigrations');
$result = null;
Events::removeAllListeners();
Events::on('migrate', static function ($arg) use (&$result): void {
$result = $arg;
});
$runner->latest();
$this->assertIsArray($result);
$this->assertSame('latest', $result['method']);
}
public function testRegressTriggersEvent(): void
{
$forge = Database::forge();
$forge->dropTable('foo', true);
$runner = new MigrationRunner($this->config);
$runner->setSilent(false)
->setNamespace('Tests\Support\MigrationTestMigrations');
$result = null;
Events::removeAllListeners();
Events::on('migrate', static function ($arg) use (&$result): void {
$result = $arg;
});
$runner->latest();
$runner->regress();
$this->assertIsArray($result);
$this->assertSame('regress', $result['method']);
}
public function testHistoryRecordsBatches(): void
{
$config = $this->config;
$runner = new MigrationRunner($config);
$runner->setSilent(false);
$runner->clearHistory();
$this->resetTables();
$runner = $runner->setNamespace('Tests\Support\MigrationTestMigrations');
$runner->latest();
$version = $runner->getBatchEnd($runner->getLastBatch());
$this->assertSame('2018-01-24-102302', $version);
$history = $runner->getHistory('tests');
$this->assertSame(1, (int) $history[0]->batch);
$this->assertSame(1, (int) $history[1]->batch);
$this->seeInDatabase('migrations', ['batch' => 1]);
}
public function testGetBatchVersions(): void
{
$config = $this->config;
$runner = new MigrationRunner($config);
$runner->setSilent(false);
$runner->clearHistory();
$this->resetTables();
$runner = $runner->setNamespace('Tests\Support\MigrationTestMigrations');
$runner->latest();
$this->assertSame('2018-01-24-102301', $runner->getBatchStart(1));
$this->assertSame('2018-01-24-102302', $runner->getBatchEnd(1));
}
public function testMigrationUsesSameConnectionAsMigrationRunner(): void
{
$config = ['database' => WRITEPATH . 'runner.sqlite', 'DBDriver' => 'SQLite3', 'DBDebug' => true];
$database = Database::connect($config, false);
$this->resetTables($database);
$runner = new MigrationRunner(config(Migrations::class), $database);
$runner->clearCliMessages();
$runner->clearHistory();
$runner->setNamespace('Tests\Support\MigrationTestMigrations');
$runner->latest();
$tables = $database->listTables();
$this->assertCount(2, $tables);
$this->assertSame('migrations', $tables[0]);
$this->assertSame('foo', $tables[1]);
if (is_file($config['database'])) {
unlink($config['database']);
}
}
protected function resetTables($db = null): void
{
$forge = Database::forge($db);
/** @var BaseConnection $conn */
$conn = $forge->getConnection();
$conn->resetDataCache();
foreach (db_connect($db)->listTables() as $table) {
$table = str_replace('db_', '', $table);
$forge->dropTable($table, true);
}
}
}