Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Functional / Schema / SchemaManagerFunctionalTestCase.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Functional\Schema;
4
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;
10
11 require_once __DIR__ . '/../../../TestInit.php';
12
13 class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTestCase
14 {
15     /**
16      * @var \Doctrine\DBAL\Schema\AbstractSchemaManager
17      */
18     protected $_sm;
19
20         protected function getPlatformName()
21         {
22             $class = get_class($this);
23         $e = explode('\\', $class);
24         $testClass = end($e);
25         $dbms = strtolower(str_replace('SchemaManagerTest', null, $testClass));
26         return $dbms;
27         }
28
29     protected function setUp()
30     {
31         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         $this->_sm = $this->_conn->getSchemaManager();
40     }
41
42     /**
43      * @group DBAL-195
44      */
45     public function testDropAndCreateSequence()
46     {
47         if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
48             $this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
49         }
50
51         $sequence = new \Doctrine\DBAL\Schema\Sequence('dropcreate_sequences_test_seq', 20, 10);
52         $this->_sm->dropAndCreateSequence($sequence);
53     }
54
55     public function testListSequences()
56     {
57         if(!$this->_conn->getDatabasePlatform()->supportsSequences()) {
58             $this->markTestSkipped($this->_conn->getDriver()->getName().' does not support sequences.');
59         }
60
61         $sequence = new \Doctrine\DBAL\Schema\Sequence('list_sequences_test_seq', 20, 10);
62         $this->_sm->createSequence($sequence);
63
64         $sequences = $this->_sm->listSequences();
65
66         $this->assertInternalType('array', $sequences, 'listSequences() should return an array.');
67
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;
73             }
74         }
75
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.");
79     }
80
81     public function testListDatabases()
82     {
83         if (!$this->_sm->getDatabasePlatform()->supportsCreateDropDatabase()) {
84             $this->markTestSkipped('Cannot drop Database client side with this Driver.');
85         }
86
87         $this->_sm->dropAndCreateDatabase('test_create_database');
88         $databases = $this->_sm->listDatabases();
89
90         $databases = \array_map('strtolower', $databases);
91
92         $this->assertEquals(true, \in_array('test_create_database', $databases));
93     }
94
95     public function testListTables()
96     {
97         $this->createTestTable('list_tables_test');
98         $tables = $this->_sm->listTables();
99
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         $foundTable = false;
104         foreach ($tables AS $table) {
105             $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table);
106             if (strtolower($table->getName()) == 'list_tables_test') {
107                 $foundTable = true;
108
109                 $this->assertTrue($table->hasColumn('id'));
110                 $this->assertTrue($table->hasColumn('test'));
111                 $this->assertTrue($table->hasColumn('foreign_key_test'));
112             }
113         }
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         $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'));
129
130         return $table;
131     }
132
133     public function testListTableColumns()
134     {
135         $table = $this->createListTableColumns();
136
137         $this->_sm->dropAndCreateTable($table);
138
139         $columns = $this->_sm->listTableColumns('list_table_columns');
140
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());
148
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());
157
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());
165
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());
176
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());
182
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());
188
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());
194     }
195
196     public function testListTableColumnsDispatchEvent()
197     {
198         $table = $this->createListTableColumns();
199
200         $this->_sm->dropAndCreateTable($table);
201
202         $listenerMock = $this->getMock('ListTableColumnsDispatchEventListener', array('onSchemaColumnDefinition'));
203         $listenerMock
204             ->expects($this->exactly(7))
205             ->method('onSchemaColumnDefinition');
206
207         $oldEventManager = $this->_sm->getDatabasePlatform()->getEventManager();
208
209         $eventManager = new EventManager();
210         $eventManager->addEventListener(array(Events::onSchemaColumnDefinition), $listenerMock);
211
212         $this->_sm->getDatabasePlatform()->setEventManager($eventManager);
213
214         $this->_sm->listTableColumns('list_table_columns');
215
216         $this->_sm->getDatabasePlatform()->setEventManager($oldEventManager);
217     }
218
219     public function testListTableIndexesDispatchEvent()
220     {
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');
224
225         $this->_sm->dropAndCreateTable($table);
226
227         $listenerMock = $this->getMock('ListTableIndexesDispatchEventListener', array('onSchemaIndexDefinition'));
228         $listenerMock
229             ->expects($this->exactly(3))
230             ->method('onSchemaIndexDefinition');
231
232         $oldEventManager = $this->_sm->getDatabasePlatform()->getEventManager();
233
234         $eventManager = new EventManager();
235         $eventManager->addEventListener(array(Events::onSchemaIndexDefinition), $listenerMock);
236
237         $this->_sm->getDatabasePlatform()->setEventManager($eventManager);
238
239         $this->_sm->listTableIndexes('list_table_indexes_test');
240
241         $this->_sm->getDatabasePlatform()->setEventManager($oldEventManager);
242     }
243
244     public function testDiffListTableColumns()
245     {
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).');
248         }
249
250         $offlineTable = $this->createListTableColumns();
251         $onlineTable = $this->_sm->listTableDetails('list_table_columns');
252
253         $comparator = new \Doctrine\DBAL\Schema\Comparator();
254         $diff = $comparator->diffTable($offlineTable, $onlineTable);
255
256         $this->assertFalse($diff, "No differences should be detected with the offline vs online schema.");
257     }
258
259     public function testListTableIndexes()
260     {
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');
264
265         $this->_sm->dropAndCreateTable($table);
266
267         $tableIndexes = $this->_sm->listTableIndexes('list_table_indexes_test');
268
269         $this->assertEquals(3, count($tableIndexes));
270
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());
275
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());
280
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());
285     }
286
287     public function testDropAndCreateIndex()
288     {
289         $table = $this->getTestTable('test_create_index');
290         $table->addUniqueIndex(array('test'), 'test');
291         $this->_sm->dropAndCreateTable($table);
292
293         $this->_sm->dropAndCreateIndex($table->getIndex('test'), $table);
294         $tableIndexes = $this->_sm->listTableIndexes('test_create_index');
295         $this->assertInternalType('array', $tableIndexes);
296
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());
301     }
302
303     public function testCreateTableWithForeignKeys()
304     {
305         if(!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
306             $this->markTestSkipped('Platform does not support foreign keys.');
307         }
308
309         $tableB = $this->getTestTable('test_foreign');
310
311         $this->_sm->dropAndCreateTable($tableB);
312
313         $tableA = $this->getTestTable('test_create_fk');
314         $tableA->addForeignKeyConstraint('test_foreign', array('foreign_key_test'), array('id'));
315
316         $this->_sm->dropAndCreateTable($tableA);
317
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.");
321
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()));
327
328         $this->assertTrue($fkTable->columnsAreIndexed($fkConstraint->getColumns()), "The columns of a foreign key constraint should always be indexed.");
329     }
330
331     public function testListForeignKeys()
332     {
333         if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
334             $this->markTestSkipped('Does not support foreign key constraints.');
335         }
336
337         $this->createTestTable('test_create_fk1');
338         $this->createTestTable('test_create_fk2');
339
340         $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
341             array('foreign_key_test'), 'test_create_fk2', array('id'), 'foreign_key_test_fk', array('onDelete' => 'CASCADE')
342         );
343
344         $this->_sm->createForeignKey($foreignKey, 'test_create_fk1');
345
346         $fkeys = $this->_sm->listTableForeignKeys('test_create_fk1');
347
348         $this->assertEquals(1, count($fkeys), "Table 'test_create_fk1' has to have one foreign key.");
349
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()));
354
355         if($fkeys[0]->hasOption('onDelete')) {
356             $this->assertEquals('CASCADE', $fkeys[0]->getOption('onDelete'));
357         }
358     }
359
360     protected function getCreateExampleViewSql()
361     {
362         $this->markTestSkipped('No Create Example View SQL was defined for this SchemaManager');
363     }
364
365     public function testCreateSchema()
366     {
367         $this->createTestTable('test_table');
368
369         $schema = $this->_sm->createSchema();
370         $this->assertTrue($schema->hasTable('test_table'));
371     }
372
373     public function testAlterTableScenario()
374     {
375         if(!$this->_sm->getDatabasePlatform()->supportsAlterTable()) {
376             $this->markTestSkipped('Alter Table is not supported by this platform.');
377         }
378
379         $this->createTestTable('alter_table');
380         $this->createTestTable('alter_table_foreign');
381
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()));
388
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');
392
393         $this->_sm->alterTable($tableDiff);
394
395         $table = $this->_sm->listTableDetails('alter_table');
396         $this->assertFalse($table->hasColumn('test'));
397         $this->assertTrue($table->hasColumn('foo'));
398
399         $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
400         $tableDiff->addedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo'));
401
402         $this->_sm->alterTable($tableDiff);
403
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());
410
411         $tableDiff = new \Doctrine\DBAL\Schema\TableDiff("alter_table");
412         $tableDiff->changedIndexes[] = new \Doctrine\DBAL\Schema\Index('foo_idx', array('foo', 'foreign_key_test'));
413
414         $this->_sm->alterTable($tableDiff);
415
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()));
420
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;
425
426         $this->_sm->alterTable($tableDiff);
427         $table = $this->_sm->listTableDetails('alter_table');
428
429         // dont check for index size here, some platforms automatically add indexes for foreign keys.
430         $this->assertFalse($table->hasIndex('foo_idx'));
431
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()));
438     }
439
440     public function testCreateAndListViews()
441     {
442         if (!$this->_sm->getDatabasePlatform()->supportsViews()) {
443             $this->markTestSkipped('Views is not supported by this platform.');
444         }
445
446         $this->createTestTable('view_test_table');
447
448         $name = "doctrine_test_view";
449         $sql = "SELECT * FROM view_test_table";
450
451         $view = new \Doctrine\DBAL\Schema\View($name, $sql);
452
453         $this->_sm->dropAndCreateView($view);
454
455         $views = $this->_sm->listViews();
456     }
457
458     public function testAutoincrementDetection()
459     {
460         if (!$this->_sm->getDatabasePlatform()->supportsIdentityColumns()) {
461             $this->markTestSkipped('This test is only supported on platforms that have autoincrement');
462         }
463
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'));
468
469         $this->_sm->createTable($table);
470
471         $inferredTable = $this->_sm->listTableDetails('test_autoincrement');
472         $this->assertTrue($inferredTable->hasColumn('id'));
473         $this->assertTrue($inferredTable->getColumn('id')->getAutoincrement());
474     }
475
476     /**
477      * @group DDC-887
478      */
479     public function testUpdateSchemaWithForeignKeyRenaming()
480     {
481         if (!$this->_sm->getDatabasePlatform()->supportsForeignKeyConstraints()) {
482             $this->markTestSkipped('This test is only supported on platforms that have foreign keys.');
483         }
484
485         $table = new \Doctrine\DBAL\Schema\Table('test_fk_base');
486         $table->addColumn('id', 'integer');
487         $table->setPrimaryKey(array('id'));
488
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'));
496
497         $this->_sm->createTable($table);
498         $this->_sm->createTable($tableFK);
499
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'));
507
508         $c = new \Doctrine\DBAL\Schema\Comparator();
509         $tableDiff = $c->diffTable($tableFK, $tableFKNew);
510
511         $this->_sm->alterTable($tableDiff);
512     }
513
514     /**
515      * @group DBAL-42
516      */
517     public function testGetColumnComment()
518     {
519         if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement()) {
520             $this->markTestSkipped('Database does not support column comments.');
521         }
522
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'));
526
527         $this->_sm->createTable($table);
528
529         $columns = $this->_sm->listTableColumns("column_comment_test");
530         $this->assertEquals(1, count($columns));
531         $this->assertEquals('This is a comment', $columns['id']->getComment());
532     }
533
534     /**
535      * @group DBAL-42
536      */
537     public function testAutomaticallyAppendCommentOnMarkedColumns()
538     {
539         if (!$this->_conn->getDatabasePlatform()->supportsInlineColumnComments() && !$this->_conn->getDatabasePlatform()->supportsCommentOnStatement()) {
540             $this->markTestSkipped('Database does not support column comments.');
541         }
542
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'));
548
549         $this->_sm->createTable($table);
550
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.");
558     }
559
560     /**
561      * @group DBAL-197
562      */
563     public function testListTableWithBlob()
564     {
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'));
569
570         $this->_sm->createTable($table);
571         $blobTable = $this->_sm->listTableDetails('test_blob_table');
572     }
573
574     /**
575      * @param string $name
576      * @param array $data
577      */
578     protected function createTestTable($name = 'test_table', $data = array())
579     {
580         $options = array();
581         if (isset($data['options'])) {
582             $options = $data['options'];
583         }
584
585         $table = $this->getTestTable($name, $options);
586
587         $this->_sm->dropAndCreateTable($table);
588     }
589
590     protected function getTestTable($name, $options=array())
591     {
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');
598         return $table;
599     }
600
601     protected function getTestCompositeTable($name)
602     {
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));
609         return $table;
610     }
611
612     protected function assertHasTable($tables, $tableName)
613     {
614         $foundTable = false;
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') {
618                 $foundTable = true;
619             }
620         }
621         $this->assertTrue($foundTable, "Could not find new table");
622     }
623
624     public function testListForeignKeysComposite()
625     {
626         if(!$this->_conn->getDatabasePlatform()->supportsForeignKeyConstraints()) {
627             $this->markTestSkipped('Does not support foreign key constraints.');
628         }
629
630         $this->_sm->createTable($this->getTestTable('test_create_fk3'));
631         $this->_sm->createTable($this->getTestCompositeTable('test_create_fk4'));
632
633         $foreignKey = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(
634             array('id', 'foreign_key_test'), 'test_create_fk4', array('id', 'other_id'), 'foreign_key_test_fk'
635         );
636
637         $this->_sm->createForeignKey($foreignKey, 'test_create_fk3');
638
639         $fkeys = $this->_sm->listTableForeignKeys('test_create_fk3');
640
641         $this->assertEquals(1, count($fkeys), "Table 'test_create_fk3' has to have one foreign key.");
642
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()));
646     }
647 }