3 namespace Doctrine\Tests\DBAL\Schema;
5 require_once __DIR__ . '/../../TestInit.php';
7 use Doctrine\DBAL\Schema\Schema;
8 use Doctrine\DBAL\Schema\Table;
9 use Doctrine\DBAL\Schema\Sequence;
11 class SchemaTest extends \PHPUnit_Framework_TestCase
13 public function testAddTable()
15 $tableName = "public.foo";
16 $table = new Table($tableName);
18 $schema = new Schema(array($table));
20 $this->assertTrue($schema->hasTable($tableName));
22 $tables = $schema->getTables();
23 $this->assertTrue( isset($tables[$tableName]) );
24 $this->assertSame($table, $tables[$tableName]);
25 $this->assertSame($table, $schema->getTable($tableName));
26 $this->assertTrue($schema->hasTable($tableName));
29 public function testTableMatchingCaseInsenstive()
31 $table = new Table("Foo");
33 $schema = new Schema(array($table));
34 $this->assertTrue($schema->hasTable("foo"));
35 $this->assertTrue($schema->hasTable("FOO"));
37 $this->assertSame($table, $schema->getTable('FOO'));
38 $this->assertSame($table, $schema->getTable('foo'));
39 $this->assertSame($table, $schema->getTable('Foo'));
42 public function testGetUnknownTableThrowsException()
44 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
46 $schema = new Schema();
47 $schema->getTable("unknown");
50 public function testCreateTableTwiceThrowsException()
52 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
55 $table = new Table($tableName);
56 $tables = array($table, $table);
58 $schema = new Schema($tables);
61 public function testRenameTable()
64 $table = new Table($tableName);
65 $schema = new Schema(array($table));
67 $this->assertTrue($schema->hasTable("foo"));
68 $schema->renameTable("foo", "bar");
69 $this->assertFalse($schema->hasTable("foo"));
70 $this->assertTrue($schema->hasTable("bar"));
71 $this->assertSame($table, $schema->getTable("bar"));
74 public function testDropTable()
77 $table = new Table($tableName);
78 $schema = new Schema(array($table));
80 $this->assertTrue($schema->hasTable("foo"));
82 $schema->dropTable("foo");
84 $this->assertFalse($schema->hasTable("foo"));
87 public function testCreateTable()
89 $schema = new Schema();
91 $this->assertFalse($schema->hasTable("foo"));
93 $table = $schema->createTable("foo");
95 $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table);
96 $this->assertEquals("foo", $table->getName());
97 $this->assertTrue($schema->hasTable("foo"));
100 public function testAddSequences()
102 $sequence = new Sequence("a_seq", 1, 1);
104 $schema = new Schema(array(), array($sequence));
106 $this->assertTrue($schema->hasSequence("a_seq"));
107 $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
109 $sequences = $schema->getSequences();
110 $this->assertArrayHasKey('public.a_seq', $sequences);
113 public function testSequenceAccessCaseInsensitive()
115 $sequence = new Sequence("a_Seq");
117 $schema = new Schema(array(), array($sequence));
118 $this->assertTrue($schema->hasSequence('a_seq'));
119 $this->assertTrue($schema->hasSequence('a_Seq'));
120 $this->assertTrue($schema->hasSequence('A_SEQ'));
122 $this->assertEquals($sequence, $schema->getSequence('a_seq'));
123 $this->assertEquals($sequence, $schema->getSequence('a_Seq'));
124 $this->assertEquals($sequence, $schema->getSequence('A_SEQ'));
127 public function testGetUnknownSequenceThrowsException()
129 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
131 $schema = new Schema();
132 $schema->getSequence("unknown");
135 public function testCreateSequence()
137 $schema = new Schema();
138 $sequence = $schema->createSequence('a_seq', 10, 20);
140 $this->assertEquals('a_seq', $sequence->getName());
141 $this->assertEquals(10, $sequence->getAllocationSize());
142 $this->assertEquals(20, $sequence->getInitialValue());
144 $this->assertTrue($schema->hasSequence("a_seq"));
145 $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
147 $sequences = $schema->getSequences();
148 $this->assertArrayHasKey('public.a_seq', $sequences);
151 public function testDropSequence()
153 $sequence = new Sequence("a_seq", 1, 1);
155 $schema = new Schema(array(), array($sequence));
157 $schema->dropSequence("a_seq");
158 $this->assertFalse($schema->hasSequence("a_seq"));
161 public function testAddSequenceTwiceThrowsException()
163 $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
165 $sequence = new Sequence("a_seq", 1, 1);
167 $schema = new Schema(array(), array($sequence, $sequence));
170 public function testConfigMaxIdentifierLength()
172 $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
173 $schemaConfig->setMaxIdentifierLength(5);
175 $schema = new Schema(array(), array(), $schemaConfig);
176 $table = $schema->createTable("smalltable");
177 $table->addColumn('long_id', 'integer');
178 $table->addIndex(array('long_id'));
180 $index = current($table->getIndexes());
181 $this->assertEquals(5, strlen($index->getName()));
184 public function testDeepClone()
186 $schema = new Schema();
187 $sequence = $schema->createSequence('baz');
189 $tableA = $schema->createTable('foo');
190 $tableA->addColumn('id', 'integer');
192 $tableB = $schema->createTable('bar');
193 $tableB->addColumn('id', 'integer');
194 $tableB->addColumn('foo_id', 'integer');
195 $tableB->addForeignKeyConstraint($tableA, array('foo_id'), array('id'));
197 $schemaNew = clone $schema;
199 $this->assertNotSame($sequence, $schemaNew->getSequence('baz'));
201 $this->assertNotSame($tableA, $schemaNew->getTable('foo'));
202 $this->assertNotSame($tableA->getColumn('id'), $schemaNew->getTable('foo')->getColumn('id'));
204 $this->assertNotSame($tableB, $schemaNew->getTable('bar'));
205 $this->assertNotSame($tableB->getColumn('id'), $schemaNew->getTable('bar')->getColumn('id'));
207 $fk = $schemaNew->getTable('bar')->getForeignKeys();
209 $this->assertSame($schemaNew->getTable('bar'), $this->readAttribute($fk, '_localTable'));
215 public function testHasTableForQuotedAsset()
217 $schema = new Schema();
219 $tableA = $schema->createTable('foo');
220 $tableA->addColumn('id', 'integer');
222 $this->assertTrue($schema->hasTable('`foo`'));