3 namespace Doctrine\Tests\DBAL\Platforms;
5 use Doctrine\Common\EventManager;
6 use Doctrine\DBAL\Events;
8 abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
11 * @var Doctrine\DBAL\Platforms\AbstractPlatform
15 abstract public function createPlatform();
17 public function setUp()
19 $this->_platform = $this->createPlatform();
25 public function testQuoteIdentifier()
27 if ($this->_platform->getName() == "mssql") {
28 $this->markTestSkipped('Not working this way on mssql.');
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));
40 public function testQuoteSingleIdentifier()
42 if ($this->_platform->getName() == "mssql") {
43 $this->markTestSkipped('Not working this way on mssql.');
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));
52 public function testGetInvalidtForeignKeyReferentialActionSQL()
54 $this->setExpectedException('InvalidArgumentException');
55 $this->_platform->getForeignKeyReferentialActionSQL('unknown');
58 public function testGetUnknownDoctrineMappingType()
60 $this->setExpectedException('Doctrine\DBAL\DBALException');
61 $this->_platform->getDoctrineTypeMapping('foobar');
64 public function testRegisterDoctrineMappingType()
66 $this->_platform->registerDoctrineTypeMapping('foo', 'integer');
67 $this->assertEquals('integer', $this->_platform->getDoctrineTypeMapping('foo'));
70 public function testRegisterUnknownDoctrineMappingType()
72 $this->setExpectedException('Doctrine\DBAL\DBALException');
73 $this->_platform->registerDoctrineTypeMapping('foo', 'bar');
76 public function testCreateWithNoColumns()
78 $table = new \Doctrine\DBAL\Schema\Table('test');
80 $this->setExpectedException('Doctrine\DBAL\DBALException');
81 $sql = $this->_platform->getCreateTableSQL($table);
84 public function testGeneratesTableCreationSql()
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'));
91 $sql = $this->_platform->getCreateTableSQL($table);
92 $this->assertEquals($this->getGenerateTableSql(), $sql[0]);
95 abstract public function getGenerateTableSql();
97 public function testGenerateTableWithMultiColumnUniqueIndex()
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"));
104 $sql = $this->_platform->getCreateTableSQL($table);
105 $this->assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
108 abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();
110 public function testGeneratesIndexCreationSql()
112 $indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));
115 $this->getGenerateIndexSql(),
116 $this->_platform->getCreateIndexSQL($indexDef, 'mytable')
120 abstract public function getGenerateIndexSql();
122 public function testGeneratesUniqueIndexCreationSql()
124 $indexDef = new \Doctrine\DBAL\Schema\Index('index_name', array('test', 'test2'), true);
126 $sql = $this->_platform->getCreateIndexSQL($indexDef, 'test');
127 $this->assertEquals($this->getGenerateUniqueIndexSql(), $sql);
130 abstract public function getGenerateUniqueIndexSql();
132 public function testGeneratesForeignKeyCreationSql()
134 $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name_id'), 'other_table', array('id'), '');
136 $sql = $this->_platform->getCreateForeignKeySQL($fk, 'test');
137 $this->assertEquals($sql, $this->getGenerateForeignKeySql());
140 abstract public function getGenerateForeignKeySql();
142 public function testGeneratesConstraintCreationSql()
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);
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);
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);
157 protected function getBitAndComparisonExpressionSql($value1, $value2)
159 return '(' . $value1 . ' & ' . $value2 . ')';
165 public function testGeneratesBitAndComparisonExpressionSql()
167 $sql = $this->_platform->getBitAndComparisonExpression(2, 4);
168 $this->assertEquals($this->getBitAndComparisonExpressionSql(2, 4), $sql);
171 protected function getBitOrComparisonExpressionSql($value1, $value2)
173 return '(' . $value1 . ' | ' . $value2 . ')';
179 public function testGeneratesBitOrComparisonExpressionSql()
181 $sql = $this->_platform->getBitOrComparisonExpression(2, 4);
182 $this->assertEquals($this->getBitOrComparisonExpressionSql(2, 4), $sql);
185 public function getGenerateConstraintUniqueIndexSql()
187 return 'ALTER TABLE test ADD CONSTRAINT constraint_name UNIQUE (test)';
190 public function getGenerateConstraintPrimaryIndexSql()
192 return 'ALTER TABLE test ADD CONSTRAINT constraint_name PRIMARY KEY (test)';
195 public function getGenerateConstraintForeignKeySql()
197 return 'ALTER TABLE test ADD CONSTRAINT constraint_fk FOREIGN KEY (fk_name) REFERENCES foreign (id)';
200 abstract public function getGenerateAlterTableSql();
202 public function testGeneratesTableAlterationSql()
204 $expectedSql = $this->getGenerateAlterTableSql();
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')
214 array('type', 'notnull', 'default')
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)
220 array('type', 'notnull', 'default')
223 $sql = $this->_platform->getAlterTableSQL($tableDiff);
225 $this->assertEquals($expectedSql, $sql);
228 public function testGetCustomColumnDeclarationSql()
230 $field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
231 $this->assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field));
234 public function testGetCreateTableSqlDispatchEvent()
236 $listenerMock = $this->getMock('GetCreateTableSqlDispatchEvenListener', array('onSchemaCreateTable', 'onSchemaCreateTableColumn'));
238 ->expects($this->once())
239 ->method('onSchemaCreateTable');
241 ->expects($this->exactly(2))
242 ->method('onSchemaCreateTableColumn');
244 $eventManager = new EventManager();
245 $eventManager->addEventListener(array(Events::onSchemaCreateTable, Events::onSchemaCreateTableColumn), $listenerMock);
247 $this->_platform->setEventManager($eventManager);
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));
253 $this->_platform->getCreateTableSQL($table);
256 public function testGetDropTableSqlDispatchEvent()
258 $listenerMock = $this->getMock('GetDropTableSqlDispatchEventListener', array('onSchemaDropTable'));
260 ->expects($this->once())
261 ->method('onSchemaDropTable');
263 $eventManager = new EventManager();
264 $eventManager->addEventListener(array(Events::onSchemaDropTable), $listenerMock);
266 $this->_platform->setEventManager($eventManager);
268 $this->_platform->getDropTableSQL('TABLE');
271 public function testGetAlterTableSqlDispatchEvent()
274 'onSchemaAlterTable',
275 'onSchemaAlterTableAddColumn',
276 'onSchemaAlterTableRemoveColumn',
277 'onSchemaAlterTableChangeColumn',
278 'onSchemaAlterTableRenameColumn'
281 $listenerMock = $this->getMock('GetAlterTableSqlDispatchEvenListener', $events);
283 ->expects($this->once())
284 ->method('onSchemaAlterTable');
286 ->expects($this->once())
287 ->method('onSchemaAlterTableAddColumn');
289 ->expects($this->once())
290 ->method('onSchemaAlterTableRemoveColumn');
292 ->expects($this->once())
293 ->method('onSchemaAlterTableChangeColumn');
295 ->expects($this->once())
296 ->method('onSchemaAlterTableRenameColumn');
298 $eventManager = new EventManager();
300 Events::onSchemaAlterTable,
301 Events::onSchemaAlterTableAddColumn,
302 Events::onSchemaAlterTableRemoveColumn,
303 Events::onSchemaAlterTableChangeColumn,
304 Events::onSchemaAlterTableRenameColumn
306 $eventManager->addEventListener($events, $listenerMock);
308 $this->_platform->setEventManager($eventManager);
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()
319 $tableDiff->renamedColumns['renamed'] = new \Doctrine\DBAL\Schema\Column('renamed2', \Doctrine\DBAL\Types\Type::getType('integer'), array());
321 $this->_platform->getAlterTableSQL($tableDiff);
327 public function testCreateTableColumnComments()
329 $table = new \Doctrine\DBAL\Schema\Table('test');
330 $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
331 $table->setPrimaryKey(array('id'));
333 $this->assertEquals($this->getCreateTableColumnCommentsSQL(), $this->_platform->getCreateTableSQL($table));
339 public function testAlterTableColumnComments()
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')
350 $this->assertEquals($this->getAlterTableColumnCommentsSQL(), $this->_platform->getAlterTableSQL($tableDiff));
353 public function testCreateTableColumnTypeComments()
355 $table = new \Doctrine\DBAL\Schema\Table('test');
356 $table->addColumn('id', 'integer');
357 $table->addColumn('data', 'array');
358 $table->setPrimaryKey(array('id'));
360 $this->assertEquals($this->getCreateTableColumnTypeCommentsSQL(), $this->_platform->getCreateTableSQL($table));
363 public function getCreateTableColumnCommentsSQL()
365 $this->markTestSkipped('Platform does not support Column comments.');
368 public function getAlterTableColumnCommentsSQL()
370 $this->markTestSkipped('Platform does not support Column comments.');
373 public function getCreateTableColumnTypeCommentsSQL()
375 $this->markTestSkipped('Platform does not support Column comments.');
381 public function testKeywordList()
383 $keywordList = $this->_platform->getReservedKeywordsList();
384 $this->assertInstanceOf('Doctrine\DBAL\Platforms\Keywords\KeywordList', $keywordList);
386 $this->assertTrue($keywordList->isKeyword('table'));