TableTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
  34. $this->assertInstanceOf('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"), "foo_foo_bar_idx");
  91. $table->addUniqueIndex(array("bar", "baz"), "foo_bar_baz_uniq");
  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->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
  125. $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
  126. $this->assertInstanceOf('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->assertInstanceOf('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. $constraint = current($constraints);
  246. $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraint);
  247. $this->assertTrue($constraint->hasOption("foo"));
  248. $this->assertEquals("bar", $constraint->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->assertEquals(1, count($table->getIndexes()));
  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. $indexes = $table->getIndexes();
  299. $this->assertEquals(1, count($indexes));
  300. $index = current($indexes);
  301. $this->assertTrue($table->hasIndex($index->getName()));
  302. $this->assertEquals(array('id'), $index->getColumns());
  303. }
  304. /**
  305. * @group DBAL-50
  306. */
  307. public function testOverruleIndex()
  308. {
  309. $table = new Table("bar");
  310. $table->addColumn('baz', 'integer', array());
  311. $table->addIndex(array('baz'));
  312. $indexes = $table->getIndexes();
  313. $this->assertEquals(1, count($indexes));
  314. $index = current($indexes);
  315. $table->addUniqueIndex(array('baz'));
  316. $this->assertEquals(1, count($table->getIndexes()));
  317. $this->assertFalse($table->hasIndex($index->getName()));
  318. }
  319. public function testPrimaryKeyOverrulesUniqueIndex()
  320. {
  321. $table = new Table("bar");
  322. $table->addColumn('baz', 'integer', array());
  323. $table->addUniqueIndex(array('baz'));
  324. $table->setPrimaryKey(array('baz'));
  325. $indexes = $table->getIndexes();
  326. $this->assertEquals(1, count($indexes), "Table should only contain the primary key table index, not the unique one anymore, because it was overruled.");
  327. $index = current($indexes);
  328. $this->assertTrue($index->isPrimary());
  329. }
  330. /**
  331. * @group DBAL-64
  332. */
  333. public function testQuotedTableName()
  334. {
  335. $table = new Table("`bar`");
  336. $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
  337. $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
  338. $this->assertEquals('bar', $table->getName());
  339. $this->assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
  340. $this->assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
  341. }
  342. /**
  343. * @group DBAL-79
  344. */
  345. public function testTableHasPrimaryKey()
  346. {
  347. $table = new Table("test");
  348. $this->assertFalse($table->hasPrimaryKey());
  349. $table->addColumn("foo", "integer");
  350. $table->setPrimaryKey(array("foo"));
  351. $this->assertTrue($table->hasPrimaryKey());
  352. }
  353. /**
  354. * @group DBAL-91
  355. */
  356. public function testAddIndexWithQuotedColumns()
  357. {
  358. $table = new Table("test");
  359. $table->addColumn('"foo"', 'integer');
  360. $table->addColumn('bar', 'integer');
  361. $table->addIndex(array('"foo"', '"bar"'));
  362. }
  363. /**
  364. * @group DBAL-91
  365. */
  366. public function testAddForeignKeyWithQuotedColumnsAndTable()
  367. {
  368. $table = new Table("test");
  369. $table->addColumn('"foo"', 'integer');
  370. $table->addColumn('bar', 'integer');
  371. $table->addForeignKeyConstraint('"boing"', array('"foo"', '"bar"'), array("id"));
  372. }
  373. }