3 namespace Doctrine\Tests\DBAL\Schema;
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;
13 class TableTest extends \Doctrine\Tests\DbalTestCase
15 public function testCreateWithInvalidTableName()
17 $this->setExpectedException('Doctrine\DBAL\DBALException');
18 $table = new \Doctrine\DBAL\Schema\Table('');
21 public function testGetName()
23 $table = new Table("foo", array(), array(), array());
24 $this->assertEquals("foo", $table->getName());
27 public function testColumns()
29 $type = Type::getType('integer');
31 $columns[] = new Column("foo", $type);
32 $columns[] = new Column("bar", $type);
33 $table = new Table("foo", $columns, array(), array());
35 $this->assertTrue($table->hasColumn("foo"));
36 $this->assertTrue($table->hasColumn("bar"));
37 $this->assertFalse($table->hasColumn("baz"));
39 $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
40 $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));
42 $this->assertEquals(2, count($table->getColumns()));
45 public function testColumnsCaseInsensitive()
47 $table = new Table("foo");
48 $column = $table->addColumn('Foo', 'integer');
50 $this->assertTrue($table->hasColumn('Foo'));
51 $this->assertTrue($table->hasColumn('foo'));
52 $this->assertTrue($table->hasColumn('FOO'));
54 $this->assertSame($column, $table->getColumn('Foo'));
55 $this->assertSame($column, $table->getColumn('foo'));
56 $this->assertSame($column, $table->getColumn('FOO'));
59 public function testCreateColumn()
61 $type = Type::getType('integer');
63 $table = new Table("foo");
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());
71 public function testDropColumn()
73 $type = Type::getType('integer');
75 $columns[] = new Column("foo", $type);
76 $columns[] = new Column("bar", $type);
77 $table = new Table("foo", $columns, array(), array());
79 $this->assertTrue($table->hasColumn("foo"));
80 $this->assertTrue($table->hasColumn("bar"));
82 $table->dropColumn("foo")->dropColumn("bar");
84 $this->assertFalse($table->hasColumn("foo"));
85 $this->assertFalse($table->hasColumn("bar"));
88 public function testGetUnknownColumnThrowsException()
90 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
92 $table = new Table("foo", array(), array(), array());
93 $table->getColumn('unknown');
96 public function testAddColumnTwiceThrowsException()
98 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
100 $type = \Doctrine\DBAL\Types\Type::getType('integer');
102 $columns[] = new Column("foo", $type);
103 $columns[] = new Column("foo", $type);
104 $table = new Table("foo", $columns, array(), array());
107 public function testCreateIndex()
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);
113 $table->addIndex(array("foo", "bar"), "foo_foo_bar_idx");
114 $table->addUniqueIndex(array("bar", "baz"), "foo_bar_baz_uniq");
116 $this->assertTrue($table->hasIndex("foo_foo_bar_idx"));
117 $this->assertTrue($table->hasIndex("foo_bar_baz_uniq"));
120 public function testIndexCaseInsensitive()
122 $type = \Doctrine\DBAL\Types\Type::getType('integer');
124 new Column("foo", $type),
125 new Column("bar", $type),
126 new Column("baz", $type)
128 $table = new Table("foo", $columns);
130 $table->addIndex(array("foo", "bar", "baz"), "Foo_Idx");
132 $this->assertTrue($table->hasIndex('foo_idx'));
133 $this->assertTrue($table->hasIndex('Foo_Idx'));
134 $this->assertTrue($table->hasIndex('FOO_IDX'));
137 public function testAddIndexes()
139 $type = \Doctrine\DBAL\Types\Type::getType('integer');
141 new Column("foo", $type),
142 new Column("bar", $type),
145 new Index("the_primary", array("foo"), true, true),
146 new Index("bar_idx", array("bar"), false, false),
148 $table = new Table("foo", $columns, $indexes, array());
150 $this->assertTrue($table->hasIndex("the_primary"));
151 $this->assertTrue($table->hasIndex("bar_idx"));
152 $this->assertFalse($table->hasIndex("some_idx"));
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'));
159 public function testGetUnknownIndexThrowsException()
161 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
163 $table = new Table("foo", array(), array(), array());
164 $table->getIndex("unknownIndex");
167 public function testAddTwoPrimaryThrowsException()
169 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
171 $type = \Doctrine\DBAL\Types\Type::getType('integer');
172 $columns = array(new Column("foo", $type), new Column("bar", $type));
174 new Index("the_primary", array("foo"), true, true),
175 new Index("other_primary", array("bar"), true, true),
177 $table = new Table("foo", $columns, $indexes, array());
180 public function testAddTwoIndexesWithSameNameThrowsException()
182 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
184 $type = \Doctrine\DBAL\Types\Type::getType('integer');
185 $columns = array(new Column("foo", $type), new Column("bar", $type));
187 new Index("an_idx", array("foo"), false, false),
188 new Index("an_idx", array("bar"), false, false),
190 $table = new Table("foo", $columns, $indexes, array());
193 public function testConstraints()
195 $constraint = new ForeignKeyConstraint(array(), "foo", array());
197 $tableA = new Table("foo", array(), array(), array($constraint));
198 $constraints = $tableA->getForeignKeys();
200 $this->assertEquals(1, count($constraints));
201 $this->assertSame($constraint, array_shift($constraints));
204 public function testOptions()
206 $table = new Table("foo", array(), array(), array(), false, array("foo" => "bar"));
208 $this->assertTrue($table->hasOption("foo"));
209 $this->assertEquals("bar", $table->getOption("foo"));
212 public function testBuilderSetPrimaryKey()
214 $table = new Table("foo");
216 $table->addColumn("bar", 'integer');
217 $table->setPrimaryKey(array("bar"));
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());
225 public function testBuilderAddUniqueIndex()
227 $table = new Table("foo");
229 $table->addColumn("bar", 'integer');
230 $table->addUniqueIndex(array("bar"), "my_idx");
232 $this->assertTrue($table->hasIndex("my_idx"));
233 $this->assertTrue($table->getIndex("my_idx")->isUnique());
234 $this->assertFalse($table->getIndex("my_idx")->isPrimary());
237 public function testBuilderAddIndex()
239 $table = new Table("foo");
241 $table->addColumn("bar", 'integer');
242 $table->addIndex(array("bar"), "my_idx");
244 $this->assertTrue($table->hasIndex("my_idx"));
245 $this->assertFalse($table->getIndex("my_idx")->isUnique());
246 $this->assertFalse($table->getIndex("my_idx")->isPrimary());
249 public function testBuilderAddIndexWithInvalidNameThrowsException()
251 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
253 $table = new Table("foo");
254 $table->addColumn("bar",'integer');
255 $table->addIndex(array("bar"), "invalid name %&/");
258 public function testBuilderAddIndexWithUnknownColumnThrowsException()
260 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
262 $table = new Table("foo");
263 $table->addIndex(array("bar"), "invalidName");
266 public function testBuilderOptions()
268 $table = new Table("foo");
269 $table->addOption("foo", "bar");
270 $this->assertTrue($table->hasOption("foo"));
271 $this->assertEquals("bar", $table->getOption("foo"));
274 public function testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()
276 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
278 $table = new Table("foo");
279 $table->addColumn("id", 'integer');
281 $foreignTable = new Table("bar");
282 $foreignTable->addColumn("id", 'integer');
284 $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
287 public function testAddForeignKeyConstraint_UnknownForeignColumn_ThrowsException()
289 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
291 $table = new Table("foo");
292 $table->addColumn("id", 'integer');
294 $foreignTable = new Table("bar");
295 $foreignTable->addColumn("id", 'integer');
297 $table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
300 public function testAddForeignKeyConstraint()
302 $table = new Table("foo");
303 $table->addColumn("id", 'integer');
305 $foreignTable = new Table("bar");
306 $foreignTable->addColumn("id", 'integer');
308 $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
310 $constraints = $table->getForeignKeys();
311 $this->assertEquals(1, count($constraints));
312 $constraint = current($constraints);
314 $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraint);
316 $this->assertTrue($constraint->hasOption("foo"));
317 $this->assertEquals("bar", $constraint->getOption("foo"));
320 public function testAddIndexWithCaseSensitiveColumnProblem()
322 $table = new Table("foo");
323 $table->addColumn("id", 'integer');
325 $table->addIndex(array("ID"), "my_idx");
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')));
332 public function testAddPrimaryKey_ColumnsAreExplicitlySetToNotNull()
334 $table = new Table("foo");
335 $column = $table->addColumn("id", 'integer', array('notnull' => false));
337 $this->assertFalse($column->getNotnull());
339 $table->setPrimaryKey(array('id'));
341 $this->assertTrue($column->getNotnull());
347 public function testAllowImplicitSchemaTableInAutogeneratedIndexNames()
349 $table = new Table("foo.bar");
350 $table->addColumn('baz', 'integer', array());
351 $table->addIndex(array('baz'));
353 $this->assertEquals(1, count($table->getIndexes()));
359 public function testAddIndexTwice_IgnoreSecond()
361 $table = new Table("foo.bar");
362 $table->addColumn('baz', 'integer', array());
363 $table->addIndex(array('baz'));
364 $table->addIndex(array('baz'));
366 $this->assertEquals(1, count($table->getIndexes()));
372 public function testAddForeignKeyIndexImplicitly()
374 $table = new Table("foo");
375 $table->addColumn("id", 'integer');
377 $foreignTable = new Table("bar");
378 $foreignTable->addColumn("id", 'integer');
380 $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
382 $indexes = $table->getIndexes();
383 $this->assertEquals(1, count($indexes));
384 $index = current($indexes);
386 $this->assertTrue($table->hasIndex($index->getName()));
387 $this->assertEquals(array('id'), $index->getColumns());
393 public function testOverruleIndex()
395 $table = new Table("bar");
396 $table->addColumn('baz', 'integer', array());
397 $table->addIndex(array('baz'));
399 $indexes = $table->getIndexes();
400 $this->assertEquals(1, count($indexes));
401 $index = current($indexes);
403 $table->addUniqueIndex(array('baz'));
404 $this->assertEquals(1, count($table->getIndexes()));
405 $this->assertFalse($table->hasIndex($index->getName()));
408 public function testPrimaryKeyOverrulesUniqueIndex()
410 $table = new Table("bar");
411 $table->addColumn('baz', 'integer', array());
412 $table->addUniqueIndex(array('baz'));
414 $table->setPrimaryKey(array('baz'));
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.");
419 $index = current($indexes);
420 $this->assertTrue($index->isPrimary());
426 public function testQuotedTableName()
428 $table = new Table("`bar`");
430 $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
431 $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
433 $this->assertEquals('bar', $table->getName());
434 $this->assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
435 $this->assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
441 public function testTableHasPrimaryKey()
443 $table = new Table("test");
445 $this->assertFalse($table->hasPrimaryKey());
447 $table->addColumn("foo", "integer");
448 $table->setPrimaryKey(array("foo"));
450 $this->assertTrue($table->hasPrimaryKey());
456 public function testAddIndexWithQuotedColumns()
458 $table = new Table("test");
459 $table->addColumn('"foo"', 'integer');
460 $table->addColumn('bar', 'integer');
461 $table->addIndex(array('"foo"', '"bar"'));
467 public function testAddForeignKeyWithQuotedColumnsAndTable()
469 $table = new Table("test");
470 $table->addColumn('"foo"', 'integer');
471 $table->addColumn('bar', 'integer');
472 $table->addForeignKeyConstraint('"boing"', array('"foo"', '"bar"'), array("id"));
478 public function testQuoteSchemaPrefixed()
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));
488 public function testFullQualifiedTableName()
490 $table = new Table("`test`.`test`");
491 $this->assertEquals('test.test', $table->getFullQualifiedName("test"));
492 $this->assertEquals('test.test', $table->getFullQualifiedName("other"));
494 $table = new Table("test");
495 $this->assertEquals('test.test', $table->getFullQualifiedName("test"));
496 $this->assertEquals('other.test', $table->getFullQualifiedName("other"));
502 public function testDropIndex()
504 $table = new Table("test");
505 $table->addColumn('id', 'integer');
506 $table->addIndex(array('id'), 'idx');
508 $this->assertTrue($table->hasIndex('idx'));
510 $table->dropIndex('idx');
511 $this->assertFalse($table->hasIndex('idx'));
517 public function testDropPrimaryKey()
519 $table = new Table("test");
520 $table->addColumn('id', 'integer');
521 $table->setPrimaryKey(array('id'));
523 $this->assertTrue($table->hasPrimaryKey());
525 $table->dropPrimaryKey();
526 $this->assertFalse($table->hasPrimaryKey());