Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / tests / Doctrine / Tests / DBAL / Platforms / AbstractPlatformTestCase.php
1 <?php
2
3 namespace Doctrine\Tests\DBAL\Platforms;
4
5 use Doctrine\Common\EventManager;
6 use Doctrine\DBAL\Events;
7
8 abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
9 {
10     /**
11      * @var Doctrine\DBAL\Platforms\AbstractPlatform
12      */
13     protected $_platform;
14
15     abstract public function createPlatform();
16
17     public function setUp()
18     {
19         $this->_platform = $this->createPlatform();
20     }
21
22     /**
23      * @group DDC-1360
24      */
25     public function testQuoteIdentifier()
26     {
27         if ($this->_platform->getName() == "mssql") {
28             $this->markTestSkipped('Not working this way on mssql.');
29         }
30
31         $c = $this->_platform->getIdentifierQuoteCharacter();
32         $this->assertEquals($c."test".$c, $this->_platform->quoteIdentifier("test"));
33         $this->assertEquals($c."test".$c.".".$c."test".$c, $this->_platform->quoteIdentifier("test.test"));
34         $this->assertEquals(str_repeat($c, 4), $this->_platform->quoteIdentifier($c));
35     }
36
37     /**
38      * @group DDC-1360
39      */
40     public function testQuoteSingleIdentifier()
41     {
42         if ($this->_platform->getName() == "mssql") {
43             $this->markTestSkipped('Not working this way on mssql.');
44         }
45
46         $c = $this->_platform->getIdentifierQuoteCharacter();
47         $this->assertEquals($c."test".$c, $this->_platform->quoteSingleIdentifier("test"));
48         $this->assertEquals($c."test.test".$c, $this->_platform->quoteSingleIdentifier("test.test"));
49         $this->assertEquals(str_repeat($c, 4), $this->_platform->quoteSingleIdentifier($c));
50     }
51
52     public function testGetInvalidtForeignKeyReferentialActionSQL()
53     {
54         $this->setExpectedException('InvalidArgumentException');
55         $this->_platform->getForeignKeyReferentialActionSQL('unknown');
56     }
57
58     public function testGetUnknownDoctrineMappingType()
59     {
60         $this->setExpectedException('Doctrine\DBAL\DBALException');
61         $this->_platform->getDoctrineTypeMapping('foobar');
62     }
63
64     public function testRegisterDoctrineMappingType()
65     {
66         $this->_platform->registerDoctrineTypeMapping('foo', 'integer');
67         $this->assertEquals('integer', $this->_platform->getDoctrineTypeMapping('foo'));
68     }
69
70     public function testRegisterUnknownDoctrineMappingType()
71     {
72         $this->setExpectedException('Doctrine\DBAL\DBALException');
73         $this->_platform->registerDoctrineTypeMapping('foo', 'bar');
74     }
75
76     public function testCreateWithNoColumns()
77     {
78         $table = new \Doctrine\DBAL\Schema\Table('test');
79
80         $this->setExpectedException('Doctrine\DBAL\DBALException');
81         $sql = $this->_platform->getCreateTableSQL($table);
82     }
83
84     public function testGeneratesTableCreationSql()
85     {
86         $table = new \Doctrine\DBAL\Schema\Table('test');
87         $table->addColumn('id', 'integer', array('notnull' => true, 'autoincrement' => true));
88         $table->addColumn('test', 'string', array('notnull' => false, 'length' => 255));
89         $table->setPrimaryKey(array('id'));
90
91         $sql = $this->_platform->getCreateTableSQL($table);
92         $this->assertEquals($this->getGenerateTableSql(), $sql[0]);
93     }
94
95     abstract public function getGenerateTableSql();
96
97     public function testGenerateTableWithMultiColumnUniqueIndex()
98     {
99         $table = new \Doctrine\DBAL\Schema\Table('test');
100         $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
101         $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
102         $table->addUniqueIndex(array("foo", "bar"));
103
104         $sql = $this->_platform->getCreateTableSQL($table);
105         $this->assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
106     }
107
108     abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();
109
110     public function testGeneratesIndexCreationSql()
111     {
112         $indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));
113
114         $this->assertEquals(
115             $this->getGenerateIndexSql(),
116             $this->_platform->getCreateIndexSQL($indexDef, 'mytable')
117         );
118     }
119
120     abstract public function getGenerateIndexSql();
121
122     public function testGeneratesUniqueIndexCreationSql()
123     {
124         $indexDef = new \Doctrine\DBAL\Schema\Index('index_name', array('test', 'test2'), true);
125
126         $sql = $this->_platform->getCreateIndexSQL($indexDef, 'test');
127         $this->assertEquals($this->getGenerateUniqueIndexSql(), $sql);
128     }
129
130     abstract public function getGenerateUniqueIndexSql();
131
132     public function testGeneratesForeignKeyCreationSql()
133     {
134         $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name_id'), 'other_table', array('id'), '');
135
136         $sql = $this->_platform->getCreateForeignKeySQL($fk, 'test');
137         $this->assertEquals($sql, $this->getGenerateForeignKeySql());
138     }
139
140     abstract public function getGenerateForeignKeySql();
141
142     public function testGeneratesConstraintCreationSql()
143     {
144         $idx = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, false);
145         $sql = $this->_platform->getCreateConstraintSQL($idx, 'test');
146         $this->assertEquals($this->getGenerateConstraintUniqueIndexSql(), $sql);
147
148         $pk = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, true);
149         $sql = $this->_platform->getCreateConstraintSQL($pk, 'test');
150         $this->assertEquals($this->getGenerateConstraintPrimaryIndexSql(), $sql);
151
152         $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');
153         $sql = $this->_platform->getCreateConstraintSQL($fk, 'test');
154         $this->assertEquals($this->getGenerateConstraintForeignKeySql(), $sql);
155     }
156
157     protected function getBitAndComparisonExpressionSql($value1, $value2)
158     {
159         return '(' . $value1 . ' & ' . $value2 . ')';
160     }
161
162     /**
163      * @group DDC-1213
164      */
165     public function testGeneratesBitAndComparisonExpressionSql()
166     {
167         $sql = $this->_platform->getBitAndComparisonExpression(2, 4);
168         $this->assertEquals($this->getBitAndComparisonExpressionSql(2, 4), $sql);
169     }
170
171     protected  function getBitOrComparisonExpressionSql($value1, $value2)
172     {
173         return '(' . $value1 . ' | ' . $value2 . ')';
174     }
175
176     /**
177      * @group DDC-1213
178      */
179     public function testGeneratesBitOrComparisonExpressionSql()
180     {
181         $sql = $this->_platform->getBitOrComparisonExpression(2, 4);
182         $this->assertEquals($this->getBitOrComparisonExpressionSql(2, 4), $sql);
183     }
184
185     public function getGenerateConstraintUniqueIndexSql()
186     {
187         return 'ALTER TABLE test ADD CONSTRAINT constraint_name UNIQUE (test)';
188     }
189
190     public function getGenerateConstraintPrimaryIndexSql()
191     {
192         return 'ALTER TABLE test ADD CONSTRAINT constraint_name PRIMARY KEY (test)';
193     }
194
195     public function getGenerateConstraintForeignKeySql()
196     {
197         return 'ALTER TABLE test ADD CONSTRAINT constraint_fk FOREIGN KEY (fk_name) REFERENCES foreign (id)';
198     }
199
200     abstract public function getGenerateAlterTableSql();
201
202     public function testGeneratesTableAlterationSql()
203     {
204         $expectedSql = $this->getGenerateAlterTableSql();
205
206         $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
207         $tableDiff->newName = 'userlist';
208         $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('notnull' => false));
209         $tableDiff->removedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', \Doctrine\DBAL\Types\Type::getType('integer'));
210         $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
211             'bar', new \Doctrine\DBAL\Schema\Column(
212                 'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('default' => 'def')
213             ),
214             array('type', 'notnull', 'default')
215         );
216         $tableDiff->changedColumns['bloo'] = new \Doctrine\DBAL\Schema\ColumnDiff(
217             'bloo', new \Doctrine\DBAL\Schema\Column(
218                 'bloo', \Doctrine\DBAL\Types\Type::getType('boolean'), array('default' => false)
219             ),
220             array('type', 'notnull', 'default')
221         );
222
223         $sql = $this->_platform->getAlterTableSQL($tableDiff);
224
225         $this->assertEquals($expectedSql, $sql);
226     }
227
228     public function testGetCustomColumnDeclarationSql()
229     {
230         $field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
231         $this->assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field));
232     }
233
234     public function testGetCreateTableSqlDispatchEvent()
235     {
236         $listenerMock = $this->getMock('GetCreateTableSqlDispatchEvenListener', array('onSchemaCreateTable', 'onSchemaCreateTableColumn'));
237         $listenerMock
238             ->expects($this->once())
239             ->method('onSchemaCreateTable');
240         $listenerMock
241             ->expects($this->exactly(2))
242             ->method('onSchemaCreateTableColumn');
243
244         $eventManager = new EventManager();
245         $eventManager->addEventListener(array(Events::onSchemaCreateTable, Events::onSchemaCreateTableColumn), $listenerMock);
246
247         $this->_platform->setEventManager($eventManager);
248
249         $table = new \Doctrine\DBAL\Schema\Table('test');
250         $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
251         $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
252
253         $this->_platform->getCreateTableSQL($table);
254     }
255
256     public function testGetDropTableSqlDispatchEvent()
257     {
258         $listenerMock = $this->getMock('GetDropTableSqlDispatchEventListener', array('onSchemaDropTable'));
259         $listenerMock
260             ->expects($this->once())
261             ->method('onSchemaDropTable');
262
263         $eventManager = new EventManager();
264         $eventManager->addEventListener(array(Events::onSchemaDropTable), $listenerMock);
265
266         $this->_platform->setEventManager($eventManager);
267
268         $this->_platform->getDropTableSQL('TABLE');
269     }
270
271     public function testGetAlterTableSqlDispatchEvent()
272     {
273         $events = array(
274             'onSchemaAlterTable',
275             'onSchemaAlterTableAddColumn',
276             'onSchemaAlterTableRemoveColumn',
277             'onSchemaAlterTableChangeColumn',
278             'onSchemaAlterTableRenameColumn'
279         );
280
281         $listenerMock = $this->getMock('GetAlterTableSqlDispatchEvenListener', $events);
282         $listenerMock
283             ->expects($this->once())
284             ->method('onSchemaAlterTable');
285         $listenerMock
286             ->expects($this->once())
287             ->method('onSchemaAlterTableAddColumn');
288         $listenerMock
289             ->expects($this->once())
290             ->method('onSchemaAlterTableRemoveColumn');
291         $listenerMock
292             ->expects($this->once())
293             ->method('onSchemaAlterTableChangeColumn');
294         $listenerMock
295             ->expects($this->once())
296             ->method('onSchemaAlterTableRenameColumn');
297
298         $eventManager = new EventManager();
299         $events = array(
300             Events::onSchemaAlterTable,
301             Events::onSchemaAlterTableAddColumn,
302             Events::onSchemaAlterTableRemoveColumn,
303             Events::onSchemaAlterTableChangeColumn,
304             Events::onSchemaAlterTableRenameColumn
305         );
306         $eventManager->addEventListener($events, $listenerMock);
307
308         $this->_platform->setEventManager($eventManager);
309
310         $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
311         $tableDiff->addedColumns['added'] = new \Doctrine\DBAL\Schema\Column('added', \Doctrine\DBAL\Types\Type::getType('integer'), array());
312         $tableDiff->removedColumns['removed'] = new \Doctrine\DBAL\Schema\Column('removed', \Doctrine\DBAL\Types\Type::getType('integer'), array());
313         $tableDiff->changedColumns['changed'] = new \Doctrine\DBAL\Schema\ColumnDiff(
314             'changed', new \Doctrine\DBAL\Schema\Column(
315                 'changed2', \Doctrine\DBAL\Types\Type::getType('string'), array()
316             ),
317             array()
318         );
319         $tableDiff->renamedColumns['renamed'] = new \Doctrine\DBAL\Schema\Column('renamed2', \Doctrine\DBAL\Types\Type::getType('integer'), array());
320
321         $this->_platform->getAlterTableSQL($tableDiff);
322     }
323
324     /**
325      * @group DBAL-42
326      */
327     public function testCreateTableColumnComments()
328     {
329         $table = new \Doctrine\DBAL\Schema\Table('test');
330         $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
331         $table->setPrimaryKey(array('id'));
332
333         $this->assertEquals($this->getCreateTableColumnCommentsSQL(), $this->_platform->getCreateTableSQL($table));
334     }
335
336     /**
337      * @group DBAL-42
338      */
339     public function testAlterTableColumnComments()
340     {
341         $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
342         $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('comment' => 'A comment'));
343         $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
344             'bar', new \Doctrine\DBAL\Schema\Column(
345                 'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('comment' => 'B comment')
346             ),
347             array('comment')
348         );
349
350         $this->assertEquals($this->getAlterTableColumnCommentsSQL(), $this->_platform->getAlterTableSQL($tableDiff));
351     }
352
353     public function testCreateTableColumnTypeComments()
354     {
355         $table = new \Doctrine\DBAL\Schema\Table('test');
356         $table->addColumn('id', 'integer');
357         $table->addColumn('data', 'array');
358         $table->setPrimaryKey(array('id'));
359
360         $this->assertEquals($this->getCreateTableColumnTypeCommentsSQL(), $this->_platform->getCreateTableSQL($table));
361     }
362
363     public function getCreateTableColumnCommentsSQL()
364     {
365         $this->markTestSkipped('Platform does not support Column comments.');
366     }
367
368     public function getAlterTableColumnCommentsSQL()
369     {
370         $this->markTestSkipped('Platform does not support Column comments.');
371     }
372
373     public function getCreateTableColumnTypeCommentsSQL()
374     {
375         $this->markTestSkipped('Platform does not support Column comments.');
376     }
377
378     /**
379      * @group DBAL-45
380      */
381     public function testKeywordList()
382     {
383         $keywordList = $this->_platform->getReservedKeywordsList();
384         $this->assertInstanceOf('Doctrine\DBAL\Platforms\Keywords\KeywordList', $keywordList);
385
386         $this->assertTrue($keywordList->isKeyword('table'));
387     }
388 }