TableTest.php 30.2 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\DBAL\Schema;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
jeroendedauw's avatar
jeroendedauw committed
7 8
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
9 10
use Doctrine\DBAL\Types\Type;

11
class TableTest extends \Doctrine\Tests\DbalTestCase
12
{
13 14 15 16 17 18
    public function testCreateWithInvalidTableName()
    {
        $this->setExpectedException('Doctrine\DBAL\DBALException');
        $table = new \Doctrine\DBAL\Schema\Table('');
    }

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    public function testGetName()
    {
        $table =  new Table("foo", array(), array(), array());
        $this->assertEquals("foo", $table->getName());
    }

    public function testColumns()
    {
        $type = Type::getType('integer');
        $columns = array();
        $columns[] = new Column("foo", $type);
        $columns[] = new Column("bar", $type);
        $table = new Table("foo", $columns, array(), array());

        $this->assertTrue($table->hasColumn("foo"));
        $this->assertTrue($table->hasColumn("bar"));
        $this->assertFalse($table->hasColumn("baz"));

37 38
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));
39 40 41 42

        $this->assertEquals(2, count($table->getColumns()));
    }

43 44 45
    public function testColumnsCaseInsensitive()
    {
        $table = new Table("foo");
46
        $column = $table->addColumn('Foo', 'integer');
47 48 49 50 51 52 53 54 55 56

        $this->assertTrue($table->hasColumn('Foo'));
        $this->assertTrue($table->hasColumn('foo'));
        $this->assertTrue($table->hasColumn('FOO'));

        $this->assertSame($column, $table->getColumn('Foo'));
        $this->assertSame($column, $table->getColumn('foo'));
        $this->assertSame($column, $table->getColumn('FOO'));
    }

57 58 59 60 61 62 63
    public function testCreateColumn()
    {
        $type = Type::getType('integer');

        $table = new Table("foo");

        $this->assertFalse($table->hasColumn("bar"));
64
        $table->addColumn("bar", 'integer');
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        $this->assertTrue($table->hasColumn("bar"));
        $this->assertSame($type, $table->getColumn("bar")->getType());
    }

    public function testDropColumn()
    {
        $type = Type::getType('integer');
        $columns = array();
        $columns[] = new Column("foo", $type);
        $columns[] = new Column("bar", $type);
        $table = new Table("foo", $columns, array(), array());

        $this->assertTrue($table->hasColumn("foo"));
        $this->assertTrue($table->hasColumn("bar"));

        $table->dropColumn("foo")->dropColumn("bar");

        $this->assertFalse($table->hasColumn("foo"));
        $this->assertFalse($table->hasColumn("bar"));
    }

    public function testGetUnknownColumnThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $table = new Table("foo", array(), array(), array());
        $table->getColumn('unknown');
    }

    public function testAddColumnTwiceThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $type = \Doctrine\DBAL\Types\Type::getType('integer');
        $columns = array();
        $columns[] = new Column("foo", $type);
        $columns[] = new Column("foo", $type);
        $table = new Table("foo", $columns, array(), array());
    }

105 106 107 108 109
    public function testCreateIndex()
    {
        $type = \Doctrine\DBAL\Types\Type::getType('integer');
        $columns = array(new Column("foo", $type), new Column("bar", $type), new Column("baz", $type));
        $table = new Table("foo", $columns);
110

111 112
        $table->addIndex(array("foo", "bar"), "foo_foo_bar_idx");
        $table->addUniqueIndex(array("bar", "baz"), "foo_bar_baz_uniq");
113

114 115
        $this->assertTrue($table->hasIndex("foo_foo_bar_idx"));
        $this->assertTrue($table->hasIndex("foo_bar_baz_uniq"));
116 117
    }

118 119 120
    public function testIndexCaseInsensitive()
    {
        $type = \Doctrine\DBAL\Types\Type::getType('integer');
121 122 123 124 125
        $columns = array(
            new Column("foo", $type),
            new Column("bar", $type),
            new Column("baz", $type)
        );
126 127 128 129 130 131 132 133 134
        $table = new Table("foo", $columns);

        $table->addIndex(array("foo", "bar", "baz"), "Foo_Idx");

        $this->assertTrue($table->hasIndex('foo_idx'));
        $this->assertTrue($table->hasIndex('Foo_Idx'));
        $this->assertTrue($table->hasIndex('FOO_IDX'));
    }

