ComparatorTest.php 45.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\Tests\DBAL\Schema;

jeroendedauw's avatar
jeroendedauw committed
22 23 24 25 26 27 28 29 30 31 32 33
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaConfig;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types\Type;
34
use function array_keys;
35 36 37 38 39 40 41 42 43 44

/**
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link    www.doctrine-project.org
 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
 * @license http://ez.no/licenses/new_bsd New BSD License
 * @since   2.0
 * @version $Revision$
 * @author  Benjamin Eberlei <kontakt@beberlei.de>
 */
Luís Cobucci's avatar
Luís Cobucci committed
45
class ComparatorTest extends \PHPUnit\Framework\TestCase
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
{
    public function testCompareSame1()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer' ) ),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer') ),
                )
            ),
        ) );

64 65
        $expected = new SchemaDiff();
        $expected->fromSchema = $schema1;
66
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    }

    public function testCompareSame2()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                )
            ),
        ) );
87 88 89

        $expected = new SchemaDiff();
        $expected->fromSchema = $schema1;
90
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
91 92 93 94
    }

    public function testCompareMissingTable()
    {
95 96 97
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
        $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
        $table->setSchemaConfig($schemaConfig);
98

99 100
        $schema1 = new Schema( array($table), array(), $schemaConfig );
        $schema2 = new Schema( array(),       array(), $schemaConfig );
101

102
        $expected = new SchemaDiff( array(), array(), array('bugdb' => $table), $schema1 );
103

104
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
105 106 107 108
    }

    public function testCompareNewTable()
    {
109 110 111
        $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig;
        $table = new Table('bugdb', array ('integerfield1' => new Column('integerfield1', Type::getType('integer'))));
        $table->setSchemaConfig($schemaConfig);
112

113 114
        $schema1 = new Schema( array(),       array(), $schemaConfig );
        $schema2 = new Schema( array($table), array(), $schemaConfig );
115

116 117
        $expected = new SchemaDiff( array('bugdb' => $table), array(), array(), $schema1 );

118
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
119 120
    }

121 122 123 124 125 126 127 128
    public function testCompareOnlyAutoincrementChanged()
    {
        $column1 = new Column('foo', Type::getType('integer'), array('autoincrement' => true));
        $column2 = new Column('foo', Type::getType('integer'), array('autoincrement' => false));

        $comparator = new Comparator();
        $changedProperties = $comparator->diffColumn($column1, $column2);

129
        self::assertEquals(array('autoincrement'), $changedProperties);
130 131
    }

132 133
    public function testCompareMissingField()
    {
134
        $missingColumn = new Column('integerfield1', Type::getType('integer'));
135 136 137
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
138
                    'integerfield1' => $missingColumn,
139 140 141 142 143 144 145 146 147 148 149 150 151 152
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
153
                'bugdb' => new TableDiff( 'bugdb', array(), array(),
154
                    array (
155
                        'integerfield1' => $missingColumn,
156 157 158 159
                    )
                )
            )
        );
160 161 162
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

163
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    }

    public function testCompareNewField()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
186
                'bugdb' => new TableDiff ('bugdb',
187 188 189 190 191 192
                    array (
                        'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                    )
                ),
            )
        );
193 194 195
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

196
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
197 198 199 200
    }

    public function testCompareChangedColumns_ChangeType()
    {
201 202
        $column1 = new Column('charfield1', Type::getType('string'));
        $column2 = new Column('charfield1', Type::getType('integer'));
203

204
        $c = new Comparator();
205 206
        self::assertEquals(array('type'), $c->diffColumn($column1, $column2));
        self::assertEquals(array(), $c->diffColumn($column1, $column1));
207 208
    }

209 210 211 212 213 214 215
    public function testCompareChangedColumns_ChangeCustomSchemaOption()
    {
        $column1 = new Column('charfield1', Type::getType('string'));
        $column2 = new Column('charfield1', Type::getType('string'));

        $column1->setCustomSchemaOption('foo', 'bar');
        $column2->setCustomSchemaOption('foo', 'bar');
216

217 218 219 220
        $column1->setCustomSchemaOption('foo1', 'bar1');
        $column2->setCustomSchemaOption('foo2', 'bar2');

        $c = new Comparator();
221 222
        self::assertEquals(array('foo1', 'foo2'), $c->diffColumn($column1, $column2));
        self::assertEquals(array(), $c->diffColumn($column1, $column1));
223 224
    }

225 226 227 228 229 230 231 232 233 234 235 236
    public function testCompareChangeColumns_MultipleNewColumnsRename()
    {
        $tableA = new Table("foo");
        $tableA->addColumn('datefield1', 'datetime');

        $tableB = new Table("foo");
        $tableB->addColumn('new_datefield1', 'datetime');
        $tableB->addColumn('new_datefield2', 'datetime');

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

237 238 239 240 241 242
        self::assertCount(1, $tableDiff->renamedColumns, "we should have one rename datefield1 => new_datefield1.");
        self::assertArrayHasKey('datefield1', $tableDiff->renamedColumns, "'datefield1' should be set to be renamed to new_datefield1");
        self::assertCount(1, $tableDiff->addedColumns, "'new_datefield2' should be added");
        self::assertArrayHasKey('new_datefield2', $tableDiff->addedColumns, "'new_datefield2' should be added, not created through renaming!");
        self::assertCount(0, $tableDiff->removedColumns, "Nothing should be removed.");
        self::assertCount(0, $tableDiff->changedColumns, "Nothing should be changed as all fields old & new have diff names.");
243
    }
