TableTest.php 30 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Schema;

Luís Cobucci's avatar
Luís Cobucci committed
5
use Doctrine\DBAL\DBALException;
6 7
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
jeroendedauw's avatar
jeroendedauw committed
8 9
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
10
use Doctrine\DBAL\Types\Type;
11 12
use function array_shift;
use function current;
13

14
class TableTest extends \Doctrine\Tests\DbalTestCase
15
{
16 17
    public function testCreateWithInvalidTableName()
    {
Luís Cobucci's avatar
Luís Cobucci committed
18 19 20
        $this->expectException(DBALException::class);

        new \Doctrine\DBAL\Schema\Table('');
21 22
    }

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

    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());

37 38 39
        self::assertTrue($table->hasColumn("foo"));
        self::assertTrue($table->hasColumn("bar"));
        self::assertFalse($table->hasColumn("baz"));
40

41 42
        self::assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
        self::assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));
43

Gabriel Caruso's avatar
Gabriel Caruso committed
44
        self::assertCount(2, $table->getColumns());
45 46
    }

47 48 49
    public function testColumnsCaseInsensitive()
    {
        $table = new Table("foo");
50
        $column = $table->addColumn('Foo', 'integer');
51

52 53 54
        self::assertTrue($table->hasColumn('Foo'));
        self::assertTrue($table->hasColumn('foo'));
        self::assertTrue($table->hasColumn('FOO'));
55

56 57 58
        self::assertSame($column, $table->getColumn('Foo'));
        self::assertSame($column, $table->getColumn('foo'));
        self::assertSame($column, $table->getColumn('FOO'));
59 60
    }

61 62 63 64 65 66
    public function testCreateColumn()
    {
        $type = Type::getType('integer');

        $table = new Table("foo");

67
        self::assertFalse($table->hasColumn("bar"));
68
        $table->addColumn("bar", 'integer');
69 70
        self::assertTrue($table->hasColumn("bar"));
        self::assertSame($type, $table->getColumn("bar")->getType());
71 72 73 74 75 76 77 78 79 80
    }

    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());

81 82
        self::assertTrue($table->hasColumn("foo"));
        self::assertTrue($table->hasColumn("bar"));
83 84 85

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

86 87
        self::assertFalse($table->hasColumn("foo"));
        self::assertFalse($table->hasColumn("bar"));
88 89 90 91
    }

    public function testGetUnknownColumnThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
92
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
93 94 95 96 97 98 99

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

    public function testAddColumnTwiceThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
100
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
101 102 103 104 105 106 107 108

        $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());
    }

109 110 111 112 113
    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);
114

115 116
        $table->addIndex(array("foo", "bar"), "foo_foo_bar_idx");
        $table->addUniqueIndex(array("bar", "baz"), "foo_bar_baz_uniq");
117

118 119
        self::assertTrue($table->hasIndex("foo_foo_bar_idx"));
        self::assertTrue($table->hasIndex("foo_bar_baz_uniq"));
120 121
    }

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

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

134 135 136
        self::assertTrue($table->hasIndex('foo_idx'));
        self::assertTrue($table->hasIndex('Foo_Idx'));
        self::assertTrue($table->hasIndex('FOO_IDX'));
137 138
    }

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

152 153 154
        self::assertTrue($table->hasIndex("the_primary"));
        self::assertTrue($table->hasIndex("bar_idx"));
        self::assertFalse($table->hasIndex("some_idx"));
155

156 157 158
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('bar_idx'));
159 160 161 162
    }

    public function testGetUnknownIndexThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
163
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
164 165 166 167 168 169 170

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

    public function testAddTwoPrimaryThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
171
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
172 173

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

    public function testAddTwoIndexesWithSameNameThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
184
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
185 186

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

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

        $tableA = new Table("foo", array(), array(), array($constraint));
200
        $constraints = $tableA->getForeignKeys();
201

Gabriel Caruso's avatar
Gabriel Caruso committed
202
        self::assertCount(1, $constraints);
