SchemaManagerFunctionalTestCase.php 28 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\Tests\DBAL\Functional\Schema;

5 6
use Doctrine\DBAL\Types\Type,
    Doctrine\DBAL\Schema\AbstractSchemaManager;
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8 9
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Events;
10

romanb's avatar
romanb committed
11 12 13
require_once __DIR__ . '/../../../TestInit.php';

class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase
14
{
15 16 17 18
    /**
     * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
     */
    protected $_sm;
19

20 21 22 23 24 25
	protected function getPlatformName()
	{
	    $class = get_class($this);
        $e = explode('\\', $class);
        $testClass = end($e);
        $dbms = strtolower(str_replace('SchemaManagerTest', null, $testClass));
26
        return $dbms;
27
	}
28 29 30 31

    protected function setUp()
    {
        parent::setUp();
32 33

        $dbms = $this->getPlatformName();
34

35
        if ($this->_conn->getDatabasePlatform()->getName() !== $dbms) {
36
            $this->markTestSkipped(get_class($this) . ' requires the use of ' . $dbms);
37 38 39 40 41
        }

        $this->_sm = $this->_conn->getSchemaManager();
    }

Benjamin Eberlei's avatar
Benjamin Eberlei committed
42 43 44 45 46 47 48 49 50 51 52 53 54
    /**
     * @group DBAL-195
     */
    public function testDropAndCreateSequence()
    {
        if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
            $this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
        }

        $sequence = new \Doctrine\DBAL\Schema\Sequence('dropcreate_sequences_test_seq', 20, 10);
        $this->_sm->dropAndCreateSequence($sequence);
    }

55 56 57 58 59 60
    public function testListSequences()
    {
        if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
            $this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
        }

61 62
        $sequence = new \Doctrine\DBAL\Schema\Sequence('list_sequences_test_seq', 20, 10);
        $this->_sm->createSequence($sequence);
63

64
        $sequences = $this->_sm->listSequences();
65

66
        $this->assertInternalType('array', $sequences, 'listSequences() should return an array.');
67 68 69

        $foundSequence = null;
        foreach($sequences AS $sequence) {
70
            $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $sequence, 'Array elements of listSequences() should be Sequence instances.');
71 72 73 74 75 76 77 78 79 80
            if(strtolower($sequence->getName()) == 'list_sequences_test_seq') {
                $foundSequence = $sequence;
            }
        }

        $this->assertNotNull($foundSequence, "Sequence with name 'list_sequences_test_seq' was not found.");
        $this->assertEquals(20, $foundSequence->getAllocationSize(), "Allocation Size is expected to be 20.");
        $this->assertEquals(10, $foundSequence->getInitialValue(), "Initial Value is expected to be 10.");
    }

81 82
    public function testListDatabases()
    {
83 84 85 86
        if (!$this->_sm->getDatabasePlatform()->supportsCreateDropDatabase()) {
            $this->markTestSkipped('Cannot drop Database client side with this Driver.');
        }

87 88 89 90
        $this->_sm->dropAndCreateDatabase('test_create_database');
        $databases = $this->_sm->listDatabases();

        $databases = \array_map('strtolower', $databases);
91

92
        $this->assertEquals(true, \in_array('test_create_database', $databases));
93 94 95 96 97 98 99
    }

    public function testListTables()
    {
        $this->createTestTable('list_tables_test');
        $tables = $this->_sm->listTables();

100
        $this->assertInternalType('array', $tables);
101
        $this->assertTrue(count($tables) > 0, "List Tables has to find at least one table named 'list_tables_test'.");
102 103 104

        $foundTable = false;
        foreach ($tables AS $table) {
105
            $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table);
106
            if (strtolower($table->getName()) == 'list_tables_test') {
107 108 109 110 111 112 113
                $foundTable = true;

                $this->assertTrue($table->hasColumn('id'));
                $this->assertTrue($table->hasColumn('test'));
                $this->assertTrue($table->hasColumn('foreign_key_test'));
            }
        }
