Failed Conditions
Pull Request — master (#3233)
by Sergey
10:50
created

ComparatorTest   F

Complexity

Total Complexity 59

Size/Duplication

Total Lines 1277
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 59
eloc 687
dl 0
loc 1277
rs 3.9929
c 0
b 0
f 0

58 Methods

Rating   Name   Duplication   Size   Complexity  
A testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff() 0 15 1
A testCompareColumnCompareCaseInsensitive() 0 12 1
A testCompareForeignKey_RestrictNoAction_AreTheSame() 0 7 1
A testChangedSequence() 0 13 1
A testDetectRenameColumn() 0 15 1
A testFqnSchemaComparison() 0 15 1
A testCompareOnlyAutoincrementChanged() 0 9 1
A testChangedViews() 0 13 1
A testRemovedSequence() 0 12 1
A testComplexDiffColumn() 0 15 1
A testDetectChangeIdentifierType() 0 15 1
A testAddedViews() 0 12 1
A testSequencesCaseInsensitive() 0 18 1
A testCompareGuidColumns() 0 13 1
A testFqnSchemaComparisonNoSchemaSame() 0 14 1
A testCompareColumnComments() 0 16 2
A testCompareChangedColumns_ChangeType() 0 8 1
A testForeignKeyRemovalWithRenamedLocalColumn() 0 38 1
A testCompareChangedBinaryColumn() 0 20 1
A testAvoidMultipleDropForeignKey() 0 31 1
A testCompareSequences() 0 10 1
A testTableAddForeignKey() 0 17 1
A testCompareForeignKeyNamesUnqualified_AsNoSchemaInformationIsAvailable() 0 7 1
A testCompareChangedColumns_ChangeCustomSchemaOption() 0 14 1
A testDiffColumnPlatformOptions() 0 15 1
A testCompareNewField() 0 31 1
A testCompareIndexBasedOnPropertiesNotName() 0 18 1
A testCompareMissingTable() 0 12 1
A assertSchemaSequenceChangeCount() 0 5 1
A testCompareChangedIndex() 0 52 1
A testAddedSequence() 0 12 1
A testNamespacesComparison() 0 22 1
A testTablesCaseInsensitive() 0 18 1
A testAutoIncrementSequences() 0 17 1
A testTableUpdateForeignKey() 0 18 1
A testCompareSame1() 0 20 1
A testCompareNewTable() 0 12 1
A testDetectRenameColumnAmbiguous() 0 18 1
A assertSchemaTableChangeCount() 0 5 1
A testDetectRenameIndexAmbiguous() 0 21 1
A getCompareColumnComments() 0 22 1
A testAutoIncrementNoSequences() 0 17 1
A testComparesNamespaces() 0 44 1
A testCompareChangedIndexFieldPositions() 0 38 1
A testMovedForeignKeyForeignTable() 0 21 1
A testDiff() 0 22 1
A testTableRemoveForeignKey() 0 17 1
A testDetectRenameIndex() 0 18 1
A testRemovedViews() 0 12 1
A testCompareSame2() 0 22 1
A testDiffDecimalWithNullPrecision() 0 9 1
A testCompareChangeColumns_MultipleNewColumnsRename() 0 18 1
A testCompareMissingField() 0 32 1
A testCompareQuotedAndUnquotedForeignKeyColumns() 0 9 1
A testCompareChangedColumn() 0 20 1
A testCompareForeignKeyBasedOnPropertiesNotName() 0 14 1
A testCompareRemovedIndex() 0 45 1
A testCompareNewIndex() 0 45 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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\Tests\DBAL\Schema;
21
22
use Doctrine\DBAL\Schema\Column;
23
use Doctrine\DBAL\Schema\ColumnDiff;
24
use Doctrine\DBAL\Schema\Comparator;
25
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
26
use Doctrine\DBAL\Schema\Index;
27
use Doctrine\DBAL\Schema\Schema;
28
use Doctrine\DBAL\Schema\SchemaConfig;
29
use Doctrine\DBAL\Schema\SchemaDiff;
30
use Doctrine\DBAL\Schema\Sequence;
31
use Doctrine\DBAL\Schema\Table;
32
use Doctrine\DBAL\Schema\TableDiff;
33
use Doctrine\DBAL\Schema\View;
34
use Doctrine\DBAL\Types\Type;
35
use function array_keys;
36
37
/**
38
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
39
 * @link    www.doctrine-project.org
40
 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
41
 * @license http://ez.no/licenses/new_bsd New BSD License
42
 * @since   2.0
43
 * @version $Revision$
44
 * @author  Benjamin Eberlei <[email protected]>
45
 */
46
class ComparatorTest extends \PHPUnit\Framework\TestCase
47
{
48
    public function testCompareSame1()
49
    {
50
        $schema1 = new Schema( array(
51
            'bugdb' => new Table('bugdb',
52
                array (
53
                    'integerfield1' => new Column('integerfield1', Type::getType('integer' ) ),
54
                )
55
            ),
56
        ) );
57
        $schema2 = new Schema( array(
58
            'bugdb' => new Table('bugdb',
59
                array (
60
                    'integerfield1' => new Column('integerfield1', Type::getType('integer') ),
61
                )
62
            ),
63
        ) );
64
65
        $expected = new SchemaDiff();
66
        $expected->fromSchema = $schema1;
67
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
68
    }
69
70
    public function testCompareSame2()
71
    {
72
        $schema1 = new Schema( array(
73
            'bugdb' => new Table('bugdb',
74
                array (
75
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
76
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
77
                )
78
            ),
79
        ) );
80
        $schema2 = new Schema( array(
81
            'bugdb' => new Table('bugdb',
82
                array (
83
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
84
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
85
                )
86
            ),
87
        ) );
88
89
        $expected = new SchemaDiff();
90
        $expected->fromSchema = $schema1;
91
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
92
    }
93
94
    public function testCompareMissingTable()
95
    {
96
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
97
        $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
98
        $table->setSchemaConfig($schemaConfig);
99
100
        $schema1 = new Schema( array($table), array(), $schemaConfig );
101
        $schema2 = new Schema( array(),       array(), $schemaConfig );
102
103
        $expected = new SchemaDiff( array(), array(), array('bugdb' => $table), $schema1 );
104
105
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
106
    }
107
108
    public function testCompareNewTable()
109
    {
110
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
111
        $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
112
        $table->setSchemaConfig($schemaConfig);
113
114
        $schema1 = new Schema( array(),       array(), $schemaConfig );
115
        $schema2 = new Schema( array($table), array(), $schemaConfig );
116
117
        $expected = new SchemaDiff( array('bugdb' => $table), array(), array(), $schema1 );
118
119
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
120
    }
121
122
    public function testCompareOnlyAutoincrementChanged()
123
    {
124
        $column1 = new Column('foo', Type::getType('integer'), array('autoincrement' => true));
125
        $column2 = new Column('foo', Type::getType('integer'), array('autoincrement' => false));
126
127
        $comparator = new Comparator();
128
        $changedProperties = $comparator->diffColumn($column1, $column2);
129
130
        self::assertEquals(array('autoincrement'), $changedProperties);
131
    }
132
133
    public function testCompareMissingField()
134
    {
135
        $missingColumn = new Column('integerfield1', Type::getType('integer'));
136
        $schema1 = new Schema( array(
137
            'bugdb' => new Table('bugdb',
138
                array (
139
                    'integerfield1' => $missingColumn,
140
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
141
                )
142
            ),
143
        ) );
144
        $schema2 = new Schema( array(
145
            'bugdb' => new Table('bugdb',
146
                array (
147
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
148
                )
149
            ),
150
        ) );
151
152
        $expected = new SchemaDiff ( array(),
153
            array (
154
                'bugdb' => new TableDiff( 'bugdb', array(), array(),
155
                    array (
156
                        'integerfield1' => $missingColumn,
157
                    )
158
                )
159
            )
160
        );
161
        $expected->fromSchema = $schema1;
162
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
163
164
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
165
    }
166
167
    public function testCompareNewField()
168
    {
169
        $schema1 = new Schema( array(
170
            'bugdb' => new Table('bugdb',
171
                array (
172
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
173
                )
174
            ),
175
        ) );
176
        $schema2 = new Schema( array(
177
            'bugdb' => new Table('bugdb',
178
                array (
179
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
180
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
181
                )
182
            ),
183
        ) );
184
185
        $expected = new SchemaDiff ( array(),
186
            array (
187
                'bugdb' => new TableDiff ('bugdb',
188
                    array (
189
                        'integerfield2' => new Column('integerfield2', Type::getType('integer')),
190
                    )
191
                ),
192
            )
193
        );
194
        $expected->fromSchema = $schema1;
195
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
196
197
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
198
    }
199
200
    public function testCompareChangedColumns_ChangeType()
201
    {
202
        $column1 = new Column('charfield1', Type::getType('string'));
203
        $column2 = new Column('charfield1', Type::getType('integer'));
204
205
        $c = new Comparator();
206
        self::assertEquals(array('type'), $c->diffColumn($column1, $column2));
207
        self::assertEquals(array(), $c->diffColumn($column1, $column1));
208
    }
209
210
    public function testCompareChangedColumns_ChangeCustomSchemaOption()
211
    {
212
        $column1 = new Column('charfield1', Type::getType('string'));
213
        $column2 = new Column('charfield1', Type::getType('string'));
214
215
        $column1->setCustomSchemaOption('foo', 'bar');
216
        $column2->setCustomSchemaOption('foo', 'bar');
217
218
        $column1->setCustomSchemaOption('foo1', 'bar1');
219
        $column2->setCustomSchemaOption('foo2', 'bar2');
220
221
        $c = new Comparator();
222
        self::assertEquals(array('foo1', 'foo2'), $c->diffColumn($column1, $column2));
223
        self::assertEquals(array(), $c->diffColumn($column1, $column1));
224
    }
225
226
    public function testCompareChangeColumns_MultipleNewColumnsRename()
227
    {
228
        $tableA = new Table("foo");
229
        $tableA->addColumn('datefield1', 'datetime');
230
231
        $tableB = new Table("foo");
232
        $tableB->addColumn('new_datefield1', 'datetime');
233
        $tableB->addColumn('new_datefield2', 'datetime');
234
235
        $c = new Comparator();
236
        $tableDiff = $c->diffTable($tableA, $tableB);
237
238
        self::assertCount(1, $tableDiff->renamedColumns, "we should have one rename datefield1 => new_datefield1.");
239
        self::assertArrayHasKey('datefield1', $tableDiff->renamedColumns, "'datefield1' should be set to be renamed to new_datefield1");
240
        self::assertCount(1, $tableDiff->addedColumns, "'new_datefield2' should be added");
241
        self::assertArrayHasKey('new_datefield2', $tableDiff->addedColumns, "'new_datefield2' should be added, not created through renaming!");
242
        self::assertCount(0, $tableDiff->removedColumns, "Nothing should be removed.");
243
        self::assertCount(0, $tableDiff->changedColumns, "Nothing should be changed as all fields old & new have diff names.");
244
    }
245
246
    public function testCompareRemovedIndex()
247
    {
248
        $schema1 = new Schema( array(
249
            'bugdb' => new Table('bugdb',
250
                array (
251
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
252
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
253
                ),
254
                array (
255
                    'primary' => new Index('primary',
256
                        array(
257
                            'integerfield1'
258
                        ),
259
                        true
260
                    )
261
                )
262
            ),
263
        ) );
264
        $schema2 = new Schema( array(
265
            'bugdb' => new Table('bugdb',
266
                array (
267
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
268
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
269
                )
270
            ),
271
        ) );
272
273
        $expected = new SchemaDiff ( array(),
274
            array (
275
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(), array(),
276
                    array (
277
                        'primary' => new Index('primary',
278
                        array(
279
                            'integerfield1'
280
                        ),
281
                        true
282
                    )
283
                    )
284
                ),
285
            )
286
        );
287
        $expected->fromSchema = $schema1;
288
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
289
290
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
291
    }
292
293
    public function testCompareNewIndex()
294
    {
295
        $schema1 = new Schema( array(
296
            'bugdb' => new Table('bugdb',
297
                array (
298
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
299
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
300
                )
301
            ),
302
        ) );
303
        $schema2 = new Schema( array(
304
            'bugdb' => new Table('bugdb',
305
                array (
306
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
307
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
308
                ),
309
                array (
310
                    'primary' => new Index('primary',
311
                        array(
312
                            'integerfield1'
313
                        ),
314
                        true
315
                    )
316
                )
317
            ),
318
        ) );
319
320
        $expected = new SchemaDiff ( array(),
321
            array (
322
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(),
323
                    array (
324
                        'primary' => new Index('primary',
325
                            array(
326
                                'integerfield1'
327
                            ),
328
                            true
329
                        )
330
                    )
331
                ),
332
            )
333
        );
334
        $expected->fromSchema = $schema1;
335
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
336
337
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
338
    }