135 136 137
    public function testAddIndexes()
    {
        $type = \Doctrine\DBAL\Types\Type::getType('integer');
138 139 140 141
        $columns = array(
            new Column("foo", $type),
            new Column("bar", $type),
        );
142 143
        $indexes = array(
            new Index("the_primary", array("foo"), true, true),
144
            new Index("bar_idx", array("bar"), false, false),
145 146 147 148
        );
        $table = new Table("foo", $columns, $indexes, array());

        $this->assertTrue($table->hasIndex("the_primary"));
149
        $this->assertTrue($table->hasIndex("bar_idx"));
150 151
        $this->assertFalse($table->hasIndex("some_idx"));

152 153 154
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('bar_idx'));
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    }

    public function testGetUnknownIndexThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $table = new Table("foo", array(), array(), array());
        $table->getIndex("unknownIndex");
    }

    public function testAddTwoPrimaryThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $type = \Doctrine\DBAL\Types\Type::getType('integer');
170
        $columns = array(new Column("foo", $type), new Column("bar", $type));
171 172
        $indexes = array(
            new Index("the_primary", array("foo"), true, true),
173
            new Index("other_primary", array("bar"), true, true),
174 175 176 177 178 179 180 181 182
        );
        $table = new Table("foo", $columns, $indexes, array());
    }

    public function testAddTwoIndexesWithSameNameThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $type = \Doctrine\DBAL\Types\Type::getType('integer');
183
        $columns = array(new Column("foo", $type), new Column("bar", $type));
184 185
        $indexes = array(
            new Index("an_idx", array("foo"), false, false),
186
            new Index("an_idx", array("bar"), false, false),
187 188 189 190 191 192 193 194 195
        );
        $table = new Table("foo", $columns, $indexes, array());
    }

    public function testConstraints()
    {
        $constraint = new ForeignKeyConstraint(array(), "foo", array());

        $tableA = new Table("foo", array(), array(), array($constraint));
196
        $constraints = $tableA->getForeignKeys();
197 198

        $this->assertEquals(1, count($constraints));
199
        $this->assertSame($constraint, array_shift($constraints));
200 201 202 203
    }

    public function testOptions()
    {
204
        $table = new Table("foo", array(), array(), array(), false, array("foo" => "bar"));
205 206 207 208 209 210 211 212 213

        $this->assertTrue($table->hasOption("foo"));
        $this->assertEquals("bar", $table->getOption("foo"));
    }

    public function testBuilderSetPrimaryKey()
    {
        $table = new Table("foo");

214
        $table->addColumn("bar", 'integer');
215 216 217
        $table->setPrimaryKey(array("bar"));

        $this->assertTrue($table->hasIndex("primary"));
218
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
219 220 221 222 223 224 225 226
        $this->assertTrue($table->getIndex("primary")->isUnique());
        $this->assertTrue($table->getIndex("primary")->isPrimary());
    }

    public function testBuilderAddUniqueIndex()
    {
        $table = new Table("foo");

227
        $table->addColumn("bar", 'integer');
228 229 230 231 232 233 234 235 236 237 238
        $table->addUniqueIndex(array("bar"), "my_idx");

        $this->assertTrue($table->hasIndex("my_idx"));
        $this->assertTrue($table->getIndex("my_idx")->isUnique());
        $this->assertFalse($table->getIndex("my_idx")->isPrimary());
    }

    public function testBuilderAddIndex()
    {
        $table = new Table("foo");

239
        $table->addColumn("bar", 'integer');
240 241 242 243 244 245 246 247 248 249 250 251
        $table->addIndex(array("bar"), "my_idx");

        $this->assertTrue($table->hasIndex("my_idx"));
        $this->assertFalse($table->getIndex("my_idx")->isUnique());
        $this->assertFalse($table->getIndex("my_idx")->isPrimary());
    }

    public function testBuilderAddIndexWithInvalidNameThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $table = new Table("foo");
252
        $table->addColumn("bar",'integer');
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
        $table->addIndex(array("bar"), "invalid name %&/");
    }

    public function testBuilderAddIndexWithUnknownColumnThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $table = new Table("foo");
        $table->addIndex(array("bar"), "invalidName");
    }

    public function testBuilderOptions()
    {
        $table = new Table("foo");
        $table->addOption("foo", "bar");
        $this->assertTrue($table->hasOption("foo"));
        $this->assertEquals("bar", $table->getOption("foo"));
    }

    public function testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $table = new Table("foo");