203
        self::assertSame($constraint, array_shift($constraints));
204 205 206 207
    }

    public function testOptions()
    {
208
        $table = new Table("foo", array(), array(), array(), false, array("foo" => "bar"));
209

210 211
        self::assertTrue($table->hasOption("foo"));
        self::assertEquals("bar", $table->getOption("foo"));
212 213 214 215 216 217
    }

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

218
        $table->addColumn("bar", 'integer');
219 220
        $table->setPrimaryKey(array("bar"));

221 222 223 224
        self::assertTrue($table->hasIndex("primary"));
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
        self::assertTrue($table->getIndex("primary")->isUnique());
        self::assertTrue($table->getIndex("primary")->isPrimary());
225 226 227 228 229 230
    }

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

231
        $table->addColumn("bar", 'integer');
232 233
        $table->addUniqueIndex(array("bar"), "my_idx");

234 235 236
        self::assertTrue($table->hasIndex("my_idx"));
        self::assertTrue($table->getIndex("my_idx")->isUnique());
        self::assertFalse($table->getIndex("my_idx")->isPrimary());
237 238 239 240 241 242
    }

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

243
        $table->addColumn("bar", 'integer');
244 245
        $table->addIndex(array("bar"), "my_idx");

246 247 248
        self::assertTrue($table->hasIndex("my_idx"));
        self::assertFalse($table->getIndex("my_idx")->isUnique());
        self::assertFalse($table->getIndex("my_idx")->isPrimary());
249 250 251 252
    }

    public function testBuilderAddIndexWithInvalidNameThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
253
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
254 255

        $table = new Table("foo");
256
        $table->addColumn("bar",'integer');
257 258 259 260 261
        $table->addIndex(array("bar"), "invalid name %&/");
    }

    public function testBuilderAddIndexWithUnknownColumnThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
262
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
263 264 265 266 267 268 269 270 271

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

    public function testBuilderOptions()
    {
        $table = new Table("foo");
        $table->addOption("foo", "bar");
272 273
        self::assertTrue($table->hasOption("foo"));
        self::assertEquals("bar", $table->getOption("foo"));
274 275 276 277
    }

    public function testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
278
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
279 280

        $table = new Table("foo");
281
        $table->addColumn("id", 'integer');
282 283

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

286
        $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
287 288 289 290
    }

    public function testAddForeignKeyConstraint_UnknownForeignColumn_ThrowsException()
    {
Luís Cobucci's avatar
Luís Cobucci committed
291
        $this->expectException("Doctrine\DBAL\Schema\SchemaException");
292 293

        $table = new Table("foo");
294
        $table->addColumn("id", 'integer');
295 296

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

299
        $table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
300 301 302 303 304
    }

    public function testAddForeignKeyConstraint()
    {
        $table = new Table("foo");
305
        $table->addColumn("id", 'integer');
306 307

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

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

312
        $constraints = $table->getForeignKeys();
Gabriel Caruso's avatar
Gabriel Caruso committed
313
        self::assertCount(1, $constraints);
314
        $constraint = current($constraints);
315

316
        self::assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraint);
317

318 319
        self::assertTrue($constraint->hasOption("foo"));
        self::assertEquals("bar", $constraint->getOption("foo"));
320
    }
321 322 323 324

    public function testAddIndexWithCaseSensitiveColumnProblem()
    {
        $table = new Table("foo");
325
        $table->addColumn("id", 'integer');
326 327 328

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

329 330 331
        self::assertTrue($table->hasIndex('my_idx'));
        self::assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns());
        self::assertTrue($table->getIndex('my_idx')->spansColumns(array('id')));
332
    }
333 334 335 336

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

339
        self::assertFalse($column->getNotnull());
340 341

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

343
        self::assertTrue($column->getNotnull());
344
    }
345 346 347 348 349 350 351 352 353 354

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

Gabriel Caruso's avatar
Gabriel Caruso committed
355
        self::assertCount(1, $table->getIndexes());
356
    }