339
340
    public function testCompareChangedIndex()
341
    {
342
        $schema1 = new Schema( array(
343
            'bugdb' => new Table('bugdb',
344
                array (
345
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
346
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
347
                ),
348
                array (
349
                    'primary' => new Index('primary',
350
                        array(
351
                            'integerfield1'
352
                        ),
353
                        true
354
                    )
355
                )
356
            ),
357
        ) );
358
        $schema2 = new Schema( array(
359
            'bugdb' => new Table('bugdb',
360
                array (
361
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
362
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
363
                ),
364
                array (
365
                    'primary' => new Index('primary',
366
                        array('integerfield1', 'integerfield2'),
367
                        true
368
                    )
369
                )
370
            ),
371
        ) );
372
373
        $expected = new SchemaDiff ( array(),
374
            array (
375
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(),
376
                    array (
377
                        'primary' => new Index('primary',
378
                            array(
379
                                'integerfield1',
380
                                'integerfield2'
381
                            ),
382
                            true
383
                        )
384
                    )
385
                ),
386
            )
387
        );
388
        $expected->fromSchema = $schema1;
389
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
390
391
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
392
    }
393
394
    public function testCompareChangedIndexFieldPositions()
395
    {
396
        $schema1 = new Schema( array(
397
            'bugdb' => new Table('bugdb',
398
                array (
399
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
400
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
401
                ),
402
                array (
403
                    'primary' => new Index('primary', array('integerfield1', 'integerfield2'), true)
404
                )
405
            ),
406
        ) );
407
        $schema2 = new Schema( array(
408
            'bugdb' => new Table('bugdb',
409
                array (
410
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
411
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
412
                ),
413
                array (
414
                    'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
415
                )
416
            ),
417
        ) );
418
419
        $expected = new SchemaDiff ( array(),
420
            array (
421
                'bugdb' => new TableDiff('bugdb', array(), array(), array(), array(),
422
                    array (
423
                        'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
424
                    )
425
                ),
426
            )
427
        );
428
        $expected->fromSchema = $schema1;
429
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
430
431
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
432
    }