277
        $table->addColumn("id", 'integer');
278 279

        $foreignTable = new Table("bar");
280
        $foreignTable->addColumn("id", 'integer');
281

282
        $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
283 284 285 286 287 288 289
    }

    public function testAddForeignKeyConstraint_UnknownForeignColumn_ThrowsException()
    {
        $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");

        $table = new Table("foo");
290
        $table->addColumn("id", 'integer');
291 292

        $foreignTable = new Table("bar");
293
        $foreignTable->addColumn("id", 'integer');
294

295
        $table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
296 297 298 299 300
    }

    public function testAddForeignKeyConstraint()
    {
        $table = new Table("foo");
301
        $table->addColumn("id", 'integer');
302 303

        $foreignTable = new Table("bar");
304
        $foreignTable->addColumn("id", 'integer');
305

306
        $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
307

308
        $constraints = $table->getForeignKeys();
309
        $this->assertEquals(1, count($constraints));
310
        $constraint = current($constraints);
311

312
        $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraint);
313

314 315
        $this->assertTrue($constraint->hasOption("foo"));
        $this->assertEquals("bar", $constraint->getOption("foo"));
316
    }
317 318 319 320

    public function testAddIndexWithCaseSensitiveColumnProblem()
    {
        $table = new Table("foo");
321
        $table->addColumn("id", 'integer');
322 323 324 325

        $table->addIndex(array("ID"), "my_idx");

        $this->assertTrue($table->hasIndex('my_idx'));
326 327
        $this->assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns());
        $this->assertTrue($table->getIndex('my_idx')->spansColumns(array('id')));
328
    }
329 330 331 332

    public function testAddPrimaryKey_ColumnsAreExplicitlySetToNotNull()
    {
        $table = new Table("foo");
333
        $column = $table->addColumn("id", 'integer', array('notnull' => false));
334 335 336 337

        $this->assertFalse($column->getNotnull());

        $table->setPrimaryKey(array('id'));
338

339 340
        $this->assertTrue($column->getNotnull());
    }
341 342 343 344 345 346 347 348 349 350

    /**
     * @group DDC-133
     */
    public function testAllowImplicitSchemaTableInAutogeneratedIndexNames()
    {
        $table = new Table("foo.bar");
        $table->addColumn('baz', 'integer', array());
        $table->addIndex(array('baz'));

351
        $this->assertEquals(1, count($table->getIndexes()));
352
    }
353 354 355 356 357 358 359 360 361 362 363 364 365 366

    /**
     * @group DBAL-50
     */
    public function testAddForeignKeyIndexImplicitly()
    {
        $table = new Table("foo");
        $table->addColumn("id", 'integer');

        $foreignTable = new Table("bar");
        $foreignTable->addColumn("id", 'integer');

        $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));

367 368 369 370 371 372
        $indexes = $table->getIndexes();
        $this->assertEquals(1, count($indexes));
        $index = current($indexes);

        $this->assertTrue($table->hasIndex($index->getName()));
        $this->assertEquals(array('id'), $index->getColumns());
373 374
    }

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 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 420
    /**
     * @group DBAL-1063
     */
    public function testAddForeignKeyDoesNotCreateDuplicateIndex()
    {
        $table = new Table('foo');
        $table->addColumn('bar', 'integer');
        $table->addIndex(array('bar'), 'bar_idx');

        $foreignTable = new Table('bar');
        $foreignTable->addColumn('foo', 'integer');

        $table->addForeignKeyConstraint($foreignTable, array('bar'), array('foo'));

        $this->assertCount(1, $table->getIndexes());
        $this->assertTrue($table->hasIndex('bar_idx'));
        $this->assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
    }

    /**
     * @group DBAL-1063
     */
    public function testAddForeignKeyAddsImplicitIndexIfIndexColumnsDoNotSpan()
    {
        $table = new Table('foo');
        $table->addColumn('bar', 'integer');
        $table->addColumn('baz', 'string');
        $table->addColumn('bloo', 'string');
        $table->addIndex(array('baz', 'bar'), 'composite_idx');
        $table->addIndex(array('bar', 'baz', 'bloo'), 'full_idx');

        $foreignTable = new Table('bar');
        $foreignTable->addColumn('foo', 'integer');
        $foreignTable->addColumn('baz', 'string');

        $table->addForeignKeyConstraint($foreignTable, array('bar', 'baz'), array('foo', 'baz'));

        $this->assertCount(3, $table->getIndexes());
        $this->assertTrue($table->hasIndex('composite_idx'));
        $this->assertTrue($table->hasIndex('full_idx'));
        $this->assertTrue($table->hasIndex('idx_8c73652176ff8caa78240498'));
        $this->assertSame(array('baz', 'bar'), $table->getIndex('composite_idx')->getColumns());
        $this->assertSame(array('bar', 'baz', 'bloo'), $table->getIndex('full_idx')->getColumns());
        $this->assertSame(array('bar', 'baz'), $table->getIndex('idx_8c73652176ff8caa78240498')->getColumns());
    }

