Completed
Pull Request — master (#3610)
by Sergei
06:29
created

ComparatorTest::testNamespacesComparison()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
20
class ComparatorTest extends TestCase
21
{
22
    public function testCompareSame1() : void
23
    {
24
        $schema1 = new Schema([
25
            'bugdb' => new Table(
26
                'bugdb',
27
                [
28
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
29
                ]
30
            ),
31
        ]);
32
        $schema2 = new Schema([
33
            'bugdb' => new Table(
34
                'bugdb',
35
                [
36
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
37
                ]
38
            ),
39
        ]);
40
41
        $expected             = new SchemaDiff();
42
        $expected->fromSchema = $schema1;
43
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
44
    }
45
46
    public function testCompareSame2() : void
47
    {
48
        $schema1 = new Schema([
49
            'bugdb' => new Table(
50
                'bugdb',
51
                [
52
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
53
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
54
                ]
55
            ),
56
        ]);
57
        $schema2 = new Schema([
58
            'bugdb' => new Table(
59
                'bugdb',
60
                [
61
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
62
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
63
                ]
64
            ),
65
        ]);
66
67
        $expected             = new SchemaDiff();
68
        $expected->fromSchema = $schema1;
69
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
70
    }
71
72
    public function testCompareMissingTable() : void
73
    {
74
        $schemaConfig = new SchemaConfig();
75
        $table        = new Table('bugdb', ['integerfield1' => new Column('integerfield1', Type::getType('integer'))]);
76
        $table->setSchemaConfig($schemaConfig);
77
78
        $schema1 = new Schema([$table], [], $schemaConfig);
79
        $schema2 = new Schema([], [], $schemaConfig);
80
81
        $expected = new SchemaDiff([], [], ['bugdb' => $table], $schema1);
82
83
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
84
    }
85
86
    public function testCompareNewTable() : void
87
    {
88
        $schemaConfig = new SchemaConfig();
89
        $table        = new Table('bugdb', ['integerfield1' => new Column('integerfield1', Type::getType('integer'))]);
90
        $table->setSchemaConfig($schemaConfig);
91
92
        $schema1 = new Schema([], [], $schemaConfig);
93
        $schema2 = new Schema([$table], [], $schemaConfig);
94
95
        $expected = new SchemaDiff(['bugdb' => $table], [], [], $schema1);
96
97
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
98
    }
99
100
    public function testCompareOnlyAutoincrementChanged() : void
101
    {
102
        $column1 = new Column('foo', Type::getType('integer'), ['autoincrement' => true]);
103
        $column2 = new Column('foo', Type::getType('integer'), ['autoincrement' => false]);
104
105
        $comparator        = new Comparator();
106
        $changedProperties = $comparator->diffColumn($column1, $column2);
107
108
        self::assertEquals(['autoincrement'], $changedProperties);
109
    }
110
111
    public function testCompareMissingField() : void
112
    {
113
        $missingColumn = new Column('integerfield1', Type::getType('integer'));
114
        $schema1       = new Schema([
115
            'bugdb' => new Table(
116
                'bugdb',
117
                [
118
                    'integerfield1' => $missingColumn,
119
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
120
                ]
121
            ),
122
        ]);
123
        $schema2       = new Schema([
124
            'bugdb' => new Table(
125
                'bugdb',
126
                [
127
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
128
                ]
129
            ),
130
        ]);
131
132
        $expected                                    = new SchemaDiff(
133
            [],
134
            [
135
                'bugdb' => new TableDiff(
136
                    'bugdb',
137
                    [],
138
                    [],
139
                    ['integerfield1' => $missingColumn]
140
                ),
141
            ]
142
        );
143
        $expected->fromSchema                        = $schema1;
144
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
145
146
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
147
    }
148
149
    public function testCompareNewField() : void
150
    {
151
        $schema1 = new Schema([
152
            'bugdb' => new Table(
153
                'bugdb',
154
                [
155
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
156
                ]
157
            ),
158
        ]);
159
        $schema2 = new Schema([
160
            'bugdb' => new Table(
161
                'bugdb',
162
                [
163
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
164
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
165
                ]
166
            ),
167
        ]);
168
169
        $expected                                    = new SchemaDiff(
170
            [],
171
            [
172
                'bugdb' => new TableDiff(
173
                    'bugdb',
174
                    [
175
                        'integerfield2' => new Column('integerfield2', Type::getType('integer')),
176
                    ]
177
                ),
178
            ]
179
        );
180
        $expected->fromSchema                        = $schema1;
181
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
182
183
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
184
    }
185
186
    public function testCompareChangedColumnsChangeType() : void
187
    {
188
        $column1 = new Column('charfield1', Type::getType('string'));
189
        $column2 = new Column('charfield1', Type::getType('integer'));
190
191
        $c = new Comparator();
192
        self::assertEquals(['type'], $c->diffColumn($column1, $column2));
193
        self::assertEquals([], $c->diffColumn($column1, $column1));
194
    }
195
196
    public function testCompareChangedColumnsChangeCustomSchemaOption() : void
197
    {
198
        $column1 = new Column('charfield1', Type::getType('string'));
199
        $column2 = new Column('charfield1', Type::getType('string'));
200
201
        $column1->setCustomSchemaOption('foo', 'bar');
202
        $column2->setCustomSchemaOption('foo', 'bar');
203
204
        $column1->setCustomSchemaOption('foo1', 'bar1');
205
        $column2->setCustomSchemaOption('foo2', 'bar2');
206
207
        $c = new Comparator();
208
        self::assertEquals(['foo1', 'foo2'], $c->diffColumn($column1, $column2));
209
        self::assertEquals([], $c->diffColumn($column1, $column1));
210
    }
211
212
    public function testCompareChangeColumnsMultipleNewColumnsRename() : void
213
    {
214
        $tableA = new Table('foo');
215
        $tableA->addColumn('datefield1', 'datetime');
216
217
        $tableB = new Table('foo');
218
        $tableB->addColumn('new_datefield1', 'datetime');
219
        $tableB->addColumn('new_datefield2', 'datetime');
220
221
        $c         = new Comparator();
222
        $tableDiff = $c->diffTable($tableA, $tableB);
223
224
        self::assertCount(1, $tableDiff->renamedColumns, 'we should have one rename datefield1 => new_datefield1.');
225
        self::assertArrayHasKey('datefield1', $tableDiff->renamedColumns, "'datefield1' should be set to be renamed to new_datefield1");
226
        self::assertCount(1, $tableDiff->addedColumns, "'new_datefield2' should be added");
227
        self::assertArrayHasKey('new_datefield2', $tableDiff->addedColumns, "'new_datefield2' should be added, not created through renaming!");
228
        self::assertCount(0, $tableDiff->removedColumns, 'Nothing should be removed.');
229
        self::assertCount(0, $tableDiff->changedColumns, 'Nothing should be changed as all fields old & new have diff names.');
230
    }
231
232
    public function testCompareRemovedIndex() : void
233
    {
234
        $schema1 = new Schema([
235
            'bugdb' => new Table(
236
                'bugdb',
237
                [
238
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
239
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
240
                ],
241
                [
242
                    'primary' => new Index(
243
                        'primary',
244
                        ['integerfield1'],
245
                        true
246
                    ),
247
                ]
248
            ),
249
        ]);
250
        $schema2 = new Schema([
251
            'bugdb' => new Table(
252
                'bugdb',
253
                [
254
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
255
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
256
                ]
257
            ),
258
        ]);
259
260
        $expected                                    = new SchemaDiff(
261
            [],
262
            [
263
                'bugdb' => new TableDiff(
264
                    'bugdb',
265
                    [],
266
                    [],
267
                    [],
268
                    [],
269
                    [],
270
                    [
271
                        'primary' => new Index(
272
                            'primary',
273
                            ['integerfield1'],
274
                            true
275
                        ),
276
                    ]
277
                ),
278
            ]
279
        );
280
        $expected->fromSchema                        = $schema1;
281
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
282
283
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
284
    }
285
286
    public function testCompareNewIndex() : void
287
    {
288
        $schema1 = new Schema([
289
            'bugdb' => new Table(
290
                'bugdb',
291
                [
292
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
293
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
294
                ]
295
            ),
296
        ]);
297
        $schema2 = new Schema([
298
            'bugdb' => new Table(
299
                'bugdb',
300
                [
301
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
302
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
303
                ],
304
                [
305
                    'primary' => new Index(
306
                        'primary',
307
                        ['integerfield1'],
308
                        true
309
                    ),
310
                ]
311
            ),
312
        ]);
313
314
        $expected                                    = new SchemaDiff(
315
            [],
316
            [
317
                'bugdb' => new TableDiff(
318
                    'bugdb',
319
                    [],
320
                    [],
321
                    [],
322
                    [
323
                        'primary' => new Index(
324
                            'primary',
325
                            ['integerfield1'],
326
                            true
327
                        ),
328
                    ]
329
                ),
330
            ]
331
        );
332
        $expected->fromSchema                        = $schema1;
333
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
334
335
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
336
    }
337
338
    public function testCompareChangedIndex() : void
339
    {
340
        $schema1 = new Schema([
341
            'bugdb' => new Table(
342
                'bugdb',
343
                [
344
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
345
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
346
                ],
347
                [
348
                    'primary' => new Index(
349
                        'primary',
350
                        ['integerfield1'],
351
                        true
352
                    ),
353
                ]
354
            ),
355
        ]);
356
        $schema2 = new Schema([
357
            'bugdb' => new Table(
358
                'bugdb',
359
                [
360
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
361
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
362
                ],
363
                [
364
                    'primary' => new Index(
365
                        'primary',
366
                        ['integerfield1', 'integerfield2'],
367
                        true
368
                    ),
369
                ]
370
            ),
371
        ]);
372
373
        $expected                                    = new SchemaDiff(
374
            [],
375
            [
376
                'bugdb' => new TableDiff(
377
                    'bugdb',
378
                    [],
379
                    [],
380
                    [],
381
                    [],
382
                    [
383
                        'primary' => new Index(
384
                            'primary',
385
                            [
386
                                'integerfield1',
387
                                'integerfield2',
388
                            ],
389
                            true
390
                        ),
391
                    ]
392
                ),
393
            ]
394
        );
395
        $expected->fromSchema                        = $schema1;
396
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
397
398
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
399
    }
400
401
    public function testCompareChangedIndexFieldPositions() : void
402
    {
403
        $schema1 = new Schema([
404
            'bugdb' => new Table(
405
                'bugdb',
406
                [
407
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
408
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
409
                ],
410
                [
411
                    'primary' => new Index('primary', ['integerfield1', 'integerfield2'], true),
412
                ]
413
            ),
414
        ]);
415
        $schema2 = new Schema([
416
            'bugdb' => new Table(
417
                'bugdb',
418
                [
419
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
420
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
421
                ],
422
                [
423
                    'primary' => new Index('primary', ['integerfield2', 'integerfield1'], true),
424
                ]
425
            ),
426
        ]);
427
428
        $expected                                    = new SchemaDiff(
429
            [],
430
            [
431
                'bugdb' => new TableDiff(
432
                    'bugdb',
433
                    [],
434
                    [],
435
                    [],
436
                    [],
437
                    [
438
                        'primary' => new Index('primary', ['integerfield2', 'integerfield1'], true),
439
                    ]
440
                ),
441
            ]
442
        );
443
        $expected->fromSchema                        = $schema1;
444
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
445
446
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
447
    }
448
449
    public function testCompareSequences() : void
450
    {
451
        $seq1 = new Sequence('foo', 1, 1);
452
        $seq2 = new Sequence('foo', 1, 2);
453
        $seq3 = new Sequence('foo', 2, 1);
454
        $seq4 = new Sequence('foo', '1', '1');
455
456
        $c = new Comparator();
457
458
        self::assertTrue($c->diffSequence($seq1, $seq2));
459
        self::assertTrue($c->diffSequence($seq1, $seq3));
460
        self::assertFalse($c->diffSequence($seq1, $seq4));
461
    }
462
463
    public function testRemovedSequence() : void
464
    {
465
        $schema1 = new Schema();
466
        $seq     = $schema1->createSequence('foo');
467
468
        $schema2 = new Schema();
469
470
        $c          = new Comparator();
471
        $diffSchema = $c->compare($schema1, $schema2);
472
473
        self::assertCount(1, $diffSchema->removedSequences);
0 ignored issues
show
Documentation introduced by
$diffSchema->removedSequences is of type array<integer,object<Doc...\DBAL\Schema\Sequence>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
474
        self::assertSame($seq, $diffSchema->removedSequences[0]);
475
    }
476
477
    public function testAddedSequence() : void
478
    {
479
        $schema1 = new Schema();
480
481
        $schema2 = new Schema();
482
        $seq     = $schema2->createSequence('foo');
483
484
        $c          = new Comparator();
485
        $diffSchema = $c->compare($schema1, $schema2);
486
487
        self::assertCount(1, $diffSchema->newSequences);
0 ignored issues
show
Documentation introduced by
$diffSchema->newSequences is of type array<integer,object<Doc...\DBAL\Schema\Sequence>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
488
        self::assertSame($seq, $diffSchema->newSequences[0]);
489
    }
490
491
    public function testTableAddForeignKey() : void
492
    {
493
        $tableForeign = new Table('bar');
494
        $tableForeign->addColumn('id', 'integer');
495
496
        $table1 = new Table('foo');
497
        $table1->addColumn('fk', 'integer');
498
499
        $table2 = new Table('foo');
500
        $table2->addColumn('fk', 'integer');
501
        $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
502
503
        $c         = new Comparator();
504
        $tableDiff = $c->diffTable($table1, $table2);
505
506
        self::assertInstanceOf(TableDiff::class, $tableDiff);
507
        self::assertCount(1, $tableDiff->addedForeignKeys);
508
    }
509
510
    public function testTableRemoveForeignKey() : void
511
    {
512
        $tableForeign = new Table('bar');
513
        $tableForeign->addColumn('id', 'integer');
514
515
        $table1 = new Table('foo');
516
        $table1->addColumn('fk', 'integer');
517
518
        $table2 = new Table('foo');
519
        $table2->addColumn('fk', 'integer');
520
        $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
521
522
        $c         = new Comparator();
523
        $tableDiff = $c->diffTable($table2, $table1);
524
525
        self::assertInstanceOf(TableDiff::class, $tableDiff);
526
        self::assertCount(1, $tableDiff->removedForeignKeys);
527
    }
528
529
    public function testTableUpdateForeignKey() : void
530
    {
531
        $tableForeign = new Table('bar');
532
        $tableForeign->addColumn('id', 'integer');
533
534
        $table1 = new Table('foo');
535
        $table1->addColumn('fk', 'integer');
536
        $table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
537
538
        $table2 = new Table('foo');
539
        $table2->addColumn('fk', 'integer');
540
        $table2->addForeignKeyConstraint($tableForeign, ['fk'], ['id'], ['onUpdate' => 'CASCADE']);
541
542
        $c         = new Comparator();
543
        $tableDiff = $c->diffTable($table1, $table2);
544
545
        self::assertInstanceOf(TableDiff::class, $tableDiff);
546
        self::assertCount(1, $tableDiff->changedForeignKeys);
547
    }
548
549
    public function testMovedForeignKeyForeignTable() : void
550
    {
551
        $tableForeign = new Table('bar');
552
        $tableForeign->addColumn('id', 'integer');
553
554
        $tableForeign2 = new Table('bar2');
555
        $tableForeign2->addColumn('id', 'integer');
556
557
        $table1 = new Table('foo');
558
        $table1->addColumn('fk', 'integer');
559
        $table1->addForeignKeyConstraint($tableForeign, ['fk'], ['id']);
560
561
        $table2 = new Table('foo');
562
        $table2->addColumn('fk', 'integer');
563
        $table2->addForeignKeyConstraint($tableForeign2, ['fk'], ['id']);
564
565
        $c         = new Comparator();
566
        $tableDiff = $c->diffTable($table1, $table2);
567
568
        self::assertInstanceOf(TableDiff::class, $tableDiff);
569
        self::assertCount(1, $tableDiff->changedForeignKeys);
570
    }
571
572
    public function testTablesCaseInsensitive() : void
573
    {
574
        $schemaA = new Schema();
575
        $schemaA->createTable('foo');
576
        $schemaA->createTable('bAr');
577
        $schemaA->createTable('BAZ');
578
        $schemaA->createTable('new');
579
580
        $schemaB = new Schema();
581
        $schemaB->createTable('FOO');
582
        $schemaB->createTable('bar');
583
        $schemaB->createTable('Baz');
584
        $schemaB->createTable('old');
585
586
        $c    = new Comparator();
587
        $diff = $c->compare($schemaA, $schemaB);
588
589
        self::assertSchemaTableChangeCount($diff, 1, 0, 1);
590
    }
591
592
    public function testSequencesCaseInsensitive() : void
593
    {
594
        $schemaA = new Schema();
595
        $schemaA->createSequence('foo');
596
        $schemaA->createSequence('BAR');
597
        $schemaA->createSequence('Baz');
598
        $schemaA->createSequence('new');
599
600
        $schemaB = new Schema();
601
        $schemaB->createSequence('FOO');
602
        $schemaB->createSequence('Bar');
603
        $schemaB->createSequence('baz');
604
        $schemaB->createSequence('old');
605
606
        $c    = new Comparator();
607
        $diff = $c->compare($schemaA, $schemaB);
608
609
        self::assertSchemaSequenceChangeCount($diff, 1, 0, 1);
610
    }
611
612
    public function testCompareColumnCompareCaseInsensitive() : void
613
    {
614
        $tableA = new Table('foo');
615
        $tableA->addColumn('id', 'integer');
616
617
        $tableB = new Table('foo');
618
        $tableB->addColumn('ID', 'integer');
619
620
        $c         = new Comparator();
621
        $tableDiff = $c->diffTable($tableA, $tableB);
622
623
        self::assertFalse($tableDiff);
624
    }
625
626
    public function testCompareIndexBasedOnPropertiesNotName() : void
627
    {
628
        $tableA = new Table('foo');
629
        $tableA->addColumn('id', 'integer');
630
        $tableA->addIndex(['id'], 'foo_bar_idx');
631
632
        $tableB = new Table('foo');
633
        $tableB->addColumn('ID', 'integer');
634
        $tableB->addIndex(['id'], 'bar_foo_idx');
635
636
        $c                                        = new Comparator();
637
        $tableDiff                                = new TableDiff('foo');
638
        $tableDiff->fromTable                     = $tableA;
639
        $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', ['id']);
640
641
        self::assertEquals(
642
            $tableDiff,
643
            $c->diffTable($tableA, $tableB)
644
        );
645
    }
646
647
    public function testCompareForeignKeyBasedOnPropertiesNotName() : void
648
    {
649
        $tableA = new Table('foo');
650
        $tableA->addColumn('id', 'integer');
651
        $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', ['id'], ['id']);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Schema\Tab...dForeignKeyConstraint() has been deprecated with message: Use {@link addForeignKeyConstraint}

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

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

Loading history...
652
653
        $tableB = new Table('foo');
654
        $tableB->addColumn('ID', 'integer');
655
        $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', ['id'], ['id']);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Schema\Tab...dForeignKeyConstraint() has been deprecated with message: Use {@link addForeignKeyConstraint}

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

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

Loading history...
656
657
        $c         = new Comparator();
658
        $tableDiff = $c->diffTable($tableA, $tableB);
659
660
        self::assertFalse($tableDiff);
661
    }
662
663
    public function testCompareForeignKeyRestrictNoActionAreTheSame() : void
664
    {
665
        $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']);
666
        $fk2 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'RESTRICT']);
667
668
        $c = new Comparator();
669
        self::assertFalse($c->diffForeignKey($fk1, $fk2));
670
    }