433
434
    public function testCompareSequences()
435
    {
436
        $seq1 = new Sequence('foo', 1, 1);
437
        $seq2 = new Sequence('foo', 1, 2);
438
        $seq3 = new Sequence('foo', 2, 1);
439
440
        $c = new Comparator();
441
442
        self::assertTrue($c->diffSequence($seq1, $seq2));
443
        self::assertTrue($c->diffSequence($seq1, $seq3));
444
    }
445
446
    public function testRemovedSequence()
447
    {
448
        $schema1 = new Schema();
449
        $seq = $schema1->createSequence('foo');
450
451
        $schema2 = new Schema();
452
453
        $c = new Comparator();
454
        $diffSchema = $c->compare($schema1, $schema2);
455
456
        self::assertCount(1, $diffSchema->removedSequences);
457
        self::assertSame($seq, $diffSchema->removedSequences[0]);
458
    }
459
460
    public function testAddedSequence()
461
    {
462
        $schema1 = new Schema();
463
464
        $schema2 = new Schema();
465
        $seq = $schema2->createSequence('foo');
466
467
        $c = new Comparator();
468
        $diffSchema = $c->compare($schema1, $schema2);
469
470
        self::assertCount(1, $diffSchema->newSequences);
471
        self::assertSame($seq, $diffSchema->newSequences[0]);
472
    }