Lee Davis's avatar
Lee Davis committed
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    public function testCompareRemovedIndex()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary',
                        array(
                            'integerfield1'
                        ),
                        true
                    )
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
274
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(), array(),
275
                    array (
276 277 278 279 280 281
                        'primary' => new Index('primary',
                        array(
                            'integerfield1'
                        ),
                        true
                    )
282 283 284 285
                    )
                ),
            )
        );
286 287 288
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

289
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    }

    public function testCompareNewIndex()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary',
                        array(
                            'integerfield1'
                        ),
                        true
                    )
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
321
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(),
322 323 324 325 326 327 328 329 330 331 332
                    array (
                        'primary' => new Index('primary',
                            array(
                                'integerfield1'
                            ),
                            true
                        )
                    )
                ),
            )
        );
333 334 335
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

336
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ) );
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
    }

    public function testCompareChangedIndex()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary',
                        array(
                            'integerfield1'
                        ),
                        true
                    )
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary',
                        array('integerfield1', 'integerfield2'),
                        true
                    )
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
374
                'bugdb' => new TableDiff( 'bugdb', array(), array(), array(), array(),
375 376 377 378 379 380 381 382 383 384 385 386
                    array (
                        'primary' => new Index('primary',
                            array(
                                'integerfield1',
                                'integerfield2'
                            ),
                            true
                        )
                    )
                ),
            )
        );
387 388 389
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

390
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
391
    }
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

    public function testCompareChangedIndexFieldPositions()
    {
        $schema1 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary', array('integerfield1', 'integerfield2'), true)
                )
            ),
        ) );
        $schema2 = new Schema( array(
            'bugdb' => new Table('bugdb',
                array (
                    'integerfield1' => new Column('integerfield1', Type::getType('integer')),
                    'integerfield2' => new Column('integerfield2', Type::getType('integer')),
                ),
                array (
                    'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
                )
            ),
        ) );

        $expected = new SchemaDiff ( array(),
            array (
420
                'bugdb' => new TableDiff('bugdb', array(), array(), array(), array(),
421 422 423 424 425 426
                    array (
                        'primary' => new Index('primary', array('integerfield2', 'integerfield1'), true)
                    )
                ),
            )
        );
427 428 429
        $expected->fromSchema = $schema1;
        $expected->changedTables['bugdb']->fromTable = $schema1->getTable('bugdb');

430
        self::assertEquals($expected, Comparator::compareSchemas( $schema1, $schema2 ));
431 432 433 434 435 436 437 438 439 440
    }

    public function testCompareSequences()
    {
        $seq1 = new Sequence('foo', 1, 1);
        $seq2 = new Sequence('foo', 1, 2);
        $seq3 = new Sequence('foo', 2, 1);

        $c = new Comparator();

441 442
        self::assertTrue($c->diffSequence($seq1, $seq2));
        self::assertTrue($c->diffSequence($seq1, $seq3));
443 444 445 446 447 448 449 450 451 452 453 454
    }

    public function testRemovedSequence()
    {
        $schema1 = new Schema();
        $seq = $schema1->createSequence('foo');

        $schema2 = new Schema();

        $c = new Comparator();
        $diffSchema = $c->compare($schema1, $schema2);

Gabriel Caruso's avatar
Gabriel Caruso committed
455
        self::assertCount(1, $diffSchema->removedSequences);
456
        self::assertSame($seq, $diffSchema->removedSequences[0]);
457 458 459 460 461 462 463 464 465 466 467 468
    }

    public function testAddedSequence()
    {
        $schema1 = new Schema();

        $schema2 = new Schema();
        $seq = $schema2->createSequence('foo');

        $c = new Comparator();
        $diffSchema = $c->compare($schema1, $schema2);

Gabriel Caruso's avatar
Gabriel Caruso committed
469
        self::assertCount(1, $diffSchema->newSequences);
470
        self::assertSame($seq, $diffSchema->newSequences[0]);
471 472 473 474 475
    }

    public function testTableAddForeignKey()
    {
        $tableForeign = new Table("bar");
476
        $tableForeign->addColumn('id', 'integer');
477 478

        $table1 = new Table("foo");
479
        $table1->addColumn('fk', 'integer');
480 481

        $table2 = new Table("foo");
482
        $table2->addColumn('fk', 'integer');
483 484 485 486 487
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table1, $table2);

488
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
Gabriel Caruso's avatar
Gabriel Caruso committed
489
        self::assertCount(1, $tableDiff->addedForeignKeys);
