Completed
Push — master ( f9e00b...0152fb )
by Sergei
26s queued 15s
created

ComparatorTest   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 1307
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 58
eloc 668
dl 0
loc 1307
rs 4.492
c 0
b 0
f 0

57 Methods

Rating   Name   Duplication   Size   Complexity  
A testCompareChangedColumnsChangeType() 0 8 1
A testCompareOnlyAutoincrementChanged() 0 9 1
A testCompareNewField() 0 35 1
A testCompareMissingTable() 0 12 1
A testCompareSame1() 0 22 1
A testCompareNewTable() 0 12 1
A testCompareSame2() 0 24 1
A testCompareMissingField() 0 36 1
A testCompareColumnCompareCaseInsensitive() 0 12 1
A testDetectRenameColumn() 0 15 1
A testCompareForeignKeyRestrictNoActionAreTheSame() 0 7 1
A testRemovedSequence() 0 12 1
A testDetectChangeIdentifierType() 0 15 1
A testSequencesCaseInsensitive() 0 18 1
A testCompareSequences() 0 12 1
A testTableAddForeignKey() 0 17 1
A testCompareColumnsMultipleTypeInstances() 0 11 1
A testCompareIndexBasedOnPropertiesNotName() 0 18 1
A testCompareChangedIndex() 0 61 1
A testAddedSequence() 0 12 1
A testTablesCaseInsensitive() 0 18 1
A testCompareChangeColumnsMultipleNewColumnsRename() 0 18 1
A testTableUpdateForeignKey() 0 18 1
A testDetectRenameColumnAmbiguous() 0 18 1
A testDetectRenameIndexAmbiguous() 0 21 1
A testCompareForeignKeyNamesUnqualifiedAsNoSchemaInformationIsAvailable() 0 7 1
A testCompareChangedIndexFieldPositions() 0 46 1
A testMovedForeignKeyForeignTable() 0 21 1
A testTableRemoveForeignKey() 0 17 1
A testDetectRenameIndex() 0 18 1
A testCompareForeignKeyBasedOnPropertiesNotName() 0 14 1
A testCompareRemovedIndex() 0 52 1
A testCompareChangedColumnsChangeCustomSchemaOption() 0 14 1
A testCompareNewIndex() 0 50 1
A testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff() 0 15 1
A testChangedSequence() 0 12 1
A testFqnSchemaComparison() 0 15 1
A testFqnSchemaComparisonNoSchemaSame() 0 14 1
A testCompareChangedBinaryColumn() 0 20 1
A testAvoidMultipleDropForeignKey() 0 31 1
A testNamespacesComparison() 0 22 1
A testAutoIncrementSequences() 0 17 1
A testAutoIncrementNoSequences() 0 17 1
A testDiff() 0 22 1
A testDiffDecimalWithNullPrecision() 0 9 1
A testCompareQuotedAndUnquotedForeignKeyColumns() 0 9 1
A testCompareChangedColumn() 0 20 1
A testComplexDiffColumn() 0 15 1
A testCompareGuidColumns() 0 13 1
A testCompareColumnComments() 0 16 2
A testForeignKeyRemovalWithRenamedLocalColumn() 0 46 1
A testDiffColumnPlatformOptions() 0 15 1
A assertSchemaSequenceChangeCount() 0 9 1
A assertSchemaTableChangeCount() 0 5 1
A getCompareColumnComments() 0 22 1
A testComparesNamespaces() 0 44 1
A testCompareColumnsOverriddenType() 0 15 1

How to fix   Complexity   

Complex Class