357 358 359 360 361 362 363 364 365 366 367 368 369 370

    /**
     * @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"));

371
        $indexes = $table->getIndexes();
Gabriel Caruso's avatar
Gabriel Caruso committed
372
        self::assertCount(1, $indexes);
373 374
        $index = current($indexes);

375 376
        self::assertTrue($table->hasIndex($index->getName()));
        self::assertEquals(array('id'), $index->getColumns());
377 378
    }

379 380 381 382 383 384 385 386 387 388 389 390 391 392
    /**
     * @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'));

393 394 395
        self::assertCount(1, $table->getIndexes());
        self::assertTrue($table->hasIndex('bar_idx'));
        self::assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    }

    /**
     * @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'));

416 417 418 419 420 421 422
        self::assertCount(3, $table->getIndexes());
        self::assertTrue($table->hasIndex('composite_idx'));
        self::assertTrue($table->hasIndex('full_idx'));
        self::assertTrue($table->hasIndex('idx_8c73652176ff8caa78240498'));
        self::assertSame(array('baz', 'bar'), $table->getIndex('composite_idx')->getColumns());
        self::assertSame(array('bar', 'baz', 'bloo'), $table->getIndex('full_idx')->getColumns());
        self::assertSame(array('bar', 'baz'), $table->getIndex('idx_8c73652176ff8caa78240498')->getColumns());
423 424
    }

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

        $indexes = $table->getIndexes();
Gabriel Caruso's avatar
Gabriel Caruso committed
436
        self::assertCount(1, $indexes);
437
        $index = current($indexes);
438 439

        $table->addUniqueIndex(array('baz'));
Gabriel Caruso's avatar
Gabriel Caruso committed
440
        self::assertCount(2, $table->getIndexes());
441
        self::assertTrue($table->hasIndex($index->getName()));
442 443 444 445 446 447 448 449 450 451 452 453
    }

    /**
     * @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');

454 455 456 457 458
        self::assertCount(2, $table->getIndexes());
        self::assertTrue($table->hasIndex('bar_idx'));
        self::assertTrue($table->hasIndex('duplicate_idx'));
        self::assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
        self::assertSame(array('bar'), $table->getIndex('duplicate_idx')->getColumns());
459
    }
460

461 462 463 464 465 466 467 468 469 470 471
    /**
     * @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');

472 473 474 475 476
        self::assertCount(2, $table->getIndexes());
        self::assertTrue($table->hasIndex('bar_idx'));
        self::assertTrue($table->hasIndex('fulfilling_idx'));
        self::assertSame(array('bar'), $table->getIndex('bar_idx')->getColumns());
        self::assertSame(array('bar', 'baz'), $table->getIndex('fulfilling_idx')->getColumns());
477 478 479 480 481 482 483
    }

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

489
        $table->setPrimaryKey(array('baz'));
490 491

        $indexes = $table->getIndexes();
Gabriel Caruso's avatar
Gabriel Caruso committed
492
        self::assertCount(2, $indexes, "Table should only contain both the primary key table index and the unique one, even though it was overruled.");
493

494 495
        self::assertTrue($table->hasPrimaryKey());
        self::assertTrue($table->hasIndex('idx_unique'));
496
    }
497

498 499 500 501 502 503 504 505 506
    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'));

507
        self::assertCount(1, $localTable->getIndexes());
508 509 510

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

511 512
        self::assertCount(1, $localTable->getIndexes());
        self::assertTrue($localTable->hasIndex('explicit_idx'));
513 514 515 516 517 518 519 520 521 522 523
    }

    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'));

524
        self::assertCount(1, $localTable->getIndexes());
525 526 527

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

528 529
        self::assertCount(1, $localTable->getIndexes());
        self::assertTrue($localTable->hasIndex('explicit_idx'));
530 531 532 533 534 535 536 537 538 539 540
    }

    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'));

541
        self::assertCount(1, $localTable->getIndexes());
542 543 544

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

545 546
        self::assertCount(1, $localTable->getIndexes());
        self::assertTrue($localTable->hasIndex('explicit_idx'));
547 548 549 550 551 552 553 554 555 556 557
    }

    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'));

558 559
        self::assertCount(1, $localTable->getIndexes());
        self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));
560 561 562 563 564

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

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

565 566 567
        self::assertCount(1, $localTable->getIndexes());
        self::assertTrue($localTable->hasIndex('IDX_8BD688E8BF396750'));
        self::assertNotSame($implicitIndex, $localTable->getIndex('IDX_8BD688E8BF396750'));
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();

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

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

592
        self::assertFalse($table->hasPrimaryKey());
593 594 595 596

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

597
        self::assertTrue($table->hasPrimaryKey());
598
    }
599 600 601 602 603 604 605 606 607

    /**
     * @group DBAL-91
     */
    public function testAddIndexWithQuotedColumns()
    {
        $table = new Table("test");
        $table->addColumn('"foo"', 'integer');
        $table->addColumn('bar', 'integer');
Luís Cobucci's avatar
Luís Cobucci committed
608 609 610
        $table->addIndex(['"foo"', '"bar"']);

        self::assertTrue($table->columnsAreIndexed(['"foo"', '"bar"']));
611 612 613 614 615 616 617 618 619 620
    }

    /**
     * @group DBAL-91
     */
    public function testAddForeignKeyWithQuotedColumnsAndTable()
    {
        $table = new Table("test");
        $table->addColumn('"foo"', 'integer');
        $table->addColumn('bar', 'integer');
Luís Cobucci's avatar
Luís Cobucci committed
621 622 623
        $table->addForeignKeyConstraint('"boing"', ['"foo"', '"bar"'], ["id"]);

        self::assertCount(1, $table->getForeignKeys());
624
    }