114 115

        $this->assertTrue( $foundTable , "The 'list_tables_test' table has to be found.");
116 117
    }

118
    public function createListTableColumns()
119
    {
120
        $table = new \Doctrine\DBAL\Schema\Table('list_table_columns');
121
        $table->addColumn('id', 'integer', array('notnull' => true));
122 123
        $table->addColumn('test', 'string', array('length' => 255, 'notnull' => false, 'default' => 'expected default'));
        $table->addColumn('foo', 'text', array('notnull' => true));
124 125 126 127
        $table->addColumn('bar', 'decimal', array('precision' => 10, 'scale' => 4, 'notnull' => false));
        $table->addColumn('baz1', 'datetime');
        $table->addColumn('baz2', 'time');
        $table->addColumn('baz3', 'date');
128

129 130 131 132 133 134 135
        return $table;
    }

    public function testListTableColumns()
    {
        $table = $this->createListTableColumns();

136
        $this->_sm->dropAndCreateTable($table);
137 138 139 140

        $columns = $this->_sm->listTableColumns('list_table_columns');

        $this->assertArrayHasKey('id', $columns);
141
        $this->assertEquals('id',   strtolower($columns['id']->getname()));
142
        $this->assertInstanceOf('Doctrine\DBAL\Types\IntegerType', $columns['id']->gettype());
143 144 145
        $this->assertEquals(false,  $columns['id']->getunsigned());
        $this->assertEquals(true,   $columns['id']->getnotnull());
        $this->assertEquals(null,   $columns['id']->getdefault());
146
        $this->assertInternalType('array',  $columns['id']->getPlatformOptions());
147 148

        $this->assertArrayHasKey('test', $columns);
149
        $this->assertEquals('test', strtolower($columns['test']->getname()));
150
        $this->assertInstanceOf('Doctrine\DBAL\Types\StringType', $columns['test']->gettype());
151 152 153
        $this->assertEquals(255,    $columns['test']->getlength());
        $this->assertEquals(false,  $columns['test']->getfixed());
        $this->assertEquals(false,  $columns['test']->getnotnull());
154
        $this->assertEquals('expected default',   $columns['test']->getdefault());
155
        $this->assertInternalType('array',  $columns['test']->getPlatformOptions());
156

157
        $this->assertEquals('foo',  strtolower($columns['foo']->getname()));
158
        $this->assertInstanceOf('Doctrine\DBAL\Types\TextType', $columns['foo']->gettype());
159 160 161
        $this->assertEquals(false,  $columns['foo']->getunsigned());
        $this->assertEquals(false,  $columns['foo']->getfixed());
        $this->assertEquals(true,   $columns['foo']->getnotnull());
162
        $this->assertEquals(null,   $columns['foo']->getdefault());
163
        $this->assertInternalType('array',  $columns['foo']->getPlatformOptions());
164

165
        $this->assertEquals('bar',  strtolower($columns['bar']->getname()));
166
        $this->assertInstanceOf('Doctrine\DBAL\Types\DecimalType', $columns['bar']->gettype());
167
        $this->assertEquals(null,   $columns['bar']->getlength());
168 169
        $this->assertEquals(10,     $columns['bar']->getprecision());
        $this->assertEquals(4,      $columns['bar']->getscale());
170 171
        $this->assertEquals(false,  $columns['bar']->getunsigned());
        $this->assertEquals(false,  $columns['bar']->getfixed());
172
        $this->assertEquals(false,  $columns['bar']->getnotnull());
173
        $this->assertEquals(null,   $columns['bar']->getdefault());
174
        $this->assertInternalType('array',  $columns['bar']->getPlatformOptions());
175

176
        $this->assertEquals('baz1', strtolower($columns['baz1']->getname()));
177
        $this->assertInstanceOf('Doctrine\DBAL\Types\DateTimeType', $columns['baz1']->gettype());
178
        $this->assertEquals(true,   $columns['baz1']->getnotnull());
179
        $this->assertEquals(null,   $columns['baz1']->getdefault());
180
        $this->assertInternalType('array',  $columns['baz1']->getPlatformOptions());
181

182
        $this->assertEquals('baz2', strtolower($columns['baz2']->getname()));
183
        $this->assertContains($columns['baz2']->gettype()->getName(), array('time', 'date', 'datetime'));
184
        $this->assertEquals(true,   $columns['baz2']->getnotnull());
185
        $this->assertEquals(null,   $columns['baz2']->getdefault());
186
        $this->assertInternalType('array',  $columns['baz2']->getPlatformOptions());
187

188
        $this->assertEquals('baz3', strtolower($columns['baz3']->getname()));
189
        $this->assertContains($columns['baz2']->gettype()->getName(), array('time', 'date', 'datetime'));
190
        $this->assertEquals(true,   $columns['baz3']->getnotnull());
191
        $this->assertEquals(null,   $columns['baz3']->getdefault());
192
        $this->assertInternalType('array',  $columns['baz3']->getPlatformOptions());
193 194
    }

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    public function testListTableColumnsDispatchEvent()
    {
        $table = $this->createListTableColumns();

        $this->_sm->dropAndCreateTable($table);

        $listenerMock = $this->getMock('ListTableColumnsDispatchEventListener', array('onSchemaColumnDefinition'));
        $listenerMock
            ->expects($this->exactly(7))
            ->method('onSchemaColumnDefinition');

        $oldEventManager = $this->_sm->getDatabasePlatform()->getEventManager();

        $eventManager = new EventManager();
        $eventManager->addEventListener(array(Events::onSchemaColumnDefinition), $listenerMock);

        $this->_sm->getDatabasePlatform()->setEventManager($eventManager);

        $this->_sm->listTableColumns('list_table_columns');

        $this->_sm->getDatabasePlatform()->setEventManager($oldEventManager);
    }

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    public function testListTableIndexesDispatchEvent()
    {
        $table = $this->getTestTable('list_table_indexes_test');
        $table->addUniqueIndex(array('test'), 'test_index_name');
        $table->addIndex(array('id', 'test'), 'test_composite_idx');

        $this->_sm->dropAndCreateTable($table);

        $listenerMock = $this->getMock('ListTableIndexesDispatchEventListener', array('onSchemaIndexDefinition'));
        $listenerMock
            ->expects($this->exactly(3))
            ->method('onSchemaIndexDefinition');

        $oldEventManager = $this->_sm->getDatabasePlatform()->getEventManager();

        $eventManager = new EventManager();
        $eventManager->addEventListener(array(Events::onSchemaIndexDefinition), $listenerMock);

        $this->_sm->getDatabasePlatform()->setEventManager($eventManager);

        $this->_sm->listTableIndexes('list_table_indexes_test');

        $this->_sm->getDatabasePlatform()->setEventManager($oldEventManager);
    }

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    public function testDiffListTableColumns()
    {
        if ($this->_sm->getDatabasePlatform()->getName() == 'oracle') {
            $this->markTestSkipped('Does not work with Oracle, since it cannot detect DateTime, Date and Time differenecs (at the moment).');
        }

        $offlineTable = $this->createListTableColumns();
        $onlineTable = $this->_sm->listTableDetails('list_table_columns');

        $comparator = new \Doctrine\DBAL\Schema\Comparator();
        $diff = $comparator->diffTable($offlineTable, $onlineTable);

        $this->assertFalse($diff, "No differences should be detected with the offline vs online schema.");
    }