Complex classes like ComparatorTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ComparatorTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Doctrine\Tests\DBAL\Schema;
4
5
use Doctrine\DBAL\Schema\Column;
6
use Doctrine\DBAL\Schema\ColumnDiff;
7
use Doctrine\DBAL\Schema\Comparator;
8
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
9
use Doctrine\DBAL\Schema\Index;
10
use Doctrine\DBAL\Schema\Schema;
11
use Doctrine\DBAL\Schema\SchemaConfig;
12
use Doctrine\DBAL\Schema\SchemaDiff;
13
use Doctrine\DBAL\Schema\Sequence;
14
use Doctrine\DBAL\Schema\Table;
15
use Doctrine\DBAL\Schema\TableDiff;
16
use Doctrine\DBAL\Types\Type;
17
use PHPUnit\Framework\TestCase;
18
use function array_keys;
19
use function get_class;
20
21
class ComparatorTest extends TestCase
22
{
23
    public function testCompareSame1() : void
24
    {
25
        $schema1 = new Schema([
26
            'bugdb' => new Table(
27
                'bugdb',
28
                [
29
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
30
                ]
31
            ),
32
        ]);
33
        $schema2 = new Schema([
34
            'bugdb' => new Table(
35
                'bugdb',
36
                [
37
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
38
                ]
39
            ),
40
        ]);
41
42
        $expected             = new SchemaDiff();
43
        $expected->fromSchema = $schema1;
44
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
45
    }
46
47
    public function testCompareSame2() : void
48
    {
49
        $schema1 = new Schema([
50
            'bugdb' => new Table(
51
                'bugdb',
52
                [
53
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
54
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
55
                ]
56
            ),
57
        ]);
58
        $schema2 = new Schema([
59
            'bugdb' => new Table(
60
                'bugdb',
61
                [
62
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
63
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
64
                ]
65
            ),
66
        ]);
67
68
        $expected             = new SchemaDiff();
69
        $expected->fromSchema = $schema1;
70
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
71
    }
72
73
    public function testCompareMissingTable() : void
74
    {
75
        $schemaConfig = new SchemaConfig();
76
        $table        = new Table('bugdb', ['integerfield1' => new Column('integerfield1', Type::getType('integer'))]);
77
        $table->setSchemaConfig($schemaConfig);
78
79
        $schema1 = new Schema([$table], [], $schemaConfig);
80
        $schema2 = new Schema([], [], $schemaConfig);
81
82
        $expected = new SchemaDiff([], [], ['bugdb' => $table], $schema1);
83
84
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
85
    }
86
87
    public function testCompareNewTable() : void
88
    {
89
        $schemaConfig = new SchemaConfig();
90
        $table        = new Table('bugdb', ['integerfield1' => new Column('integerfield1', Type::getType('integer'))]);
91
        $table->setSchemaConfig($schemaConfig);
92
93
        $schema1 = new Schema([], [], $schemaConfig);
94
        $schema2 = new Schema([$table], [], $schemaConfig);
95
96
        $expected = new SchemaDiff(['bugdb' => $table], [], [], $schema1);
97
98
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
99
    }
100
101
    public function testCompareOnlyAutoincrementChanged() : void
102
    {
103
        $column1 = new Column('foo', Type::getType('integer'), ['autoincrement' => true]);
104
        $column2 = new Column('foo', Type::getType('integer'), ['autoincrement' => false]);
105
106
        $comparator        = new Comparator();
107
        $changedProperties = $comparator->diffColumn($column1, $column2);
108
109
        self::assertEquals(['autoincrement'], $changedProperties);
110
    }
111
112
    public function testCompareMissingField() : void
113
    {
114
        $missingColumn = new Column('integerfield1', Type::getType('integer'));
115
        $schema1       = new Schema([
116
            'bugdb' => new Table(
117
                'bugdb',
118
                [
119
                    'integerfield1' => $missingColumn,
120
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
121
                ]
122
            ),
123
        ]);
124
        $schema2       = new Schema([
125
            'bugdb' => new Table(
126
                'bugdb',
127
                [
128
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
129
                ]
130
            ),
131
        ]);
132
133
        $expected                                    = new SchemaDiff(
134
            [],
135
            [
136
                'bugdb' => new TableDiff(
137
                    'bugdb',
138
                    [],
139
                    [],
140
                    ['integerfield1' => $missingColumn]
141
                ),
142
            ]
143
        );
144
        $expected->fromSchema                        = $schema1;
145
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
146
147
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
148
    }
149
150
    public function testCompareNewField() : void
151
    {
152
        $schema1 = new Schema([
153
            'bugdb' => new Table(
154
                'bugdb',
155
                [
156
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
157
                ]
158
            ),
159
        ]);
160
        $schema2 = new Schema([
161
            'bugdb' => new Table(
162
                'bugdb',
163
                [
164
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
165
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
166
                ]
167
            ),
168
        ]);
169
170
        $expected                                    = new SchemaDiff(
171
            [],
172
            [
173
                'bugdb' => new TableDiff(
174
                    'bugdb',
175
                    [
176
                        'integerfield2' => new Column('integerfield2', Type::getType('integer')),
177
                    ]
178
                ),
179
            ]
180
        );
181
        $expected->fromSchema                        = $schema1;
182
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
183
184
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
185
    }
186
187
    public function testCompareChangedColumnsChangeType() : void
188
    {
189
        $column1 = new Column('charfield1', Type::getType('string'));
190
        $column2 = new Column('charfield1', Type::getType('integer'));
191
192
        $c = new Comparator();
193
        self::assertEquals(['type'], $c->diffColumn($column1, $column2));
194
        self::assertEquals([], $c->diffColumn($column1, $column1));
195
    }
196
197
    public function testCompareColumnsMultipleTypeInstances() : void
198
    {
199
        $integerType1 = Type::getType('integer');
200
        Type::overrideType('integer', get_class($integerType1));
201
        $integerType2 = Type::getType('integer');
202
203
        $column1 = new Column('integerfield1', $integerType1);
204
        $column2 = new Column('integerfield1', $integerType2);
205
206
        $c = new Comparator();
207
        self::assertEquals([], $c->diffColumn($column1, $column2));
208
    }
209
210
    public function testCompareColumnsOverriddenType() : void
211
    {
212
        $oldStringInstance = Type::getType('string');
213
        $integerType       = Type::getType('integer');
214
215
        Type::overrideType('string', get_class($integerType));
216
        $overriddenStringType = Type::getType('string');
217
218
        Type::overrideType('string', get_class($oldStringInstance));
219
220
        $column1 = new Column('integerfield1', $integerType);
221
        $column2 = new Column('integerfield1', $overriddenStringType);
222
223
        $c = new Comparator();
224
        self::assertEquals([], $c->diffColumn($column1, $column2));
225
    }
226
227
    public function testCompareChangedColumnsChangeCustomSchemaOption() : void
228
    {
229
        $column1 = new Column('charfield1', Type::getType('string'));
230
        $column2 = new Column('charfield1', Type::getType('string'));
231
232
        $column1->setCustomSchemaOption('foo', 'bar');
233
        $column2->setCustomSchemaOption('foo', 'bar');
234
235
        $column1->setCustomSchemaOption('foo1', 'bar1');
236
        $column2->setCustomSchemaOption('foo2', 'bar2');
237
238
        $c = new Comparator();
239
        self::assertEquals(['foo1', 'foo2'], $c->diffColumn($column1, $column2));
240
        self::assertEquals([], $c->diffColumn($column1, $column1));
241
    }
242
243
    public function testCompareChangeColumnsMultipleNewColumnsRename() : void
244
    {
245
        $tableA = new Table('foo');
246
        $tableA->addColumn('datefield1', 'datetime');
247
248
        $tableB = new Table('foo');
249
        $tableB->addColumn('new_datefield1', 'datetime');
250
        $tableB->addColumn('new_datefield2', 'datetime');
251
252
        $c         = new Comparator();
253
        $tableDiff = $c->diffTable($tableA, $tableB);
254
255
        self::assertCount(1, $tableDiff->renamedColumns, 'we should have one rename datefield1 => new_datefield1.');
256
        self::assertArrayHasKey('datefield1', $tableDiff->renamedColumns, "'datefield1' should be set to be renamed to new_datefield1");
257
        self::assertCount(1, $tableDiff->addedColumns, "'new_datefield2' should be added");
258
        self::assertArrayHasKey('new_datefield2', $tableDiff->addedColumns, "'new_datefield2' should be added, not created through renaming!");
259
        self::assertCount(0, $tableDiff->removedColumns, 'Nothing should be removed.');
260
        self::assertCount(0, $tableDiff->changedColumns, 'Nothing should be changed as all fields old & new have diff names.');
261
    }
262
263
    public function testCompareRemovedIndex() : void
264
    {
265
        $schema1 = new Schema([
266
            'bugdb' => new Table(
267
                'bugdb',
268
                [
269
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
270
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
271
                ],
272
                [
273
                    'primary' => new Index(
274
                        'primary',
275
                        ['integerfield1'],
276
                        true
277
                    ),
278
                ]
279
            ),
280
        ]);
281
        $schema2 = new Schema([
282
            'bugdb' => new Table(
283
                'bugdb',
284
                [
285
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
286
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
287
                ]
288
            ),
289
        ]);
290
291
        $expected                                    = new SchemaDiff(
292
            [],
293
            [
294
                'bugdb' => new TableDiff(
295
                    'bugdb',
296
                    [],
297
                    [],
298
                    [],
299
                    [],
300
                    [],
301
                    [
302
                        'primary' => new Index(
303
                            'primary',
304
                            ['integerfield1'],
305
                            true
306
                        ),
307
                    ]
308
                ),
309
            ]
310
        );
311
        $expected->fromSchema                        = $schema1;
312
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
313
314
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
315
    }
316
317
    public function testCompareNewIndex() : void
318
    {
319
        $schema1 = new Schema([
320
            'bugdb' => new Table(
321
                'bugdb',
322
                [
323
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
324
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
325
                ]
326
            ),
327
        ]);
328
        $schema2 = new Schema([
329
            'bugdb' => new Table(
330
                'bugdb',
331
                [
332
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
333
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
334
                ],
335
                [
336
                    'primary' => new Index(
337
                        'primary',
338
                        ['integerfield1'],
339
                        true
340
                    ),
341
                ]
342
            ),
343
        ]);
344
345
        $expected                                    = new SchemaDiff(
346
            [],
347
            [
348
                'bugdb' => new TableDiff(
349
                    'bugdb',
350
                    [],
351
                    [],
352
                    [],
353
                    [
354
                        'primary' => new Index(
355
                            'primary',
356
                            ['integerfield1'],
357
                            true
358
                        ),
359
                    ]
360
                ),
361
            ]
362
        );
363
        $expected->fromSchema                        = $schema1;
364
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
365
366
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
367
    }
368
369
    public function testCompareChangedIndex() : void
370
    {
371
        $schema1 = new Schema([
372
            'bugdb' => new Table(
373
                'bugdb',
374
                [
375
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
376
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
377
                ],
378
                [
379
                    'primary' => new Index(
380
                        'primary',
381
                        ['integerfield1'],
382
                        true
383
                    ),
384
                ]
385
            ),
386
        ]);
387
        $schema2 = new Schema([
388
            'bugdb' => new Table(
389
                'bugdb',
390
                [
391
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
392
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
393
                ],
394
                [
395
                    'primary' => new Index(
396
                        'primary',
397
                        ['integerfield1', 'integerfield2'],
398
                        true
399
                    ),
400
                ]
401
            ),
402
        ]);
403
404
        $expected                                    = new SchemaDiff(
405
            [],
406
            [
407
                'bugdb' => new TableDiff(
408
                    'bugdb',
409
                    [],
410
                    [],
411
                    [],
412
                    [],
413
                    [
414
                        'primary' => new Index(
415
                            'primary',
416
                            [
417
                                'integerfield1',
418
                                'integerfield2',
419
                            ],
420
                            true
421
                        ),
422
                    ]
423
                ),
424
            ]
425
        );
426
        $expected->fromSchema                        = $schema1;
427
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
428
429
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
430
    }
431
432
    public function testCompareChangedIndexFieldPositions() : void
433
    {
434
        $schema1 = new Schema([
435
            'bugdb' => new Table(
436
                'bugdb',
437
                [
438
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
439
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
440
                ],
441
                [
442
                    'primary' => new Index('primary', ['integerfield1', 'integerfield2'], true),
443
                ]
444
            ),
445
        ]);
446
        $schema2 = new Schema([
447
            'bugdb' => new Table(
448
                'bugdb',
449
                [
450
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
451
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
452
                ],
453
                [
454
                    'primary' => new Index('primary', ['integerfield2', 'integerfield1'], true),
455
                ]
456
            ),
457
        ]);
458
459
        $expected                                    = new SchemaDiff(
460
            [],
461
            [
462
                'bugdb' => new TableDiff(
463
                    'bugdb',
464
                    [],
465
                    [],
466
                    [],
467
                    [],
468
                    [
469
                        'primary' => new Index('primary', ['integerfield2', 'integerfield1'], true),
470
                    ]
471
                ),
472
            ]
473
        );
474
        $expected->fromSchema                        = $schema1;
475
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
476
477
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
478
    }
479
480
    public function testCompareSequences() : void
481
    {
482
        $seq1 = new Sequence('foo', 1, 1);
483
        $seq2 = new Sequence('foo', 1, 2);
484
        $seq3 = new Sequence('foo', 2, 1);
485
        $seq4 = new Sequence('foo', '1', '1');
486
487
        $c = new Comparator();
488
489
        self::assertTrue($c->diffSequence($seq1, $seq2));
490
        self::assertTrue($c->diffSequence($seq1, $seq3));
491
        self::assertFalse($c->diffSequence($seq1, $seq4));
492
    }
493
494
    public function testRemovedSequence() : void
495
    {
496
        $schema1 = new Schema();
497
        $seq     = $schema1->createSequence('foo');
498
499
        $schema2 = new Schema();
500
501
        $c          = new Comparator();
502
        $diffSchema = $c->compare($schema1, $schema2);
503
504
        self::assertCount(1, $diffSchema->removedSequences);
505
        self::assertSame($seq, $diffSchema->removedSequences[0]);
506
    }
507
508
    public function testAddedSequence() : void
509
    {
510
        $schema1 = new Schema();
511
512
        $schema2 = new Schema();
513
        $seq     = $schema2->createSequence('foo');
514
515
        $c          = new Comparator();
516
        $diffSchema = $c->compare($schema1, $schema2);
517
518
        self::assertCount(1, $diffSchema->newSequences);
519
        self::assertSame($seq, $diffSchema->newSequences[0]);
520
    }
521
522
    public function testTableAddForeignKey() : void
523
    {
524
        $tableForeign = new Table('bar');
525
        $tableForeign->addColumn('id', 'integer');
526
527
        $table1 = new Table('foo');
528
        $table1->addColumn('fk', 'integer');
529
530
        $table2 = new Table('foo');
531
        $table2->addColumn('fk', 'integer');
532
        $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
533
534
        $c         = new Comparator();
535
        $tableDiff = $c->diffTable($table1, $table2);
536
537
        self::assertInstanceOf(TableDiff::class, $tableDiff);
538
        self::assertCount(1, $tableDiff->addedForeignKeys);
539
    }
540
541
    public function testTableRemoveForeignKey() : void
542
    {
543
        $tableForeign = new Table('bar');
544
        $tableForeign->addColumn('id', 'integer');
545
546
        $table1 = new Table('foo');
547
        $table1->addColumn('fk', 'integer');
548
549
        $table2 = new Table('foo');
550
        $table2->addColumn('fk', 'integer');
551
        $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
552
553
        $c         = new Comparator();
554
        $tableDiff = $c->diffTable($table2, $table1);
555
556
        self::assertInstanceOf(TableDiff::class, $tableDiff);
557
        self::assertCount(1, $tableDiff->removedForeignKeys);
558
    }
559
560
    public function testTableUpdateForeignKey() : void
561
    {
562
        $tableForeign = new Table('bar');
563
        $tableForeign->addColumn('id', 'integer');
564
565
        $table1 = new Table('foo');
566
        $table1->addColumn('fk', 'integer');
567
        $table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
568
569
        $table2 = new Table('foo');
570
        $table2->addColumn('fk', 'integer');
571
        $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id'], ['onUpdate' => 'CASCADE']);
572
573
        $c         = new Comparator();
574
        $tableDiff = $c->diffTable($table1, $table2);
575
576
        self::assertInstanceOf(TableDiff::class, $tableDiff);
577
        self::assertCount(1, $tableDiff->changedForeignKeys);
578
    }
579
580
    public function testMovedForeignKeyForeignTable() : void
581
    {
582
        $tableForeign = new Table('bar');
583
        $tableForeign->addColumn('id', 'integer');
584
585
        $tableForeign2 = new Table('bar2');
586
        $tableForeign2->addColumn('id', 'integer');
587
588
        $table1 = new Table('foo');
589
        $table1->addColumn('fk', 'integer');
590
        $table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
591
592
        $table2 = new Table('foo');
593
        $table2->addColumn('fk', 'integer');
594
        $table2->addForeignKeyConstraint($tableForeign2, ['fk'], ['id']);
595
596
        $c         = new Comparator();
597
        $tableDiff = $c->diffTable($table1, $table2);
598
599
        self::assertInstanceOf(TableDiff::class, $tableDiff);
600
        self::assertCount(1, $tableDiff->changedForeignKeys);
601
    }
602
603
    public function testTablesCaseInsensitive() : void
604
    {
605
        $schemaA = new Schema();
606
        $schemaA->createTable('foo');
607
        $schemaA->createTable('bAr');
608
        $schemaA->createTable('BAZ');
609
        $schemaA->createTable('new');
610
611
        $schemaB = new Schema();
612
        $schemaB->createTable('FOO');
613
        $schemaB->createTable('bar');
614
        $schemaB->createTable('Baz');
615
        $schemaB->createTable('old');
616
617
        $c    = new Comparator();
618
        $diff = $c->compare($schemaA, $schemaB);
619
620
        self::assertSchemaTableChangeCount($diff, 1, 0, 1);
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\DBAL\Sche...chemaTableChangeCount() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

620
        self::/** @scrutinizer ignore-call */ 
621
              assertSchemaTableChangeCount($diff, 1, 0, 1);
Loading history...
621
    }
622
623
    public function testSequencesCaseInsensitive() : void
624
    {
625
        $schemaA = new Schema();
626
        $schemaA->createSequence('foo');
627
        $schemaA->createSequence('BAR');
628
        $schemaA->createSequence('Baz');
629
        $schemaA->createSequence('new');
630
631
        $schemaB = new Schema();
632
        $schemaB->createSequence('FOO');
633
        $schemaB->createSequence('Bar');
634
        $schemaB->createSequence('baz');
635
        $schemaB->createSequence('old');
636
637
        $c    = new Comparator();
638
        $diff = $c->compare($schemaA, $schemaB);
639
640
        self::assertSchemaSequenceChangeCount($diff, 1, 0, 1);
0 ignored issues
show
Bug Best Practice introduced by
The method Doctrine\Tests\DBAL\Sche...maSequenceChangeCount() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

640
        self::/** @scrutinizer ignore-call */ 
641
              assertSchemaSequenceChangeCount($diff, 1, 0, 1);
Loading history...
641
    }
642
643
    public function testCompareColumnCompareCaseInsensitive() : void
644
    {
645
        $tableA = new Table('foo');
646
        $tableA->addColumn('id', 'integer');
647
648
        $tableB = new Table('foo');
649
        $tableB->addColumn('ID', 'integer');
650
651
        $c         = new Comparator();
652
        $tableDiff = $c->diffTable($tableA, $tableB);
653
654
        self::assertFalse($tableDiff);
655
    }
656
657
    public function testCompareIndexBasedOnPropertiesNotName() : void
658
    {
659
        $tableA = new Table('foo');
660
        $tableA->addColumn('id', 'integer');
661
        $tableA->addIndex(['id'], 'foo_bar_idx');
662
663
        $tableB = new Table('foo');
664
        $tableB->addColumn('ID', 'integer');
665
        $tableB->addIndex(['id'], 'bar_foo_idx');
666
667
        $c                                        = new Comparator();
668
        $tableDiff                                = new TableDiff('foo');
669
        $tableDiff->fromTable                     = $tableA;
670
        $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', ['id']);
671
672
        self::assertEquals(
673
            $tableDiff,
674
            $c->diffTable($tableA, $tableB)
675
        );
676
    }
677
678
    public function testCompareForeignKeyBasedOnPropertiesNotName() : void
679
    {
680
        $tableA = new Table('foo');
681
        $tableA->addColumn('id', 'integer');
682
        $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', ['id'], ['id']);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Tab...dForeignKeyConstraint() has been deprecated: Use {@link addForeignKeyConstraint} ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

682
        /** @scrutinizer ignore-deprecated */ $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', ['id'], ['id']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
683
684
        $tableB = new Table('foo');
685
        $tableB->addColumn('ID', 'integer');
686
        $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', ['id'], ['id']);
1 ignored issue
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Tab...dForeignKeyConstraint() has been deprecated: Use {@link addForeignKeyConstraint} ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

686
        /** @scrutinizer ignore-deprecated */ $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', ['id'], ['id']);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
687
688
        $c         = new Comparator();
689
        $tableDiff = $c->diffTable($tableA, $tableB);
690
691
        self::assertFalse($tableDiff);
692
    }
693
694
    public function testCompareForeignKeyRestrictNoActionAreTheSame() : void
695
    {
696
        $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']);
697
        $fk2 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'RESTRICT']);
