Failed Conditions
Pull Request — master (#2849)
by Luís
63:28
created

testCompareForeignKeyWithRestrictOrNoActionAreTheSame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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\Types\Type;
34
35
/**
36
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
37
 * @link    www.doctrine-project.org
38
 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
39
 * @license http://ez.no/licenses/new_bsd New BSD License
40
 * @since   2.0
41
 * @version $Revision$
42
 * @author  Benjamin Eberlei <[email protected]>
43
 */
44
class ComparatorTest extends \PHPUnit\Framework\TestCase
45
{
46
    public function testCompareSame1()
47
    {
48
        $schema1 = new Schema(array(
49
            'bugdb' => new Table(
50
                'bugdb',
51
                array (
52
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
53
                )
54
            ),
55
        ));
56
        $schema2 = new Schema(array(
57
            'bugdb' => new Table(
58
                '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(
74
                'bugdb',
75
                array (
76
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
77
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
78
                )
79
            ),
80
        ));
81
        $schema2 = new Schema(array(
82
            'bugdb' => new Table(
83
                'bugdb',
84
                array (
85
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
86
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
87
                )
88
            ),
89
        ));
90
91
        $expected             = new SchemaDiff();
92
        $expected->fromSchema = $schema1;
93
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
94
    }
95
96 View Code Duplication
    public function testCompareMissingTable()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
99
        $table        = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
100
        $table->setSchemaConfig($schemaConfig);
101
102
        $schema1 = new Schema(array($table), array(), $schemaConfig);
103
        $schema2 = new Schema(array(), array(), $schemaConfig);
104
105
        $expected = new SchemaDiff(array(), array(), array('bugdb' => $table), $schema1);
106
107
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
108
    }
109
110 View Code Duplication
    public function testCompareNewTable()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
113
        $table        = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
114
        $table->setSchemaConfig($schemaConfig);
115
116
        $schema1 = new Schema(array(), array(), $schemaConfig);
117
        $schema2 = new Schema(array($table), array(), $schemaConfig);
118
119
        $expected = new SchemaDiff(array('bugdb' => $table), array(), array(), $schema1);
120
121
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
122
    }
123
124
    public function testCompareOnlyAutoincrementChanged()
125
    {
126
        $column1 = new Column('foo', Type::getType('integer'), array('autoincrement' => true));
127
        $column2 = new Column('foo', Type::getType('integer'), array('autoincrement' => false));
128
129
        $comparator        = new Comparator();
130
        $changedProperties = $comparator->diffColumn($column1, $column2);
131
132
        self::assertEquals(array('autoincrement'), $changedProperties);
133
    }
134
135
    public function testCompareMissingField()
136
    {
137
        $missingColumn = new Column('integerfield1', Type::getType('integer'));
138
        $schema1       = new Schema(array(
139
            'bugdb' => new Table(
140
                'bugdb',
141
                array (
142
                    'integerfield1' => $missingColumn,
143
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
144
                )
145
            ),
146
        ));
147
        $schema2       = new Schema(array(
148
            'bugdb' => new Table(
149
                'bugdb',
150
                array (
151
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
152
                )
153
            ),
154
        ));
155
156
        $expected                                    = new SchemaDiff(
157
            array(),
158
            array (
159
                'bugdb' => new TableDiff(
160
                    'bugdb',
161
                    array(),
162
                    array(),
163
                    array (
164
                        'integerfield1' => $missingColumn,
165
                    )
166
                )
167
            )
168
        );
169
        $expected->fromSchema                        = $schema1;
170
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
171
172
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
173
    }
174
175
    public function testCompareNewField()
176
    {
177
        $schema1 = new Schema(array(
178
            'bugdb' => new Table(
179
                'bugdb',
180
                array (
181
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
182
                )
183
            ),
184
        ));
185
        $schema2 = new Schema(array(
186
            'bugdb' => new Table(
187
                'bugdb',
188
                array (
189
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
190
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
191
                )
192
            ),
193
        ));
194
195
        $expected                                    = new SchemaDiff(
196
            array(),
197
            array (
198
                'bugdb' => new TableDiff(
199
                    'bugdb',
200
                    array (
201
                        'integerfield2' => new Column('integerfield2', Type::getType('integer')),
202
                    )
203
                ),
204
            )
205
        );
206
        $expected->fromSchema                        = $schema1;
207
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
208
209
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
210
    }
211
212 View Code Duplication
    public function testCompareChangedColumnsChangeType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
    {
214
        $column1 = new Column('charfield1', Type::getType('string'));
215
        $column2 = new Column('charfield1', Type::getType('integer'));
216
217
        $c = new Comparator();
218
        self::assertEquals(array('type'), $c->diffColumn($column1, $column2));
219
        self::assertEquals(array(), $c->diffColumn($column1, $column1));
220
    }
221
222
    public function testCompareChangedColumnsChangeCustomSchemaOption()
223
    {
224
        $column1 = new Column('charfield1', Type::getType('string'));
225
        $column2 = new Column('charfield1', Type::getType('string'));
226
227
        $column1->setCustomSchemaOption('foo', 'bar');
228
        $column2->setCustomSchemaOption('foo', 'bar');
229
230
        $column1->setCustomSchemaOption('foo1', 'bar1');
231
        $column2->setCustomSchemaOption('foo2', 'bar2');
232
233
        $c = new Comparator();
234
        self::assertEquals(array('foo1', 'foo2'), $c->diffColumn($column1, $column2));
235
        self::assertEquals(array(), $c->diffColumn($column1, $column1));
236
    }
237
238
    public function testCompareChangeColumnsMultipleNewColumnsRename()