258 259
    public function testListTableIndexes()
    {
260
        $table = $this->getTestCompositeTable('list_table_indexes_test');
261 262
        $table->addUniqueIndex(array('test'), 'test_index_name');
        $table->addIndex(array('id', 'test'), 'test_composite_idx');
263

264
        $this->_sm->dropAndCreateTable($table);
265 266

        $tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
267

268 269
        $this->assertEquals(3, count($tableIndexes));

270
        $this->assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
271
        $this->assertEquals(array('id', 'other_id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
272 273
        $this->assertTrue($tableIndexes['primary']->isUnique());
        $this->assertTrue($tableIndexes['primary']->isPrimary());
274

275 276 277 278
        $this->assertEquals('test_index_name', $tableIndexes['test_index_name']->getName());
        $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test_index_name']->getColumns()));
        $this->assertTrue($tableIndexes['test_index_name']->isUnique());
        $this->assertFalse($tableIndexes['test_index_name']->isPrimary());
279

280 281 282 283
        $this->assertEquals('test_composite_idx', $tableIndexes['test_composite_idx']->getName());
        $this->assertEquals(array('id', 'test'), array_map('strtolower', $tableIndexes['test_composite_idx']->getColumns()));
        $this->assertFalse($tableIndexes['test_composite_idx']->isUnique());
        $this->assertFalse($tableIndexes['test_composite_idx']->isPrimary());
284 285 286 287
    }

    public function testDropAndCreateIndex()
    {
288 289 290
        $table = $this->getTestTable('test_create_index');
        $table->addUniqueIndex(array('test'), 'test');
        $this->_sm->dropAndCreateTable($table);
291

292
        $this->_sm->dropAndCreateIndex($table->getIndex('test'), $table);
293
        $tableIndexes = $this->_sm->listTableIndexes('test_create_index');
294
        $this->assertInternalType('array', $tableIndexes);
295

296
        $this->assertEquals('test',        strtolower($tableIndexes['test']->getName()));
297 298 299 300 301
        $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test']->getColumns()));
        $this->assertTrue($tableIndexes['test']->isUnique());
        $this->assertFalse($tableIndexes['test']->isPrimary());
    }

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    public function testCreateTableWithForeignKeys()
    {
        if(!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
            $this->markTestSkipped('Platform does not support foreign keys.');
        }

        $tableB = $this->getTestTable('test_foreign');

        $this->_sm->dropAndCreateTable($tableB);

        $tableA = $this->getTestTable('test_create_fk');
        $tableA->addForeignKeyConstraint('test_foreign', array('foreign_key_test'), array('id'));

        $this->_sm->dropAndCreateTable($tableA);

317 318
        $fkTable = $this->_sm->listTableDetails('test_create_fk');
        $fkConstraints = $fkTable->getForeignKeys();
319
        $this->assertEquals(1, count($fkConstraints), "Table 'test_create_fk1' has to have one foreign key.");
320 321

        $fkConstraint = current($fkConstraints);
322
        $this->assertInstanceOf('\Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkConstraint);
323 324 325
        $this->assertEquals('test_foreign',             strtolower($fkConstraint->getForeignTableName()));
        $this->assertEquals(array('foreign_key_test'),  array_map('strtolower', $fkConstraint->getColumns()));
        $this->assertEquals(array('id'),                array_map('strtolower', $fkConstraint->getForeignColumns()));
326 327

        $this->assertTrue($fkTable->columnsAreIndexed($fkConstraint->getColumns()), "The columns of a foreign key constraint should always be indexed.");
328 329
    }

330 331 332 333 334 335 336 337 338
    public function testListForeignKeys()
    {
        if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
            $this->markTestSkipped('Does not support foreign key constraints.');
        }

        $this->createTestTable('test_create_fk1');
        $this->createTestTable('test_create_fk2');

339
        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
340
            array('foreign_key_test'), 'test_create_fk2', array('id'), 'foreign_key_test_fk', array('onDelete' => 'CASCADE')
341 342
        );

343
        $this->_sm->createForeignKey($foreignKey, 'test_create_fk1');
344 345 346

        $fkeys = $this->_sm->listTableForeignKeys('test_create_fk1');

347
        $this->assertEquals(1, count($fkeys), "Table 'test_create_fk1' has to have one foreign key.");
348

349
        $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
350 351 352
        $this->assertEquals(array('foreign_key_test'),  array_map('strtolower', $fkeys[0]->getLocalColumns()));
        $this->assertEquals(array('id'),                array_map('strtolower', $fkeys[0]->getForeignColumns()));
        $this->assertEquals('test_create_fk2',          strtolower($fkeys[0]->getForeignTableName()));
353

354 355 356
        if($fkeys[0]->hasOption('onDelete')) {
            $this->assertEquals('CASCADE', $fkeys[0]->getOption('onDelete'));
        }
357 358
    }

359 360 361 362 363
    protected function getCreateExampleViewSql()
    {
        $this->markTestSkipped('No Create Example View SQL was defined for this SchemaManager');
    }

364 365 366 367 368 369 370 371
    public function testCreateSchema()
    {
        $this->createTestTable('test_table');

        $schema = $this->_sm->createSchema();
        $this->assertTrue($schema->hasTable('test_table'));
    }

372 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 421 422 423 424 425 426 427 428 429 430 431
    public function testAlterTableScenario()
    {
        if(!$this->_sm->getDatabasePlatform()->supportsAlterTable()) {
            $this->markTestSkipped('Alter Table is not supported by this platform.');
        }

        $this->createTestTable('alter_table');
        $this->createTestTable('alter_table_foreign');

        $table = $this->_sm->listTableDetails('alter_table');
        $this->assertTrue($table->hasColumn('id'));
        $this->assertTrue($table->hasColumn('test'));
        $this->assertTrue($table->hasColumn('foreign_key_test'));
        $this->assertEquals(0, count($table->getForeignKeys()));
        $this->assertEquals(1, count($table->getIndexes()));

        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
        $tableDiff->addedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', Type::getType('integer'));
        $tableDiff->removedColumns['test'] = $table->getColumn('test');

        $this->_sm->alterTable($tableDiff);

        $table = $this->_sm->listTableDetails('alter_table');
        $this->assertFalse($table->hasColumn('test'));
        $this->assertTrue($table->hasColumn('foo'));

        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
        $tableDiff->addedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo'));

        $this->_sm->alterTable($tableDiff);

        $table = $this->_sm->listTableDetails('alter_table');
        $this->assertEquals(2, count($table->getIndexes()));
        $this->assertTrue($table->hasIndex('foo_idx'));
        $this->assertEquals(array('foo'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns()));
        $this->assertFalse($table->getIndex('foo_idx')->isPrimary());
        $this->assertFalse($table->getIndex('foo_idx')->isUnique());

        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
        $tableDiff->changedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));

        $this->_sm->alterTable($tableDiff);

        $table = $this->_sm->listTableDetails('alter_table');
        $this->assertEquals(2, count($table->getIndexes()));
        $this->assertTrue($table->hasIndex('foo_idx'));
        $this->assertEquals(array('foo', 'foreign_key_test'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns()));

        $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
        $tableDiff->removedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));
        $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('foreign_key_test'), 'alter_table_foreign', array('id'));
        $tableDiff->addedForeignKeys[] = $fk;

        $this->_sm->alterTable($tableDiff);
        $table = $this->_sm->listTableDetails('alter_table');

        // dont check for index size here, some platforms automatically add indexes for foreign keys.
        $this->assertFalse($table->hasIndex('foo_idx'));

        $this->assertEquals(1, count($table->getForeignKeys()));