473
474
    public function testRemovedViews()
475
    {
476
        $schema1 = new Schema();
477
        $view = $schema1->createView('foo', 'bar');
478
479
        $schema2 = new Schema();
480
481
        $c = new Comparator();
482
        $diffSchema = $c->compare($schema1, $schema2);
483
484
        $this->assertEquals(1, count($diffSchema->removedViews));
485
        $this->assertSame($view, $diffSchema->removedViews[$view->getName()]);
486
    }
487
488
    public function testChangedViews()
489
    {
490
        $fromSchema = new Schema();
491
        $fromSchema->createView('foo', 'bar');
492
493
        $toSchema = new Schema();
494
        $view2 = $toSchema->createView('foo', 'baz');
495
496
        $c = new Comparator();
497
        $diffSchema = $c->compare($fromSchema, $toSchema);
498
499
        $this->assertEquals(1, count($diffSchema->changedViews));
500
        $this->assertSame($view2, $diffSchema->changedViews[$view2->getName()]);
501
    }
502
503
    public function testAddedViews()
504
    {
505
        $schema1 = new Schema();
506
507
        $schema2 = new Schema();
508
        $view = $schema2->createView('foo', 'bar');
509
510
        $c = new Comparator();
511
        $diffSchema = $c->compare($schema1, $schema2);
512
513
        $this->assertEquals(1, count($diffSchema->newViews));
514
        $this->assertSame($view, $diffSchema->newViews[$view->getName()]);
515
    }