239
    {
240
        $tableA = new Table("foo");
241
        $tableA->addColumn('datefield1', 'datetime');
242
243
        $tableB = new Table("foo");
244
        $tableB->addColumn('new_datefield1', 'datetime');
245
        $tableB->addColumn('new_datefield2', 'datetime');
246
247
        $c         = new Comparator();
248
        $tableDiff = $c->diffTable($tableA, $tableB);
249
250
        self::assertCount(1, $tableDiff->renamedColumns, "we should have one rename datefield1 => new_datefield1.");
251
        self::assertArrayHasKey('datefield1', $tableDiff->renamedColumns, "'datefield1' should be set to be renamed to new_datefield1");
252
        self::assertCount(1, $tableDiff->addedColumns, "'new_datefield2' should be added");
253
        self::assertArrayHasKey('new_datefield2', $tableDiff->addedColumns, "'new_datefield2' should be added, not created through renaming!");
254
        self::assertCount(0, $tableDiff->removedColumns, "Nothing should be removed.");
255
        self::assertCount(0, $tableDiff->changedColumns, "Nothing should be changed as all fields old & new have diff names.");
256
    }
257
258 View Code Duplication
    public function testCompareRemovedIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
    {
260
        $schema1 = new Schema(array(
261
            'bugdb' => new Table(
262
                'bugdb',
263
                array (
264
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
265
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
266
                ),
267
                array (
268
                    'primary' => new Index(
269
                        'primary',
270
                        array(
271
                            'integerfield1'
272
                        ),
273
                        true
274
                    )
275
                )
276
            ),
277
        ));
278
        $schema2 = new Schema(array(
279
            'bugdb' => new Table(
280
                'bugdb',
281
                array (
282
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
283
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
284
                )
285
            ),
286
        ));
287
288
        $expected                                    = new SchemaDiff(
289
            array(),
290
            array (
291
                'bugdb' => new TableDiff(
292
                    'bugdb',
293
                    array(),
294
                    array(),
295
                    array(),
296
                    array(),
297
                    array(),
298
                    array (
299
                        'primary' => new Index(
300
                            'primary',
301
                            array(
302
                            'integerfield1'
303
                            ),
304
                            true
305
                        )
306
                    )
307
                ),
308
            )
309
        );
310
        $expected->fromSchema                        = $schema1;
311
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
312
313
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
314
    }
315
316 View Code Duplication
    public function testCompareNewIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
317
    {
318
        $schema1 = new Schema(array(
319
            'bugdb' => new Table(
320
                'bugdb',
321
                array (
322
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
323
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
324
                )
325
            ),
326
        ));
327
        $schema2 = new Schema(array(
328
            'bugdb' => new Table(
329
                'bugdb',
330
                array (
331
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
332
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
333
                ),
334
                array (
335
                    'primary' => new Index(
336
                        'primary',
337
                        array(
338
                            'integerfield1'
339
                        ),
340
                        true
341
                    )
342
                )
343
            ),
344
        ));
345
346
        $expected                                    = new SchemaDiff(
347
            array(),
348
            array (
349
                'bugdb' => new TableDiff(
350
                    'bugdb',
351
                    array(),
352
                    array(),
353
                    array(),
354
                    array (
355
                        'primary' => new Index(
356
                            'primary',
357
                            array(
358
                                'integerfield1'
359
                            ),
360
                            true
361
                        )
362
                    )
363
                ),
364
            )
365
        );
366
        $expected->fromSchema                        = $schema1;
367
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
368
369
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
370
    }
371
372 View Code Duplication
    public function testCompareChangedIndex()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
373
    {
374
        $schema1 = new Schema(array(
375
            'bugdb' => new Table(
376
                'bugdb',
377
                array (
378
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
379
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
380
                ),
381
                array (
382
                    'primary' => new Index(
383
                        'primary',
384
                        array(
385
                            'integerfield1'
386
                        ),
387
                        true
388
                    )
389
                )
390
            ),
391
        ));
392
        $schema2 = new Schema(array(
393
            'bugdb' => new Table(
394
                'bugdb',
395
                array (
396
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
397
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
398
                ),
399
                array (
400
                    'primary' => new Index(
401
                        'primary',
402
                        array('integerfield1', 'integerfield2'),
403
                        true
404
                    )
405
                )
406
            ),
407
        ));
408
409
        $expected                                    = new SchemaDiff(
410
            array(),
411
            array (
412
                'bugdb' => new TableDiff(
413
                    'bugdb',
414
                    array(),
415
                    array(),
416
                    array(),
417
                    array(),
418
                    array (
419
                        'primary' => new Index(
420
                            'primary',
421
                            array(
422
                                'integerfield1',
423
                                'integerfield2'
424
                            ),
425
                            true
426
                        )
427
                    )
428
                ),
429
            )
430
        );
431
        $expected->fromSchema                        = $schema1;
432
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
433
434
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
435
    }
436
437 View Code Duplication
    public function testCompareChangedIndexFieldPositions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
438
    {
439
        $schema1 = new Schema(array(
440
            'bugdb' => new Table(
441
                'bugdb',
442
                array (
443
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
444
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
445
                ),
446
                array (
447
                    'primary' => new Index('primary', array('integerfield1', 'integerfield2'), true)
448
                )
449
            ),
450
        ));
451
        $schema2 = new Schema(array(
452
            'bugdb' => new Table(
453
                'bugdb',
454
                array (
455
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
456
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
457
                ),
458
                array (
459
                    'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
460
                )
461
            ),
462
        ));
463
464
        $expected                                    = new SchemaDiff(
465
            array(),
466
            array (
467
                'bugdb' => new TableDiff(
468
                    'bugdb',
469
                    array(),
470
                    array(),
471
                    array(),
472
                    array(),
473
                    array (
474
                        'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
475
                    )
476
                ),
477
            )
478
        );
479
        $expected->fromSchema                        = $schema1;
480
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');
481
482
        self::assertEquals($expected, Comparator::compareSchemas($schema1, $schema2));
483
    }