421 422
    /**
     * @group DBAL-50
423
     * @group DBAL-1063
424
     */
425
    public function testOverrulingIndexDoesNotDropOverruledIndex()
426 427 428 429
    {
        $table = new Table("bar");
        $table->addColumn('baz', 'integer', array());
        $table->addIndex(array('baz'));
430 431 432 433

        $indexes = $table->getIndexes();
        $this->assertEquals(1, count($indexes));
        $index = current($indexes);
434 435

        $table->addUniqueIndex(array('baz'));
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
        $this->assertEquals(2, count($table->getIndexes()));
        $this->assertTrue($table->hasIndex($index->getName()));
    }

    /**
     * @group DBAL-1063
     */
    public function testAllowsAddingDuplicateIndexesBasedOnColumns()
    {
        $table = new Table('foo');
        $table->addColumn('bar', 'integer');
        $table->addIndex(array('bar'), 'bar_idx');
        $table->addIndex(array('bar'), 'duplicate_idx');

        $this->assertCount(2, $table->getIndexes());
        $this->assertTrue($table->hasIndex('bar_idx'));
        $this->assertTrue($table->hasIndex('duplicate_idx'));
        $this->assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
        $this->assertSame(array('bar'), $table->getIndex('duplicate_idx')->getColumns());
455
    }
456

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
    /**
     * @group DBAL-1063
     */
    public function testAllowsAddingFulfillingIndexesBasedOnColumns()
    {
        $table = new Table('foo');
        $table->addColumn('bar', 'integer');
        $table->addColumn('baz', 'string');
        $table->addIndex(array('bar'), 'bar_idx');
        $table->addIndex(array('bar', 'baz'), 'fulfilling_idx');

        $this->assertCount(2, $table->getIndexes());
        $this->assertTrue($table->hasIndex('bar_idx'));
        $this->assertTrue($table->hasIndex('fulfilling_idx'));
        $this->assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
        $this->assertSame(array('bar', 'baz'), $table->getIndex('fulfilling_idx')->getColumns());
    }

    /**
     * @group DBAL-50
     * @group DBAL-1063
     */
    public function testPrimaryKeyOverrulingUniqueIndexDoesNotDropUniqueIndex()
