3 namespace Doctrine\Tests\DBAL\Functional\Schema;
5 use Doctrine\DBAL\Types\Type,
6 Doctrine\DBAL\Schema\AbstractSchemaManager;
7 use Doctrine\DBAL\Platforms\AbstractPlatform;
8 use Doctrine\Common\EventManager;
9 use Doctrine\DBAL\Events;
11 require_once __DIR__ . '/../../../TestInit.php';
13 class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase
16 * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
20 protected function getPlatformName()
22 $class = get_class($this);
23 $e = explode('\\', $class);
25 $dbms = strtolower(str_replace('SchemaManagerTest', null, $testClass));
29 protected function setUp()
33 $dbms = $this->getPlatformName();
35 if ($this->_conn->getDatabasePlatform()->getName() !== $dbms) {
36 $this->markTestSkipped(get_class($this) . ' requires the use of ' . $dbms);
39 $this->_sm = $this->_conn->getSchemaManager();
45 public function testDropAndCreateSequence()
47 if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
48 $this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
51 $sequence = new \Doctrine\DBAL\Schema\Sequence('dropcreate_sequences_test_seq', 20, 10);
52 $this->_sm->dropAndCreateSequence($sequence);
55 public function testListSequences()
57 if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
58 $this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
61 $sequence = new \Doctrine\DBAL\Schema\Sequence('list_sequences_test_seq', 20, 10);
62 $this->_sm->createSequence($sequence);
64 $sequences = $this->_sm->listSequences();
66 $this->assertInternalType('array', $sequences, 'listSequences() should return an array.');
68 $foundSequence = null;
69 foreach($sequences AS $sequence) {
70 $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $sequence, 'Array elements of listSequences() should be Sequence instances.');
71 if(strtolower($sequence->getName()) == 'list_sequences_test_seq') {
72 $foundSequence = $sequence;
76 $this->assertNotNull($foundSequence, "Sequence with name 'list_sequences_test_seq' was not found.");
77 $this->assertEquals(20, $foundSequence->getAllocationSize(), "Allocation Size is expected to be 20.");
78 $this->assertEquals(10, $foundSequence->getInitialValue(), "Initial Value is expected to be 10.");
81 public function testListDatabases()
83 if (!$this->_sm->getDatabasePlatform()->supportsCreateDropDatabase()) {
84 $this->markTestSkipped('Cannot drop Database client side with this Driver.');
87 $this->_sm->dropAndCreateDatabase('test_create_database');
88 $databases = $this->_sm->listDatabases();
90 $databases = \array_map('strtolower', $databases);
92 $this->assertEquals(true, \in_array('test_create_database', $databases));
95 public function testListTables()
97 $this->createTestTable('list_tables_test');
98 $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'.");
104 foreach ($tables AS $table) {
105 $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table);
106 if (strtolower($table->getName()) == 'list_tables_test') {
109 $this->assertTrue($table->hasColumn('id'));
110 $this->assertTrue($table->hasColumn('test'));
111 $this->assertTrue($table->hasColumn('foreign_key_test'));
115 $this->assertTrue( $foundTable , "The 'list_tables_test' table has to be found.");
118 public function createListTableColumns()
120 $table = new \Doctrine\DBAL\Schema\Table('list_table_columns');
121 $table->addColumn('id', 'integer', array('notnull' => true));
122 $table->addColumn('test', 'string', array('length' => 255, 'notnull' => false, 'default' => 'expected default'));
123 $table->addColumn('foo', 'text', array('notnull' => true));
124 $table->addColumn('bar', 'decimal', array('precision' => 10, 'scale' => 4, 'notnull' => false));
125 $table->addColumn('baz1', 'datetime');
126 $table->addColumn('baz2', 'time');
127 $table->addColumn('baz3', 'date');
128 $table->setPrimaryKey(array('id'));
133 public function testListTableColumns()
135 $table = $this->createListTableColumns();
137 $this->_sm->dropAndCreateTable($table);
139 $columns = $this->_sm->listTableColumns('list_table_columns');
141 $this->assertArrayHasKey('id', $columns);
142 $this->assertEquals('id', strtolower($columns['id']->getname()));
143 $this->assertInstanceOf('Doctrine\DBAL\Types\IntegerType', $columns['id']->gettype());
144 $this->assertEquals(false, $columns['id']->getunsigned());
145 $this->assertEquals(true, $columns['id']->getnotnull());
146 $this->assertEquals(null, $columns['id']->getdefault());
147 $this->assertInternalType('array', $columns['id']->getPlatformOptions());
149 $this->assertArrayHasKey('test', $columns);
150 $this->assertEquals('test', strtolower($columns['test']->getname()));
151 $this->assertInstanceOf('Doctrine\DBAL\Types\StringType', $columns['test']->gettype());
152 $this->assertEquals(255, $columns['test']->getlength());
153 $this->assertEquals(false, $columns['test']->getfixed());
154 $this->assertEquals(false, $columns['test']->getnotnull());
155 $this->assertEquals('expected default', $columns['test']->getdefault());
156 $this->assertInternalType('array', $columns['test']->getPlatformOptions());
158 $this->assertEquals('foo', strtolower($columns['foo']->getname()));
159 $this->assertInstanceOf('Doctrine\DBAL\Types\TextType', $columns['foo']->gettype());
160 $this->assertEquals(false, $columns['foo']->getunsigned());
161 $this->assertEquals(false, $columns['foo']->getfixed());
162 $this->assertEquals(true, $columns['foo']->getnotnull());
163 $this->assertEquals(null, $columns['foo']->getdefault());
164 $this->assertInternalType('array', $columns['foo']->getPlatformOptions());
166 $this->assertEquals('bar', strtolower($columns['bar']->getname()));
167 $this->assertInstanceOf('Doctrine\DBAL\Types\DecimalType', $columns['bar']->gettype());
168 $this->assertEquals(null, $columns['bar']->getlength());
169 $this->assertEquals(10, $columns['bar']->getprecision());
170 $this->assertEquals(4, $columns['bar']->getscale());
171 $this->assertEquals(false, $columns['bar']->getunsigned());
172 $this->assertEquals(false, $columns['bar']->getfixed());
173 $this->assertEquals(false, $columns['bar']->getnotnull());
174 $this->assertEquals(null, $columns['bar']->getdefault());
175 $this->assertInternalType('array', $columns['bar']->getPlatformOptions());
177 $this->assertEquals('baz1', strtolower($columns['baz1']->getname()));
178 $this->assertInstanceOf('Doctrine\DBAL\Types\DateTimeType', $columns['baz1']->gettype());
179 $this->assertEquals(true, $columns['baz1']->getnotnull());
180 $this->assertEquals(null, $columns['baz1']->getdefault());
181 $this->assertInternalType('array', $columns['baz1']->getPlatformOptions());
183 $this->assertEquals('baz2', strtolower($columns['baz2']->getname()));
184 $this->assertContains($columns['baz2']->gettype()->getName(), array('time', 'date', 'datetime'));
185 $this->assertEquals(true, $columns['baz2']->getnotnull());
186 $this->assertEquals(null, $columns['baz2']->getdefault());
187 $this->assertInternalType('array', $columns['baz2']->getPlatformOptions());
189 $this->assertEquals('baz3', strtolower($columns['baz3']->getname()));
190 $this->assertContains($columns['baz2']->gettype()->getName(), array('time', 'date', 'datetime'));
191 $this->assertEquals(true, $columns['baz3']->getnotnull());
192 $this->assertEquals(null, $columns['baz3']->getdefault());
193 $this->assertInternalType('array', $columns['baz3']->getPlatformOptions());
196 public function testListTableColumnsDispatchEvent()
198 $table = $this->createListTableColumns();
200 $this->_sm->dropAndCreateTable($table);
202 $listenerMock = $this->getMock('ListTableColumnsDispatchEventListener', array('onSchemaColumnDefinition'));
204 ->expects($this->exactly(7))
205 ->method('onSchemaColumnDefinition');
207 $oldEventManager = $this->_sm->getDatabasePlatform()->getEventManager();
209 $eventManager = new EventManager();
210 $eventManager->addEventListener(array(Events::onSchemaColumnDefinition), $listenerMock);
212 $this->_sm->getDatabasePlatform()->setEventManager($eventManager);
214 $this->_sm->listTableColumns('list_table_columns');
216 $this->_sm->getDatabasePlatform()->setEventManager($oldEventManager);
219 public function testListTableIndexesDispatchEvent()
221 $table = $this->getTestTable('list_table_indexes_test');
222 $table->addUniqueIndex(array('test'), 'test_index_name');
223 $table->addIndex(array('id', 'test'), 'test_composite_idx');
225 $this->_sm->dropAndCreateTable($table);
227 $listenerMock = $this->getMock('ListTableIndexesDispatchEventListener', array('onSchemaIndexDefinition'));
229 ->expects($this->exactly(3))
230 ->method('onSchemaIndexDefinition');
232 $oldEventManager = $this->_sm->getDatabasePlatform()->getEventManager();
234 $eventManager = new EventManager();
235 $eventManager->addEventListener(array(Events::onSchemaIndexDefinition), $listenerMock);
237 $this->_sm->getDatabasePlatform()->setEventManager($eventManager);
239 $this->_sm->listTableIndexes('list_table_indexes_test');
241 $this->_sm->getDatabasePlatform()->setEventManager($oldEventManager);
244 public function testDiffListTableColumns()
246 if ($this->_sm->getDatabasePlatform()->getName() == 'oracle') {
247 $this->markTestSkipped('Does not work with Oracle, since it cannot detect DateTime, Date and Time differenecs (at the moment).');
250 $offlineTable = $this->createListTableColumns();
251 $onlineTable = $this->_sm->listTableDetails('list_table_columns');
253 $comparator = new \Doctrine\DBAL\Schema\Comparator();
254 $diff = $comparator->diffTable($offlineTable, $onlineTable);
256 $this->assertFalse($diff, "No differences should be detected with the offline vs online schema.");
259 public function testListTableIndexes()
261 $table = $this->getTestCompositeTable('list_table_indexes_test');
262 $table->addUniqueIndex(array('test'), 'test_index_name');
263 $table->addIndex(array('id', 'test'), 'test_composite_idx');
265 $this->_sm->dropAndCreateTable($table);
267 $tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
269 $this->assertEquals(3, count($tableIndexes));
271 $this->assertArrayHasKey('primary', $tableIndexes, 'listTableIndexes() has to return a "primary" array key.');
272 $this->assertEquals(array('id', 'other_id'), array_map('strtolower', $tableIndexes['primary']->getColumns()));
273 $this->assertTrue($tableIndexes['primary']->isUnique());
274 $this->assertTrue($tableIndexes['primary']->isPrimary());
276 $this->assertEquals('test_index_name', $tableIndexes['test_index_name']->getName());
277 $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test_index_name']->getColumns()));
278 $this->assertTrue($tableIndexes['test_index_name']->isUnique());
279 $this->assertFalse($tableIndexes['test_index_name']->isPrimary());
281 $this->assertEquals('test_composite_idx', $tableIndexes['test_composite_idx']->getName());
282 $this->assertEquals(array('id', 'test'), array_map('strtolower', $tableIndexes['test_composite_idx']->getColumns()));
283 $this->assertFalse($tableIndexes['test_composite_idx']->isUnique());
284 $this->assertFalse($tableIndexes['test_composite_idx']->isPrimary());
287 public function testDropAndCreateIndex()
289 $table = $this->getTestTable('test_create_index');
290 $table->addUniqueIndex(array('test'), 'test');
291 $this->_sm->dropAndCreateTable($table);
293 $this->_sm->dropAndCreateIndex($table->getIndex('test'), $table);
294 $tableIndexes = $this->_sm->listTableIndexes('test_create_index');
295 $this->assertInternalType('array', $tableIndexes);
297 $this->assertEquals('test', strtolower($tableIndexes['test']->getName()));
298 $this->assertEquals(array('test'), array_map('strtolower', $tableIndexes['test']->getColumns()));
299 $this->assertTrue($tableIndexes['test']->isUnique());
300 $this->assertFalse($tableIndexes['test']->isPrimary());
303 public function testCreateTableWithForeignKeys()
305 if(!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
306 $this->markTestSkipped('Platform does not support foreign keys.');
309 $tableB = $this->getTestTable('test_foreign');
311 $this->_sm->dropAndCreateTable($tableB);
313 $tableA = $this->getTestTable('test_create_fk');
314 $tableA->addForeignKeyConstraint('test_foreign', array('foreign_key_test'), array('id'));
316 $this->_sm->dropAndCreateTable($tableA);
318 $fkTable = $this->_sm->listTableDetails('test_create_fk');
319 $fkConstraints = $fkTable->getForeignKeys();
320 $this->assertEquals(1, count($fkConstraints), "Table 'test_create_fk1' has to have one foreign key.");
322 $fkConstraint = current($fkConstraints);
323 $this->assertInstanceOf('\Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkConstraint);
324 $this->assertEquals('test_foreign', strtolower($fkConstraint->getForeignTableName()));
325 $this->assertEquals(array('foreign_key_test'), array_map('strtolower', $fkConstraint->getColumns()));
326 $this->assertEquals(array('id'), array_map('strtolower', $fkConstraint->getForeignColumns()));
328 $this->assertTrue($fkTable->columnsAreIndexed($fkConstraint->getColumns()), "The columns of a foreign key constraint should always be indexed.");
331 public function testListForeignKeys()
333 if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
334 $this->markTestSkipped('Does not support foreign key constraints.');
337 $this->createTestTable('test_create_fk1');
338 $this->createTestTable('test_create_fk2');
340 $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
341 array('foreign_key_test'), 'test_create_fk2', array('id'), 'foreign_key_test_fk', array('onDelete' => 'CASCADE')
344 $this->_sm->createForeignKey($foreignKey, 'test_create_fk1');
346 $fkeys = $this->_sm->listTableForeignKeys('test_create_fk1');
348 $this->assertEquals(1, count($fkeys), "Table 'test_create_fk1' has to have one foreign key.");
350 $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
351 $this->assertEquals(array('foreign_key_test'), array_map('strtolower', $fkeys[0]->getLocalColumns()));
352 $this->assertEquals(array('id'), array_map('strtolower', $fkeys[0]->getForeignColumns()));
353 $this->assertEquals('test_create_fk2', strtolower($fkeys[0]->getForeignTableName()));
355 if($fkeys[0]->hasOption('onDelete')) {
356 $this->assertEquals('CASCADE', $fkeys[0]->getOption('onDelete'));
360 protected function getCreateExampleViewSql()
362 $this->markTestSkipped('No Create Example View SQL was defined for this SchemaManager');
365 public function testCreateSchema()
367 $this->createTestTable('test_table');
369 $schema = $this->_sm->createSchema();
370 $this->assertTrue($schema->hasTable('test_table'));
373 public function testAlterTableScenario()
375 if(!$this->_sm->getDatabasePlatform()->supportsAlterTable()) {
376 $this->markTestSkipped('Alter Table is not supported by this platform.');
379 $this->createTestTable('alter_table');
380 $this->createTestTable('alter_table_foreign');
382 $table = $this->_sm->listTableDetails('alter_table');
383 $this->assertTrue($table->hasColumn('id'));
384 $this->assertTrue($table->hasColumn('test'));
385 $this->assertTrue($table->hasColumn('foreign_key_test'));
386 $this->assertEquals(0, count($table->getForeignKeys()));
387 $this->assertEquals(1, count($table->getIndexes()));
389 $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
390 $tableDiff->addedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', Type::getType('integer'));
391 $tableDiff->removedColumns['test'] = $table->getColumn('test');
393 $this->_sm->alterTable($tableDiff);
395 $table = $this->_sm->listTableDetails('alter_table');
396 $this->assertFalse($table->hasColumn('test'));
397 $this->assertTrue($table->hasColumn('foo'));
399 $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
400 $tableDiff->addedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo'));
402 $this->_sm->alterTable($tableDiff);
404 $table = $this->_sm->listTableDetails('alter_table');
405 $this->assertEquals(2, count($table->getIndexes()));
406 $this->assertTrue($table->hasIndex('foo_idx'));
407 $this->assertEquals(array('foo'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns()));
408 $this->assertFalse($table->getIndex('foo_idx')->isPrimary());
409 $this->assertFalse($table->getIndex('foo_idx')->isUnique());
411 $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
412 $tableDiff->changedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));
414 $this->_sm->alterTable($tableDiff);
416 $table = $this->_sm->listTableDetails('alter_table');
417 $this->assertEquals(2, count($table->getIndexes()));
418 $this->assertTrue($table->hasIndex('foo_idx'));
419 $this->assertEquals(array('foo', 'foreign_key_test'), array_map('strtolower', $table->getIndex('foo_idx')->getColumns()));
421 $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
422 $tableDiff->removedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));
423 $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('foreign_key_test'), 'alter_table_foreign', array('id'));
424 $tableDiff->addedForeignKeys[] = $fk;
426 $this->_sm->alterTable($tableDiff);
427 $table = $this->_sm->listTableDetails('alter_table');
429 // dont check for index size here, some platforms automatically add indexes for foreign keys.
430 $this->assertFalse($table->hasIndex('foo_idx'));
432 $this->assertEquals(1, count($table->getForeignKeys()));
433 $fks = $table->getForeignKeys();
434 $foreignKey = current($fks);
435 $this->assertEquals('alter_table_foreign', strtolower($foreignKey->getForeignTableName()));
436 $this->assertEquals(array('foreign_key_test'), array_map('strtolower', $foreignKey->getColumns()));
437 $this->assertEquals(array('id'), array_map('strtolower', $foreignKey->getForeignColumns()));
440 public function testCreateAndListViews()
442 if (!$this->_sm->getDatabasePlatform()->supportsViews()) {
443 $this->markTestSkipped('Views is not supported by this platform.');
446 $this->createTestTable('view_test_table');
448 $name = "doctrine_test_view";
449 $sql = "SELECT * FROM view_test_table";
451 $view = new \Doctrine\DBAL\Schema\View($name, $sql);
453 $this->_sm->dropAndCreateView($view);
455 $views = $this->_sm->listViews();
458 public function testAutoincrementDetection()
460 if (!$this->_sm->getDatabasePlatform()->supportsIdentityColumns()) {
461 $this->markTestSkipped('This test is only supported on platforms that have autoincrement');
464 $table = new \Doctrine\DBAL\Schema\Table('test_autoincrement');
465 $table->setSchemaConfig($this->_sm->createSchemaConfig());
466 $table->addColumn('id', 'integer', array('autoincrement' => true));
467 $table->setPrimaryKey(array('id'));
469 $this->_sm->createTable($table);
471 $inferredTable = $this->_sm->listTableDetails('test_autoincrement');
472 $this->assertTrue($inferredTable->hasColumn('id'));
473 $this->assertTrue($inferredTable->getColumn('id')->getAutoincrement());
479 public function testUpdateSchemaWithForeignKeyRenaming()
481 if (!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
482 $this->markTestSkipped('This test is only supported on platforms that have foreign keys.');
485 $table = new \Doctrine\DBAL\Schema\Table('test_fk_base');
486 $table->addColumn('id', 'integer');
487 $table->setPrimaryKey(array('id'));
489 $tableFK = new \Doctrine\DBAL\Schema\Table('test_fk_rename');
490 $tableFK->setSchemaConfig($this->_sm->createSchemaConfig());
491 $tableFK->addColumn('id', 'integer');
492 $tableFK->addColumn('fk_id', 'integer');
493 $tableFK->setPrimaryKey(array('id'));
494 $tableFK->addIndex(array('fk_id'), 'fk_idx');
495 $tableFK->addForeignKeyConstraint('test_fk_base', array('fk_id'), array('id'));
497 $this->_sm->createTable($table);
498 $this->_sm->createTable($tableFK);
500 $tableFKNew = new \Doctrine\DBAL\Schema\Table('test_fk_rename');
501 $tableFKNew->setSchemaConfig($this->_sm->createSchemaConfig());
502 $tableFKNew->addColumn('id', 'integer');
503 $tableFKNew->addColumn('rename_fk_id', 'integer');
504 $tableFKNew->setPrimaryKey(array('id'));
505 $tableFKNew->addIndex(array('rename_fk_id'), 'fk_idx');
506 $tableFKNew->addForeignKeyConstraint('test_fk_base', array('rename_fk_id'), array('id'));
508 $c = new \Doctrine\DBAL\Schema\Comparator();
509 $tableDiff = $c->diffTable($tableFK, $tableFKNew);
511 $this->_sm->alterTable($tableDiff);
517 public function testGetColumnComment()
519 if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement()) {
520 $this->markTestSkipped('Database does not support column comments.');
523 $table = new \Doctrine\DBAL\Schema\Table('column_comment_test');
524 $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
525 $table->setPrimaryKey(array('id'));
527 $this->_sm->createTable($table);
529 $columns = $this->_sm->listTableColumns("column_comment_test");
530 $this->assertEquals(1, count($columns));
531 $this->assertEquals('This is a comment', $columns['id']->getComment());
537 public function testAutomaticallyAppendCommentOnMarkedColumns()
539 if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement()) {
540 $this->markTestSkipped('Database does not support column comments.');
543 $table = new \Doctrine\DBAL\Schema\Table('column_comment_test2');
544 $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
545 $table->addColumn('obj', 'object', array('comment' => 'This is a comment'));
546 $table->addColumn('arr', 'array', array('comment' => 'This is a comment'));
547 $table->setPrimaryKey(array('id'));
549 $this->_sm->createTable($table);
551 $columns = $this->_sm->listTableColumns("column_comment_test2");
552 $this->assertEquals(3, count($columns));
553 $this->assertEquals('This is a comment', $columns['id']->getComment());
554 $this->assertEquals('This is a comment', $columns['obj']->getComment(), "The Doctrine2 Typehint should be stripped from comment.");
555 $this->assertInstanceOf('Doctrine\DBAL\Types\ObjectType', $columns['obj']->getType(), "The Doctrine2 should be detected from comment hint.");
556 $this->assertEquals('This is a comment', $columns['arr']->getComment(), "The Doctrine2 Typehint should be stripped from comment.");
557 $this->assertInstanceOf('Doctrine\DBAL\Types\ArrayType', $columns['arr']->getType(), "The Doctrine2 should be detected from comment hint.");
563 public function testListTableWithBlob()
565 $table = new \Doctrine\DBAL\Schema\Table('test_blob_table');
566 $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
567 $table->addColumn('binarydata', 'blob', array());
568 $table->setPrimaryKey(array('id'));
570 $this->_sm->createTable($table);
571 $blobTable = $this->_sm->listTableDetails('test_blob_table');
575 * @param string $name
578 protected function createTestTable($name = 'test_table', $data = array())
581 if (isset($data['options'])) {
582 $options = $data['options'];
585 $table = $this->getTestTable($name, $options);
587 $this->_sm->dropAndCreateTable($table);
590 protected function getTestTable($name, $options=array())
592 $table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), false, $options);
593 $table->setSchemaConfig($this->_sm->createSchemaConfig());
594 $table->addColumn('id', 'integer', array('notnull' => true));
595 $table->setPrimaryKey(array('id'));
596 $table->addColumn('test', 'string', array('length' => 255));
597 $table->addColumn('foreign_key_test', 'integer');
601 protected function getTestCompositeTable($name)
603 $table = new \Doctrine\DBAL\Schema\Table($name, array(), array(), array(), false, array());
604 $table->setSchemaConfig($this->_sm->createSchemaConfig());
605 $table->addColumn('id', 'integer', array('notnull' => true));
606 $table->addColumn('other_id', 'integer', array('notnull' => true));
607 $table->setPrimaryKey(array('id', 'other_id'));
608 $table->addColumn('test', 'string', array('length' => 255));
612 protected function assertHasTable($tables, $tableName)
615 foreach ($tables AS $table) {
616 $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table, 'No Table instance was found in tables array.');
617 if (strtolower($table->getName()) == 'list_tables_test_new_name') {
621 $this->assertTrue($foundTable, "Could not find new table");
624 public function testListForeignKeysComposite()
626 if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
627 $this->markTestSkipped('Does not support foreign key constraints.');
630 $this->_sm->createTable($this->getTestTable('test_create_fk3'));
631 $this->_sm->createTable($this->getTestCompositeTable('test_create_fk4'));
633 $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
634 array('id', 'foreign_key_test'), 'test_create_fk4', array('id', 'other_id'), 'foreign_key_test_fk'
637 $this->_sm->createForeignKey($foreignKey, 'test_create_fk3');
639 $fkeys = $this->_sm->listTableForeignKeys('test_create_fk3');
641 $this->assertEquals(1, count($fkeys), "Table 'test_create_fk3' has to have one foreign key.");
643 $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $fkeys[0]);
644 $this->assertEquals(array('id', 'foreign_key_test'), array_map('strtolower', $fkeys[0]->getLocalColumns()));
645 $this->assertEquals(array('id', 'other_id'), array_map('strtolower', $fkeys[0]->getForeignColumns()));