671
672
    /**
673
     * @group DBAL-492
674
     */
675
    public function testCompareForeignKeyNamesUnqualifiedAsNoSchemaInformationIsAvailable() : void
676
    {
677
        $fk1 = new ForeignKeyConstraint(['foo'], 'foo.bar', ['baz'], 'fk1');
678
        $fk2 = new ForeignKeyConstraint(['foo'], 'baz.bar', ['baz'], 'fk1');
679
680
        $c = new Comparator();
681
        self::assertFalse($c->diffForeignKey($fk1, $fk2));
682
    }
683
684
    public function testDetectRenameColumn() : void
685
    {
686
        $tableA = new Table('foo');
687
        $tableA->addColumn('foo', 'integer');
688
689
        $tableB = new Table('foo');
690
        $tableB->addColumn('bar', 'integer');
691
692
        $c         = new Comparator();
693
        $tableDiff = $c->diffTable($tableA, $tableB);
694
695
        self::assertCount(0, $tableDiff->addedColumns);
696
        self::assertCount(0, $tableDiff->removedColumns);
697
        self::assertArrayHasKey('foo', $tableDiff->renamedColumns);
698
        self::assertEquals('bar', $tableDiff->renamedColumns['foo']->getName());
699
    }
700
701
    /**
702
     * You can easily have ambiguities in the column renaming. If these
703
     * are detected no renaming should take place, instead adding and dropping
704
     * should be used exclusively.
705
     *
706
     * @group DBAL-24
707
     */