625 626 627 628 629 630 631

    /**
     * @group DBAL-177
     */
    public function testQuoteSchemaPrefixed()
    {
        $table = new Table("`test`.`test`");
632 633
        self::assertEquals("test.test", $table->getName());
        self::assertEquals("`test`.`test`", $table->getQuotedName(new \Doctrine\DBAL\Platforms\MySqlPlatform));
634
    }
635 636 637 638 639 640 641

    /**
     * @group DBAL-204
     */
    public function testFullQualifiedTableName()
    {
        $table = new Table("`test`.`test`");
642 643
        self::assertEquals('test.test', $table->getFullQualifiedName("test"));
        self::assertEquals('test.test', $table->getFullQualifiedName("other"));
644 645

        $table = new Table("test");
646 647
        self::assertEquals('test.test', $table->getFullQualifiedName("test"));
        self::assertEquals('other.test', $table->getFullQualifiedName("other"));
648
    }
649 650 651 652 653 654 655 656 657 658

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

659
        self::assertTrue($table->hasIndex('idx'));
660 661

        $table->dropIndex('idx');
662
        self::assertFalse($table->hasIndex('idx'));
663 664 665 666 667 668 669 670 671 672 673
    }

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

674
        self::assertTrue($table->hasPrimaryKey());
675 676

        $table->dropPrimaryKey();
677
        self::assertFalse($table->hasPrimaryKey());