490 491 492 493 494
    }

    public function testTableRemoveForeignKey()
    {
        $tableForeign = new Table("bar");
495
        $tableForeign->addColumn('id', 'integer');
496 497

        $table1 = new Table("foo");
498
        $table1->addColumn('fk', 'integer');
499 500

        $table2 = new Table("foo");
501
        $table2->addColumn('fk', 'integer');
502 503 504 505 506
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table2, $table1);

507
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
Gabriel Caruso's avatar
Gabriel Caruso committed
508
        self::assertCount(1, $tableDiff->removedForeignKeys);
509 510 511 512 513
    }

    public function testTableUpdateForeignKey()
    {
        $tableForeign = new Table("bar");
514
        $tableForeign->addColumn('id', 'integer');
515 516

        $table1 = new Table("foo");
517
        $table1->addColumn('fk', 'integer');
518 519 520
        $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));

        $table2 = new Table("foo");
521
        $table2->addColumn('fk', 'integer');
522 523 524 525 526
        $table2->addForeignKeyConstraint($tableForeign, array('fk'), array('id'), array('onUpdate' => 'CASCADE'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table1, $table2);

527
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
Gabriel Caruso's avatar
Gabriel Caruso committed
528
        self::assertCount(1, $tableDiff->changedForeignKeys);
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    }

    public function testMovedForeignKeyForeignTable()
    {
        $tableForeign = new Table("bar");
        $tableForeign->addColumn('id', 'integer');

        $tableForeign2 = new Table("bar2");
        $tableForeign2->addColumn('id', 'integer');

        $table1 = new Table("foo");
        $table1->addColumn('fk', 'integer');
        $table1->addForeignKeyConstraint($tableForeign, array('fk'), array('id'));

        $table2 = new Table("foo");
        $table2->addColumn('fk', 'integer');
        $table2->addForeignKeyConstraint($tableForeign2, array('fk'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table1, $table2);

550
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
Gabriel Caruso's avatar
Gabriel Caruso committed
551
        self::assertCount(1, $tableDiff->changedForeignKeys);
552
    }
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

    public function testTablesCaseInsensitive()
    {
        $schemaA = new Schema();
        $schemaA->createTable('foo');
        $schemaA->createTable('bAr');
        $schemaA->createTable('BAZ');
        $schemaA->createTable('new');

        $schemaB = new Schema();
        $schemaB->createTable('FOO');
        $schemaB->createTable('bar');
        $schemaB->createTable('Baz');
        $schemaB->createTable('old');

        $c = new Comparator();
        $diff = $c->compare($schemaA, $schemaB);

571
        self::assertSchemaTableChangeCount($diff, 1, 0, 1);
572 573
    }

Possum's avatar
Possum committed
574
    public function testSequencesCaseInsensitive()
575
    {
576 577 578 579 580 581 582 583 584 585 586
        $schemaA = new Schema();
        $schemaA->createSequence('foo');
        $schemaA->createSequence('BAR');
        $schemaA->createSequence('Baz');
        $schemaA->createSequence('new');

        $schemaB = new Schema();
        $schemaB->createSequence('FOO');
        $schemaB->createSequence('Bar');
        $schemaB->createSequence('baz');
        $schemaB->createSequence('old');
587

588 589 590
        $c = new Comparator();
        $diff = $c->compare($schemaA, $schemaB);

591
        self::assertSchemaSequenceChangeCount($diff, 1, 0, 1);
592 593 594 595 596
    }

    public function testCompareColumnCompareCaseInsensitive()
    {
        $tableA = new Table("foo");
597
        $tableA->addColumn('id', 'integer');
598 599

        $tableB = new Table("foo");
600
        $tableB->addColumn('ID', 'integer');
601 602 603 604

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

605
        self::assertFalse($tableDiff);
606 607 608 609 610
    }

    public function testCompareIndexBasedOnPropertiesNotName()
    {
        $tableA = new Table("foo");
611
        $tableA->addColumn('id', 'integer');
612 613 614
        $tableA->addIndex(array("id"), "foo_bar_idx");

        $tableB = new Table("foo");
615
        $tableB->addColumn('ID', 'integer');
616 617 618
        $tableB->addIndex(array("id"), "bar_foo_idx");

        $c = new Comparator();
619 620 621
        $tableDiff = new TableDiff('foo');
        $tableDiff->fromTable = $tableA;
        $tableDiff->renamedIndexes['foo_bar_idx'] = new Index('bar_foo_idx', array('id'));
622

623
        self::assertEquals(
624 625 626
            $tableDiff,
            $c->diffTable($tableA, $tableB)
        );
627 628 629 630 631
    }

    public function testCompareForeignKeyBasedOnPropertiesNotName()
    {
        $tableA = new Table("foo");
632
        $tableA->addColumn('id', 'integer');
633 634 635
        $tableA->addNamedForeignKeyConstraint('foo_constraint', 'bar', array('id'), array('id'));

        $tableB = new Table("foo");
636
        $tableB->addColumn('ID', 'integer');
637 638 639 640 641
        $tableB->addNamedForeignKeyConstraint('bar_constraint', 'bar', array('id'), array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

642
        self::assertFalse($tableDiff);
643 644
    }

645 646 647 648 649 650
    public function testCompareForeignKey_RestrictNoAction_AreTheSame()
    {
        $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION'));
        $fk2 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'RESTRICT'));

        $c = new Comparator();
651
        self::assertFalse($c->diffForeignKey($fk1, $fk2));
652 653
    }