708
    public function testDetectRenameColumnAmbiguous() : void
709
    {
710
        $tableA = new Table('foo');
711
        $tableA->addColumn('foo', 'integer');
712
        $tableA->addColumn('bar', 'integer');
713
714
        $tableB = new Table('foo');
715
        $tableB->addColumn('baz', 'integer');
716
717
        $c         = new Comparator();
718
        $tableDiff = $c->diffTable($tableA, $tableB);
719
720
        self::assertCount(1, $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
721
        self::assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
722
        self::assertCount(2, $tableDiff->removedColumns, "'foo' and 'bar' should both be dropped, an ambiguity exists which one could be renamed to 'baz'.");
723
        self::assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed.");
724
        self::assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed.");
725
        self::assertCount(0, $tableDiff->renamedColumns, 'no renamings should take place.');
726
    }
727
728
    /**
729
     * @group DBAL-1063
730
     */
731
    public function testDetectRenameIndex() : void
732
    {
733
        $table1 = new Table('foo');
734
        $table1->addColumn('foo', 'integer');
735
736
        $table2 = clone $table1;
737
738
        $table1->addIndex(['foo'], 'idx_foo');
739
740
        $table2->addIndex(['foo'], 'idx_bar');
741
742
        $comparator = new Comparator();
743
        $tableDiff  = $comparator->diffTable($table1, $table2);
744
745
        self::assertCount(0, $tableDiff->addedIndexes);
746
        self::assertCount(0, $tableDiff->removedIndexes);
747
        self::assertArrayHasKey('idx_foo', $tableDiff->renamedIndexes);
748
        self::assertEquals('idx_bar', $tableDiff->renamedIndexes['idx_foo']->getName());
749
    }
750
751
    /**
752
     * You can easily have ambiguities in the index renaming. If these
753
     * are detected no renaming should take place, instead adding and dropping
754
     * should be used exclusively.
755
     *
756
     * @group DBAL-1063
757
     */
758
    public function testDetectRenameIndexAmbiguous() : void
759
    {
760
        $table1 = new Table('foo');
761
        $table1->addColumn('foo', 'integer');
762
763
        $table2 = clone $table1;
764
765
        $table1->addIndex(['foo'], 'idx_foo');
766
        $table1->addIndex(['foo'], 'idx_bar');
767
768
        $table2->addIndex(['foo'], 'idx_baz');
769
770
        $comparator = new Comparator();
771
        $tableDiff  = $comparator->diffTable($table1, $table2);
772
773
        self::assertCount(1, $tableDiff->addedIndexes);
774
        self::assertArrayHasKey('idx_baz', $tableDiff->addedIndexes);
775
        self::assertCount(2, $tableDiff->removedIndexes);
776
        self::assertArrayHasKey('idx_foo', $tableDiff->removedIndexes);
777
        self::assertArrayHasKey('idx_bar', $tableDiff->removedIndexes);
778
        self::assertCount(0, $tableDiff->renamedIndexes);
779
    }
780
781
    public function testDetectChangeIdentifierType() : void
782
    {
783
        $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.');
784
785
        $tableA = new Table('foo');
786
        $tableA->addColumn('id', 'integer', ['autoincrement' => false]);
787
788
        $tableB = new Table('foo');
789
        $tableB->addColumn('id', 'integer', ['autoincrement' => true]);
790
791
        $c         = new Comparator();
792
        $tableDiff = $c->diffTable($tableA, $tableB);
793
794
        self::assertInstanceOf(TableDiff::class, $tableDiff);
795
        self::assertArrayHasKey('id', $tableDiff->changedColumns);
796
    }
797
798
    /**
799
     * @group DBAL-105
800
     */
801
    public function testDiff() : void
802
    {
803
        $table = new Table('twitter_users');
804
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
805
        $table->addColumn('twitterId', 'integer');
806
        $table->addColumn('displayName', 'string');
807
        $table->setPrimaryKey(['id']);
808
809
        $newtable = new Table('twitter_users');
810
        $newtable->addColumn('id', 'integer', ['autoincrement' => true]);
811
        $newtable->addColumn('twitter_id', 'integer');
812
        $newtable->addColumn('display_name', 'string');
813
        $newtable->addColumn('logged_in_at', 'datetime');
814
        $newtable->setPrimaryKey(['id']);
815
816
        $c         = new Comparator();
817
        $tableDiff = $c->diffTable($table, $newtable);
818
819
        self::assertInstanceOf(TableDiff::class, $tableDiff);
820
        self::assertEquals(['twitterid', 'displayname'], array_keys($tableDiff->renamedColumns));
821
        self::assertEquals(['logged_in_at'], array_keys($tableDiff->addedColumns));
822
        self::assertCount(0, $tableDiff->removedColumns);
823
    }
824
825
    /**
826
     * @group DBAL-112
827
     */
828
    public function testChangedSequence() : void
829
    {
830
        $schema   = new Schema();
831
        $sequence = $schema->createSequence('baz');
0 ignored issues
show
Unused Code introduced by
$sequence is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
832
833
        $schemaNew = clone $schema;
834
        $schemaNew->getSequence('baz')->setAllocationSize(20);
835
836
        $c    = new Comparator();
837
        $diff = $c->compare($schema, $schemaNew);
838
839
        self::assertSame($diff->changedSequences[0], $schemaNew->getSequence('baz'));
840
    }
841
842
    /**
843
     * @group DBAL-106
844
     */
845
    public function testDiffDecimalWithNullPrecision() : void
846
    {
847
        $column = new Column('foo', Type::getType('decimal'));
848
        $column->setPrecision(null);
849
850
        $column2 = new Column('foo', Type::getType('decimal'));
851
852
        $c = new Comparator();
853
        self::assertEquals([], $c->diffColumn($column, $column2));
854
    }
855
856
    /**
857
     * @group DBAL-204
858
     */
859
    public function testFqnSchemaComparison() : void
860
    {
861
        $config = new SchemaConfig();
862
        $config->setName('foo');
863
864
        $oldSchema = new Schema([], [], $config);
865
        $oldSchema->createTable('bar');
866
867
        $newSchema = new Schema([], [], $config);
868
        $newSchema->createTable('foo.bar');
869
870
        $expected             = new SchemaDiff();
871
        $expected->fromSchema = $oldSchema;
872
873
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
874
    }
875
876
    /**
877
     * @group DBAL-669
878
     */
879
    public function testNamespacesComparison() : void
880
    {
881
        $config = new SchemaConfig();
882
        $config->setName('schemaName');
883
884
        $oldSchema = new Schema([], [], $config);
885
        $oldSchema->createTable('taz');
886
        $oldSchema->createTable('war.tab');
887
888
        $newSchema = new Schema([], [], $config);
889
        $newSchema->createTable('bar.tab');
890
        $newSchema->createTable('baz.tab');
891
        $newSchema->createTable('war.tab');
892
893
        $expected                = new SchemaDiff();
894
        $expected->fromSchema    = $oldSchema;
895
        $expected->newNamespaces = ['bar' => 'bar', 'baz' => 'baz'];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('bar' => 'bar', 'baz' => 'baz') of type array<string,string,{"ba...tring","baz":"string"}> is incompatible with the declared type array<integer,string> of property $newNamespaces.

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...
896
897
        $diff = Comparator::compareSchemas($oldSchema, $newSchema);
898
899
        self::assertEquals(['bar' => 'bar', 'baz' => 'baz'], $diff->newNamespaces);
900
        self::assertCount(2, $diff->newTables);
0 ignored issues
show
Documentation introduced by
$diff->newTables is of type array<integer,object<Doctrine\DBAL\Schema\Table>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
901
    }
902
903
    /**
904
     * @group DBAL-204
905
     */
906
    public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff() : void
907
    {
908
        $config = new SchemaConfig();
909
        $config->setName('foo');
910
911
        $oldSchema = new Schema([], [], $config);
912
        $oldSchema->createTable('foo.bar');
913
914
        $newSchema = new Schema();
915
        $newSchema->createTable('bar');
916
917
        $expected             = new SchemaDiff();
918
        $expected->fromSchema = $oldSchema;
919
920
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
921
    }
922
923
    /**
924
     * @group DBAL-204
925
     */
926
    public function testFqnSchemaComparisonNoSchemaSame() : void
927
    {
928
        $config = new SchemaConfig();
929
        $config->setName('foo');
930
        $oldSchema = new Schema([], [], $config);
931
        $oldSchema->createTable('bar');
932
933
        $newSchema = new Schema();
934
        $newSchema->createTable('bar');
935
936
        $expected             = new SchemaDiff();
937
        $expected->fromSchema = $oldSchema;
938
939
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
940
    }
941
942
    /**
943
     * @group DDC-1657
944
     */
945
    public function testAutoIncrementSequences() : void
946
    {
947
        $oldSchema = new Schema();
948
        $table     = $oldSchema->createTable('foo');
949
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
950
        $table->setPrimaryKey(['id']);
951
        $oldSchema->createSequence('foo_id_seq');
952
953
        $newSchema = new Schema();
954
        $table     = $newSchema->createTable('foo');
955
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
956
        $table->setPrimaryKey(['id']);
957
958
        $c    = new Comparator();
959
        $diff = $c->compare($oldSchema, $newSchema);
960
961
        self::assertCount(0, $diff->removedSequences);
0 ignored issues
show
Documentation introduced by
$diff->removedSequences is of type array<integer,object<Doc...\DBAL\Schema\Sequence>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
962
    }
963
964
    /**
965
     * Check that added autoincrement sequence is not populated in newSequences
966
     *
967
     * @group DBAL-562
968
     */
969
    public function testAutoIncrementNoSequences() : void
970
    {
971
        $oldSchema = new Schema();
972
        $table     = $oldSchema->createTable('foo');
973
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
974
        $table->setPrimaryKey(['id']);
975
976
        $newSchema = new Schema();
977
        $table     = $newSchema->createTable('foo');
978
        $table->addColumn('id', 'integer', ['autoincrement' => true]);
979
        $table->setPrimaryKey(['id']);
980
        $newSchema->createSequence('foo_id_seq');
981
982
        $c    = new Comparator();
983
        $diff = $c->compare($oldSchema, $newSchema);
984
985
        self::assertCount(0, $diff->newSequences);
0 ignored issues
show
Documentation introduced by
$diff->newSequences is of type array<integer,object<Doc...\DBAL\Schema\Sequence>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
986
    }
987
988
    /**
989
     * You can get multiple drops for a FK when a table referenced by a foreign
990
     * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys
991
     * array because of the dropped table, and once on changedTables array. We
992
     * now check that the key is present once.
993
     */
994
    public function testAvoidMultipleDropForeignKey() : void
995
    {
996
        $oldSchema = new Schema();
997
998
        $tableA = $oldSchema->createTable('table_a');
999
        $tableA->addColumn('id', 'integer');
1000
1001
        $tableB = $oldSchema->createTable('table_b');
1002
        $tableB->addColumn('id', 'integer');
1003
1004
        $tableC = $oldSchema->createTable('table_c');
1005
        $tableC->addColumn('id', 'integer');
1006
        $tableC->addColumn('table_a_id', 'integer');
1007
        $tableC->addColumn('table_b_id', 'integer');
1008
1009
        $tableC->addForeignKeyConstraint($tableA, ['table_a_id'], ['id']);
1010
        $tableC->addForeignKeyConstraint($tableB, ['table_b_id'], ['id']);
1011
1012
        $newSchema = new Schema();
1013
1014
        $tableB = $newSchema->createTable('table_b');
1015
        $tableB->addColumn('id', 'integer');
1016
1017
        $tableC = $newSchema->createTable('table_c');
1018
        $tableC->addColumn('id', 'integer');
1019
1020
        $comparator = new Comparator();
1021
        $schemaDiff = $comparator->compare($oldSchema, $newSchema);
1022
1023
        self::assertCount(1, $schemaDiff->changedTables['table_c']->removedForeignKeys);
0 ignored issues
show
Documentation introduced by
$schemaDiff->changedTabl...c']->removedForeignKeys is of type array<integer,object<Doc...nKeyConstraint>|string>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1024
        self::assertCount(1, $schemaDiff->orphanedForeignKeys);
0 ignored issues
show
Documentation introduced by
$schemaDiff->orphanedForeignKeys is of type array<integer,object<Doc...\ForeignKeyConstraint>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1025
    }
1026
1027
    public function testCompareChangedColumn() : void
1028
    {
1029
        $oldSchema = new Schema();
1030
1031
        $tableFoo = $oldSchema->createTable('foo');
1032
        $tableFoo->addColumn('id', 'integer');
1033
1034
        $newSchema = new Schema();
1035
        $table     = $newSchema->createTable('foo');
1036
        $table->addColumn('id', 'string');
1037
1038
        $expected                      = new SchemaDiff();
1039
        $expected->fromSchema          = $oldSchema;
1040
        $tableDiff                     = $expected->changedTables['foo'] = new TableDiff('foo');
1041
        $tableDiff->fromTable          = $tableFoo;
1042
        $columnDiff                    = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
1043
        $columnDiff->fromColumn        = $tableFoo->getColumn('id');
1044
        $columnDiff->changedProperties = ['type'];
1045
1046
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
1047
    }
1048
1049
    public function testCompareChangedBinaryColumn() : void
1050
    {
1051
        $oldSchema = new Schema();
1052
1053
        $tableFoo = $oldSchema->createTable('foo');
1054
        $tableFoo->addColumn('id', 'binary');
1055
1056
        $newSchema = new Schema();
1057
        $table     = $newSchema->createTable('foo');
1058
        $table->addColumn('id', 'binary', ['length' => 42, 'fixed' => true]);
1059
1060
        $expected                      = new SchemaDiff();
1061
        $expected->fromSchema          = $oldSchema;
1062
        $tableDiff                     = $expected->changedTables['foo'] = new TableDiff('foo');
1063
        $tableDiff->fromTable          = $tableFoo;
1064
        $columnDiff                    = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
1065
        $columnDiff->fromColumn        = $tableFoo->getColumn('id');
1066
        $columnDiff->changedProperties = ['length', 'fixed'];
1067
1068
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
1069
    }
1070
1071
    /**
1072
     * @group DBAL-617
1073
     */
1074
    public function testCompareQuotedAndUnquotedForeignKeyColumns() : void
1075
    {
1076
        $fk1 = new ForeignKeyConstraint(['foo'], 'bar', ['baz'], 'fk1', ['onDelete' => 'NO ACTION']);
1077
        $fk2 = new ForeignKeyConstraint(['`foo`'], 'bar', ['`baz`'], 'fk1', ['onDelete' => 'NO ACTION']);
1078
1079
        $comparator = new Comparator();
1080
        $diff       = $comparator->diffForeignKey($fk1, $fk2);
1081
1082
        self::assertFalse($diff);
1083
    }
1084
1085
    public function assertSchemaTableChangeCount(SchemaDiff $diff, int $newTableCount = 0, int $changeTableCount = 0, int $removeTableCount = 0) : void
1086
    {
1087
        self::assertCount($newTableCount, $diff->newTables);
0 ignored issues
show
Documentation introduced by
$diff->newTables is of type array<integer,object<Doctrine\DBAL\Schema\Table>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1088
        self::assertCount($changeTableCount, $diff->changedTables);
0 ignored issues
show
Documentation introduced by
$diff->changedTables is of type array<integer,object<Doc...DBAL\Schema\TableDiff>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1089
        self::assertCount($removeTableCount, $diff->removedTables);
0 ignored issues
show
Documentation introduced by
$diff->removedTables is of type array<integer,object<Doctrine\DBAL\Schema\Table>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1090
    }
1091
1092
    public function assertSchemaSequenceChangeCount(
1093
        SchemaDiff $diff,
1094
        int $newSequenceCount = 0,
1095
        int $changeSequenceCount = 0,
1096
        int $removeSequenceCount = 0
1097
    ) : void {
1098
        self::assertCount($newSequenceCount, $diff->newSequences, 'Expected number of new sequences is wrong.');
0 ignored issues
show
Documentation introduced by
$diff->newSequences is of type array<integer,object<Doc...\DBAL\Schema\Sequence>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1099
        self::assertCount($changeSequenceCount, $diff->changedSequences, 'Expected number of changed sequences is wrong.');
0 ignored issues
show
Documentation introduced by
$diff->changedSequences is of type array<integer,object<Doc...\DBAL\Schema\Sequence>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1100
        self::assertCount($removeSequenceCount, $diff->removedSequences, 'Expected number of removed sequences is wrong.');
0 ignored issues
show
Documentation introduced by
$diff->removedSequences is of type array<integer,object<Doc...\DBAL\Schema\Sequence>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1101
    }
1102
1103
    public function testDiffColumnPlatformOptions() : void
1104
    {
1105
        $column1 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'bar' => 'bar']]);