516
517
    public function testTableAddForeignKey()
518
    {
519
        $tableForeign = new Table("bar");
520
        $tableForeign->addColumn('id', 'integer');
521
522
        $table1 = new Table("foo");
523
        $table1->addColumn('fk', 'integer');
524
525
        $table2 = new Table("foo");
526
        $table2->addColumn('fk', 'integer');
527
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
528
529
        $c = new Comparator();
530
        $tableDiff = $c->diffTable($table1, $table2);
531
532
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
533
        self::assertCount(1, $tableDiff->addedForeignKeys);
534
    }
535
536
    public function testTableRemoveForeignKey()
537
    {
538
        $tableForeign = new Table("bar");
539
        $tableForeign->addColumn('id', 'integer');
540
541
        $table1 = new Table("foo");
542
        $table1->addColumn('fk', 'integer');
543
544
        $table2 = new Table("foo");
545
        $table2->addColumn('fk', 'integer');
546
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
547
548
        $c = new Comparator();
549
        $tableDiff = $c->diffTable($table2, $table1);
550
551
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
552
        self::assertCount(1, $tableDiff->removedForeignKeys);
553
    }
554
555
    public function testTableUpdateForeignKey()
556
    {
557
        $tableForeign = new Table("bar");
558
        $tableForeign->addColumn('id', 'integer');
559
560
        $table1 = new Table("foo");
561
        $table1->addColumn('fk', 'integer');
562
        $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
563
564
        $table2 = new Table("foo");
565
        $table2->addColumn('fk', 'integer');
566
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'), array('onUpdate' => 'CASCADE'));
567
568
        $c = new Comparator();
569
        $tableDiff = $c->diffTable($table1, $table2);
570
571
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
572
        self::assertCount(1, $tableDiff->changedForeignKeys);
573
    }