698
699
        $c = new Comparator();
700
        self::assertFalse($c->diffForeignKey($fk1, $fk2));
701
    }
702
703
    /**
704
     * @group DBAL-492
705
     */
706
    public function testCompareForeignKeyNamesUnqualifiedAsNoSchemaInformationIsAvailable() : void
707
    {
708
        $fk1 = new ForeignKeyConstraint(['foo'], 'foo.bar', ['baz'], 'fk1');
709
        $fk2 = new ForeignKeyConstraint(['foo'], 'baz.bar', ['baz'], 'fk1');
710
711
        $c = new Comparator();
712
        self::assertFalse($c->diffForeignKey($fk1, $fk2));
713
    }
714
715
    public function testDetectRenameColumn() : void
716
    {
717
        $tableA = new Table('foo');
718
        $tableA->addColumn('foo', 'integer');
719
720
        $tableB = new Table('foo');
721
        $tableB->addColumn('bar', 'integer');
722
723
        $c         = new Comparator();
724
        $tableDiff = $c->diffTable($tableA, $tableB);
725
726
        self::assertCount(0, $tableDiff->addedColumns);
727
        self::assertCount(0, $tableDiff->removedColumns);
728
        self::assertArrayHasKey('foo', $tableDiff->renamedColumns);
729
        self::assertEquals('bar', $tableDiff->renamedColumns['foo']->getName());
730
    }