1106
        $column2 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'foobar' => 'foobar']]);
1107
        $column3 = new Column('foo', Type::getType('string'), ['platformOptions' => ['foo' => 'foo', 'bar' => 'rab']]);
1108
        $column4 = new Column('foo', Type::getType('string'));
1109
1110
        $comparator = new Comparator();
1111
1112
        self::assertEquals([], $comparator->diffColumn($column1, $column2));
1113
        self::assertEquals([], $comparator->diffColumn($column2, $column1));
1114
        self::assertEquals(['bar'], $comparator->diffColumn($column1, $column3));
1115
        self::assertEquals(['bar'], $comparator->diffColumn($column3, $column1));
1116
        self::assertEquals([], $comparator->diffColumn($column1, $column4));
1117
        self::assertEquals([], $comparator->diffColumn($column4, $column1));
1118
    }
1119
1120
    public function testComplexDiffColumn() : void
1121
    {
1122
        $column1 = new Column('foo', Type::getType('string'), [
1123
            'platformOptions' => ['foo' => 'foo'],
1124
            'customSchemaOptions' => ['foo' => 'bar'],
1125
        ]);
1126
1127
        $column2 = new Column('foo', Type::getType('string'), [
1128
            'platformOptions' => ['foo' => 'bar'],
1129
        ]);
1130
1131
        $comparator = new Comparator();
1132
1133
        self::assertEquals([], $comparator->diffColumn($column1, $column2));
1134
        self::assertEquals([], $comparator->diffColumn($column2, $column1));
1135
    }
