PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/vendor_full/doctrine-dbal/tests/Doctrine/Tests/DBAL/Schema/TableTest.php

https://github.com/l3l0/BehatExamples
PHP | 419 lines | 314 code | 90 blank | 15 comment | 0 complexity | 3c75df662305fac4277feb5400b5fbae MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Schema;
  3. require_once __DIR__ . '/../../TestInit.php';
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\DBAL\Schema\Table;
  6. use Doctrine\DBAL\Schema\TableBuilder;
  7. use Doctrine\DBAL\Schema\Column;
  8. use Doctrine\DBAL\Schema\Index;
  9. use Doctrine\DBAL\Schema\ForeignKeyConstraint;
  10. use Doctrine\DBAL\Types\Type;
  11. class TableTest extends \PHPUnit_Framework_TestCase
  12. {
  13. public function testCreateWithInvalidTableName()
  14. {
  15. $this->setExpectedException('Doctrine\DBAL\DBALException');
  16. $table = new \Doctrine\DBAL\Schema\Table('');
  17. }
  18. public function testGetName()
  19. {
  20. $table = new Table("foo", array(), array(), array());
  21. $this->assertEquals("foo", $table->getName());
  22. }
  23. public function testColumns()
  24. {
  25. $type = Type::getType('integer');
  26. $columns = array();
  27. $columns[] = new Column("foo", $type);
  28. $columns[] = new Column("bar", $type);
  29. $table = new Table("foo", $columns, array(), array());
  30. $this->assertTrue($table->hasColumn("foo"));
  31. $this->assertTrue($table->hasColumn("bar"));
  32. $this->assertFalse($table->hasColumn("baz"));
  33. $this->assertType('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
  34. $this->assertType('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));
  35. $this->assertEquals(2, count($table->getColumns()));
  36. }
  37. public function testColumnsCaseInsensitive()
  38. {
  39. $table = new Table("foo");
  40. $column = $table->addColumn('Foo', 'integer');
  41. $this->assertTrue($table->hasColumn('Foo'));
  42. $this->assertTrue($table->hasColumn('foo'));
  43. $this->assertTrue($table->hasColumn('FOO'));
  44. $this->assertSame($column, $table->getColumn('Foo'));
  45. $this->assertSame($column, $table->getColumn('foo'));
  46. $this->assertSame($column, $table->getColumn('FOO'));
  47. }
  48. public function testCreateColumn()
  49. {
  50. $type = Type::getType('integer');
  51. $table = new Table("foo");
  52. $this->assertFalse($table->hasColumn("bar"));
  53. $table->addColumn("bar", 'integer');
  54. $this->assertTrue($table->hasColumn("bar"));
  55. $this->assertSame($type, $table->getColumn("bar")->getType());
  56. }
  57. public function testDropColumn()
  58. {
  59. $type = Type::getType('integer');
  60. $columns = array();
  61. $columns[] = new Column("foo", $type);
  62. $columns[] = new Column("bar", $type);
  63. $table = new Table("foo", $columns, array(), array());
  64. $this->assertTrue($table->hasColumn("foo"));
  65. $this->assertTrue($table->hasColumn("bar"));
  66. $table->dropColumn("foo")->dropColumn("bar");
  67. $this->assertFalse($table->hasColumn("foo"));
  68. $this->assertFalse($table->hasColumn("bar"));
  69. }
  70. public function testGetUnknownColumnThrowsException()
  71. {
  72. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  73. $table = new Table("foo", array(), array(), array());
  74. $table->getColumn('unknown');
  75. }
  76. public function testAddColumnTwiceThrowsException()
  77. {
  78. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  79. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  80. $columns = array();
  81. $columns[] = new Column("foo", $type);
  82. $columns[] = new Column("foo", $type);
  83. $table = new Table("foo", $columns, array(), array());
  84. }
  85. public function testCreateIndex()
  86. {
  87. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  88. $columns = array(new Column("foo", $type), new Column("bar", $type), new Column("baz", $type));
  89. $table = new Table("foo", $columns);
  90. $table->addIndex(array("foo", "bar"));
  91. $table->addUniqueIndex(array("bar", "baz"));
  92. $this->assertTrue($table->hasIndex("foo_foo_bar_idx"));
  93. $this->assertTrue($table->hasIndex("foo_bar_baz_uniq"));
  94. }
  95. public function testIndexCaseInsensitive()
  96. {
  97. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  98. $columns = array(
  99. new Column("foo", $type),
  100. new Column("bar", $type),
  101. new Column("baz", $type)
  102. );
  103. $table = new Table("foo", $columns);
  104. $table->addIndex(array("foo", "bar", "baz"), "Foo_Idx");
  105. $this->assertTrue($table->hasIndex('foo_idx'));
  106. $this->assertTrue($table->hasIndex('Foo_Idx'));
  107. $this->assertTrue($table->hasIndex('FOO_IDX'));
  108. }
  109. public function testAddIndexes()
  110. {
  111. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  112. $columns = array(
  113. new Column("foo", $type),
  114. new Column("bar", $type),
  115. );
  116. $indexes = array(
  117. new Index("the_primary", array("foo"), true, true),
  118. new Index("bar_idx", array("bar"), false, false),
  119. );
  120. $table = new Table("foo", $columns, $indexes, array());
  121. $this->assertTrue($table->hasIndex("the_primary"));
  122. $this->assertTrue($table->hasIndex("bar_idx"));
  123. $this->assertFalse($table->hasIndex("some_idx"));
  124. $this->assertType('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
  125. $this->assertType('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
  126. $this->assertType('Doctrine\DBAL\Schema\Index', $table->getIndex('bar_idx'));
  127. }
  128. public function testGetUnknownIndexThrowsException()
  129. {
  130. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  131. $table = new Table("foo", array(), array(), array());
  132. $table->getIndex("unknownIndex");
  133. }
  134. public function testAddTwoPrimaryThrowsException()
  135. {
  136. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  137. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  138. $columns = array(new Column("foo", $type), new Column("bar", $type));
  139. $indexes = array(
  140. new Index("the_primary", array("foo"), true, true),
  141. new Index("other_primary", array("bar"), true, true),
  142. );
  143. $table = new Table("foo", $columns, $indexes, array());
  144. }
  145. public function testAddTwoIndexesWithSameNameThrowsException()
  146. {
  147. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  148. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  149. $columns = array(new Column("foo", $type), new Column("bar", $type));
  150. $indexes = array(
  151. new Index("an_idx", array("foo"), false, false),
  152. new Index("an_idx", array("bar"), false, false),
  153. );
  154. $table = new Table("foo", $columns, $indexes, array());
  155. }
  156. public function testConstraints()
  157. {
  158. $constraint = new ForeignKeyConstraint(array(), "foo", array());
  159. $tableA = new Table("foo", array(), array(), array($constraint));
  160. $constraints = $tableA->getForeignKeys();
  161. $this->assertEquals(1, count($constraints));
  162. $this->assertSame($constraint, array_shift($constraints));
  163. }
  164. public function testOptions()
  165. {
  166. $table = new Table("foo", array(), array(), array(), false, array("foo" => "bar"));
  167. $this->assertTrue($table->hasOption("foo"));
  168. $this->assertEquals("bar", $table->getOption("foo"));
  169. }
  170. public function testBuilderSetPrimaryKey()
  171. {
  172. $table = new Table("foo");
  173. $table->addColumn("bar", 'integer');
  174. $table->setPrimaryKey(array("bar"));
  175. $this->assertTrue($table->hasIndex("primary"));
  176. $this->assertType('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
  177. $this->assertTrue($table->getIndex("primary")->isUnique());
  178. $this->assertTrue($table->getIndex("primary")->isPrimary());
  179. }
  180. public function testBuilderAddUniqueIndex()
  181. {
  182. $table = new Table("foo");
  183. $table->addColumn("bar", 'integer');
  184. $table->addUniqueIndex(array("bar"), "my_idx");
  185. $this->assertTrue($table->hasIndex("my_idx"));
  186. $this->assertTrue($table->getIndex("my_idx")->isUnique());
  187. $this->assertFalse($table->getIndex("my_idx")->isPrimary());
  188. }
  189. public function testBuilderAddIndex()
  190. {
  191. $table = new Table("foo");
  192. $table->addColumn("bar", 'integer');
  193. $table->addIndex(array("bar"), "my_idx");
  194. $this->assertTrue($table->hasIndex("my_idx"));
  195. $this->assertFalse($table->getIndex("my_idx")->isUnique());
  196. $this->assertFalse($table->getIndex("my_idx")->isPrimary());
  197. }
  198. public function testBuilderAddIndexWithInvalidNameThrowsException()
  199. {
  200. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  201. $table = new Table("foo");
  202. $table->addColumn("bar",'integer');
  203. $table->addIndex(array("bar"), "invalid name %&/");
  204. }
  205. public function testBuilderAddIndexWithUnknownColumnThrowsException()
  206. {
  207. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  208. $table = new Table("foo");
  209. $table->addIndex(array("bar"), "invalidName");
  210. }
  211. public function testBuilderOptions()
  212. {
  213. $table = new Table("foo");
  214. $table->addOption("foo", "bar");
  215. $this->assertTrue($table->hasOption("foo"));
  216. $this->assertEquals("bar", $table->getOption("foo"));
  217. }
  218. public function testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()
  219. {
  220. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  221. $table = new Table("foo");
  222. $table->addColumn("id", 'integer');
  223. $foreignTable = new Table("bar");
  224. $foreignTable->addColumn("id", 'integer');
  225. $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
  226. }
  227. public function testAddForeignKeyConstraint_UnknownForeignColumn_ThrowsException()
  228. {
  229. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  230. $table = new Table("foo");
  231. $table->addColumn("id", 'integer');
  232. $foreignTable = new Table("bar");
  233. $foreignTable->addColumn("id", 'integer');
  234. $table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
  235. }
  236. public function testAddForeignKeyConstraint()
  237. {
  238. $table = new Table("foo");
  239. $table->addColumn("id", 'integer');
  240. $foreignTable = new Table("bar");
  241. $foreignTable->addColumn("id", 'integer');
  242. $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
  243. $constraints = $table->getForeignKeys();
  244. $this->assertEquals(1, count($constraints));
  245. $this->assertType('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraints["foo_id_fk"]);
  246. $this->assertEquals("foo_id_fk", $constraints["foo_id_fk"]->getName());
  247. $this->assertTrue($constraints["foo_id_fk"]->hasOption("foo"));
  248. $this->assertEquals("bar", $constraints["foo_id_fk"]->getOption("foo"));
  249. }
  250. public function testAddIndexWithCaseSensitiveColumnProblem()
  251. {
  252. $table = new Table("foo");
  253. $table->addColumn("id", 'integer');
  254. $table->addIndex(array("ID"), "my_idx");
  255. $this->assertTrue($table->hasIndex('my_idx'));
  256. $this->assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns());
  257. $this->assertTrue($table->getIndex('my_idx')->spansColumns(array('id')));
  258. }
  259. public function testAddPrimaryKey_ColumnsAreExplicitlySetToNotNull()
  260. {
  261. $table = new Table("foo");
  262. $column = $table->addColumn("id", 'integer', array('notnull' => false));
  263. $this->assertFalse($column->getNotnull());
  264. $table->setPrimaryKey(array('id'));
  265. $this->assertTrue($column->getNotnull());
  266. }
  267. /**
  268. * @group DDC-133
  269. */
  270. public function testAllowImplicitSchemaTableInAutogeneratedIndexNames()
  271. {
  272. $table = new Table("foo.bar");
  273. $table->addColumn('baz', 'integer', array());
  274. $table->addIndex(array('baz'));
  275. $this->assertTrue($table->hasIndex('foo_bar_baz_idx'));
  276. }
  277. /**
  278. * @group DBAL-50
  279. */
  280. public function testAddIndexTwice_IgnoreSecond()
  281. {
  282. $table = new Table("foo.bar");
  283. $table->addColumn('baz', 'integer', array());
  284. $table->addIndex(array('baz'));
  285. $table->addIndex(array('baz'));
  286. $this->assertEquals(1, count($table->getIndexes()));
  287. }
  288. /**
  289. * @group DBAL-50
  290. */
  291. public function testAddForeignKeyIndexImplicitly()
  292. {
  293. $table = new Table("foo");
  294. $table->addColumn("id", 'integer');
  295. $foreignTable = new Table("bar");
  296. $foreignTable->addColumn("id", 'integer');
  297. $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
  298. $this->assertEquals(1, count($table->getIndexes()));
  299. $this->assertTrue($table->hasIndex("foo_id_idx"));
  300. $this->assertEquals(array('id'), $table->getIndex('foo_id_idx')->getColumns());
  301. }
  302. /**
  303. * @group DBAL-50
  304. */
  305. public function testOverruleIndex()
  306. {
  307. $table = new Table("bar");
  308. $table->addColumn('baz', 'integer', array());
  309. $table->addIndex(array('baz'));
  310. $this->assertEquals(1, count($table->getIndexes()));
  311. $this->assertTrue($table->hasIndex('bar_baz_idx'));
  312. $table->addUniqueIndex(array('baz'));
  313. $this->assertEquals(1, count($table->getIndexes()));
  314. $this->assertFalse($table->hasIndex('bar_baz_idx'));
  315. $this->assertTrue($table->hasIndex('bar_baz_uniq'));
  316. }
  317. /**
  318. * @group DBAL-64
  319. */
  320. public function testQuotedTableName()
  321. {
  322. $table = new Table("`bar`");
  323. $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
  324. $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
  325. $this->assertEquals('bar', $table->getName());
  326. $this->assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
  327. $this->assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
  328. }
  329. }