484
485
    public function testCompareSequences()
486
    {
487
        $seq1 = new Sequence('foo', 1, 1);
488
        $seq2 = new Sequence('foo', 1, 2);
489
        $seq3 = new Sequence('foo', 2, 1);
490
491
        $c = new Comparator();
492
493
        self::assertTrue($c->diffSequence($seq1, $seq2));
494
        self::assertTrue($c->diffSequence($seq1, $seq3));
495
    }
496
497 View Code Duplication
    public function testRemovedSequence()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
498
    {
499
        $schema1 = new Schema();
500
        $seq     = $schema1->createSequence('foo');
501
502
        $schema2 = new Schema();
503
504
        $c          = new Comparator();
505
        $diffSchema = $c->compare($schema1, $schema2);
506
507
        self::assertEquals(1, count($diffSchema->removedSequences));
508
        self::assertSame($seq, $diffSchema->removedSequences[0]);
509
    }
510
511 View Code Duplication
    public function testAddedSequence()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
512
    {
513
        $schema1 = new Schema();
514
515
        $schema2 = new Schema();
516
        $seq     = $schema2->createSequence('foo');
517
518
        $c          = new Comparator();
519
        $diffSchema = $c->compare($schema1, $schema2);
520
521
        self::assertEquals(1, count($diffSchema->newSequences));
522
        self::assertSame($seq, $diffSchema->newSequences[0]);
523
    }
524
525 View Code Duplication
    public function testTableAddForeignKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
526
    {
527
        $tableForeign = new Table("bar");
528
        $tableForeign->addColumn('id', 'integer');
529
530
        $table1 = new Table("foo");
531
        $table1->addColumn('fk', 'integer');
532
533
        $table2 = new Table("foo");
534
        $table2->addColumn('fk', 'integer');
535
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
536
537
        $c         = new Comparator();
538
        $tableDiff = $c->diffTable($table1, $table2);
539
540
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
541
        self::assertEquals(1, count($tableDiff->addedForeignKeys));
542
    }
543
544 View Code Duplication
    public function testTableRemoveForeignKey()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
545
    {
546
        $tableForeign = new Table("bar");
547
        $tableForeign->addColumn('id', 'integer');
548
549
        $table1 = new Table("foo");
550
        $table1->addColumn('fk', 'integer');
551
552
        $table2 = new Table("foo");
553
        $table2->addColumn('fk', 'integer');
554
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
555
556
        $c         = new Comparator();
557
        $tableDiff = $c->diffTable($table2, $table1);
558
559
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
560
        self::assertEquals(1, count($tableDiff->removedForeignKeys));
561
    }
562
563
    public function testTableUpdateForeignKey()
564
    {
565
        $tableForeign = new Table("bar");
566
        $tableForeign->addColumn('id', 'integer');
567
568
        $table1 = new Table("foo");
569
        $table1->addColumn('fk', 'integer');
570
        $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
571
572
        $table2 = new Table("foo");
573
        $table2->addColumn('fk', 'integer');
574
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'), array('onUpdate' => 'CASCADE'));
575
576
        $c         = new Comparator();
577
        $tableDiff = $c->diffTable($table1, $table2);
578
579
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
580
        self::assertEquals(1, count($tableDiff->changedForeignKeys));
581
    }
582
583
    public function testMovedForeignKeyForeignTable()
584
    {
585
        $tableForeign = new Table("bar");
586
        $tableForeign->addColumn('id', 'integer');
587
588
        $tableForeign2 = new Table("bar2");
589
        $tableForeign2->addColumn('id', 'integer');
590
591
        $table1 = new Table("foo");
592
        $table1->addColumn('fk', 'integer');
593
        $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));
594
595
        $table2 = new Table("foo");
596
        $table2->addColumn('fk', 'integer');
597
        $table2->addForeignKeyConstraint($tableForeign2, array('fk'), array('id'));
598
599
        $c         = new Comparator();
600
        $tableDiff = $c->diffTable($table1, $table2);
601
602
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
603
        self::assertEquals(1, count($tableDiff->changedForeignKeys));
604
    }
605
606 View Code Duplication
    public function testTablesCaseInsensitive()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
607
    {
608
        $schemaA = new Schema();
609
        $schemaA->createTable('foo');
610
        $schemaA->createTable('bAr');
611
        $schemaA->createTable('BAZ');
612
        $schemaA->createTable('new');
613
614
        $schemaB = new Schema();
615
        $schemaB->createTable('FOO');
616
        $schemaB->createTable('bar');
617
        $schemaB->createTable('Baz');
618
        $schemaB->createTable('old');
619
620
        $c    = new Comparator();
621
        $diff = $c->compare($schemaA, $schemaB);
622
623
        self::assertSchemaTableChangeCount($diff, 1, 0, 1);
624
    }
625
626 View Code Duplication
    public function testSequencesCaseInsensitive()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