1136
1137
    /**
1138
     * @group DBAL-669
1139
     */
1140
    public function testComparesNamespaces() : void
1141
    {
1142
        $comparator = new Comparator();
1143
        $fromSchema = $this->getMockBuilder(Schema::class)
1144
            ->setMethods(['getNamespaces', 'hasNamespace'])
1145
            ->getMock();
1146
        $toSchema   = $this->getMockBuilder(Schema::class)
1147
            ->setMethods(['getNamespaces', 'hasNamespace'])
1148
            ->getMock();
1149
1150
        $fromSchema->expects($this->once())
1151
            ->method('getNamespaces')
1152
            ->will($this->returnValue(['foo', 'bar']));
1153
1154
        $fromSchema->expects($this->at(0))
1155
            ->method('hasNamespace')
1156
            ->with('bar')
1157
            ->will($this->returnValue(true));
1158
1159
        $fromSchema->expects($this->at(1))
1160
            ->method('hasNamespace')
1161
            ->with('baz')
1162
            ->will($this->returnValue(false));
1163
1164
        $toSchema->expects($this->once())
1165
            ->method('getNamespaces')
1166
            ->will($this->returnValue(['bar', 'baz']));
1167
1168
        $toSchema->expects($this->at(1))
1169
            ->method('hasNamespace')
1170
            ->with('foo')
1171
            ->will($this->returnValue(false));
1172
1173
        $toSchema->expects($this->at(2))
1174
            ->method('hasNamespace')
1175
            ->with('bar')
1176
            ->will($this->returnValue(true));
1177
1178
        $expected                    = new SchemaDiff();
1179
        $expected->fromSchema        = $fromSchema;
0 ignored issues
show
Documentation Bug introduced by
It seems like $fromSchema of type object<PHPUnit\Framework\MockObject\MockObject> is incompatible with the declared type object<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...
1180
        $expected->newNamespaces     = ['baz' => 'baz'];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('baz' => 'baz') of type array<string,string,{"baz":"string"}> is incompatible with the declared type array<integer,string> of property $newNamespaces.

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...
1181
        $expected->removedNamespaces = ['foo' => 'foo'];
0 ignored issues
show
Documentation Bug introduced by
It seems like array('foo' => 'foo') of type array<string,string,{"foo":"string"}> is incompatible with the declared type array<integer,string> of property $removedNamespaces.

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...
1182
1183
        self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema));