574
575
    public function testMovedForeignKeyForeignTable()
576
    {
577
        $tableForeign = new Table("bar");
578
        $tableForeign->addColumn('id', 'integer');
579
580
        $tableForeign2 = new Table("bar2");
581
        $tableForeign2->addColumn('id', 'integer');
582
583
        $table1 = new Table("foo");
584
        $table1->addColumn('fk', 'integer');
585
        $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
586
587
        $table2 = new Table("foo");
588
        $table2->addColumn('fk', 'integer');
589
        $table2->addForeignKeyConstraint($tableForeign2, array('fk'), array('id'));
590
591
        $c = new Comparator();
592
        $tableDiff = $c->diffTable($table1, $table2);
593
594
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
595
        self::assertCount(1, $tableDiff->changedForeignKeys);
596
    }
597
598
    public function testTablesCaseInsensitive()
599
    {
600
        $schemaA = new Schema();
601
        $schemaA->createTable('foo');
602
        $schemaA->createTable('bAr');
603
        $schemaA->createTable('BAZ');
604
        $schemaA->createTable('new');
605
606
        $schemaB = new Schema();
607
        $schemaB->createTable('FOO');
608
        $schemaB->createTable('bar');
609
        $schemaB->createTable('Baz');
610
        $schemaB->createTable('old');
611
612
        $c = new Comparator();
613
        $diff = $c->compare($schemaA, $schemaB);
614
615
        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

615
        self::/** @scrutinizer ignore-call */ 
616
              assertSchemaTableChangeCount($diff, 1, 0, 1);
Loading history...
616
    }
617
618
    public function testSequencesCaseInsensitive()
619
    {
620
        $schemaA = new Schema();
621
        $schemaA->createSequence('foo');
622
        $schemaA->createSequence('BAR');
623
        $schemaA->createSequence('Baz');
624
        $schemaA->createSequence('new');
625
626
        $schemaB = new Schema();
627
        $schemaB->createSequence('FOO');
628
        $schemaB->createSequence('Bar');
629
        $schemaB->createSequence('baz');
630
        $schemaB->createSequence('old');
631
632
        $c = new Comparator();
633
        $diff = $c->compare($schemaA, $schemaB);
634
635
        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

635
        self::/** @scrutinizer ignore-call */ 
636
              assertSchemaSequenceChangeCount($diff, 1, 0, 1);
Loading history...
636
    }
637
638
    public function testCompareColumnCompareCaseInsensitive()
639
    {
640
        $tableA = new Table("foo");
641
        $tableA->addColumn('id', 'integer');
642
643
        $tableB = new Table("foo");
644
        $tableB->addColumn('ID', 'integer');
645
646
        $c = new Comparator();
647
        $tableDiff = $c->diffTable($tableA, $tableB);
648
649
        self::assertFalse($tableDiff);
650
    }
651
652
    public function testCompareIndexBasedOnPropertiesNotName()
653
    {
654
        $tableA = new Table("foo");
655
        $tableA->addColumn('id', 'integer');
656
        $tableA->addIndex(array("id"), "foo_bar_idx");
657
658
        $tableB = new Table("foo");
659
        $tableB->addColumn('ID', 'integer');
660
        $tableB->addIndex(array("id"), "bar_foo_idx");
661
662
        $c = new Comparator();
663
        $tableDiff = new TableDiff('foo');
664
        $tableDiff->fromTable = $tableA;
665
        $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', array('id'));
666
667
        self::assertEquals(
668
            $tableDiff,
669
            $c->diffTable($tableA, $tableB)
670
        );
671
    }
672
673
    public function testCompareForeignKeyBasedOnPropertiesNotName()
674
    {
675
        $tableA = new Table("foo");
676
        $tableA->addColumn('id', 'integer');
677
        $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('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

677
        /** @scrutinizer ignore-deprecated */ $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('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...
678
679
        $tableB = new Table("foo");
680
        $tableB->addColumn('ID', 'integer');
681
        $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', array('id'), array('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

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