627
    {
628
        $schemaA = new Schema();
629
        $schemaA->createSequence('foo');
630
        $schemaA->createSequence('BAR');
631
        $schemaA->createSequence('Baz');
632
        $schemaA->createSequence('new');
633
634
        $schemaB = new Schema();
635
        $schemaB->createSequence('FOO');
636
        $schemaB->createSequence('Bar');
637
        $schemaB->createSequence('baz');
638
        $schemaB->createSequence('old');
639
640
        $c    = new Comparator();
641
        $diff = $c->compare($schemaA, $schemaB);
642
643
        self::assertSchemaSequenceChangeCount($diff, 1, 0, 1);
644
    }
645
646
    public function testCompareColumnCompareCaseInsensitive()
647
    {
648
        $tableA = new Table("foo");
649
        $tableA->addColumn('id', 'integer');
650
651
        $tableB = new Table("foo");
652
        $tableB->addColumn('ID', 'integer');
653
654
        $c         = new Comparator();
655
        $tableDiff = $c->diffTable($tableA, $tableB);
656
657
        self::assertFalse($tableDiff);
0 ignored issues
show
Bug introduced by
It seems like $tableDiff defined by $c->diffTable($tableA, $tableB) on line 655 can also be of type object<Doctrine\DBAL\Schema\TableDiff>; however, PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
658
    }
659
660
    public function testCompareIndexBasedOnPropertiesNotName()
661
    {
662
        $tableA = new Table("foo");
663
        $tableA->addColumn('id', 'integer');
664
        $tableA->addIndex(array("id"), "foo_bar_idx");
665
666
        $tableB = new Table("foo");
667
        $tableB->addColumn('ID', 'integer');
668
        $tableB->addIndex(array("id"), "bar_foo_idx");
669
670
        $c                                        = new Comparator();
671
        $tableDiff                                = new TableDiff('foo');
672
        $tableDiff->fromTable                     = $tableA;
673
        $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', array('id'));
674
675
        self::assertEquals(
676
            $tableDiff,
677
            $c->diffTable($tableA, $tableB)
678
        );
679
    }
680
681
    public function testCompareForeignKeyBasedOnPropertiesNotName()
682
    {
683
        $tableA = new Table("foo");
684
        $tableA->addColumn('id', 'integer');
685
        $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('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...
686
687
        $tableB = new Table("foo");
688
        $tableB->addColumn('ID', 'integer');
689
        $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', array('id'), array('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...
690
691
        $c         = new Comparator();
692
        $tableDiff = $c->diffTable($tableA, $tableB);
693
694
        self::assertFalse($tableDiff);
0 ignored issues
show
Bug introduced by
It seems like $tableDiff defined by $c->diffTable($tableA, $tableB) on line 692 can also be of type object<Doctrine\DBAL\Schema\TableDiff>; however, PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
695
    }
696
697 View Code Duplication
    public function testCompareForeignKeyWithRestrictOrNoActionAreTheSame()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
698
    {
699
        $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION'));
700
        $fk2 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'RESTRICT'));
701
702
        $c = new Comparator();
703
        self::assertFalse($c->diffForeignKey($fk1, $fk2));
704
    }
705
706
    /**
707
     * @group DBAL-492
708
     */
709
    public function testCompareForeignKeyNamesUnqualifiedAsNoSchemaInformationIsAvailable()
710
    {
711
        $fk1 = new ForeignKeyConstraint(array("foo"), "foo.bar", array("baz"), "fk1");
712
        $fk2 = new ForeignKeyConstraint(array("foo"), "baz.bar", array("baz"), "fk1");
713
714
        $c = new Comparator();
715
        self::assertFalse($c->diffForeignKey($fk1, $fk2));
716
    }
717
718
    public function testDetectRenameColumn()
719
    {
720
        $tableA = new Table("foo");
721
        $tableA->addColumn('foo', 'integer');
722
723
        $tableB = new Table("foo");
724
        $tableB->addColumn('bar', 'integer');
725
726
        $c         = new Comparator();
727
        $tableDiff = $c->diffTable($tableA, $tableB);
728
729
        self::assertEquals(0, count($tableDiff->addedColumns));
730
        self::assertEquals(0, count($tableDiff->removedColumns));
731
        self::assertArrayHasKey('foo', $tableDiff->renamedColumns);
732
        self::assertEquals('bar', $tableDiff->renamedColumns['foo']->getName());
733
    }
734
735
    /**
736
     * You can easily have ambiguities in the column renaming. If these
737
     * are detected no renaming should take place, instead adding and dropping
738
     * should be used exclusively.
739
     *
740
     * @group DBAL-24
741
     */
742
    public function testDetectRenameColumnAmbiguous()
743
    {
744
        $tableA = new Table("foo");
745
        $tableA->addColumn('foo', 'integer');
746
        $tableA->addColumn('bar', 'integer');
747
748
        $tableB = new Table("foo");
749
        $tableB->addColumn('baz', 'integer');
750
751
        $c         = new Comparator();
752
        $tableDiff = $c->diffTable($tableA, $tableB);
753
754
        self::assertEquals(1, count($tableDiff->addedColumns), "'baz' should be added, not created through renaming!");
755
        self::assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
756
        self::assertEquals(2, count($tableDiff->removedColumns), "'foo' and 'bar' should both be dropped, an ambiguity exists which one could be renamed to 'baz'.");
757
        self::assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed.");
758
        self::assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed.");
759
        self::assertEquals(0, count($tableDiff->renamedColumns), "no renamings should take place.");
760
    }
761
762
    /**
763
     * @group DBAL-1063
764
     */
765
    public function testDetectRenameIndex()