480 481 482
    {
        $table = new Table("bar");
        $table->addColumn('baz', 'integer', array());
483
        $table->addUniqueIndex(array('baz'), 'idx_unique');
484

485
        $table->setPrimaryKey(array('baz'));
486 487

        $indexes = $table->getIndexes();
488
        $this->assertEquals(2, count($indexes), "Table should only contain both the primary key table index and the unique one, even though it was overruled.");
489

490 491
        $this->assertTrue($table->hasPrimaryKey());
        $this->assertTrue($table->hasIndex('idx_unique'));
492
    }
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    public function testAddingFulfillingRegularIndexOverridesImplicitForeignKeyConstraintIndex()
    {
        $foreignTable = new Table('foreign');
        $foreignTable->addColumn('id', 'integer');

        $localTable = new Table('local');
        $localTable->addColumn('id', 'integer');
        $localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));

        $this->assertCount(1, $localTable->getIndexes());

        $localTable->addIndex(array('id'), 'explicit_idx');

        $this->assertCount(1, $localTable->getIndexes());
        $this->assertTrue($localTable->hasIndex('explicit_idx'));
    }

    public function testAddingFulfillingUniqueIndexOverridesImplicitForeignKeyConstraintIndex()
    {
        $foreignTable = new Table('foreign');
        $foreignTable->addColumn('id', 'integer');

        $localTable = new Table('local');
        $localTable->addColumn('id', 'integer');
        $localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));

        $this->assertCount(1, $localTable->getIndexes());

        $localTable->addUniqueIndex(array('id'), 'explicit_idx');

        $this->assertCount(1, $localTable->getIndexes());
        $this->assertTrue($localTable->hasIndex('explicit_idx'));
    }

    public function testAddingFulfillingPrimaryKeyOverridesImplicitForeignKeyConstraintIndex()
    {
        $foreignTable = new Table('foreign');
        $foreignTable->addColumn('id', 'integer');

        $localTable = new Table('local');
        $localTable->addColumn('id', 'integer');
        $localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));

        $this->assertCount(1, $localTable->getIndexes());

        $localTable->setPrimaryKey(array('id'), 'explicit_idx');

        $this->assertCount(1, $localTable->getIndexes());
        $this->assertTrue($localTable->hasIndex('explicit_idx'));
    }

    public function testAddingFulfillingExplicitIndexOverridingImplicitForeignKeyConstraintIndexWithSameNameDoesNotThrowException()
    {
        $foreignTable = new Table('foreign');
        $foreignTable->addColumn('id', 'integer');

        $localTable = new Table('local');
        $localTable->addColumn('id', 'integer');
        $localTable->addForeignKeyConstraint($foreignTable, array('id'), array('id'));

        $this->assertCount(1, $localTable->getIndexes());
        $this->assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));

        $implicitIndex = $localTable->getIndex('IDX_8BD688E8BF396750');

        $localTable->addIndex(array('id'), 'IDX_8BD688E8BF396750');

        $this->assertCount(1, $localTable->getIndexes());
        $this->assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));
        $this->assertNotSame($implicitIndex, $localTable->getIndex('IDX_8BD688E8BF396750'));
    }

566 567 568 569 570 571 572 573 574 575 576 577 578 579
    /**
     * @group DBAL-64
     */
    public function testQuotedTableName()
    {
        $table = new Table("`bar`");

        $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
        $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();

        $this->assertEquals('bar', $table->getName());
        $this->assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
        $this->assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
    }
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594

    /**
     * @group DBAL-79
     */
    public function testTableHasPrimaryKey()
    {
        $table = new Table("test");

        $this->assertFalse($table->hasPrimaryKey());

        $table->addColumn("foo", "integer");
        $table->setPrimaryKey(array("foo"));

        $this->assertTrue($table->hasPrimaryKey());
    }
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

    /**
     * @group DBAL-91
     */
    public function testAddIndexWithQuotedColumns()
    {
        $table = new Table("test");
        $table->addColumn('"foo"', 'integer');
        $table->addColumn('bar', 'integer');
        $table->addIndex(array('"foo"', '"bar"'));
    }

    /**
     * @group DBAL-91
     */
    public function testAddForeignKeyWithQuotedColumnsAndTable()
    {
        $table = new Table("test");
        $table->addColumn('"foo"', 'integer');
        $table->addColumn('bar', 'integer');
        $table->addForeignKeyConstraint('"boing"', array('"foo"', '"bar"'), array("id"));
    }
617 618 619 620 621 622 623 624 625 626

    /**
     * @group DBAL-177
     */
    public function testQuoteSchemaPrefixed()
    {
        $table = new Table("`test`.`test`");
        $this->assertEquals("test.test", $table->getName());
        $this->assertEquals("`test`.`test`", $table->getQuotedName(new \Doctrine\DBAL\Platforms\MySqlPlatform));
    }