731
732
    /**
733
     * You can easily have ambiguities in the column renaming. If these
734
     * are detected no renaming should take place, instead adding and dropping
735
     * should be used exclusively.
736
     *
737
     * @group DBAL-24
738
     */
739
    public function testDetectRenameColumnAmbiguous() : void
740
    {
741
        $tableA = new Table('foo');
742
        $tableA->addColumn('foo', 'integer');
743
        $tableA->addColumn('bar', 'integer');
744
745
        $tableB = new Table('foo');
746
        $tableB->addColumn('baz', 'integer');
747
748
        $c         = new Comparator();
749
        $tableDiff = $c->diffTable($tableA, $tableB);
750
751
        self::assertCount(1, $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
752
        self::assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
753
        self::assertCount(2, $tableDiff->removedColumns, "'foo' and 'bar' should both be dropped, an ambiguity exists which one could be renamed to 'baz'.");
754
        self::assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed.");
755
        self::assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed.");
756
        self::assertCount(0, $tableDiff->renamedColumns, 'no renamings should take place.');
757
    }
758
759
    /**
760
     * @group DBAL-1063
761
     */
762
    public function testDetectRenameIndex() : void
763
    {
764
        $table1 = new Table('foo');
765
        $table1->addColumn('foo', 'integer');
766
767
        $table2 = clone $table1;
768
769
        $table1->addIndex(['foo'], 'idx_foo');
770
771
        $table2->addIndex(['foo'], 'idx_bar');
772
773
        $comparator = new Comparator();
774
        $tableDiff  = $comparator->diffTable($table1, $table2);
775
776
        self::assertCount(0, $tableDiff->addedIndexes);
777
        self::assertCount(0, $tableDiff->removedIndexes);
778
        self::assertArrayHasKey('idx_foo', $tableDiff->renamedIndexes);
779
        self::assertEquals('idx_bar', $tableDiff->renamedIndexes['idx_foo']->getName());
780
    }
781
782
    /**
783
     * You can easily have ambiguities in the index renaming. If these
784
     * are detected no renaming should take place, instead adding and dropping
785
     * should be used exclusively.
786
     *
787
     * @group DBAL-1063
788
     */
789
    public function testDetectRenameIndexAmbiguous() : void
790
    {
791
        $table1 = new Table('foo');
792
        $table1->addColumn('foo', 'integer');
793
794
        $table2 = clone $table1;
795
796
        $table1->addIndex(['foo'], 'idx_foo');
797
        $table1->addIndex(['foo'], 'idx_bar');
798
799
        $table2->addIndex(['foo'], 'idx_baz');
800
801
        $comparator = new Comparator();
802
        $tableDiff  = $comparator->diffTable($table1, $table2);
803
804
        self::assertCount(1, $tableDiff->addedIndexes);
805
        self::assertArrayHasKey('idx_baz', $tableDiff->addedIndexes);
806
        self::assertCount(2, $tableDiff->removedIndexes);
807
        self::assertArrayHasKey('idx_foo', $tableDiff->removedIndexes);
808
        self::assertArrayHasKey('idx_bar', $tableDiff->removedIndexes);
809
        self::assertCount(0, $tableDiff->renamedIndexes);
810
    }
811
812
    public function testDetectChangeIdentifierType() : void
813
    {
814
        $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.');
815
816
        $tableA = new Table('foo');
817
        $tableA->addColumn('id', 'integer', ['autoincrement' => false]);
818
819
        $tableB = new Table('foo');
820
        $tableB->addColumn('id', 'integer', ['autoincrement' => true]);
821
822
        $c         = new Comparator();
823
        $tableDiff = $c->diffTable($tableA, $tableB);
824
825
        self::assertInstanceOf(TableDiff::class, $tableDiff);
826
        self::assertArrayHasKey('id', $tableDiff->changedColumns);
827
    }
828
829
    /**
830
     * @group DBAL-105
831
     */
832
    public function testDiff() : void
833
    {
834
        $table = new Table('twitter_users');
835
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
836
        $table->addColumn('twitterId', 'integer');
837
        $table->addColumn('displayName', 'string');
838
        $table->setPrimaryKey(['id']);
839
840
        $newtable = new Table('twitter_users');
841
        $newtable->addColumn('id', 'integer', ['autoincrement' => true]);
842
        $newtable->addColumn('twitter_id', 'integer');
843
        $newtable->addColumn('display_name', 'string');
844
        $newtable->addColumn('logged_in_at', 'datetime');
845
        $newtable->setPrimaryKey(['id']);
846
847
        $c         = new Comparator();
848
        $tableDiff = $c->diffTable($table, $newtable);
849
850
        self::assertInstanceOf(TableDiff::class, $tableDiff);
851
        self::assertEquals(['twitterid', 'displayname'], array_keys($tableDiff->renamedColumns));
852
        self::assertEquals(['logged_in_at'], array_keys($tableDiff->addedColumns));
853
        self::assertCount(0, $tableDiff->removedColumns);
854
    }
855
856
    /**
857
     * @group DBAL-112
858
     */
859
    public function testChangedSequence() : void
860
    {
861
        $schema   = new Schema();
862
        $sequence = $schema->createSequence('baz');
0 ignored issues
show
Unused Code introduced by
The assignment to $sequence is dead and can be removed.
Loading history...
863
864
        $schemaNew = clone $schema;
865
        $schemaNew->getSequence('baz')->setAllocationSize(20);
866
867
        $c    = new Comparator();
868
        $diff = $c->compare($schema, $schemaNew);
869
870
        self::assertSame($diff->changedSequences[0], $schemaNew->getSequence('baz'));
871
    }
872
873
    /**
874
     * @group DBAL-106
875
     */
876
    public function testDiffDecimalWithNullPrecision() : void
877
    {
878
        $column = new Column('foo', Type::getType('decimal'));
879
        $column->setPrecision(null);
880
881
        $column2 = new Column('foo', Type::getType('decimal'));
882
883
        $c = new Comparator();
884
        self::assertEquals([], $c->diffColumn($column, $column2));
885
    }
886
887
    /**
888
     * @group DBAL-204
889
     */
890
    public function testFqnSchemaComparison() : void
891
    {
892
        $config = new SchemaConfig();
893
        $config->setName('foo');
894
895
        $oldSchema = new Schema([], [], $config);
896
        $oldSchema->createTable('bar');
897
898
        $newSchema = new Schema([], [], $config);
899
        $newSchema->createTable('foo.bar');
900
901
        $expected             = new SchemaDiff();
902
        $expected->fromSchema = $oldSchema;
903
904
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
905
    }
906
907
    /**
908
     * @group DBAL-669
909
     */
910
    public function testNamespacesComparison() : void
911
    {
912
        $config = new SchemaConfig();
913
        $config->setName('schemaName');
914
915
        $oldSchema = new Schema([], [], $config);
916
        $oldSchema->createTable('taz');
917
        $oldSchema->createTable('war.tab');
918
919
        $newSchema = new Schema([], [], $config);
920
        $newSchema->createTable('bar.tab');
921
        $newSchema->createTable('baz.tab');
922
        $newSchema->createTable('war.tab');
923
924
        $expected                = new SchemaDiff();
925
        $expected->fromSchema    = $oldSchema;
926
        $expected->newNamespaces = ['bar' => 'bar', 'baz' => 'baz'];
927
928
        $diff = Comparator::compareSchemas($oldSchema, $newSchema);
929
930
        self::assertEquals(['bar' => 'bar', 'baz' => 'baz'], $diff->newNamespaces);
931
        self::assertCount(2, $diff->newTables);
932
    }
933
934
    /**
935
     * @group DBAL-204
936
     */
937
    public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff() : void
938
    {
939
        $config = new SchemaConfig();
940
        $config->setName('foo');
941
942
        $oldSchema = new Schema([], [], $config);
943
        $oldSchema->createTable('foo.bar');
944
945
        $newSchema = new Schema();
946
        $newSchema->createTable('bar');
947
948
        $expected             = new SchemaDiff();
949
        $expected->fromSchema = $oldSchema;
950
951
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
952
    }
953
954
    /**
955
     * @group DBAL-204
956
     */
957
    public function testFqnSchemaComparisonNoSchemaSame() : void
958
    {
959
        $config = new SchemaConfig();
960
        $config->setName('foo');
961
        $oldSchema = new Schema([], [], $config);
962
        $oldSchema->createTable('bar');
963
964
        $newSchema = new Schema();
965
        $newSchema->createTable('bar');
966
967
        $expected             = new SchemaDiff();
968
        $expected->fromSchema = $oldSchema;
969
970
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
971
    }
972
973
    /**
974
     * @group DDC-1657
975
     */
976
    public function testAutoIncrementSequences() : void
977
    {
978
        $oldSchema = new Schema();
979
        $table     = $oldSchema->createTable('foo');
980
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
981
        $table->setPrimaryKey(['id']);
982
        $oldSchema->createSequence('foo_id_seq');
983
984
        $newSchema = new Schema();
985
        $table     = $newSchema->createTable('foo');
986
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
987
        $table->setPrimaryKey(['id']);
988
989
        $c    = new Comparator();
990
        $diff = $c->compare($oldSchema, $newSchema);
991
992
        self::assertCount(0, $diff->removedSequences);
993
    }
994
995
    /**
996
     * Check that added autoincrement sequence is not populated in newSequences
997
     *
998
     * @group DBAL-562
999
     */
1000
    public function testAutoIncrementNoSequences() : void
1001
    {
1002
        $oldSchema = new Schema();
1003
        $table     = $oldSchema->createTable('foo');
1004
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
1005
        $table->setPrimaryKey(['id']);
1006
1007
        $newSchema = new Schema();
1008
        $table     = $newSchema->createTable('foo');
1009
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
1010
        $table->setPrimaryKey(['id']);
1011
        $newSchema->createSequence('foo_id_seq');
1012
1013
        $c    = new Comparator();
1014
        $diff = $c->compare($oldSchema, $newSchema);
1015
1016
        self::assertCount(0, $diff->newSequences);
1017
    }
1018
1019
    /**
1020
     * You can get multiple drops for a FK when a table referenced by a foreign
1021
     * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys
1022
     * array because of the dropped table, and once on changedTables array. We
1023
     * now check that the key is present once.
1024
     */
1025
    public function testAvoidMultipleDropForeignKey() : void
1026
    {
1027
        $oldSchema = new Schema();
1028
1029
        $tableA = $oldSchema->createTable('table_a');
1030
        $tableA->addColumn('id', 'integer');
1031
1032
        $tableB = $oldSchema->createTable('table_b');
1033
        $tableB->addColumn('id', 'integer');
1034
1035
        $tableC = $oldSchema->createTable('table_c');
1036
        $tableC->addColumn('id', 'integer');
1037
        $tableC->addColumn('table_a_id', 'integer');
1038
        $tableC->addColumn('table_b_id', 'integer');
1039
1040
        $tableC->addForeignKeyConstraint($tableA, ['table_a_id'], ['id']);
1041
        $tableC->addForeignKeyConstraint($tableB, ['table_b_id'], ['id']);
1042
1043
        $newSchema = new Schema();
1044
1045
        $tableB = $newSchema->createTable('table_b');
1046
        $tableB->addColumn('id', 'integer');
1047
1048
        $tableC = $newSchema->createTable('table_c');
1049
        $tableC->addColumn('id', 'integer');
1050
1051
        $comparator = new Comparator();
1052
        $schemaDiff = $comparator->compare($oldSchema, $newSchema);
1053
1054
        self::assertCount(1, $schemaDiff->changedTables['table_c']->removedForeignKeys);
1055
        self::assertCount(1, $schemaDiff->orphanedForeignKeys);
1056
    }
1057
1058
    public function testCompareChangedColumn() : void
1059
    {
1060
        $oldSchema = new Schema();
1061
1062
        $tableFoo = $oldSchema->createTable('foo');
1063
        $tableFoo->addColumn('id', 'integer');
1064
1065
        $newSchema = new Schema();
1066
        $table     = $newSchema->createTable('foo');
1067
        $table->addColumn('id', 'string');
1068
1069
        $expected                      = new SchemaDiff();
1070
        $expected->fromSchema          = $oldSchema;
1071
        $tableDiff                     = $expected->changedTables['foo'] = new TableDiff('foo');
1072
        $tableDiff->fromTable          = $tableFoo;
1073
        $columnDiff                    = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
1074
        $columnDiff->fromColumn        = $tableFoo->getColumn('id');
1075
        $columnDiff->changedProperties = ['type'];
1076
1077
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
1078
    }
1079
1080
    public function testCompareChangedBinaryColumn() : void
1081
    {
1082
        $oldSchema = new Schema();
1083
1084
        $tableFoo = $oldSchema->createTable('foo');
1085
        $tableFoo->addColumn('id', 'binary');
1086
1087
        $newSchema = new Schema();
1088
        $table     = $newSchema->createTable('foo');
1089
        $table->addColumn('id', 'binary', ['length' => 42, 'fixed' => true]);
1090
1091
        $expected                      = new SchemaDiff();
1092
        $expected->fromSchema          = $oldSchema;
1093
        $tableDiff                     = $expected->changedTables['foo'] = new TableDiff('foo');
1094
        $tableDiff->fromTable          = $tableFoo;
1095
        $columnDiff                    = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
1096
        $columnDiff->fromColumn        = $tableFoo->getColumn('id');
1097
        $columnDiff->changedProperties = ['length', 'fixed'];
1098
1099
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
1100
    }
1101
1102
    /**
1103
     * @group DBAL-617
1104
     */
1105
    public function testCompareQuotedAndUnquotedForeignKeyColumns() : void
1106
    {
1107
        $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']);
1108
        $fk2 = new ForeignKeyConstraint(['`foo`'], 'bar', ['`baz`'], 'fk1', ['onDelete' => 'NO ACTION']);
1109
1110
        $comparator = new Comparator();
1111
        $diff       = $comparator->diffForeignKey($fk1, $fk2);
1112
1113
        self::assertFalse($diff);
1114
    }
1115
1116
    public function assertSchemaTableChangeCount(SchemaDiff $diff, int $newTableCount = 0, int $changeTableCount = 0, int $removeTableCount = 0) : void
1117
    {
1118
        self::assertCount($newTableCount, $diff->newTables);
1119
        self::assertCount($changeTableCount, $diff->changedTables);
1120
        self::assertCount($removeTableCount, $diff->removedTables);
1121
    }
1122
1123
    public function assertSchemaSequenceChangeCount(
1124
        SchemaDiff $diff,
1125
        int $newSequenceCount = 0,
1126
        int $changeSequenceCount = 0,
1127
        int $removeSequenceCount = 0
1128
    ) : void {
1129
        self::assertCount($newSequenceCount, $diff->newSequences, 'Expected number of new sequences is wrong.');
1130
        self::assertCount($changeSequenceCount, $diff->changedSequences, 'Expected number of changed sequences is wrong.');
1131
        self::assertCount($removeSequenceCount, $diff->removedSequences, 'Expected number of removed sequences is wrong.');
1132
    }
1133
1134
    public function testDiffColumnPlatformOptions() : void
1135
    {
1136
        $column1 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'bar' => 'bar']]);