766
    {
767
        $table1 = new Table('foo');
768
        $table1->addColumn('foo', 'integer');
769
770
        $table2 = clone $table1;
771
772
        $table1->addIndex(array('foo'), 'idx_foo');
773
774
        $table2->addIndex(array('foo'), 'idx_bar');
775
776
        $comparator = new Comparator();
777
        $tableDiff  = $comparator->diffTable($table1, $table2);
778
779
        self::assertCount(0, $tableDiff->addedIndexes);
780
        self::assertCount(0, $tableDiff->removedIndexes);
781
        self::assertArrayHasKey('idx_foo', $tableDiff->renamedIndexes);
782
        self::assertEquals('idx_bar', $tableDiff->renamedIndexes['idx_foo']->getName());
783
    }
784
785
    /**
786
     * You can easily have ambiguities in the index renaming. If these
787
     * are detected no renaming should take place, instead adding and dropping
788
     * should be used exclusively.
789
     *
790
     * @group DBAL-1063
791
     */
792
    public function testDetectRenameIndexAmbiguous()
793
    {
794
        $table1 = new Table('foo');
795
        $table1->addColumn('foo', 'integer');
796
797
        $table2 = clone $table1;
798
799
        $table1->addIndex(array('foo'), 'idx_foo');
800
        $table1->addIndex(array('foo'), 'idx_bar');
801
802
        $table2->addIndex(array('foo'), 'idx_baz');
803
804
        $comparator = new Comparator();
805
        $tableDiff  = $comparator->diffTable($table1, $table2);
806
807
        self::assertCount(1, $tableDiff->addedIndexes);
808
        self::assertArrayHasKey('idx_baz', $tableDiff->addedIndexes);
809
        self::assertCount(2, $tableDiff->removedIndexes);
810
        self::assertArrayHasKey('idx_foo', $tableDiff->removedIndexes);
811
        self::assertArrayHasKey('idx_bar', $tableDiff->removedIndexes);
812
        self::assertCount(0, $tableDiff->renamedIndexes);
813
    }
814
815 View Code Duplication
    public function testDetectChangeIdentifierType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
816
    {
817
        $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.');
818
819
        $tableA = new Table("foo");
820
        $tableA->addColumn('id', 'integer', array('autoincrement' => false));
821
822
        $tableB = new Table("foo");
823
        $tableB->addColumn('id', 'integer', array('autoincrement' => true));
824
825
        $c         = new Comparator();
826
        $tableDiff = $c->diffTable($tableA, $tableB);
827
828
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
829
        self::assertArrayHasKey('id', $tableDiff->changedColumns);
830
    }
831
832
833
    /**
834
     * @group DBAL-105
835
     */
836
    public function testDiff()
837
    {
838
        $table = new \Doctrine\DBAL\Schema\Table('twitter_users');
839
        $table->addColumn('id', 'integer', array('autoincrement' => true));
840
        $table->addColumn('twitterId', 'integer', array('nullable' => false));
841
        $table->addColumn('displayName', 'string', array('nullable' => false));
842
        $table->setPrimaryKey(array('id'));
843
844
        $newtable = new \Doctrine\DBAL\Schema\Table('twitter_users');
845
        $newtable->addColumn('id', 'integer', array('autoincrement' => true));
846
        $newtable->addColumn('twitter_id', 'integer', array('nullable' => false));
847
        $newtable->addColumn('display_name', 'string', array('nullable' => false));
848
        $newtable->addColumn('logged_in_at', 'datetime', array('nullable' => true));
849
        $newtable->setPrimaryKey(array('id'));
850
851
        $c         = new Comparator();
852
        $tableDiff = $c->diffTable($table, $newtable);
853
854
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
855
        self::assertEquals(array('twitterid', 'displayname'), array_keys($tableDiff->renamedColumns));
856
        self::assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns));
857
        self::assertEquals(0, count($tableDiff->removedColumns));
858
    }
859
860
861
    /**
862
     * @group DBAL-112
863
     */
864
    public function testChangedSequence()
865
    {
866
        $schema   = new Schema();
867
        $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...
868
869
        $schemaNew = clone $schema;
870
        /* @var $schemaNew Schema */
871
        $schemaNew->getSequence('baz')->setAllocationSize(20);
872
873
        $c    = new \Doctrine\DBAL\Schema\Comparator;
874
        $diff = $c->compare($schema, $schemaNew);
875
876
        self::assertSame($diff->changedSequences[0], $schemaNew->getSequence('baz'));
877
    }
878
879
    /**
880
     * @group DBAL-106
881
     */
882 View Code Duplication
    public function testDiffDecimalWithNullPrecision()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
883
    {
884
        $column = new Column('foo', Type::getType('decimal'));
885
        $column->setPrecision(null);
886
887
        $column2 = new Column('foo', Type::getType('decimal'));
888
889
        $c = new Comparator();
890
        self::assertEquals(array(), $c->diffColumn($column, $column2));
891
    }
892
893
    /**
894
     * @group DBAL-204
895
     */
896 View Code Duplication
    public function testFqnSchemaComparison()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
897
    {
898
        $config = new SchemaConfig();
899
        $config->setName("foo");
900
901
        $oldSchema = new Schema(array(), array(), $config);
902
        $oldSchema->createTable('bar');
903
904
        $newSchema = new Schema(array(), array(), $config);
905
        $newSchema->createTable('foo.bar');
906
907
        $expected             = new SchemaDiff();
908
        $expected->fromSchema = $oldSchema;
909
910
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
911
    }
912
913
    /**
914
     * @group DBAL-669
915
     */
916
    public function testNamespacesComparison()
