PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/doctrine/dbal/tests/Doctrine/Tests/DBAL/Platforms/MySqlPlatformTest.php

https://bitbucket.org/iiic/iszp
PHP | 243 lines | 198 code | 36 blank | 9 comment | 0 complexity | 20f1666bf985f523a508b501595d5ce9 MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Platforms;
  3. use Doctrine\DBAL\Platforms\MySqlPlatform;
  4. use Doctrine\DBAL\Types\Type;
  5. use Doctrine\DBAL\Schema\Table;
  6. use Doctrine\DBAL\Schema\TableDiff;
  7. use Doctrine\DBAL\Schema\Schema;
  8. use Doctrine\DBAL\Schema\Index;
  9. class MySqlPlatformTest extends AbstractPlatformTestCase
  10. {
  11. public function createPlatform()
  12. {
  13. return new MysqlPlatform;
  14. }
  15. public function testGenerateMixedCaseTableCreate()
  16. {
  17. $table = new Table("Foo");
  18. $table->addColumn("Bar", "integer");
  19. $sql = $this->_platform->getCreateTableSQL($table);
  20. $this->assertEquals('CREATE TABLE Foo (Bar INT NOT NULL) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB', array_shift($sql));
  21. }
  22. public function getGenerateTableSql()
  23. {
  24. return 'CREATE TABLE test (id INT AUTO_INCREMENT NOT NULL, test VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB';
  25. }
  26. public function getGenerateTableWithMultiColumnUniqueIndexSql()
  27. {
  28. return array(
  29. 'CREATE TABLE test (foo VARCHAR(255) DEFAULT NULL, bar VARCHAR(255) DEFAULT NULL, UNIQUE INDEX UNIQ_D87F7E0C8C73652176FF8CAA (foo, bar)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
  30. );
  31. }
  32. public function getGenerateAlterTableSql()
  33. {
  34. return array(
  35. "ALTER TABLE mytable RENAME TO userlist, ADD quota INT DEFAULT NULL, DROP foo, CHANGE bar baz VARCHAR(255) DEFAULT 'def' NOT NULL, CHANGE bloo bloo TINYINT(1) DEFAULT '0' NOT NULL"
  36. );
  37. }
  38. public function testGeneratesSqlSnippets()
  39. {
  40. $this->assertEquals('RLIKE', $this->_platform->getRegexpExpression(), 'Regular expression operator is not correct');
  41. $this->assertEquals('`', $this->_platform->getIdentifierQuoteCharacter(), 'Quote character is not correct');
  42. $this->assertEquals('CONCAT(column1, column2, column3)', $this->_platform->getConcatExpression('column1', 'column2', 'column3'), 'Concatenation function is not correct');
  43. }
  44. public function testGeneratesTransactionsCommands()
  45. {
  46. $this->assertEquals(
  47. 'SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED',
  48. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED),
  49. ''
  50. );
  51. $this->assertEquals(
  52. 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
  53. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED)
  54. );
  55. $this->assertEquals(
  56. 'SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ',
  57. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ)
  58. );
  59. $this->assertEquals(
  60. 'SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE',
  61. $this->_platform->getSetTransactionIsolationSQL(\Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE)
  62. );
  63. }
  64. public function testGeneratesDDLSnippets()
  65. {
  66. $this->assertEquals('SHOW DATABASES', $this->_platform->getShowDatabasesSQL());
  67. $this->assertEquals('CREATE DATABASE foobar', $this->_platform->getCreateDatabaseSQL('foobar'));
  68. $this->assertEquals('DROP DATABASE foobar', $this->_platform->getDropDatabaseSQL('foobar'));
  69. $this->assertEquals('DROP TABLE foobar', $this->_platform->getDropTableSQL('foobar'));
  70. }
  71. public function testGeneratesTypeDeclarationForIntegers()
  72. {
  73. $this->assertEquals(
  74. 'INT',
  75. $this->_platform->getIntegerTypeDeclarationSQL(array())
  76. );
  77. $this->assertEquals(
  78. 'INT AUTO_INCREMENT',
  79. $this->_platform->getIntegerTypeDeclarationSQL(array('autoincrement' => true)
  80. ));
  81. $this->assertEquals(
  82. 'INT AUTO_INCREMENT',
  83. $this->_platform->getIntegerTypeDeclarationSQL(
  84. array('autoincrement' => true, 'primary' => true)
  85. ));
  86. }
  87. public function testGeneratesTypeDeclarationForStrings()
  88. {
  89. $this->assertEquals(
  90. 'CHAR(10)',
  91. $this->_platform->getVarcharTypeDeclarationSQL(
  92. array('length' => 10, 'fixed' => true)
  93. ));
  94. $this->assertEquals(
  95. 'VARCHAR(50)',
  96. $this->_platform->getVarcharTypeDeclarationSQL(array('length' => 50)),
  97. 'Variable string declaration is not correct'
  98. );
  99. $this->assertEquals(
  100. 'VARCHAR(255)',
  101. $this->_platform->getVarcharTypeDeclarationSQL(array()),
  102. 'Long string declaration is not correct'
  103. );
  104. }
  105. public function testPrefersIdentityColumns()
  106. {
  107. $this->assertTrue($this->_platform->prefersIdentityColumns());
  108. }
  109. public function testSupportsIdentityColumns()
  110. {
  111. $this->assertTrue($this->_platform->supportsIdentityColumns());
  112. }
  113. public function testDoesSupportSavePoints()
  114. {
  115. $this->assertTrue($this->_platform->supportsSavepoints());
  116. }
  117. public function getGenerateIndexSql()
  118. {
  119. return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
  120. }
  121. public function getGenerateUniqueIndexSql()
  122. {
  123. return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
  124. }
  125. public function getGenerateForeignKeySql()
  126. {
  127. return 'ALTER TABLE test ADD FOREIGN KEY (fk_name_id) REFERENCES other_table (id)';
  128. }
  129. /**
  130. * @group DBAL-126
  131. */
  132. public function testUniquePrimaryKey()
  133. {
  134. $keyTable = new Table("foo");
  135. $keyTable->addColumn("bar", "integer");
  136. $keyTable->addColumn("baz", "string");
  137. $keyTable->setPrimaryKey(array("bar"));
  138. $keyTable->addUniqueIndex(array("baz"));
  139. $oldTable = new Table("foo");
  140. $oldTable->addColumn("bar", "integer");
  141. $oldTable->addColumn("baz", "string");
  142. $c = new \Doctrine\DBAL\Schema\Comparator;
  143. $diff = $c->diffTable($oldTable, $keyTable);
  144. $sql = $this->_platform->getAlterTableSQL($diff);
  145. $this->assertEquals(array(
  146. "ALTER TABLE foo ADD PRIMARY KEY (bar)",
  147. "CREATE UNIQUE INDEX UNIQ_8C73652178240498 ON foo (baz)",
  148. ), $sql);
  149. }
  150. public function testModifyLimitQuery()
  151. {
  152. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10, 0);
  153. $this->assertEquals('SELECT * FROM user LIMIT 10 OFFSET 0', $sql);
  154. }
  155. public function testModifyLimitQueryWithEmptyOffset()
  156. {
  157. $sql = $this->_platform->modifyLimitQuery('SELECT * FROM user', 10);
  158. $this->assertEquals('SELECT * FROM user LIMIT 10', $sql);
  159. }
  160. /**
  161. * @group DDC-118
  162. */
  163. public function testGetDateTimeTypeDeclarationSql()
  164. {
  165. $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => false)));
  166. $this->assertEquals("TIMESTAMP", $this->_platform->getDateTimeTypeDeclarationSQL(array('version' => true)));
  167. $this->assertEquals("DATETIME", $this->_platform->getDateTimeTypeDeclarationSQL(array()));
  168. }
  169. public function getCreateTableColumnCommentsSQL()
  170. {
  171. return array("CREATE TABLE test (id INT NOT NULL COMMENT 'This is a comment', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
  172. }
  173. public function getAlterTableColumnCommentsSQL()
  174. {
  175. return array("ALTER TABLE mytable ADD quota INT NOT NULL COMMENT 'A comment', CHANGE bar baz VARCHAR(255) NOT NULL COMMENT 'B comment'");
  176. }
  177. public function getCreateTableColumnTypeCommentsSQL()
  178. {
  179. return array("CREATE TABLE test (id INT NOT NULL, data LONGTEXT NOT NULL COMMENT '(DC2Type:array)', PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB");
  180. }
  181. /**
  182. * @group DBAL-237
  183. */
  184. public function testChangeIndexWithForeignKeys()
  185. {
  186. $index = new Index("idx", array("col"), false);
  187. $unique = new Index("uniq", array("col"), true);
  188. $diff = new TableDiff("test", array(), array(), array(), array($unique), array(), array($index));
  189. $sql = $this->_platform->getAlterTableSQL($diff);
  190. $this->assertEquals(array("ALTER TABLE test DROP INDEX idx, ADD UNIQUE INDEX uniq (col)"), $sql);
  191. $diff = new TableDiff("test", array(), array(), array(), array($index), array(), array($unique));
  192. $sql = $this->_platform->getAlterTableSQL($diff);
  193. $this->assertEquals(array("ALTER TABLE test DROP INDEX uniq, ADD INDEX idx (col)"), $sql);
  194. }
  195. protected function getQuotedColumnInPrimaryKeySQL()
  196. {
  197. return array(
  198. 'CREATE TABLE `quoted` (`key` VARCHAR(255) NOT NULL, PRIMARY KEY(`key`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
  199. );
  200. }
  201. protected function getQuotedColumnInIndexSQL()
  202. {
  203. return array(
  204. 'CREATE TABLE `quoted` (`key` VARCHAR(255) NOT NULL, INDEX IDX_22660D028A90ABA9 (`key`)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB'
  205. );
  206. }
  207. }