654 655 656 657 658 659 660 661 662
    /**
     * @group DBAL-492
     */
    public function testCompareForeignKeyNamesUnqualified_AsNoSchemaInformationIsAvailable()
    {
        $fk1 = new ForeignKeyConstraint(array("foo"), "foo.bar", array("baz"), "fk1");
        $fk2 = new ForeignKeyConstraint(array("foo"), "baz.bar", array("baz"), "fk1");

        $c = new Comparator();
663
        self::assertFalse($c->diffForeignKey($fk1, $fk2));
664 665
    }

666 667 668
    public function testDetectRenameColumn()
    {
        $tableA = new Table("foo");
669
        $tableA->addColumn('foo', 'integer');
670 671

        $tableB = new Table("foo");
672
        $tableB->addColumn('bar', 'integer');
673 674 675 676

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

Gabriel Caruso's avatar
Gabriel Caruso committed
677 678
        self::assertCount(0, $tableDiff->addedColumns);
        self::assertCount(0, $tableDiff->removedColumns);
679 680
        self::assertArrayHasKey('foo', $tableDiff->renamedColumns);
        self::assertEquals('bar', $tableDiff->renamedColumns['foo']->getName());
681 682
    }

683
    /**
Possum's avatar
Possum committed
684
     * You can easily have ambiguities in the column renaming. If these
685 686 687 688 689 690 691 692 693 694 695 696 697
     * are detected no renaming should take place, instead adding and dropping
     * should be used exclusively.
     *
     * @group DBAL-24
     */
    public function testDetectRenameColumnAmbiguous()
    {
        $tableA = new Table("foo");
        $tableA->addColumn('foo', 'integer');
        $tableA->addColumn('bar', 'integer');

        $tableB = new Table("foo");
        $tableB->addColumn('baz', 'integer');
698

699 700 701
        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

Gabriel Caruso's avatar
Gabriel Caruso committed
702
        self::assertCount(1, $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
703
        self::assertArrayHasKey('baz', $tableDiff->addedColumns, "'baz' should be added, not created through renaming!");
Gabriel Caruso's avatar
Gabriel Caruso committed
704
        self::assertCount(2, $tableDiff->removedColumns, "'foo' and 'bar' should both be dropped, an ambiguity exists which one could be renamed to 'baz'.");
705 706
        self::assertArrayHasKey('foo', $tableDiff->removedColumns, "'foo' should be removed.");
        self::assertArrayHasKey('bar', $tableDiff->removedColumns, "'bar' should be removed.");
Gabriel Caruso's avatar
Gabriel Caruso committed
707
        self::assertCount(0, $tableDiff->renamedColumns, "no renamings should take place.");
708 709
    }

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
    /**
     * @group DBAL-1063
     */
    public function testDetectRenameIndex()
    {
        $table1 = new Table('foo');
        $table1->addColumn('foo', 'integer');

        $table2 = clone $table1;

        $table1->addIndex(array('foo'), 'idx_foo');

        $table2->addIndex(array('foo'), 'idx_bar');

        $comparator = new Comparator();
        $tableDiff = $comparator->diffTable($table1, $table2);

727 728 729 730
        self::assertCount(0, $tableDiff->addedIndexes);
        self::assertCount(0, $tableDiff->removedIndexes);
        self::assertArrayHasKey('idx_foo', $tableDiff->renamedIndexes);
        self::assertEquals('idx_bar', $tableDiff->renamedIndexes['idx_foo']->getName());
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
    }

    /**
     * You can easily have ambiguities in the index renaming. If these
     * are detected no renaming should take place, instead adding and dropping
     * should be used exclusively.
     *
     * @group DBAL-1063
     */
    public function testDetectRenameIndexAmbiguous()
    {
        $table1 = new Table('foo');
        $table1->addColumn('foo', 'integer');

        $table2 = clone $table1;

        $table1->addIndex(array('foo'), 'idx_foo');
        $table1->addIndex(array('foo'), 'idx_bar');

        $table2->addIndex(array('foo'), 'idx_baz');

        $comparator = new Comparator();
        $tableDiff = $comparator->diffTable($table1, $table2);

755 756 757 758 759 760
        self::assertCount(1, $tableDiff->addedIndexes);
        self::assertArrayHasKey('idx_baz', $tableDiff->addedIndexes);
        self::assertCount(2, $tableDiff->removedIndexes);
        self::assertArrayHasKey('idx_foo', $tableDiff->removedIndexes);
        self::assertArrayHasKey('idx_bar', $tableDiff->removedIndexes);
        self::assertCount(0, $tableDiff->renamedIndexes);
761 762
    }

beberlei's avatar
beberlei committed
763 764
    public function testDetectChangeIdentifierType()
    {
765 766
        $this->markTestSkipped('DBAL-2 was reopened, this test cannot work anymore.');

beberlei's avatar
beberlei committed
767
        $tableA = new Table("foo");
768
        $tableA->addColumn('id', 'integer', array('autoincrement' => false));
beberlei's avatar
beberlei committed
769 770

        $tableB = new Table("foo");
771
        $tableB->addColumn('id', 'integer', array('autoincrement' => true));
beberlei's avatar
beberlei committed
772 773 774 775

        $c = new Comparator();
        $tableDiff = $c->diffTable($tableA, $tableB);

776 777
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
        self::assertArrayHasKey('id', $tableDiff->changedColumns);
beberlei's avatar
beberlei committed
778 779
    }

780 781 782 783 784 785 786 787

    /**
     * @group DBAL-105
     */
    public function testDiff()
    {
        $table = new \Doctrine\DBAL\Schema\Table('twitter_users');
        $table->addColumn('id', 'integer', array('autoincrement' => true));
788 789
        $table->addColumn('twitterId', 'integer');
        $table->addColumn('displayName', 'string');
790 791 792 793
        $table->setPrimaryKey(array('id'));

        $newtable = new \Doctrine\DBAL\Schema\Table('twitter_users');
        $newtable->addColumn('id', 'integer', array('autoincrement' => true));
794 795 796
        $newtable->addColumn('twitter_id', 'integer');
        $newtable->addColumn('display_name', 'string');
        $newtable->addColumn('logged_in_at', 'datetime');
797 798 799 800 801
        $newtable->setPrimaryKey(array('id'));

        $c = new Comparator();
        $tableDiff = $c->diffTable($table, $newtable);

802 803 804
        self::assertInstanceOf('Doctrine\DBAL\Schema\TableDiff', $tableDiff);
        self::assertEquals(array('twitterid', 'displayname'), array_keys($tableDiff->renamedColumns));
        self::assertEquals(array('logged_in_at'), array_keys($tableDiff->addedColumns));
Gabriel Caruso's avatar
Gabriel Caruso committed
805
        self::assertCount(0, $tableDiff->removedColumns);
806
    }
807 808


809 810 811 812 813 814 815
    /**
     * @group DBAL-112
     */
    public function testChangedSequence()
    {
        $schema = new Schema();
        $sequence = $schema->createSequence('baz');
816

817 818 819
        $schemaNew = clone $schema;
        /* @var $schemaNew Schema */
        $schemaNew->getSequence('baz')->setAllocationSize(20);
820

821 822
        $c = new \Doctrine\DBAL\Schema\Comparator;
        $diff = $c->compare($schema, $schemaNew);
823

824
        self::assertSame($diff->changedSequences[0] , $schemaNew->getSequence('baz'));
825
    }
826

827 828 829 830 831 832 833 834 835 836 837
    /**
     * @group DBAL-106
     */
    public function testDiffDecimalWithNullPrecision()
    {
        $column = new Column('foo', Type::getType('decimal'));
        $column->setPrecision(null);

        $column2 = new Column('foo', Type::getType('decimal'));

        $c = new Comparator();
838
        self::assertEquals(array(), $c->diffColumn($column, $column2));
839 840
    }

841 842 843
    /**
     * @group DBAL-204
     */
Possum's avatar
Possum committed
844
    public function testFqnSchemaComparison()
845 846
    {
        $config = new SchemaConfig();
847 848
        $config->setName("foo");

849 850 851
        $oldSchema = new Schema(array(), array(), $config);
        $oldSchema->createTable('bar');

852
        $newSchema= new Schema(array(), array(), $config);
853 854
        $newSchema->createTable('foo.bar');

855 856 857
        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;

858
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
859 860
    }

Marco Pivetta's avatar
Marco Pivetta committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    /**
     * @group DBAL-669
     */
    public function testNamespacesComparison()
    {
        $config = new SchemaConfig();
        $config->setName("schemaName");

        $oldSchema = new Schema(array(), array(), $config);
        $oldSchema->createTable('taz');
        $oldSchema->createTable('war.tab');

        $newSchema= new Schema(array(), array(), $config);
        $newSchema->createTable('bar.tab');
        $newSchema->createTable('baz.tab');
        $newSchema->createTable('war.tab');

        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;
        $expected->newNamespaces = array('bar' => 'bar', 'baz' => 'baz');

        $diff = Comparator::compareSchemas($oldSchema, $newSchema);

884 885
        self::assertEquals(array('bar' => 'bar', 'baz' => 'baz'), $diff->newNamespaces);
        self::assertCount(2, $diff->newTables);
Marco Pivetta's avatar
Marco Pivetta committed
886 887
    }

888 889 890
    /**
     * @group DBAL-204
     */
Possum's avatar
Possum committed
891
    public function testFqnSchemaComparisonDifferentSchemaNameButSameTableNoDiff()
892 893
    {
        $config = new SchemaConfig();
894 895
        $config->setName("foo");

896
        $oldSchema = new Schema(array(), array(), $config);
897
        $oldSchema->createTable('foo.bar');
898 899

        $newSchema = new Schema();
900
        $newSchema->createTable('bar');
901

902 903 904
        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;

905
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
906 907 908 909 910
    }

    /**
     * @group DBAL-204
     */
Possum's avatar
Possum committed
911
    public function testFqnSchemaComparisonNoSchemaSame()
912 913
    {
        $config = new SchemaConfig();
914
        $config->setName("foo");
915 916 917 918 919 920
        $oldSchema = new Schema(array(), array(), $config);
        $oldSchema->createTable('bar');

        $newSchema = new Schema();
        $newSchema->createTable('bar');

921 922
        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;
923

924
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
925 926
    }

927 928 929
    /**
     * @group DDC-1657
     */
Possum's avatar
Possum committed
930
    public function testAutoIncrementSequences()
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
    {
        $oldSchema = new Schema();
        $table = $oldSchema->createTable("foo");
        $table->addColumn("id", "integer", array("autoincrement" => true));
        $table->setPrimaryKey(array("id"));
        $oldSchema->createSequence("foo_id_seq");

        $newSchema = new Schema();
        $table = $newSchema->createTable("foo");
        $table->addColumn("id", "integer", array("autoincrement" => true));
        $table->setPrimaryKey(array("id"));

        $c = new Comparator();
        $diff = $c->compare($oldSchema, $newSchema);

946
        self::assertCount(0, $diff->removedSequences);
947 948
    }

949

950 951 952 953
    /**
     * Check that added autoincrement sequence is not populated in newSequences
     * @group DBAL-562
     */
Possum's avatar
Possum committed
954
    public function testAutoIncrementNoSequences()
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
    {
        $oldSchema = new Schema();
        $table = $oldSchema->createTable("foo");
        $table->addColumn("id", "integer", array("autoincrement" => true));
        $table->setPrimaryKey(array("id"));

        $newSchema = new Schema();
        $table = $newSchema->createTable("foo");
        $table->addColumn("id", "integer", array("autoincrement" => true));
        $table->setPrimaryKey(array("id"));
        $newSchema->createSequence("foo_id_seq");

        $c = new Comparator();
        $diff = $c->compare($oldSchema, $newSchema);

970
        self::assertCount(0, $diff->newSequences);
971
    }
972
    /**
973 974 975 976
     * You can get multiple drops for a FK when a table referenced by a foreign
     * key is deleted, as this FK is referenced twice, once on the orphanedForeignKeys
     * array because of the dropped table, and once on changedTables array. We
     * now check that the key is present once.
977 978 979 980 981
     */
    public function testAvoidMultipleDropForeignKey()
    {
        $oldSchema = new Schema();

982 983 984 985 986
        $tableA = $oldSchema->createTable('table_a');
        $tableA->addColumn('id', 'integer');

        $tableB = $oldSchema->createTable('table_b');
        $tableB->addColumn('id', 'integer');
987

988 989 990 991
        $tableC = $oldSchema->createTable('table_c');
        $tableC->addColumn('id', 'integer');
        $tableC->addColumn('table_a_id', 'integer');
        $tableC->addColumn('table_b_id', 'integer');
992

993 994
        $tableC->addForeignKeyConstraint($tableA, array('table_a_id'), array('id'));
        $tableC->addForeignKeyConstraint($tableB, array('table_b_id'), array('id'));
995 996 997

        $newSchema = new Schema();

998 999 1000 1001 1002 1003 1004 1005
        $tableB = $newSchema->createTable('table_b');
        $tableB->addColumn('id', 'integer');

        $tableC = $newSchema->createTable('table_c');
        $tableC->addColumn('id', 'integer');

        $comparator = new Comparator();
        $schemaDiff = $comparator->compare($oldSchema, $newSchema);
1006

1007 1008
        self::assertCount(1, $schemaDiff->changedTables['table_c']->removedForeignKeys);
        self::assertCount(1, $schemaDiff->orphanedForeignKeys);
1009 1010
    }

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
    public function testCompareChangedColumn()
    {
        $oldSchema = new Schema();

        $tableFoo = $oldSchema->createTable('foo');
        $tableFoo->addColumn('id', 'integer');

        $newSchema = new Schema();
        $table = $newSchema->createTable('foo');
        $table->addColumn('id', 'string');

        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;
        $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo');
        $tableDiff->fromTable = $tableFoo;
        $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
        $columnDiff->fromColumn = $tableFoo->getColumn('id');
        $columnDiff->changedProperties = array('type');

1030
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
1031
    }
1032

Steve Müller's avatar
Steve Müller committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    public function testCompareChangedBinaryColumn()
    {
        $oldSchema = new Schema();

        $tableFoo = $oldSchema->createTable('foo');
        $tableFoo->addColumn('id', 'binary');

        $newSchema = new Schema();
        $table = $newSchema->createTable('foo');
        $table->addColumn('id', 'binary', array('length' => 42, 'fixed' => true));

        $expected = new SchemaDiff();
        $expected->fromSchema = $oldSchema;
        $tableDiff = $expected->changedTables['foo'] = new TableDiff('foo');
        $tableDiff->fromTable = $tableFoo;
        $columnDiff = $tableDiff->changedColumns['id'] = new ColumnDiff('id', $table->getColumn('id'));
        $columnDiff->fromColumn = $tableFoo->getColumn('id');
        $columnDiff->changedProperties = array('length', 'fixed');

1052
        self::assertEquals($expected, Comparator::compareSchemas($oldSchema, $newSchema));
Steve Müller's avatar
Steve Müller committed
1053 1054
    }

1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
    /**
     * @group DBAL-617
     */
    public function testCompareQuotedAndUnquotedForeignKeyColumns()
    {
        $fk1 = new ForeignKeyConstraint(array("foo"), "bar", array("baz"), "fk1", array('onDelete' => 'NO ACTION'));
        $fk2 = new ForeignKeyConstraint(array("`foo`"), "bar", array("`baz`"), "fk1", array('onDelete' => 'NO ACTION'));

        $comparator = new Comparator();
        $diff = $comparator->diffForeignKey($fk1, $fk2);

1066
        self::assertFalse($diff);
1067 1068
    }

1069 1070
    /**
     * @param SchemaDiff $diff
1071 1072 1073
     * @param int        $newTableCount
     * @param int        $changeTableCount
     * @param int        $removeTableCount
1074 1075 1076
     */
    public function assertSchemaTableChangeCount($diff, $newTableCount=0, $changeTableCount=0, $removeTableCount=0)
    {
Gabriel Caruso's avatar
Gabriel Caruso committed
1077 1078 1079
        self::assertCount($newTableCount, $diff->newTables);
        self::assertCount($changeTableCount, $diff->changedTables);
        self::assertCount($removeTableCount, $diff->removedTables);
1080
    }
1081 1082 1083

    /**
     * @param SchemaDiff $diff
1084 1085 1086
     * @param int        $newSequenceCount
     * @param int        $changeSequenceCount
     * @param int        $changeSequenceCount
1087 1088 1089
     */
    public function assertSchemaSequenceChangeCount($diff, $newSequenceCount=0, $changeSequenceCount=0, $removeSequenceCount=0)
    {
Gabriel Caruso's avatar
Gabriel Caruso committed
1090 1091 1092
        self::assertCount($newSequenceCount, $diff->newSequences, "Expected number of new sequences is wrong.");
        self::assertCount($changeSequenceCount, $diff->changedSequences, "Expected number of changed sequences is wrong.");
        self::assertCount($removeSequenceCount, $diff->removedSequences, "Expected number of removed sequences is wrong.");
1093
    }
1094 1095 1096

    public function testDiffColumnPlatformOptions()
    {
1097 1098 1099
        $column1 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'bar' => 'bar')));
        $column2 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'foobar' => 'foobar')));
        $column3 = new Column('foo', Type::getType('string'), array('platformOptions' => array('foo' => 'foo', 'bar' => 'rab')));