432 433
        $fks = $table->getForeignKeys();
        $foreignKey = current($fks);
434 435 436 437 438
        $this->assertEquals('alter_table_foreign', strtolower($foreignKey->getForeignTableName()));
        $this->assertEquals(array('foreign_key_test'), array_map('strtolower', $foreignKey->getColumns()));
        $this->assertEquals(array('id'), array_map('strtolower', $foreignKey->getForeignColumns()));
    }

439
    public function testCreateAndListViews()
440
    {
441 442 443 444
        if (!$this->_sm->getDatabasePlatform()->supportsViews()) {
            $this->markTestSkipped('Views is not supported by this platform.');
        }

445
        $this->createTestTable('view_test_table');
446

447 448
        $name = "doctrine_test_view";
        $sql = "SELECT * FROM view_test_table";
449

450
        $view = new \Doctrine\DBAL\Schema\View($name, $sql);
451

452 453 454
        $this->_sm->dropAndCreateView($view);

        $views = $this->_sm->listViews();
455 456
    }

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    public function testAutoincrementDetection()
    {
        if (!$this->_sm->getDatabasePlatform()->supportsIdentityColumns()) {
            $this->markTestSkipped('This test is only supported on platforms that have autoincrement');
        }

        $table = new \Doctrine\DBAL\Schema\Table('test_autoincrement');
        $table->setSchemaConfig($this->_sm->createSchemaConfig());
        $table->addColumn('id', 'integer', array('autoincrement' => true));
        $table->setPrimaryKey(array('id'));

        $this->_sm->createTable($table);

        $inferredTable = $this->_sm->listTableDetails('test_autoincrement');
        $this->assertTrue($inferredTable->hasColumn('id'));
        $this->assertTrue($inferredTable->getColumn('id')->getAutoincrement());
473
    }