1137
        $column2 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'foobar' => 'foobar']]);
1138
        $column3 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'bar' => 'rab']]);
1139
        $column4 = new Column('foo', Type::getType('string'));
1140
1141
        $comparator = new Comparator();
1142
1143
        self::assertEquals([], $comparator->diffColumn($column1, $column2));
1144
        self::assertEquals([], $comparator->diffColumn($column2, $column1));
1145
        self::assertEquals(['bar'], $comparator->diffColumn($column1, $column3));
1146
        self::assertEquals(['bar'], $comparator->diffColumn($column3, $column1));
1147
        self::assertEquals([], $comparator->diffColumn($column1, $column4));
1148
        self::assertEquals([], $comparator->diffColumn($column4, $column1));
1149
    }
1150
1151
    public function testComplexDiffColumn() : void
1152
    {
1153
        $column1 = new Column('foo', Type::getType('string'), [
1154
            'platformOptions' => ['foo' => 'foo'],
1155
            'customSchemaOptions' => ['foo' => 'bar'],
1156
        ]);
1157
1158
        $column2 = new Column('foo', Type::getType('string'), [
1159
            'platformOptions' => ['foo' => 'bar'],
1160
        ]);
1161
1162
        $comparator = new Comparator();
1163
1164
        self::assertEquals([], $comparator->diffColumn($column1, $column2));
1165
        self::assertEquals([], $comparator->diffColumn($column2, $column1));
1166
    }
