Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Schema / SchemaTest.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Schema;
4
5 require_once __DIR__ . '/../../TestInit.php';
6
7 use Doctrine\DBAL\Schema\Schema;
8 use Doctrine\DBAL\Schema\Table;
9 use Doctrine\DBAL\Schema\Sequence;
10
11 class SchemaTest extends \PHPUnit_Framework_TestCase
12 {
13     public function testAddTable()
14     {
15         $tableName = "public.foo";
16         $table = new Table($tableName);
17
18         $schema = new Schema(array($table));
19
20         $this->assertTrue($schema->hasTable($tableName));
21
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));
27     }
28
29     public function testTableMatchingCaseInsenstive()
30     {
31         $table = new Table("Foo");
32
33         $schema = new Schema(array($table));
34         $this->assertTrue($schema->hasTable("foo"));
35         $this->assertTrue($schema->hasTable("FOO"));
36
37         $this->assertSame($table, $schema->getTable('FOO'));
38         $this->assertSame($table, $schema->getTable('foo'));
39         $this->assertSame($table, $schema->getTable('Foo'));
40     }
41
42     public function testGetUnknownTableThrowsException()
43     {
44         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
45
46         $schema = new Schema();
47         $schema->getTable("unknown");
48     }
49
50     public function testCreateTableTwiceThrowsException()
51     {
52         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
53
54         $tableName = "foo";
55         $table = new Table($tableName);
56         $tables = array($table, $table);
57
58         $schema = new Schema($tables);
59     }
60
61     public function testRenameTable()
62     {
63         $tableName = "foo";
64         $table = new Table($tableName);
65         $schema = new Schema(array($table));
66
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"));
72     }
73
74     public function testDropTable()
75     {
76         $tableName = "foo";
77         $table = new Table($tableName);
78         $schema = new Schema(array($table));
79
80         $this->assertTrue($schema->hasTable("foo"));
81
82         $schema->dropTable("foo");
83
84         $this->assertFalse($schema->hasTable("foo"));
85     }
86
87     public function testCreateTable()
88     {
89         $schema = new Schema();
90
91         $this->assertFalse($schema->hasTable("foo"));
92
93         $table = $schema->createTable("foo");
94
95         $this->assertInstanceOf('Doctrine\DBAL\Schema\Table', $table);
96         $this->assertEquals("foo", $table->getName());
97         $this->assertTrue($schema->hasTable("foo"));
98     }
99
100     public function testAddSequences()
101     {
102         $sequence = new Sequence("a_seq", 1, 1);
103
104         $schema = new Schema(array(), array($sequence));
105
106         $this->assertTrue($schema->hasSequence("a_seq"));
107         $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
108
109         $sequences = $schema->getSequences();
110         $this->assertArrayHasKey('public.a_seq', $sequences);
111     }
112
113     public function testSequenceAccessCaseInsensitive()
114     {
115         $sequence = new Sequence("a_Seq");
116
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'));
121
122         $this->assertEquals($sequence, $schema->getSequence('a_seq'));
123         $this->assertEquals($sequence, $schema->getSequence('a_Seq'));
124         $this->assertEquals($sequence, $schema->getSequence('A_SEQ'));
125     }
126
127     public function testGetUnknownSequenceThrowsException()
128     {
129         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
130
131         $schema = new Schema();
132         $schema->getSequence("unknown");
133     }
134
135     public function testCreateSequence()
136     {
137         $schema = new Schema();
138         $sequence = $schema->createSequence('a_seq', 10, 20);
139
140         $this->assertEquals('a_seq', $sequence->getName());
141         $this->assertEquals(10, $sequence->getAllocationSize());
142         $this->assertEquals(20, $sequence->getInitialValue());
143
144         $this->assertTrue($schema->hasSequence("a_seq"));
145         $this->assertInstanceOf('Doctrine\DBAL\Schema\Sequence', $schema->getSequence("a_seq"));
146
147         $sequences = $schema->getSequences();
148         $this->assertArrayHasKey('public.a_seq', $sequences);
149     }
150
151     public function testDropSequence()
152     {
153         $sequence = new Sequence("a_seq", 1, 1);
154
155         $schema = new Schema(array(), array($sequence));
156
157         $schema->dropSequence("a_seq");
158         $this->assertFalse($schema->hasSequence("a_seq"));
159     }
160
161     public function testAddSequenceTwiceThrowsException()
162     {
163         $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
164
165         $sequence = new Sequence("a_seq", 1, 1);
166
167         $schema = new Schema(array(), array($sequence, $sequence));
168     }
169
170     public function testConfigMaxIdentifierLength()
171     {
172         $schemaConfig = new \Doctrine\DBAL\Schema\SchemaConfig();
173         $schemaConfig->setMaxIdentifierLength(5);
174
175         $schema = new Schema(array(), array(), $schemaConfig);
176         $table = $schema->createTable("smalltable");
177         $table->addColumn('long_id', 'integer');
178         $table->addIndex(array('long_id'));
179
180         $index = current($table->getIndexes());
181         $this->assertEquals(5, strlen($index->getName()));
182     }
183
184     public function testDeepClone()
185     {
186         $schema = new Schema();
187         $sequence = $schema->createSequence('baz');
188
189         $tableA = $schema->createTable('foo');
190         $tableA->addColumn('id', 'integer');
191
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'));
196
197         $schemaNew = clone $schema;
198
199         $this->assertNotSame($sequence, $schemaNew->getSequence('baz'));
200
201         $this->assertNotSame($tableA, $schemaNew->getTable('foo'));
202         $this->assertNotSame($tableA->getColumn('id'), $schemaNew->getTable('foo')->getColumn('id'));
203
204         $this->assertNotSame($tableB, $schemaNew->getTable('bar'));
205         $this->assertNotSame($tableB->getColumn('id'), $schemaNew->getTable('bar')->getColumn('id'));
206
207         $fk = $schemaNew->getTable('bar')->getForeignKeys();
208         $fk = current($fk);
209         $this->assertSame($schemaNew->getTable('bar'), $this->readAttribute($fk, '_localTable'));
210     }
211
212     /**
213      * @group DBAL-219
214      */
215     public function testHasTableForQuotedAsset()
216     {
217         $schema = new Schema();
218
219         $tableA = $schema->createTable('foo');
220         $tableA->addColumn('id', 'integer');
221
222         $this->assertTrue($schema->hasTable('`foo`'));
223     }
224 }