PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Doctrine/Tests/DBAL/Platforms/AbstractPlatformTestCase.php

https://github.com/maartendekeizer/dbal
PHP | 228 lines | 168 code | 48 blank | 12 comment | 2 complexity | ce649b335195bb6457f5bb7d9163c3e7 MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Platforms;
  3. abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
  4. {
  5. /**
  6. * @var Doctrine\DBAL\Platforms\AbstractPlatform
  7. */
  8. protected $_platform;
  9. abstract public function createPlatform();
  10. public function setUp()
  11. {
  12. $this->_platform = $this->createPlatform();
  13. }
  14. public function testQuoteIdentifier()
  15. {
  16. if ($this->_platform->getName() == "mssql") {
  17. $this->markTestSkipped('Not working this way on mssql.');
  18. }
  19. $c = $this->_platform->getIdentifierQuoteCharacter();
  20. $this->assertEquals(str_repeat($c, 4), $this->_platform->quoteIdentifier($c));
  21. }
  22. public function testGetInvalidtForeignKeyReferentialActionSQL()
  23. {
  24. $this->setExpectedException('InvalidArgumentException');
  25. $this->_platform->getForeignKeyReferentialActionSQL('unknown');
  26. }
  27. public function testGetUnknownDoctrineMappingType()
  28. {
  29. $this->setExpectedException('Doctrine\DBAL\DBALException');
  30. $this->_platform->getDoctrineTypeMapping('foobar');
  31. }
  32. public function testRegisterDoctrineMappingType()
  33. {
  34. $this->_platform->registerDoctrineTypeMapping('foo', 'integer');
  35. $this->assertEquals('integer', $this->_platform->getDoctrineTypeMapping('foo'));
  36. }
  37. public function testRegisterUnknownDoctrineMappingType()
  38. {
  39. $this->setExpectedException('Doctrine\DBAL\DBALException');
  40. $this->_platform->registerDoctrineTypeMapping('foo', 'bar');
  41. }
  42. public function testCreateWithNoColumns()
  43. {
  44. $table = new \Doctrine\DBAL\Schema\Table('test');
  45. $this->setExpectedException('Doctrine\DBAL\DBALException');
  46. $sql = $this->_platform->getCreateTableSQL($table);
  47. }
  48. public function testGeneratesTableCreationSql()
  49. {
  50. $table = new \Doctrine\DBAL\Schema\Table('test');
  51. $table->addColumn('id', 'integer', array('notnull' => true, 'autoincrement' => true));
  52. $table->addColumn('test', 'string', array('notnull' => false, 'length' => 255));
  53. $table->setPrimaryKey(array('id'));
  54. $sql = $this->_platform->getCreateTableSQL($table);
  55. $this->assertEquals($this->getGenerateTableSql(), $sql[0]);
  56. }
  57. abstract public function getGenerateTableSql();
  58. public function testGenerateTableWithMultiColumnUniqueIndex()
  59. {
  60. $table = new \Doctrine\DBAL\Schema\Table('test');
  61. $table->addColumn('foo', 'string', array('notnull' => false, 'length' => 255));
  62. $table->addColumn('bar', 'string', array('notnull' => false, 'length' => 255));
  63. $table->addUniqueIndex(array("foo", "bar"));
  64. $sql = $this->_platform->getCreateTableSQL($table);
  65. $this->assertEquals($this->getGenerateTableWithMultiColumnUniqueIndexSql(), $sql);
  66. }
  67. abstract public function getGenerateTableWithMultiColumnUniqueIndexSql();
  68. public function testGeneratesIndexCreationSql()
  69. {
  70. $indexDef = new \Doctrine\DBAL\Schema\Index('my_idx', array('user_name', 'last_login'));
  71. $this->assertEquals(
  72. $this->getGenerateIndexSql(),
  73. $this->_platform->getCreateIndexSQL($indexDef, 'mytable')
  74. );
  75. }
  76. abstract public function getGenerateIndexSql();
  77. public function testGeneratesUniqueIndexCreationSql()
  78. {
  79. $indexDef = new \Doctrine\DBAL\Schema\Index('index_name', array('test', 'test2'), true);
  80. $sql = $this->_platform->getCreateIndexSQL($indexDef, 'test');
  81. $this->assertEquals($this->getGenerateUniqueIndexSql(), $sql);
  82. }
  83. abstract public function getGenerateUniqueIndexSql();
  84. public function testGeneratesForeignKeyCreationSql()
  85. {
  86. $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name_id'), 'other_table', array('id'), '');
  87. $sql = $this->_platform->getCreateForeignKeySQL($fk, 'test');
  88. $this->assertEquals($sql, $this->getGenerateForeignKeySql());
  89. }
  90. abstract public function getGenerateForeignKeySql();
  91. public function testGeneratesConstraintCreationSql()
  92. {
  93. $idx = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, false);
  94. $sql = $this->_platform->getCreateConstraintSQL($idx, 'test');
  95. $this->assertEquals($this->getGenerateConstraintUniqueIndexSql(), $sql);
  96. $pk = new \Doctrine\DBAL\Schema\Index('constraint_name', array('test'), true, true);
  97. $sql = $this->_platform->getCreateConstraintSQL($pk, 'test');
  98. $this->assertEquals($this->getGenerateConstraintPrimaryIndexSql(), $sql);
  99. $fk = new \Doctrine\DBAL\Schema\ForeignKeyConstraint(array('fk_name'), 'foreign', array('id'), 'constraint_fk');
  100. $sql = $this->_platform->getCreateConstraintSQL($fk, 'test');
  101. $this->assertEquals($this->getGenerateConstraintForeignKeySql(), $sql);
  102. }
  103. public function getGenerateConstraintUniqueIndexSql()
  104. {
  105. return 'ALTER TABLE test ADD CONSTRAINT constraint_name UNIQUE (test)';
  106. }
  107. public function getGenerateConstraintPrimaryIndexSql()
  108. {
  109. return 'ALTER TABLE test ADD CONSTRAINT constraint_name PRIMARY KEY (test)';
  110. }
  111. public function getGenerateConstraintForeignKeySql()
  112. {
  113. return 'ALTER TABLE test ADD CONSTRAINT constraint_fk FOREIGN KEY (fk_name) REFERENCES foreign (id)';
  114. }
  115. abstract public function getGenerateAlterTableSql();
  116. public function testGeneratesTableAlterationSql()
  117. {
  118. $expectedSql = $this->getGenerateAlterTableSql();
  119. $columnDiff = new \Doctrine\DBAL\Schema\ColumnDiff(
  120. 'bar', new \Doctrine\DBAL\Schema\Column(
  121. 'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('default' => 'def')
  122. ),
  123. array('type', 'notnull', 'default')
  124. );
  125. $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
  126. $tableDiff->newName = 'userlist';
  127. $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('notnull' => false));
  128. $tableDiff->removedColumns['foo'] = new \Doctrine\DBAL\Schema\Column('foo', \Doctrine\DBAL\Types\Type::getType('integer'));
  129. $tableDiff->changedColumns['bar'] = $columnDiff;
  130. $sql = $this->_platform->getAlterTableSQL($tableDiff);
  131. $this->assertEquals($expectedSql, $sql);
  132. }
  133. public function testGetCustomColumnDeclarationSql()
  134. {
  135. $field = array('columnDefinition' => 'MEDIUMINT(6) UNSIGNED');
  136. $this->assertEquals('foo MEDIUMINT(6) UNSIGNED', $this->_platform->getColumnDeclarationSQL('foo', $field));
  137. }
  138. /**
  139. * @group DBAL-42
  140. */
  141. public function testCreateTableColumnComments()
  142. {
  143. $table = new \Doctrine\DBAL\Schema\Table('test');
  144. $table->addColumn('id', 'integer', array('comment' => 'This is a comment'));
  145. $table->setPrimaryKey(array('id'));
  146. $this->assertEquals($this->getCreateTableColumnCommentsSQL(), $this->_platform->getCreateTableSQL($table));
  147. }
  148. /**
  149. * @group DBAL-42
  150. */
  151. public function testAlterTableColumnComments()
  152. {
  153. $tableDiff = new \Doctrine\DBAL\Schema\TableDiff('mytable');
  154. $tableDiff->addedColumns['quota'] = new \Doctrine\DBAL\Schema\Column('quota', \Doctrine\DBAL\Types\Type::getType('integer'), array('comment' => 'A comment'));
  155. $tableDiff->changedColumns['bar'] = new \Doctrine\DBAL\Schema\ColumnDiff(
  156. 'bar', new \Doctrine\DBAL\Schema\Column(
  157. 'baz', \Doctrine\DBAL\Types\Type::getType('string'), array('comment' => 'B comment')
  158. ),
  159. array('comment')
  160. );
  161. $this->assertEquals($this->getAlterTableColumnCommentsSQL(), $this->_platform->getAlterTableSQL($tableDiff));
  162. }
  163. public function getCreateTableColumnCommentsSQL()
  164. {
  165. $this->markTestSkipped('Platform does not support Column comments.');
  166. }
  167. public function getAlterTableColumnCommentsSQL()
  168. {
  169. $this->markTestSkipped('Platform does not support Column comments.');
  170. }
  171. /**
  172. * @group DBAL-45
  173. */
  174. public function testKeywordList()
  175. {
  176. $keywordList = $this->_platform->getReservedKeywordsList();
  177. $this->assertInstanceOf('Doctrine\DBAL\Platforms\Keywords\KeywordList', $keywordList);
  178. $this->assertTrue($keywordList->isKeyword('table'));
  179. }
  180. }