1167
1168
    /**
1169
     * @group DBAL-669
1170
     */
1171
    public function testComparesNamespaces() : void
1172
    {
1173
        $comparator = new Comparator();
1174
        $fromSchema = $this->getMockBuilder(Schema::class)
1175
            ->setMethods(['getNamespaces', 'hasNamespace'])
1176
            ->getMock();
1177
        $toSchema   = $this->getMockBuilder(Schema::class)
1178
            ->setMethods(['getNamespaces', 'hasNamespace'])
1179
            ->getMock();
1180
1181
        $fromSchema->expects($this->once())
1182
            ->method('getNamespaces')
1183
            ->will($this->returnValue(['foo', 'bar']));
1184
1185
        $fromSchema->expects($this->at(0))
1186
            ->method('hasNamespace')
1187
            ->with('bar')
1188
            ->will($this->returnValue(true));
1189
1190
        $fromSchema->expects($this->at(1))
1191
            ->method('hasNamespace')
1192
            ->with('baz')
1193
            ->will($this->returnValue(false));
1194
1195
        $toSchema->expects($this->once())
1196
            ->method('getNamespaces')
1197
            ->will($this->returnValue(['bar', 'baz']));
1198
1199
        $toSchema->expects($this->at(1))
1200
            ->method('hasNamespace')
1201
            ->with('foo')
1202
            ->will($this->returnValue(false));
1203
1204
        $toSchema->expects($this->at(2))
1205
            ->method('hasNamespace')
1206
            ->with('bar')
1207
            ->will($this->returnValue(true));
1208
1209
        $expected                    = new SchemaDiff();
1210
        $expected->fromSchema        = $fromSchema;
1 ignored issue
show
Documentation Bug introduced by
It seems like $fromSchema of type PHPUnit\Framework\MockObject\MockObject is incompatible with the declared type Doctrine\DBAL\Schema\Schema|null of property $fromSchema.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1211
        $expected->newNamespaces     = ['baz' => 'baz'];
1212
        $expected->removedNamespaces = ['foo' => 'foo'];
1213
1214
        self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema));