917
    {
918
        $config = new SchemaConfig();
919
        $config->setName("schemaName");
920
921
        $oldSchema = new Schema(array(), array(), $config);
922
        $oldSchema->createTable('taz');
923
        $oldSchema->createTable('war.tab');
924
925
        $newSchema = new Schema(array(), array(), $config);
926
        $newSchema->createTable('bar.tab');
927
        $newSchema->createTable('baz.tab');
928
        $newSchema->createTable('war.tab');
929
930
        $expected                = new SchemaDiff();
931
        $expected->fromSchema    = $oldSchema;
932
        $expected->newNamespaces = array('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...
933
934
        $diff = Comparator::compareSchemas($oldSchema, $newSchema);
935
936
        self::assertEquals(array('bar' => 'bar', 'baz' => 'baz'), $diff->newNamespaces);
937
        self::assertCount(2, $diff->newTables);
938
    }
939
940
    /**
941
     * @group DBAL-204
942
     */
943 View Code Duplication
    public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
944
    {
945
        $config = new SchemaConfig();
946
        $config->setName("foo");
947
948
        $oldSchema = new Schema(array(), array(), $config);
949
        $oldSchema->createTable('foo.bar');
950
951
        $newSchema = new Schema();
952
        $newSchema->createTable('bar');
953
954
        $expected             = new SchemaDiff();
955
        $expected->fromSchema = $oldSchema;
956
957
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
958
    }
959
960
    /**
961
     * @group DBAL-204
962
     */
963 View Code Duplication
    public function testFqnSchemaComparisonNoSchemaSame()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
964
    {
965
        $config = new SchemaConfig();
966
        $config->setName("foo");
967
        $oldSchema = new Schema(array(), array(), $config);
968
        $oldSchema->createTable('bar');
969
970
        $newSchema = new Schema();
971
        $newSchema->createTable('bar');
972
973
        $expected             = new SchemaDiff();
974
        $expected->fromSchema = $oldSchema;
975
976
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
977
    }
978
979
    /**
980
     * @group DDC-1657
981
     */
982 View Code Duplication
    public function testAutoIncrementSequences()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
983
    {
984
        $oldSchema = new Schema();
985
        $table     = $oldSchema->createTable("foo");
986
        $table->addColumn("id", "integer", array("autoincrement" => true));
987
        $table->setPrimaryKey(array("id"));
988
        $oldSchema->createSequence("foo_id_seq");
989
990
        $newSchema = new Schema();
991
        $table     = $newSchema->createTable("foo");
992
        $table->addColumn("id", "integer", array("autoincrement" => true));
993
        $table->setPrimaryKey(array("id"));
994
995
        $c    = new Comparator();
996
        $diff = $c->compare($oldSchema, $newSchema);
997
998
        self::assertCount(0, $diff->removedSequences);
999
    }
1000
1001
1002
    /**
1003
     * Check that added autoincrement sequence is not populated in newSequences
1004
     * @group DBAL-562
1005
     */
1006 View Code Duplication
    public function testAutoIncrementNoSequences()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1007
    {
1008
        $oldSchema = new Schema();
1009
        $table     = $oldSchema->createTable("foo");
1010
        $table->addColumn("id", "integer", array("autoincrement" => true));
1011
        $table->setPrimaryKey(array("id"));
1012
1013
        $newSchema = new Schema();
1014
        $table     = $newSchema->createTable("foo");
1015
        $table->addColumn("id", "integer", array("autoincrement" => true));
1016
        $table->setPrimaryKey(array("id"));
1017
        $newSchema->createSequence("foo_id_seq");
1018
1019
        $c    = new Comparator();
1020
        $diff = $c->compare($oldSchema, $newSchema);
1021
1022
        self::assertCount(0, $diff->newSequences);
1023
    }
1024
    /**
1025
     * You can get multiple drops for a FK when a table referenced by a foreign
1026
     * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys
1027
     * array because of the dropped table, and once on changedTables array. We
1028
     * now check that the key is present once.
1029
     */
1030
    public function testAvoidMultipleDropForeignKey()
1031
    {
1032
        $oldSchema = new Schema();
1033
1034
        $tableA = $oldSchema->createTable('table_a');
1035
        $tableA->addColumn('id', 'integer');
1036
1037
        $tableB = $oldSchema->createTable('table_b');
1038
        $tableB->addColumn('id', 'integer');
1039
1040
        $tableC = $oldSchema->createTable('table_c');
1041
        $tableC->addColumn('id', 'integer');
1042
        $tableC->addColumn('table_a_id', 'integer');
1043
        $tableC->addColumn('table_b_id', 'integer');
1044
1045
        $tableC->addForeignKeyConstraint($tableA, array('table_a_id'), array('id'));
1046
        $tableC->addForeignKeyConstraint($tableB, array('table_b_id'), array('id'));
1047
1048
        $newSchema = new Schema();
1049
1050
        $tableB = $newSchema->createTable('table_b');
1051
        $tableB->addColumn('id', 'integer');
1052
1053
        $tableC = $newSchema->createTable('table_c');
1054
        $tableC->addColumn('id', 'integer');
1055
1056
        $comparator = new Comparator();
1057
        $schemaDiff = $comparator->compare($oldSchema, $newSchema);
1058
1059
        self::assertCount(1, $schemaDiff->changedTables['table_c']->removedForeignKeys);
1060
        self::assertCount(1, $schemaDiff->orphanedForeignKeys);
1061
    }
1062
1063
    public function testCompareChangedColumn()
1064
    {
1065
        $oldSchema = new Schema();
1066
1067
        $tableFoo = $oldSchema->createTable('foo');
1068
        $tableFoo->addColumn('id', 'integer');
1069
1070
        $newSchema = new Schema();
1071
        $table     = $newSchema->createTable('foo');
1072
        $table->addColumn('id', 'string');
1073
1074
        $expected                      = new SchemaDiff();
1075
        $expected->fromSchema          = $oldSchema;
1076
        $tableDiff                     = $expected->changedTables['foo'] = new TableDiff('foo');
1077
        $tableDiff->fromTable          = $tableFoo;
1078
        $columnDiff                    = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
1079
        $columnDiff->fromColumn        = $tableFoo->getColumn('id');
1080
        $columnDiff->changedProperties = array('type');
1081
1082
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
1083
    }
1084
1085
    public function testCompareChangedBinaryColumn()
1086
    {
1087
        $oldSchema = new Schema();
1088
1089
        $tableFoo = $oldSchema->createTable('foo');
1090
        $tableFoo->addColumn('id', 'binary');
1091
1092
        $newSchema = new Schema();
1093
        $table     = $newSchema->createTable('foo');
1094
        $table->addColumn('id', 'binary', array('length' => 42, 'fixed' => true));
1095
1096
        $expected                      = new SchemaDiff();
1097
        $expected->fromSchema          = $oldSchema;
1098
        $tableDiff                     = $expected->changedTables['foo'] = new TableDiff('foo');
1099
        $tableDiff->fromTable          = $tableFoo;
1100
        $columnDiff                    = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
1101
        $columnDiff->fromColumn        = $tableFoo->getColumn('id');
1102
        $columnDiff->changedProperties = array('length', 'fixed');
1103
1104
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
1105
    }
1106
1107
    /**
1108
     * @group DBAL-617
1109
     */
1110 View Code Duplication
    public function testCompareQuotedAndUnquotedForeignKeyColumns()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1111
    {
1112
        $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION'));
1113
        $fk2 = new ForeignKeyConstraint(array("`foo`"), "bar", array("`baz`"), "fk1", array('onDelete' => 'NO ACTION'));
1114
1115
        $comparator = new Comparator();
1116
        $diff       = $comparator->diffForeignKey($fk1, $fk2);
1117
1118
        self::assertFalse($diff);
1119
    }
1120
1121
    /**
1122
     * @param SchemaDiff $diff
1123
     * @param int $newTableCount
1124
     * @param int $changeTableCount
1125
     * @param int $removeTableCount
1126
     */
1127
    public function assertSchemaTableChangeCount($diff, $newTableCount = 0, $changeTableCount = 0, $removeTableCount = 0)
1128
    {
1129
        self::assertEquals($newTableCount, count($diff->newTables));
1130
        self::assertEquals($changeTableCount, count($diff->changedTables));
1131
        self::assertEquals($removeTableCount, count($diff->removedTables));
1132
    }
1133
1134
    /**
1135
     * @param SchemaDiff $diff
1136
     * @param int $newSequenceCount
1137
     * @param int $changeSequenceCount
1138
     * @param int $changeSequenceCount
1139
     */
1140
    public function assertSchemaSequenceChangeCount($diff, $newSequenceCount = 0, $changeSequenceCount = 0, $removeSequenceCount = 0)
1141
    {
1142
        self::assertEquals($newSequenceCount, count($diff->newSequences), "Expected number of new sequences is wrong.");
1143
        self::assertEquals($changeSequenceCount, count($diff->changedSequences), "Expected number of changed sequences is wrong.");
1144
        self::assertEquals($removeSequenceCount, count($diff->removedSequences), "Expected number of removed sequences is wrong.");
1145
    }
1146
1147
    public function testDiffColumnPlatformOptions()
1148
    {
1149
        $column1 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'bar' => 'bar')));