474

475 476 477 478 479 480 481 482
    /**
     * @group DDC-887
     */
    public function testUpdateSchemaWithForeignKeyRenaming()
    {
        if (!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
            $this->markTestSkipped('This test is only supported on platforms that have foreign keys.');
        }
483

484 485 486
        $table = new \Doctrine\DBAL\Schema\Table('test_fk_base');
        $table->addColumn('id', 'integer');
        $table->setPrimaryKey(array('id'));
487

488 489 490 491 492 493 494
        $tableFK = new \Doctrine\DBAL\Schema\Table('test_fk_rename');
        $tableFK->setSchemaConfig($this->_sm->createSchemaConfig());
        $tableFK->addColumn('id', 'integer');
        $tableFK->addColumn('fk_id', 'integer');
        $tableFK->setPrimaryKey(array('id'));
        $tableFK->addIndex(array('fk_id'), 'fk_idx');
        $tableFK->addForeignKeyConstraint('test_fk_base', array('fk_id'), array('id'));
495

496 497
        $this->_sm->createTable($table);
        $this->_sm->createTable($tableFK);
498

499 500 501 502 503 504 505
        $tableFKNew = new \Doctrine\DBAL\Schema\Table('test_fk_rename');
        $tableFKNew->setSchemaConfig($this->_sm->createSchemaConfig());
        $tableFKNew->addColumn('id', 'integer');
        $tableFKNew->addColumn('rename_fk_id', 'integer');
        $tableFKNew->setPrimaryKey(array('id'));
        $tableFKNew->addIndex(array('rename_fk_id'), 'fk_idx');
        $tableFKNew->addForeignKeyConstraint('test_fk_base', array('rename_fk_id'), array('id'));
506

507 508
        $c = new \Doctrine\DBAL\Schema\Comparator();
        $tableDiff = $c->diffTable($tableFK, $tableFKNew);
509

510 511
        $this->_sm->alterTable($tableDiff);
    }