1215
    }
1216
1217
    public function testCompareGuidColumns() : void
1218
    {
1219
        $comparator = new Comparator();
1220
1221
        $column1 = new Column('foo', Type::getType('guid'), ['comment' => 'GUID 1']);
1222
        $column2 = new Column(
1223
            'foo',
1224
            Type::getType('guid'),
1225
            ['notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.']
1226
        );
1227
1228
        self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column1, $column2));
1229
        self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column2, $column1));
1230
    }
1231
1232
    /**
1233
     * @group DBAL-1009
1234
     * @dataProvider getCompareColumnComments
1235
     */
1236
    public function testCompareColumnComments(?string $comment1, ?string $comment2, bool $equals) : void
1237
    {
1238
        $column1 = new Column('foo', Type::getType('integer'), ['comment' => $comment1]);
1239
        $column2 = new Column('foo', Type::getType('integer'), ['comment' => $comment2]);
1240
1241
        $comparator = new Comparator();
1242
1243
        $expectedDiff = $equals ? [] : ['comment'];
1244
1245
        $actualDiff = $comparator->diffColumn($column1, $column2);
1246
1247
        self::assertSame($expectedDiff, $actualDiff);
1248
1249
        $actualDiff = $comparator->diffColumn($column2, $column1);
1250
1251
        self::assertSame($expectedDiff, $actualDiff);
1252
    }