1150
        $column2 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'foobar' => 'foobar')));
1151
        $column3 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'bar' => 'rab')));
1152
        $column4 = new Column('foo', Type::getType('string'));
1153
1154
        $comparator = new Comparator();
1155
1156
        self::assertEquals(array(), $comparator->diffColumn($column1, $column2));
1157
        self::assertEquals(array(), $comparator->diffColumn($column2, $column1));
1158
        self::assertEquals(array('bar'), $comparator->diffColumn($column1, $column3));
1159
        self::assertEquals(array('bar'), $comparator->diffColumn($column3, $column1));
1160
        self::assertEquals(array(), $comparator->diffColumn($column1, $column4));
1161
        self::assertEquals(array(), $comparator->diffColumn($column4, $column1));
1162
    }
1163
1164
    public function testComplexDiffColumn()
1165
    {
1166
        $column1 = new Column('foo', Type::getType('string'), array(
1167
            'platformOptions' => array('foo' => 'foo'),
1168
            'customSchemaOptions' => array('foo' => 'bar'),
1169
        ));
1170
1171
        $column2 = new Column('foo', Type::getType('string'), array(
1172
            'platformOptions' => array('foo' => 'bar'),
1173
        ));
1174
1175
        $comparator = new Comparator();
1176
1177
        self::assertEquals(array(), $comparator->diffColumn($column1, $column2));
1178
        self::assertEquals(array(), $comparator->diffColumn($column2, $column1));
1179
    }
1180
1181
    /**
1182
     * @group DBAL-669
1183
     */
1184
    public function testComparesNamespaces()