1100 1101 1102 1103
        $column4 = new Column('foo', Type::getType('string'));

        $comparator = new Comparator();

1104 1105 1106 1107 1108 1109
        self::assertEquals(array(), $comparator->diffColumn($column1, $column2));
        self::assertEquals(array(), $comparator->diffColumn($column2, $column1));
        self::assertEquals(array('bar'), $comparator->diffColumn($column1, $column3));
        self::assertEquals(array('bar'), $comparator->diffColumn($column3, $column1));
        self::assertEquals(array(), $comparator->diffColumn($column1, $column4));
        self::assertEquals(array(), $comparator->diffColumn($column4, $column1));
1110
    }
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

    public function testComplexDiffColumn()
    {
        $column1 = new Column('foo', Type::getType('string'), array(
            'platformOptions' => array('foo' => 'foo'),
            'customSchemaOptions' => array('foo' => 'bar'),
        ));

        $column2 = new Column('foo', Type::getType('string'), array(
            'platformOptions' => array('foo' => 'bar'),
        ));

        $comparator = new Comparator();

1125 1126
        self::assertEquals(array(), $comparator->diffColumn($column1, $column2));
        self::assertEquals(array(), $comparator->diffColumn($column2, $column1));
1127
    }
1128 1129 1130 1131 1132 1133 1134

    /**
     * @group DBAL-669
     */
    public function testComparesNamespaces()
    {
        $comparator = new Comparator();
1135 1136 1137 1138 1139 1140
        $fromSchema = $this->getMockBuilder('Doctrine\DBAL\Schema\Schema')
            ->setMethods(array('getNamespaces', 'hasNamespace'))
            ->getMock();
        $toSchema = $this->getMockBuilder('Doctrine\DBAL\Schema\Schema')
            ->setMethods(array('getNamespaces', 'hasNamespace'))
            ->getMock();
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174

        $fromSchema->expects($this->once())
            ->method('getNamespaces')
            ->will($this->returnValue(array('foo', 'bar')));

        $fromSchema->expects($this->at(0))
            ->method('hasNamespace')
            ->with('bar')
            ->will($this->returnValue(true));

        $fromSchema->expects($this->at(1))
            ->method('hasNamespace')
            ->with('baz')
            ->will($this->returnValue(false));

        $toSchema->expects($this->once())
            ->method('getNamespaces')
            ->will($this->returnValue(array('bar', 'baz')));

        $toSchema->expects($this->at(1))
            ->method('hasNamespace')
            ->with('foo')
            ->will($this->returnValue(false));

        $toSchema->expects($this->at(2))
            ->method('hasNamespace')
            ->with('bar')
            ->will($this->returnValue(true));

        $expected = new SchemaDiff();
        $expected->fromSchema = $fromSchema;
        $expected->newNamespaces = array('baz' => 'baz');
        $expected->removedNamespaces = array('foo' => 'foo');

1175
        self::assertEquals($expected, $comparator->compare($fromSchema, $toSchema));
1176
    }
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

    public function testCompareGuidColumns()
    {
        $comparator = new Comparator();

        $column1 = new Column('foo', Type::getType('guid'), array('comment' => 'GUID 1'));
        $column2 = new Column(
            'foo',
            Type::getType('guid'),
            array('notnull' => false, 'length' => '36', 'fixed' => true, 'default' => 'NEWID()', 'comment' => 'GUID 2.')
        );

1189 1190
        self::assertEquals(array('notnull', 'default', 'comment'), $comparator->diffColumn($column1, $column2));
        self::assertEquals(array('notnull', 'default', 'comment'), $comparator->diffColumn($column2, $column1));
1191
    }
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208

    /**
     * @group DBAL-1009
     *
     * @dataProvider getCompareColumnComments
     */
    public function testCompareColumnComments($comment1, $comment2, $equals)
    {
        $column1 = new Column('foo', Type::getType('integer'), array('comment' => $comment1));
        $column2 = new Column('foo', Type::getType('integer'), array('comment' => $comment2));

        $comparator = new Comparator();

        $expectedDiff = $equals ? array() : array('comment');

        $actualDiff = $comparator->diffColumn($column1, $column2);

1209
        self::assertSame($expectedDiff, $actualDiff);
1210 1211 1212

        $actualDiff = $comparator->diffColumn($column2, $column1);

1213
        self::assertSame($expectedDiff, $actualDiff);
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
    }

    public function getCompareColumnComments()
    {
        return array(
            array(null, null, true),
            array('', '', true),
            array(' ', ' ', true),
            array('0', '0', true),
            array('foo', 'foo', true),

            array(null, '', true),
            array(null, ' ', false),
            array(null, '0', false),
            array(null, 'foo', false),

            array('', ' ', false),
            array('', '0', false),
            array('', 'foo', false),

            array(' ', '0', false),
            array(' ', 'foo', false),

            array('0', 'foo', false),
        );
    }
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273

    public function testForeignKeyRemovalWithRenamedLocalColumn()
    {
        $fromSchema = new Schema( array(
            'table1' => new Table('table1',
                array(
                    'id' => new Column('id', Type::getType('integer')),
                )),
            'table2' => new Table('table2',
                array(
                    'id' => new Column('id', Type::getType('integer')),
                    'id_table1' => new Column('id_table1', Type::getType('integer'))
                ),
                array(),
                array(
                    new ForeignKeyConstraint(array('id_table1'), 'table1', array('id'), 'fk_table2_table1')
                ))
        ));
        $toSchema = new Schema( array(
            'table2' => new Table('table2',
                array(
                    'id' => new Column('id', Type::getType('integer')),
                    'id_table3' => new Column('id_table3', Type::getType('integer'))
                ),
                array(),
                array(
                    new ForeignKeyConstraint(array('id_table3'), 'table3', array('id'), 'fk_table2_table3')
                )),
            'table3' => new Table('table3',
                array(
                    'id' => new Column('id', Type::getType('integer'))
                ))
        ));
        $actual = Comparator::compareSchemas($fromSchema, $toSchema);
1274 1275 1276 1277 1278
        self::assertArrayHasKey("table2", $actual->changedTables);
        self::assertCount(1, $actual->orphanedForeignKeys);
        self::assertEquals("fk_table2_table1", $actual->orphanedForeignKeys[0]->getName());
        self::assertCount(1, $actual->changedTables['table2']->addedForeignKeys, "FK to table3 should be added.");
        self::assertEquals("table3", $actual->changedTables['table2']->addedForeignKeys[0]->getForeignTableName());
1279
    }
1280
}