Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Schema / TableTest.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Schema;
4
5 use Doctrine\DBAL\Schema\Schema;
6 use Doctrine\DBAL\Schema\Table;
7 use Doctrine\DBAL\Schema\TableBuilder;
8 use Doctrine\DBAL\Schema\Column;
9 use Doctrine\DBAL\Schema\Index;
10 use Doctrine\DBAL\Schema\ForeignKeyConstraint;
11 use Doctrine\DBAL\Types\Type;
12
13 class TableTest extends \Doctrine\Tests\DbalTestCase
14 {
15     public function testCreateWithInvalidTableName()
16     {
17         $this->setExpectedException('Doctrine\DBAL\DBALException');
18         $table = new \Doctrine\DBAL\Schema\Table('');
19     }
20
21     public function testGetName()
22     {
23         $table =  new Table("foo", array(), array(), array());
24         $this->assertEquals("foo", $table->getName());
25     }
26
27     public function testColumns()
28     {
29         $type = Type::getType('integer');
30         $columns = array();
31         $columns[] = new Column("foo", $type);
32         $columns[] = new Column("bar", $type);
33         $table = new Table("foo", $columns, array(), array());
34
35         $this->assertTrue($table->hasColumn("foo"));
36         $this->assertTrue($table->hasColumn("bar"));
37         $this->assertFalse($table->hasColumn("baz"));
38
39         $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
40         $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));
41
42         $this->assertEquals(2, count($table->getColumns()));
43     }
44
45     public function testColumnsCaseInsensitive()
46     {
47         $table = new Table("foo");
48         $column = $table->addColumn('Foo', 'integer');
49
50         $this->assertTrue($table->hasColumn('Foo'));
51         $this->assertTrue($table->hasColumn('foo'));
52         $this->assertTrue($table->hasColumn('FOO'));
53
54         $this->assertSame($column, $table->getColumn('Foo'));
55         $this->assertSame($column, $table->getColumn('foo'));
56         $this->assertSame($column, $table->getColumn('FOO'));
57     }
58
59     public function testCreateColumn()
60     {
61         $type = Type::getType('integer');
62
63         $table = new Table("foo");
64
65         $this->assertFalse($table->hasColumn("bar"));
66         $table->addColumn("bar", 'integer');
67         $this->assertTrue($table->hasColumn("bar"));
68         $this->assertSame($type, $table->getColumn("bar")->getType());
69     }
70
71     public function testDropColumn()
72     {
73         $type = Type::getType('integer');
74         $columns = array();
75         $columns[] = new Column("foo", $type);
76         $columns[] = new Column("bar", $type);
77         $table = new Table("foo", $columns, array(), array());
78
79         $this->assertTrue($table->hasColumn("foo"));
80         $this->assertTrue($table->hasColumn("bar"));
81
82         $table->dropColumn("foo")->dropColumn("bar");
83
84         $this->assertFalse($table->hasColumn("foo"));
85         $this->assertFalse($table->hasColumn("bar"));
86     }
87
88     public function testGetUnknownColumnThrowsException()
89     {
90         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
91
92         $table = new Table("foo", array(), array(), array());
93         $table->getColumn('unknown');
94     }
95
96     public function testAddColumnTwiceThrowsException()
97     {
98         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
99
100         $type = \Doctrine\DBAL\Types\Type::getType('integer');
101         $columns = array();
102         $columns[] = new Column("foo", $type);
103         $columns[] = new Column("foo", $type);
104         $table = new Table("foo", $columns, array(), array());
105     }
106
107     public function testCreateIndex()
108     {
109         $type = \Doctrine\DBAL\Types\Type::getType('integer');
110         $columns = array(new Column("foo", $type), new Column("bar", $type), new Column("baz", $type));
111         $table = new Table("foo", $columns);
112
113         $table->addIndex(array("foo", "bar"), "foo_foo_bar_idx");
114         $table->addUniqueIndex(array("bar", "baz"), "foo_bar_baz_uniq");
115
116         $this->assertTrue($table->hasIndex("foo_foo_bar_idx"));
117         $this->assertTrue($table->hasIndex("foo_bar_baz_uniq"));
118     }
119
120     public function testIndexCaseInsensitive()
121     {
122         $type = \Doctrine\DBAL\Types\Type::getType('integer');
123         $columns = array(
124             new Column("foo", $type),
125             new Column("bar", $type),
126             new Column("baz", $type)
127         );
128         $table = new Table("foo", $columns);
129
130         $table->addIndex(array("foo", "bar", "baz"), "Foo_Idx");
131
132         $this->assertTrue($table->hasIndex('foo_idx'));
133         $this->assertTrue($table->hasIndex('Foo_Idx'));
134         $this->assertTrue($table->hasIndex('FOO_IDX'));
135     }
136
137     public function testAddIndexes()
138     {
139         $type = \Doctrine\DBAL\Types\Type::getType('integer');
140         $columns = array(
141             new Column("foo", $type),
142             new Column("bar", $type),
143         );
144         $indexes = array(
145             new Index("the_primary", array("foo"), true, true),
146             new Index("bar_idx", array("bar"), false, false),
147         );
148         $table = new Table("foo", $columns, $indexes, array());
149
150         $this->assertTrue($table->hasIndex("the_primary"));
151         $this->assertTrue($table->hasIndex("bar_idx"));
152         $this->assertFalse($table->hasIndex("some_idx"));
153
154         $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
155         $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
156         $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('bar_idx'));
157     }
158
159     public function testGetUnknownIndexThrowsException()
160     {
161         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
162
163         $table = new Table("foo", array(), array(), array());
164         $table->getIndex("unknownIndex");
165     }
166
167     public function testAddTwoPrimaryThrowsException()
168     {
169         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
170
171         $type = \Doctrine\DBAL\Types\Type::getType('integer');
172         $columns = array(new Column("foo", $type), new Column("bar", $type));
173         $indexes = array(
174             new Index("the_primary", array("foo"), true, true),
175             new Index("other_primary", array("bar"), true, true),
176         );
177         $table = new Table("foo", $columns, $indexes, array());
178     }
179
180     public function testAddTwoIndexesWithSameNameThrowsException()
181     {
182         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
183
184         $type = \Doctrine\DBAL\Types\Type::getType('integer');
185         $columns = array(new Column("foo", $type), new Column("bar", $type));
186         $indexes = array(
187             new Index("an_idx", array("foo"), false, false),
188             new Index("an_idx", array("bar"), false, false),
189         );
190         $table = new Table("foo", $columns, $indexes, array());
191     }
192
193     public function testConstraints()
194     {
195         $constraint = new ForeignKeyConstraint(array(), "foo", array());
196
197         $tableA = new Table("foo", array(), array(), array($constraint));
198         $constraints = $tableA->getForeignKeys();
199
200         $this->assertEquals(1, count($constraints));
201         $this->assertSame($constraint, array_shift($constraints));
202     }
203
204     public function testOptions()
205     {
206         $table = new Table("foo", array(), array(), array(), false, array("foo" => "bar"));
207
208         $this->assertTrue($table->hasOption("foo"));
209         $this->assertEquals("bar", $table->getOption("foo"));
210     }
211
212     public function testBuilderSetPrimaryKey()
213     {
214         $table = new Table("foo");
215
216         $table->addColumn("bar", 'integer');
217         $table->setPrimaryKey(array("bar"));
218
219         $this->assertTrue($table->hasIndex("primary"));
220         $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
221         $this->assertTrue($table->getIndex("primary")->isUnique());
222         $this->assertTrue($table->getIndex("primary")->isPrimary());
223     }
224
225     public function testBuilderAddUniqueIndex()
226     {
227         $table = new Table("foo");
228
229         $table->addColumn("bar", 'integer');
230         $table->addUniqueIndex(array("bar"), "my_idx");
231
232         $this->assertTrue($table->hasIndex("my_idx"));
233         $this->assertTrue($table->getIndex("my_idx")->isUnique());
234         $this->assertFalse($table->getIndex("my_idx")->isPrimary());
235     }
236
237     public function testBuilderAddIndex()
238     {
239         $table = new Table("foo");
240
241         $table->addColumn("bar", 'integer');
242         $table->addIndex(array("bar"), "my_idx");
243
244         $this->assertTrue($table->hasIndex("my_idx"));
245         $this->assertFalse($table->getIndex("my_idx")->isUnique());
246         $this->assertFalse($table->getIndex("my_idx")->isPrimary());
247     }
248
249     public function testBuilderAddIndexWithInvalidNameThrowsException()
250     {
251         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
252
253         $table = new Table("foo");
254         $table->addColumn("bar",'integer');
255         $table->addIndex(array("bar"), "invalid name %&/");
256     }
257
258     public function testBuilderAddIndexWithUnknownColumnThrowsException()
259     {
260         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
261
262         $table = new Table("foo");
263         $table->addIndex(array("bar"), "invalidName");
264     }
265
266     public function testBuilderOptions()
267     {
268         $table = new Table("foo");
269         $table->addOption("foo", "bar");
270         $this->assertTrue($table->hasOption("foo"));
271         $this->assertEquals("bar", $table->getOption("foo"));
272     }
273
274     public function testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()
275     {
276         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
277
278         $table = new Table("foo");
279         $table->addColumn("id", 'integer');
280
281         $foreignTable = new Table("bar");
282         $foreignTable->addColumn("id", 'integer');
283
284         $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
285     }
286
287     public function testAddForeignKeyConstraint_UnknownForeignColumn_ThrowsException()
288     {
289         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
290
291         $table = new Table("foo");
292         $table->addColumn("id", 'integer');
293
294         $foreignTable = new Table("bar");
295         $foreignTable->addColumn("id", 'integer');
296
297         $table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
298     }
299
300     public function testAddForeignKeyConstraint()
301     {
302         $table = new Table("foo");
303         $table->addColumn("id", 'integer');
304
305         $foreignTable = new Table("bar");
306         $foreignTable->addColumn("id", 'integer');
307
308         $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
309
310         $constraints = $table->getForeignKeys();
311         $this->assertEquals(1, count($constraints));
312         $constraint = current($constraints);
313
314         $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraint);
315
316         $this->assertTrue($constraint->hasOption("foo"));
317         $this->assertEquals("bar", $constraint->getOption("foo"));
318     }
319
320     public function testAddIndexWithCaseSensitiveColumnProblem()
321     {
322         $table = new Table("foo");
323         $table->addColumn("id", 'integer');
324
325         $table->addIndex(array("ID"), "my_idx");
326
327         $this->assertTrue($table->hasIndex('my_idx'));
328         $this->assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns());
329         $this->assertTrue($table->getIndex('my_idx')->spansColumns(array('id')));
330     }
331
332     public function testAddPrimaryKey_ColumnsAreExplicitlySetToNotNull()
333     {
334         $table = new Table("foo");
335         $column = $table->addColumn("id", 'integer', array('notnull' => false));
336
337         $this->assertFalse($column->getNotnull());
338
339         $table->setPrimaryKey(array('id'));
340
341         $this->assertTrue($column->getNotnull());
342     }
343
344     /**
345      * @group DDC-133
346      */
347     public function testAllowImplicitSchemaTableInAutogeneratedIndexNames()
348     {
349         $table = new Table("foo.bar");
350         $table->addColumn('baz', 'integer', array());
351         $table->addIndex(array('baz'));
352
353         $this->assertEquals(1, count($table->getIndexes()));
354     }
355
356     /**
357      * @group DBAL-50
358      */
359     public function testAddIndexTwice_IgnoreSecond()
360     {
361         $table = new Table("foo.bar");
362         $table->addColumn('baz', 'integer', array());
363         $table->addIndex(array('baz'));
364         $table->addIndex(array('baz'));
365
366         $this->assertEquals(1, count($table->getIndexes()));
367     }
368
369     /**
370      * @group DBAL-50
371      */
372     public function testAddForeignKeyIndexImplicitly()
373     {
374         $table = new Table("foo");
375         $table->addColumn("id", 'integer');
376
377         $foreignTable = new Table("bar");
378         $foreignTable->addColumn("id", 'integer');
379
380         $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
381
382         $indexes = $table->getIndexes();
383         $this->assertEquals(1, count($indexes));
384         $index = current($indexes);
385
386         $this->assertTrue($table->hasIndex($index->getName()));
387         $this->assertEquals(array('id'), $index->getColumns());
388     }
389
390     /**
391      * @group DBAL-50
392      */
393     public function testOverruleIndex()
394     {
395         $table = new Table("bar");
396         $table->addColumn('baz', 'integer', array());
397         $table->addIndex(array('baz'));
398
399         $indexes = $table->getIndexes();
400         $this->assertEquals(1, count($indexes));
401         $index = current($indexes);
402
403         $table->addUniqueIndex(array('baz'));
404         $this->assertEquals(1, count($table->getIndexes()));
405         $this->assertFalse($table->hasIndex($index->getName()));
406     }
407
408     public function testPrimaryKeyOverrulesUniqueIndex()
409     {
410         $table = new Table("bar");
411         $table->addColumn('baz', 'integer', array());
412         $table->addUniqueIndex(array('baz'));
413
414         $table->setPrimaryKey(array('baz'));
415
416         $indexes = $table->getIndexes();
417         $this->assertEquals(1, count($indexes), "Table should only contain the primary key table index, not the unique one anymore, because it was overruled.");
418
419         $index = current($indexes);
420         $this->assertTrue($index->isPrimary());
421     }
422
423     /**
424      * @group DBAL-64
425      */
426     public function testQuotedTableName()
427     {
428         $table = new Table("`bar`");
429
430         $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
431         $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
432
433         $this->assertEquals('bar', $table->getName());
434         $this->assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
435         $this->assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
436     }
437
438     /**
439      * @group DBAL-79
440      */
441     public function testTableHasPrimaryKey()
442     {
443         $table = new Table("test");
444
445         $this->assertFalse($table->hasPrimaryKey());
446
447         $table->addColumn("foo", "integer");
448         $table->setPrimaryKey(array("foo"));
449
450         $this->assertTrue($table->hasPrimaryKey());
451     }
452
453     /**
454      * @group DBAL-91
455      */
456     public function testAddIndexWithQuotedColumns()
457     {
458         $table = new Table("test");
459         $table->addColumn('"foo"', 'integer');
460         $table->addColumn('bar', 'integer');
461         $table->addIndex(array('"foo"', '"bar"'));
462     }
463
464     /**
465      * @group DBAL-91
466      */
467     public function testAddForeignKeyWithQuotedColumnsAndTable()
468     {
469         $table = new Table("test");
470         $table->addColumn('"foo"', 'integer');
471         $table->addColumn('bar', 'integer');
472         $table->addForeignKeyConstraint('"boing"', array('"foo"', '"bar"'), array("id"));
473     }
474
475     /**
476      * @group DBAL-177
477      */
478     public function testQuoteSchemaPrefixed()
479     {
480         $table = new Table("`test`.`test`");
481         $this->assertEquals("test.test", $table->getName());
482         $this->assertEquals("`test`.`test`", $table->getQuotedName(new \Doctrine\DBAL\Platforms\MySqlPlatform));
483     }
484
485     /**
486      * @group DBAL-204
487      */
488     public function testFullQualifiedTableName()
489     {
490         $table = new Table("`test`.`test`");
491         $this->assertEquals('test.test', $table->getFullQualifiedName("test"));
492         $this->assertEquals('test.test', $table->getFullQualifiedName("other"));
493
494         $table = new Table("test");
495         $this->assertEquals('test.test', $table->getFullQualifiedName("test"));
496         $this->assertEquals('other.test', $table->getFullQualifiedName("other"));
497     }
498
499     /**
500      * @group DBAL-224
501      */
502     public function testDropIndex()
503     {
504         $table = new Table("test");
505         $table->addColumn('id', 'integer');
506         $table->addIndex(array('id'), 'idx');
507
508         $this->assertTrue($table->hasIndex('idx'));
509
510         $table->dropIndex('idx');
511         $this->assertFalse($table->hasIndex('idx'));
512     }
513
514     /**
515      * @group DBAL-224
516      */
517     public function testDropPrimaryKey()
518     {
519         $table = new Table("test");
520         $table->addColumn('id', 'integer');
521         $table->setPrimaryKey(array('id'));
522
523         $this->assertTrue($table->hasPrimaryKey());
524
525         $table->dropPrimaryKey();
526         $this->assertFalse($table->hasPrimaryKey());
527     }
528 }