0 ignored issues
show
Documentation introduced by
$fromSchema is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Schema\Schema>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$toSchema is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\DBAL\Schema\Schema>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1184
    }
1185
1186
    public function testCompareGuidColumns() : void
1187
    {
1188
        $comparator = new Comparator();
1189
1190
        $column1 = new Column('foo', Type::getType('guid'), ['comment' => 'GUID 1']);
1191
        $column2 = new Column(
1192
            'foo',
1193
            Type::getType('guid'),
1194
            ['notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.']
1195
        );
1196
1197
        self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column1, $column2));
1198
        self::assertEquals(['notnull', 'default', 'comment'], $comparator->diffColumn($column2, $column1));
1199
    }
1200
1201
    /**
1202
     * @group DBAL-1009
1203
     * @dataProvider getCompareColumnComments
1204
     */
1205
    public function testCompareColumnComments(?string $comment1, ?string $comment2, bool $equals) : void
1206
    {
1207
        $column1 = new Column('foo', Type::getType('integer'), ['comment' => $comment1]);
1208
        $column2 = new Column('foo', Type::getType('integer'), ['comment' => $comment2]);
1209
1210
        $comparator = new Comparator();
1211
1212
        $expectedDiff = $equals ? [] : ['comment'];
1213
1214
        $actualDiff = $comparator->diffColumn($column1, $column2);
1215
1216
        self::assertSame($expectedDiff, $actualDiff);
1217
1218
        $actualDiff = $comparator->diffColumn($column2, $column1);
1219
1220
        self::assertSame($expectedDiff, $actualDiff);
1221
    }