1253
1254
    /**
1255
     * @return mixed[][]
1256
     */
1257
    public static function getCompareColumnComments() : iterable
1258
    {
1259
        return [
1260
            [null, null, true],
1261
            ['', '', true],
1262
            [' ', ' ', true],
1263
            ['0', '0', true],
1264
            ['foo', 'foo', true],
1265
1266
            [null, '', true],
1267
            [null, ' ', false],
1268
            [null, '0', false],
1269
            [null, 'foo', false],
1270
1271
            ['', ' ', false],
1272
            ['', '0', false],
1273
            ['', 'foo', false],
1274
1275
            [' ', '0', false],
1276
            [' ', 'foo', false],
1277
1278
            ['0', 'foo', false],
1279
        ];
1280
    }
1281
1282
    public function testForeignKeyRemovalWithRenamedLocalColumn() : void
1283
    {
1284
        $fromSchema = new Schema([
1285
            'table1' => new Table(
1286
                'table1',
1287
                [
1288
                    'id' => new Column('id', Type::getType('integer')),
1289
                ]
1290
            ),
1291
            'table2' => new Table(
1292
                'table2',
1293
                [
1294
                    'id' => new Column('id', Type::getType('integer')),
1295
                    'id_table1' => new Column('id_table1', Type::getType('integer')),
1296
                ],
1297
                [],
1298
                [
1299
                    new ForeignKeyConstraint(['id_table1'], 'table1', ['id'], 'fk_table2_table1'),
1300
                ]
1301
            ),
1302
        ]);
1303
        $toSchema   = new Schema([
1304
            'table2' => new Table(
1305
                'table2',
1306
                [
1307
                    'id' => new Column('id', Type::getType('integer')),
1308
                    'id_table3' => new Column('id_table3', Type::getType('integer')),
1309
                ],
1310
                [],
1311
                [
1312
                    new ForeignKeyConstraint(['id_table3'], 'table3', ['id'], 'fk_table2_table3'),
1313
                ]
1314
            ),
1315
            'table3' => new Table(
1316
                'table3',
1317
                [
1318
                    'id' => new Column('id', Type::getType('integer')),
1319
                ]
1320
            ),
1321
        ]);
1322
        $actual     = Comparator::compareSchemas($fromSchema, $toSchema);
1323
        self::assertArrayHasKey('table2', $actual->changedTables);
1324
        self::assertCount(1, $actual->orphanedForeignKeys);
1325
        self::assertEquals('fk_table2_table1', $actual->orphanedForeignKeys[0]->getName());
1326
        self::assertCount(1, $actual->changedTables['table2']->addedForeignKeys, 'FK to table3 should be added.');
1327
        self::assertEquals('table3', $actual->changedTables['table2']->addedForeignKeys[0]->getForeignTableName());
1328
    }
1329
}
1330