512

513 514 515 516 517 518 519 520
    /**
     * @group DBAL-42
     */
    public function testGetColumnComment()
    {
        if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement()) {
            $this->markTestSkipped('Database does not support column comments.');
        }
521

522
        $table = new \Doctrine\DBAL\Schema\Table('column_comment_test');
523 524 525 526 527
        $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
        $table->setPrimaryKey(array('id'));

        $this->_sm->createTable($table);

528
        $columns = $this->_sm->listTableColumns("column_comment_test");
529 530
        $this->assertEquals(1, count($columns));
        $this->assertEquals('This is a comment', $columns['id']->getComment());
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
    /**
     * @group DBAL-42
     */
    public function testAutomaticallyAppendCommentOnMarkedColumns()
    {
        if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement()) {
            $this->markTestSkipped('Database does not support column comments.');
        }

        $table = new \Doctrine\DBAL\Schema\Table('column_comment_test2');
        $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
        $table->addColumn('obj', 'object', array('comment' => 'This is a comment'));
        $table->addColumn('arr', 'array', array('comment' => 'This is a comment'));
        $table->setPrimaryKey(array('id'));

        $this->_sm->createTable($table);

        $columns = $this->_sm->listTableColumns("column_comment_test2");
        $this->assertEquals(3, count($columns));
        $this->assertEquals('This is a comment', $columns['id']->getComment());
        $this->assertEquals('This is a comment', $columns['obj']->getComment(), "The Doctrine2 Typehint should be stripped from comment.");
        $this->assertInstanceOf('Doctrine\DBAL\Types\ObjectType', $columns['obj']->getType(), "The Doctrine2 should be detected from comment hint.");
        $this->assertEquals('This is a comment', $columns['arr']->getComment(), "The Doctrine2 Typehint should be stripped from comment.");
        $this->assertInstanceOf('Doctrine\DBAL\Types\ArrayType', $columns['arr']->getType(), "The Doctrine2 should be detected from comment hint.");
    }