1222
1223
    /**
1224
     * @return mixed[][]
1225
     */
1226
    public static function getCompareColumnComments() : iterable
1227
    {
1228
        return [
1229
            [null, null, true],
1230
            ['', '', true],
1231
            [' ', ' ', true],
1232
            ['0', '0', true],
1233
            ['foo', 'foo', true],
1234
1235
            [null, '', true],
1236
            [null, ' ', false],
1237
            [null, '0', false],
1238
            [null, 'foo', false],
1239
1240
            ['', ' ', false],
1241
            ['', '0', false],
1242
            ['', 'foo', false],
1243
1244
            [' ', '0', false],
1245
            [' ', 'foo', false],
1246
1247
            ['0', 'foo', false],
1248
        ];
1249
    }
1250
1251
    public function testForeignKeyRemovalWithRenamedLocalColumn() : void
1252
    {
1253
        $fromSchema = new Schema([
1254
            'table1' => new Table(
1255
                'table1',
1256
                [
1257
                    'id' => new Column('id', Type::getType('integer')),
1258
                ]
1259
            ),
1260
            'table2' => new Table(
1261
                'table2',
1262
                [
1263
                    'id' => new Column('id', Type::getType('integer')),
1264
                    'id_table1' => new Column('id_table1', Type::getType('integer')),
1265
                ],
1266
                [],
1267
                [
1268
                    new ForeignKeyConstraint(['id_table1'], 'table1', ['id'], 'fk_table2_table1'),
1269
                ]
1270
            ),
1271
        ]);
1272
        $toSchema   = new Schema([
1273
            'table2' => new Table(
1274
                'table2',
1275
                [
1276
                    'id' => new Column('id', Type::getType('integer')),
1277
                    'id_table3' => new Column('id_table3', Type::getType('integer')),
1278
                ],
1279
                [],
1280
                [
1281
                    new ForeignKeyConstraint(['id_table3'], 'table3', ['id'], 'fk_table2_table3'),
1282
                ]
1283
            ),
1284
            'table3' => new Table(
1285
                'table3',
1286
                [
1287
                    'id' => new Column('id', Type::getType('integer')),
1288
                ]
1289
            ),
1290
        ]);
1291
        $actual     = Comparator::compareSchemas($fromSchema, $toSchema);
1292
        self::assertArrayHasKey('table2', $actual->changedTables);
1293
        self::assertCount(1, $actual->orphanedForeignKeys);
0 ignored issues
show
Documentation introduced by
$actual->orphanedForeignKeys is of type array<integer,object<Doc...\ForeignKeyConstraint>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1294
        self::assertEquals('fk_table2_table1', $actual->orphanedForeignKeys[0]->getName());
1295
        self::assertCount(1, $actual->changedTables['table2']->addedForeignKeys, 'FK to table3 should be added.');
0 ignored issues
show
Documentation introduced by
$actual->changedTables['...le2']->addedForeignKeys is of type array<integer,object<Doc...\ForeignKeyConstraint>>, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
1296
        self::assertEquals('table3', $actual->changedTables['table2']->addedForeignKeys[0]->getForeignTableName());
1297
    }
1298
}
1299