627 628 629 630 631 632 633

    /**
     * @group DBAL-204
     */
    public function testFullQualifiedTableName()
    {
        $table = new Table("`test`.`test`");
634 635
        $this->assertEquals('test.test', $table->getFullQualifiedName("test"));
        $this->assertEquals('test.test', $table->getFullQualifiedName("other"));
636 637

        $table = new Table("test");
638 639
        $this->assertEquals('test.test', $table->getFullQualifiedName("test"));
        $this->assertEquals('other.test', $table->getFullQualifiedName("other"));
640
    }
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

    /**
     * @group DBAL-224
     */
    public function testDropIndex()
    {
        $table = new Table("test");
        $table->addColumn('id', 'integer');
        $table->addIndex(array('id'), 'idx');

        $this->assertTrue($table->hasIndex('idx'));

        $table->dropIndex('idx');
        $this->assertFalse($table->hasIndex('idx'));
    }

    /**
     * @group DBAL-224
     */
    public function testDropPrimaryKey()
    {
        $table = new Table("test");
        $table->addColumn('id', 'integer');
        $table->setPrimaryKey(array('id'));

        $this->assertTrue($table->hasPrimaryKey());

        $table->dropPrimaryKey();
        $this->assertFalse($table->hasPrimaryKey());
    }
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

    /**
     * @group DBAL-234
     */
    public function testRenameIndex()
    {
        $table = new Table("test");
        $table->addColumn('id', 'integer');
        $table->addColumn('foo', 'integer');
        $table->addColumn('bar', 'integer');
        $table->addColumn('baz', 'integer');
        $table->setPrimaryKey(array('id'), 'pk');
        $table->addIndex(array('foo'), 'idx', array('flag'));
        $table->addUniqueIndex(array('bar', 'baz'), 'uniq');

        // Rename to custom name.
        $this->assertSame($table, $table->renameIndex('pk', 'pk_new'));
        $this->assertSame($table, $table->renameIndex('idx', 'idx_new'));
        $this->assertSame($table, $table->renameIndex('uniq', 'uniq_new'));

        $this->assertTrue($table->hasPrimaryKey());
        $this->assertTrue($table->hasIndex('pk_new'));
        $this->assertTrue($table->hasIndex('idx_new'));
        $this->assertTrue($table->hasIndex('uniq_new'));

        $this->assertFalse($table->hasIndex('pk'));
        $this->assertFalse($table->hasIndex('idx'));
        $this->assertFalse($table->hasIndex('uniq'));

        $this->assertEquals(new Index('pk_new', array('id'), true, true), $table->getPrimaryKey());
        $this->assertEquals(new Index('pk_new', array('id'), true, true), $table->getIndex('pk_new'));
        $this->assertEquals(
            new Index('idx_new', array('foo'), false, false, array('flag')),
            $table->getIndex('idx_new')
        );
        $this->assertEquals(new Index('uniq_new', array('bar', 'baz'), true), $table->getIndex('uniq_new'));

        // Rename to auto-generated name.
        $this->assertSame($table, $table->renameIndex('pk_new', null));
        $this->assertSame($table, $table->renameIndex('idx_new', null));
        $this->assertSame($table, $table->renameIndex('uniq_new', null));

        $this->assertTrue($table->hasPrimaryKey());
        $this->assertTrue($table->hasIndex('primary'));
        $this->assertTrue($table->hasIndex('IDX_D87F7E0C8C736521'));
        $this->assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498'));

        $this->assertFalse($table->hasIndex('pk_new'));
        $this->assertFalse($table->hasIndex('idx_new'));
        $this->assertFalse($table->hasIndex('uniq_new'));

        $this->assertEquals(new Index('primary', array('id'), true, true), $table->getPrimaryKey());
        $this->assertEquals(new Index('primary', array('id'), true, true), $table->getIndex('primary'));
        $this->assertEquals(
            new Index('IDX_D87F7E0C8C736521', array('foo'), false, false, array('flag')),
            $table->getIndex('IDX_D87F7E0C8C736521')
        );
        $this->assertEquals(
            new Index('UNIQ_D87F7E0C76FF8CAA78240498', array('bar', 'baz'), true),
            $table->getIndex('UNIQ_D87F7E0C76FF8CAA78240498')
        );

        // Rename to same name (changed case).
        $this->assertSame($table, $table->renameIndex('primary', 'PRIMARY'));
        $this->assertSame($table, $table->renameIndex('IDX_D87F7E0C8C736521', 'idx_D87F7E0C8C736521'));
        $this->assertSame($table, $table->renameIndex('UNIQ_D87F7E0C76FF8CAA78240498', 'uniq_D87F7E0C76FF8CAA78240498'));

        $this->assertTrue($table->hasPrimaryKey());
        $this->assertTrue($table->hasIndex('primary'));
        $this->assertTrue($table->hasIndex('IDX_D87F7E0C8C736521'));
        $this->assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498'));
    }

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    /**
     * @group DBAL-2508
     */
    public function testKeepsIndexOptionsOnRenamingRegularIndex()
    {
        $table = new Table('foo');
        $table->addColumn('id', 'integer');
        $table->addIndex(array('id'), 'idx_bar', array(), array('where' => '1 = 1'));

        $table->renameIndex('idx_bar', 'idx_baz');

        $this->assertSame(array('where' => '1 = 1'), $table->getIndex('idx_baz')->getOptions());
    }

    /**
     * @group DBAL-2508
     */
    public function testKeepsIndexOptionsOnRenamingUniqueIndex()
    {
        $table = new Table('foo');
        $table->addColumn('id', 'integer');
        $table->addUniqueIndex(array('id'), 'idx_bar', array('where' => '1 = 1'));

        $table->renameIndex('idx_bar', 'idx_baz');

        $this->assertSame(array('where' => '1 = 1'), $table->getIndex('idx_baz')->getOptions());
    }

