Passed
Pull Request — master (#2945)
by Dorian
20:40 queued 09:56
created

ComparatorTest::testTableRenamedForeignKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 18
Ratio 100 %

Importance

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

590
        self::/** @scrutinizer ignore-call */ 
591
              assertSchemaTableChangeCount($diff, 1, 0, 1);
Loading history...
591
    }
592
593 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...
594
    {
595
        $schemaA = new Schema();
596
        $schemaA->createSequence('foo');
597
        $schemaA->createSequence('BAR');
598
        $schemaA->createSequence('Baz');
599
        $schemaA->createSequence('new');
600
601
        $schemaB = new Schema();
602
        $schemaB->createSequence('FOO');
603
        $schemaB->createSequence('Bar');
604
        $schemaB->createSequence('baz');
605
        $schemaB->createSequence('old');
606
607
        $c = new Comparator();
608
        $diff = $c->compare($schemaA, $schemaB);
609
610
        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

610
        self::/** @scrutinizer ignore-call */ 
611
              assertSchemaSequenceChangeCount($diff, 1, 0, 1);
Loading history...
611
    }
612
613
    public function testCompareColumnCompareCaseInsensitive()
614
    {
615
        $tableA = new Table("foo");
616
        $tableA->addColumn('id', 'integer');
617
618
        $tableB = new Table("foo");
619
        $tableB->addColumn('ID', 'integer');
620
621
        $c = new Comparator();
622
        $tableDiff = $c->diffTable($tableA, $tableB);
623
624
        self::assertFalse($tableDiff);
0 ignored issues
show
Bug introduced by
$tableDiff of type Doctrine\DBAL\Schema\TableDiff is incompatible with the type boolean expected by parameter $condition of PHPUnit\Framework\Assert::assertFalse(). ( Ignorable by Annotation )

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

624
        self::assertFalse(/** @scrutinizer ignore-type */ $tableDiff);
Loading history...
625
    }
626
627 View Code Duplication
    public function testCompareIndexBasedOnPropertiesNotName()
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...
628
    {
629
        $tableA = new Table("foo");
630
        $tableA->addColumn('id', 'integer');
631
        $tableA->addIndex(array("id"), "foo_bar_idx");
632
633
        $tableB = new Table("foo");
634
        $tableB->addColumn('ID', 'integer');
635
        $tableB->addIndex(array("id"), "bar_foo_idx");
636
637
        $c = new Comparator();
638
        $tableDiff = new TableDiff('foo');
639
        $tableDiff->fromTable = $tableA;
640
        $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', array('id'));
641
642
        self::assertEquals(
643
            $tableDiff,
644
            $c->diffTable($tableA, $tableB)
645
        );
646
    }
647
648
    public function testCompareForeignKeyBasedOnPropertiesNotName()
649
    {
650
        $tableA = new Table("foo");
651
        $tableA->addColumn('id', 'integer');
652
        $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

652
        /** @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...
653
654
        $tableB = new Table("foo");
655
        $tableB->addColumn('ID', 'integer');
656
        $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

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

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

1174
            ->with(/** @scrutinizer ignore-type */ 'bar')
Loading history...
1175
            ->will($this->returnValue(true));
1176
1177
        $fromSchema->expects($this->at(1))
1178
            ->method('hasNamespace')
1179
            ->with('baz')
1180
            ->will($this->returnValue(false));
1181
1182
        $toSchema->expects($this->once())
1183
            ->method('getNamespaces')
1184
            ->will($this->returnValue(array('bar', 'baz')));
1185
1186
        $toSchema->expects($this->at(1))
1187
            ->method('hasNamespace')
1188
            ->with('foo')
1189
            ->will($this->returnValue(false));
1190
1191
        $toSchema->expects($this->at(2))
1192
            ->method('hasNamespace')
1193
            ->with('bar')
1194
            ->will($this->returnValue(true));
1195
1196
        $expected = new SchemaDiff();
1197
        $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...
1198
        $expected->newNamespaces = array('baz' => 'baz');
1199
        $expected->removedNamespaces = array('foo' => 'foo');
1200
1201
        self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema));
1202
    }