678
    }
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

    /**
     * @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.
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
        self::assertSame($table, $table->renameIndex('pk', 'pk_new'));
        self::assertSame($table, $table->renameIndex('idx', 'idx_new'));
        self::assertSame($table, $table->renameIndex('uniq', 'uniq_new'));

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

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

        self::assertEquals(new Index('pk_new', array('id'), true, true), $table->getPrimaryKey());
        self::assertEquals(new Index('pk_new', array('id'), true, true), $table->getIndex('pk_new'));
        self::assertEquals(
711 712 713
            new Index('idx_new', array('foo'), false, false, array('flag')),
            $table->getIndex('idx_new')
        );
714
        self::assertEquals(new Index('uniq_new', array('bar', 'baz'), true), $table->getIndex('uniq_new'));
715 716

        // Rename to auto-generated name.
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
        self::assertSame($table, $table->renameIndex('pk_new', null));
        self::assertSame($table, $table->renameIndex('idx_new', null));
        self::assertSame($table, $table->renameIndex('uniq_new', null));

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

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

        self::assertEquals(new Index('primary', array('id'), true, true), $table->getPrimaryKey());
        self::assertEquals(new Index('primary', array('id'), true, true), $table->getIndex('primary'));
        self::assertEquals(
733 734 735
            new Index('IDX_D87F7E0C8C736521', array('foo'), false, false, array('flag')),
            $table->getIndex('IDX_D87F7E0C8C736521')
        );
736
        self::assertEquals(
737 738 739 740 741
            new Index('UNIQ_D87F7E0C76FF8CAA78240498', array('bar', 'baz'), true),
            $table->getIndex('UNIQ_D87F7E0C76FF8CAA78240498')
        );

        // Rename to same name (changed case).
742 743 744
        self::assertSame($table, $table->renameIndex('primary', 'PRIMARY'));
        self::assertSame($table, $table->renameIndex('IDX_D87F7E0C8C736521', 'idx_D87F7E0C8C736521'));
        self::assertSame($table, $table->renameIndex('UNIQ_D87F7E0C76FF8CAA78240498', 'uniq_D87F7E0C76FF8CAA78240498'));
745

746 747 748 749
        self::assertTrue($table->hasPrimaryKey());
        self::assertTrue($table->hasIndex('primary'));
        self::assertTrue($table->hasIndex('IDX_D87F7E0C8C736521'));
        self::assertTrue($table->hasIndex('UNIQ_D87F7E0C76FF8CAA78240498'));
750 751
    }

752 753 754 755 756 757 758 759 760 761 762
    /**
     * @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');

763
        self::assertSame(array('where' => '1 = 1'), $table->getIndex('idx_baz')->getOptions());
764 765 766 767 768 769 770 771 772 773 774 775 776
    }

    /**
     * @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');

777
        self::assertSame(array('where' => '1 = 1'), $table->getIndex('idx_baz')->getOptions());
778 779
    }

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
    /**
     * @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');
    }
807 808 809 810 811 812 813 814 815 816 817 818 819

    /**
     * @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);

820 821 822 823
        self::assertTrue($table->hasColumn($assetName));
        self::assertTrue($table->hasColumn('foo'));
        self::assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn($assetName));
        self::assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn('foo'));
824

825 826 827 828
        self::assertTrue($table->hasIndex($assetName));
        self::assertTrue($table->hasIndex('foo'));
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex($assetName));
        self::assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('foo'));
829

830 831 832 833
        self::assertTrue($table->hasForeignKey($assetName));
        self::assertTrue($table->hasForeignKey('foo'));
        self::assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $table->getForeignKey($assetName));
        self::assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $table->getForeignKey('foo'));
834 835

        $table->renameIndex($assetName, $assetName);
836 837
        self::assertTrue($table->hasIndex($assetName));
        self::assertTrue($table->hasIndex('foo'));
838 839

        $table->renameIndex($assetName, 'foo');
840 841
        self::assertTrue($table->hasIndex($assetName));
        self::assertTrue($table->hasIndex('foo'));
842 843

        $table->renameIndex('foo', $assetName);
844 845
        self::assertTrue($table->hasIndex($assetName));
        self::assertTrue($table->hasIndex('foo'));
846 847

        $table->renameIndex($assetName, 'bar');
848 849 850
        self::assertFalse($table->hasIndex($assetName));
        self::assertFalse($table->hasIndex('foo'));
        self::assertTrue($table->hasIndex('bar'));
851 852 853 854 855 856 857

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

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

858 859 860 861 862 863
        self::assertFalse($table->hasColumn($assetName));
        self::assertFalse($table->hasColumn('foo'));
        self::assertFalse($table->hasIndex($assetName));
        self::assertFalse($table->hasIndex('foo'));
        self::assertFalse($table->hasForeignKey($assetName));
        self::assertFalse($table->hasForeignKey('foo'));
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
    }

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