1185
    {
1186
        $comparator = new Comparator();
1187
        $fromSchema = $this->getMockBuilder('Doctrine\DBAL\Schema\Schema')
1188
            ->setMethods(array('getNamespaces', 'hasNamespace'))
1189
            ->getMock();
1190
        $toSchema   = $this->getMockBuilder('Doctrine\DBAL\Schema\Schema')
1191
            ->setMethods(array('getNamespaces', 'hasNamespace'))
1192
            ->getMock();
1193
1194
        $fromSchema->expects($this->once())
1195
            ->method('getNamespaces')
1196
            ->will($this->returnValue(array('foo', 'bar')));
1197
1198
        $fromSchema->expects($this->at(0))
1199
            ->method('hasNamespace')
1200
            ->with('bar')
1201
            ->will($this->returnValue(true));
1202
1203
        $fromSchema->expects($this->at(1))
1204
            ->method('hasNamespace')
1205
            ->with('baz')
1206
            ->will($this->returnValue(false));
1207
1208
        $toSchema->expects($this->once())
1209
            ->method('getNamespaces')
1210
            ->will($this->returnValue(array('bar', 'baz')));
1211
1212
        $toSchema->expects($this->at(1))
1213
            ->method('hasNamespace')
1214
            ->with('foo')
1215
            ->will($this->returnValue(false));
1216
1217
        $toSchema->expects($this->at(2))
1218
            ->method('hasNamespace')
1219
            ->with('bar')
1220
            ->will($this->returnValue(true));
1221
1222
        $expected                    = new SchemaDiff();
1223
        $expected->fromSchema        = $fromSchema;
1224
        $expected->newNamespaces     = array('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...
1225
        $expected->removedNamespaces = array('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...
1226
1227
        self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema));
1228
    }
1229
1230
    public function testCompareGuidColumns()
1231
    {
1232
        $comparator = new Comparator();
1233
1234
        $column1 = new Column('foo', Type::getType('guid'), array('comment' => 'GUID 1'));
1235
        $column2 = new Column(
1236
            'foo',
1237
            Type::getType('guid'),
1238
            array('notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.')
1239
        );
1240
1241
        self::assertEquals(array('notnull', 'default', 'comment'), $comparator->diffColumn($column1, $column2));
1242
        self::assertEquals(array('notnull', 'default', 'comment'), $comparator->diffColumn($column2, $column1));
1243
    }
1244
1245
    /**
1246
     * @group DBAL-1009
1247
     *
1248
     * @dataProvider getCompareColumnComments
1249
     */
1250
    public function testCompareColumnComments($comment1, $comment2, $equals)
1251
    {
1252
        $column1 = new Column('foo', Type::getType('integer'), array('comment' => $comment1));
1253
        $column2 = new Column('foo', Type::getType('integer'), array('comment' => $comment2));
1254
1255
        $comparator = new Comparator();
1256
1257
        $expectedDiff = $equals ? array() : array('comment');
1258
1259
        $actualDiff = $comparator->diffColumn($column1, $column2);
1260
1261
        self::assertSame($expectedDiff, $actualDiff);
1262
1263
        $actualDiff = $comparator->diffColumn($column2, $column1);
1264
1265
        self::assertSame($expectedDiff, $actualDiff);
1266
    }
1267
1268
    public function getCompareColumnComments()
1269
    {
1270
        return array(
1271
            array(null, null, true),
1272
            array('', '', true),
1273
            array(' ', ' ', true),
1274
            array('0', '0', true),
1275
            array('foo', 'foo', true),
1276
1277
            array(null, '', true),
1278
            array(null, ' ', false),
1279
            array(null, '0', false),
1280
            array(null, 'foo', false),
1281
1282
            array('', ' ', false),
1283
            array('', '0', false),
1284
            array('', 'foo', false),
1285
1286
            array(' ', '0', false),
1287
            array(' ', 'foo', false),
1288
1289
            array('0', 'foo', false),
1290
        );
1291
    }
1292
1293
    public function testForeignKeyRemovalWithRenamedLocalColumn()
1294
    {
1295
        $fromSchema = new Schema(array(
1296
            'table1' => new Table(
1297
                'table1',
1298
                array(
1299
                    'id' => new Column('id', Type::getType('integer')),
1300
                )
1301
            ),
1302
            'table2' => new Table(
1303
                'table2',
1304
                array(
1305
                    'id' => new Column('id', Type::getType('integer')),
1306
                    'id_table1' => new Column('id_table1', Type::getType('integer'))
1307
                ),
1308
                array(),
1309
                array(
1310
                    new ForeignKeyConstraint(array('id_table1'), 'table1', array('id'), 'fk_table2_table1')
1311
                )
1312
            )
1313
        ));
1314
        $toSchema   = new Schema(array(
1315
            'table2' => new Table(
1316
                'table2',
1317
                array(
1318
                    'id' => new Column('id', Type::getType('integer')),
1319
                    'id_table3' => new Column('id_table3', Type::getType('integer'))
1320
                ),
1321
                array(),
1322
                array(
1323
                    new ForeignKeyConstraint(array('id_table3'), 'table3', array('id'), 'fk_table2_table3')
1324
                )
1325
            ),
1326
            'table3' => new Table(
1327
                'table3',
1328
                array(
1329
                    'id' => new Column('id', Type::getType('integer'))
1330
                )
1331
            )
1332
        ));
1333
        $actual     = Comparator::compareSchemas($fromSchema, $toSchema);
1334
        self::assertArrayHasKey("table2", $actual->changedTables);
1335
        self::assertCount(1, $actual->orphanedForeignKeys);
1336
        self::assertEquals("fk_table2_table1", $actual->orphanedForeignKeys[0]->getName());
1337
        self::assertCount(1, $actual->changedTables['table2']->addedForeignKeys, "FK to table3 should be added.");
1338
        self::assertEquals("table3", $actual->changedTables['table2']->addedForeignKeys[0]->getForeignTableName());
1339
    }
1340
}
1341