772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
    /**
     * @group DBAL-234
     * @expectedException \Doctrine\DBAL\Schema\SchemaException
     */
    public function testThrowsExceptionOnRenamingNonExistingIndex()
    {
        $table = new Table("test");
        $table->addColumn('id', 'integer');
        $table->addIndex(array('id'), 'idx');

        $table->renameIndex('foo', 'bar');
    }

    /**
     * @group DBAL-234
     * @expectedException \Doctrine\DBAL\Schema\SchemaException
     */
    public function testThrowsExceptionOnRenamingToAlreadyExistingIndex()
    {
        $table = new Table("test");
        $table->addColumn('id', 'integer');
        $table->addColumn('foo', 'integer');
        $table->addIndex(array('id'), 'idx_id');
        $table->addIndex(array('foo'), 'idx_foo');

        $table->renameIndex('idx_id', 'idx_foo');
    }
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

    /**
     * @dataProvider getNormalizesAssetNames
     * @group DBAL-831
     */
    public function testNormalizesColumnNames($assetName)
    {
        $table = new Table('test');

        $table->addColumn($assetName, 'integer');
        $table->addIndex(array($assetName), $assetName);
        $table->addForeignKeyConstraint('test', array($assetName), array($assetName), array(), $assetName);

        $this->assertTrue($table->hasColumn($assetName));
        $this->assertTrue($table->hasColumn('foo'));
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn($assetName));
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn('foo'));

        $this->assertTrue($table->hasIndex($assetName));
        $this->assertTrue($table->hasIndex('foo'));
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex($assetName));
        $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('foo'));

        $this->assertTrue($table->hasForeignKey($assetName));
        $this->assertTrue($table->hasForeignKey('foo'));
        $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $table->getForeignKey($assetName));
        $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $table->getForeignKey('foo'));

        $table->renameIndex($assetName, $assetName);
        $this->assertTrue($table->hasIndex($assetName));
        $this->assertTrue($table->hasIndex('foo'));

        $table->renameIndex($assetName, 'foo');
        $this->assertTrue($table->hasIndex($assetName));
        $this->assertTrue($table->hasIndex('foo'));

        $table->renameIndex('foo', $assetName);
        $this->assertTrue($table->hasIndex($assetName));
        $this->assertTrue($table->hasIndex('foo'));

        $table->renameIndex($assetName, 'bar');
        $this->assertFalse($table->hasIndex($assetName));
        $this->assertFalse($table->hasIndex('foo'));
        $this->assertTrue($table->hasIndex('bar'));

        $table->renameIndex('bar', $assetName);

        $table->dropColumn($assetName);
        $table->dropIndex($assetName);
        $table->removeForeignKey($assetName);

        $this->assertFalse($table->hasColumn($assetName));
        $this->assertFalse($table->hasColumn('foo'));
        $this->assertFalse($table->hasIndex($assetName));
        $this->assertFalse($table->hasIndex('foo'));
        $this->assertFalse($table->hasForeignKey($assetName));
        $this->assertFalse($table->hasForeignKey('foo'));
    }

    public function getNormalizesAssetNames()
    {
        return array(
            array('foo'),
            array('FOO'),
            array('`foo`'),
            array('`FOO`'),
            array('"foo"'),
            array('"FOO"'),
            array('"foo"'),
            array('"FOO"'),
        );
    }
871
}