1203
1204
    public function testCompareGuidColumns()
1205
    {
1206
        $comparator = new Comparator();
1207
1208
        $column1 = new Column('foo', Type::getType('guid'), array('comment' => 'GUID 1'));
1209
        $column2 = new Column(
1210
            'foo',
1211
            Type::getType('guid'),
1212
            array('notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.')
1213
        );
1214
1215
        self::assertEquals(array('notnull', 'default', 'comment'), $comparator->diffColumn($column1, $column2));
1216
        self::assertEquals(array('notnull', 'default', 'comment'), $comparator->diffColumn($column2, $column1));
1217
    }
1218
1219
    /**
1220
     * @group DBAL-1009
1221
     *
1222
     * @dataProvider getCompareColumnComments
1223
     */
1224
    public function testCompareColumnComments($comment1, $comment2, $equals)
1225
    {
1226
        $column1 = new Column('foo', Type::getType('integer'), array('comment' => $comment1));
1227
        $column2 = new Column('foo', Type::getType('integer'), array('comment' => $comment2));
1228
1229
        $comparator = new Comparator();
1230
1231
        $expectedDiff = $equals ? array() : array('comment');
1232
1233
        $actualDiff = $comparator->diffColumn($column1, $column2);
1234
1235
        self::assertSame($expectedDiff, $actualDiff);
1236
1237
        $actualDiff = $comparator->diffColumn($column2, $column1);
1238
1239
        self::assertSame($expectedDiff, $actualDiff);
1240
    }
1241
1242
    public function getCompareColumnComments()
1243
    {
1244
        return array(
1245
            array(null, null, true),
1246
            array('', '', true),
1247
            array(' ', ' ', true),
1248
            array('0', '0', true),
1249
            array('foo', 'foo', true),
1250
1251
            array(null, '', true),
1252
            array(null, ' ', false),
1253
            array(null, '0', false),
1254
            array(null, 'foo', false),
1255
1256
            array('', ' ', false),
1257
            array('', '0', false),
1258
            array('', 'foo', false),
1259
1260
            array(' ', '0', false),
1261
            array(' ', 'foo', false),
1262
1263
            array('0', 'foo', false),
1264
        );
1265
    }
1266
1267
    public function testForeignKeyRemovalWithRenamedLocalColumn()
1268
    {
1269
        $fromSchema = new Schema( array(
1270
            'table1' => new Table('table1',
1271
                array(
1272
                    'id' => new Column('id', Type::getType('integer')),
1273
                )),
1274
            'table2' => new Table('table2',
1275
                array(
1276
                    'id' => new Column('id', Type::getType('integer')),
1277
                    'id_table1' => new Column('id_table1', Type::getType('integer'))
1278
                ),
1279
                array(),
1280
                array(
1281
                    new ForeignKeyConstraint(array('id_table1'), 'table1', array('id'), 'fk_table2_table1')
1282
                ))
1283
        ));
1284
        $toSchema = new Schema( array(
1285
            'table2' => new Table('table2',
1286
                array(
1287
                    'id' => new Column('id', Type::getType('integer')),
1288
                    'id_table3' => new Column('id_table3', Type::getType('integer'))
1289
                ),
1290
                array(),
1291
                array(
1292
                    new ForeignKeyConstraint(array('id_table3'), 'table3', array('id'), 'fk_table2_table3')
1293
                )),
1294
            'table3' => new Table('table3',
1295
                array(
1296
                    'id' => new Column('id', Type::getType('integer'))
1297
                ))
1298
        ));
1299
        $actual = Comparator::compareSchemas($fromSchema, $toSchema);
1300
        self::assertArrayHasKey("table2", $actual->changedTables);
1301
        self::assertCount(1, $actual->orphanedForeignKeys);
1302
        self::assertEquals("fk_table2_table1", $actual->orphanedForeignKeys[0]->getName());
1303
        self::assertCount(1, $actual->changedTables['table2']->addedForeignKeys, "FK to table3 should be added.");
1304
        self::assertEquals("table3", $actual->changedTables['table2']->addedForeignKeys[0]->getForeignTableName());
1305
    }
1306
}
1307