559 560 561 562 563 564 565 566 567 568 569 570 571 572
    /**
     * @group DBAL-197
     */
    public function testListTableWithBlob()
    {
        $table = new \Doctrine\DBAL\Schema\Table('test_blob_table');
        $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
        $table->addColumn('binarydata', 'blob', array());
        $table->setPrimaryKey(array('id'));

        $this->_sm->createTable($table);
        $blobTable = $this->_sm->listTableDetails('test_blob_table');
    }

573 574 575 576
    /**
     * @param string $name
     * @param array $data
     */
romanb's avatar
romanb committed
577
    protected function createTestTable($name = 'test_table', $data = array())
578 579 580 581 582 583
    {
        $options = array();
        if (isset($data['options'])) {
            $options = $data['options'];
        }

584 585 586 587 588 589 590
        $table = $this->getTestTable($name, $options);

        $this->_sm->dropAndCreateTable($table);
    }

    protected function getTestTable($name, $options=array())
    {
591
        $table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), false, $options);
592
        $table->setSchemaConfig($this->_sm->createSchemaConfig());
593
        $table->addColumn('id', 'integer', array('notnull' => true));
594 595 596 597 598 599 600 601 602 603 604
        $table->setPrimaryKey(array('id'));
        $table->addColumn('test', 'string', array('length' => 255));
        $table->addColumn('foreign_key_test', 'integer');
        return $table;
    }

    protected function getTestCompositeTable($name)
    {
        $table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), false, array());
        $table->setSchemaConfig($this->_sm->createSchemaConfig());
        $table->addColumn('id', 'integer', array('notnull' => true));
605 606
        $table->addColumn('other_id', 'integer', array('notnull' => true));
        $table->setPrimaryKey(array('id', 'other_id'));
607
        $table->addColumn('test', 'string', array('length' => 255));
608
        return $table;
609
    }
610 611 612 613 614

    protected function assertHasTable($tables, $tableName)
    {
        $foundTable = false;
        foreach ($tables AS $table) {
615
            $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.');
616 617 618 619 620 621
            if (strtolower($table->getName()) == 'list_tables_test_new_name') {
                $foundTable = true;
            }
        }
        $this->assertTrue($foundTable, "Could not find new table");
    }
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

    public function testListForeignKeysComposite()
    {
        if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
            $this->markTestSkipped('Does not support foreign key constraints.');
        }

        $this->_sm->createTable($this->getTestTable('test_create_fk3'));
        $this->_sm->createTable($this->getTestCompositeTable('test_create_fk4'));

        $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
            array('id', 'foreign_key_test'), 'test_create_fk4', array('id', 'other_id'), 'foreign_key_test_fk'
        );

        $this->_sm->createForeignKey($foreignKey, 'test_create_fk3');

        $fkeys = $this->_sm->listTableForeignKeys('test_create_fk3');

        $this->assertEquals(1, count($fkeys), "Table 'test_create_fk3' has to have one foreign key.");

        $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
        $this->assertEquals(array('id', 'foreign_key_test'), array_map('strtolower', $fkeys[0]->getLocalColumns()));
        $this->assertEquals(array('id', 'other_id'),         array_map('strtolower', $fkeys[0]->getForeignColumns()));
    }
646
}