TableTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. namespace Doctrine\Tests\DBAL\Schema;
  3. use Doctrine\DBAL\Schema\Schema;
  4. use Doctrine\DBAL\Schema\Table;
  5. use Doctrine\DBAL\Schema\TableBuilder;
  6. use Doctrine\DBAL\Schema\Column;
  7. use Doctrine\DBAL\Schema\Index;
  8. use Doctrine\DBAL\Schema\ForeignKeyConstraint;
  9. use Doctrine\DBAL\Types\Type;
  10. class TableTest extends \Doctrine\Tests\DbalTestCase
  11. {
  12. public function testCreateWithInvalidTableName()
  13. {
  14. $this->setExpectedException('Doctrine\DBAL\DBALException');
  15. $table = new \Doctrine\DBAL\Schema\Table('');
  16. }
  17. public function testGetName()
  18. {
  19. $table = new Table("foo", array(), array(), array());
  20. $this->assertEquals("foo", $table->getName());
  21. }
  22. public function testColumns()
  23. {
  24. $type = Type::getType('integer');
  25. $columns = array();
  26. $columns[] = new Column("foo", $type);
  27. $columns[] = new Column("bar", $type);
  28. $table = new Table("foo", $columns, array(), array());
  29. $this->assertTrue($table->hasColumn("foo"));
  30. $this->assertTrue($table->hasColumn("bar"));
  31. $this->assertFalse($table->hasColumn("baz"));
  32. $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("foo"));
  33. $this->assertInstanceOf('Doctrine\DBAL\Schema\Column', $table->getColumn("bar"));
  34. $this->assertEquals(2, count($table->getColumns()));
  35. }
  36. public function testColumnsCaseInsensitive()
  37. {
  38. $table = new Table("foo");
  39. $column = $table->addColumn('Foo', 'integer');
  40. $this->assertTrue($table->hasColumn('Foo'));
  41. $this->assertTrue($table->hasColumn('foo'));
  42. $this->assertTrue($table->hasColumn('FOO'));
  43. $this->assertSame($column, $table->getColumn('Foo'));
  44. $this->assertSame($column, $table->getColumn('foo'));
  45. $this->assertSame($column, $table->getColumn('FOO'));
  46. }
  47. public function testCreateColumn()
  48. {
  49. $type = Type::getType('integer');
  50. $table = new Table("foo");
  51. $this->assertFalse($table->hasColumn("bar"));
  52. $table->addColumn("bar", 'integer');
  53. $this->assertTrue($table->hasColumn("bar"));
  54. $this->assertSame($type, $table->getColumn("bar")->getType());
  55. }
  56. public function testDropColumn()
  57. {
  58. $type = Type::getType('integer');
  59. $columns = array();
  60. $columns[] = new Column("foo", $type);
  61. $columns[] = new Column("bar", $type);
  62. $table = new Table("foo", $columns, array(), array());
  63. $this->assertTrue($table->hasColumn("foo"));
  64. $this->assertTrue($table->hasColumn("bar"));
  65. $table->dropColumn("foo")->dropColumn("bar");
  66. $this->assertFalse($table->hasColumn("foo"));
  67. $this->assertFalse($table->hasColumn("bar"));
  68. }
  69. public function testGetUnknownColumnThrowsException()
  70. {
  71. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  72. $table = new Table("foo", array(), array(), array());
  73. $table->getColumn('unknown');
  74. }
  75. public function testAddColumnTwiceThrowsException()
  76. {
  77. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  78. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  79. $columns = array();
  80. $columns[] = new Column("foo", $type);
  81. $columns[] = new Column("foo", $type);
  82. $table = new Table("foo", $columns, array(), array());
  83. }
  84. public function testCreateIndex()
  85. {
  86. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  87. $columns = array(new Column("foo", $type), new Column("bar", $type), new Column("baz", $type));
  88. $table = new Table("foo", $columns);
  89. $table->addIndex(array("foo", "bar"), "foo_foo_bar_idx");
  90. $table->addUniqueIndex(array("bar", "baz"), "foo_bar_baz_uniq");
  91. $this->assertTrue($table->hasIndex("foo_foo_bar_idx"));
  92. $this->assertTrue($table->hasIndex("foo_bar_baz_uniq"));
  93. }
  94. public function testIndexCaseInsensitive()
  95. {
  96. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  97. $columns = array(
  98. new Column("foo", $type),
  99. new Column("bar", $type),
  100. new Column("baz", $type)
  101. );
  102. $table = new Table("foo", $columns);
  103. $table->addIndex(array("foo", "bar", "baz"), "Foo_Idx");
  104. $this->assertTrue($table->hasIndex('foo_idx'));
  105. $this->assertTrue($table->hasIndex('Foo_Idx'));
  106. $this->assertTrue($table->hasIndex('FOO_IDX'));
  107. }
  108. public function testAddIndexes()
  109. {
  110. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  111. $columns = array(
  112. new Column("foo", $type),
  113. new Column("bar", $type),
  114. );
  115. $indexes = array(
  116. new Index("the_primary", array("foo"), true, true),
  117. new Index("bar_idx", array("bar"), false, false),
  118. );
  119. $table = new Table("foo", $columns, $indexes, array());
  120. $this->assertTrue($table->hasIndex("the_primary"));
  121. $this->assertTrue($table->hasIndex("bar_idx"));
  122. $this->assertFalse($table->hasIndex("some_idx"));
  123. $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
  124. $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('the_primary'));
  125. $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getIndex('bar_idx'));
  126. }
  127. public function testGetUnknownIndexThrowsException()
  128. {
  129. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  130. $table = new Table("foo", array(), array(), array());
  131. $table->getIndex("unknownIndex");
  132. }
  133. public function testAddTwoPrimaryThrowsException()
  134. {
  135. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  136. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  137. $columns = array(new Column("foo", $type), new Column("bar", $type));
  138. $indexes = array(
  139. new Index("the_primary", array("foo"), true, true),
  140. new Index("other_primary", array("bar"), true, true),
  141. );
  142. $table = new Table("foo", $columns, $indexes, array());
  143. }
  144. public function testAddTwoIndexesWithSameNameThrowsException()
  145. {
  146. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  147. $type = \Doctrine\DBAL\Types\Type::getType('integer');
  148. $columns = array(new Column("foo", $type), new Column("bar", $type));
  149. $indexes = array(
  150. new Index("an_idx", array("foo"), false, false),
  151. new Index("an_idx", array("bar"), false, false),
  152. );
  153. $table = new Table("foo", $columns, $indexes, array());
  154. }
  155. public function testConstraints()
  156. {
  157. $constraint = new ForeignKeyConstraint(array(), "foo", array());
  158. $tableA = new Table("foo", array(), array(), array($constraint));
  159. $constraints = $tableA->getForeignKeys();
  160. $this->assertEquals(1, count($constraints));
  161. $this->assertSame($constraint, array_shift($constraints));
  162. }
  163. public function testOptions()
  164. {
  165. $table = new Table("foo", array(), array(), array(), false, array("foo" => "bar"));
  166. $this->assertTrue($table->hasOption("foo"));
  167. $this->assertEquals("bar", $table->getOption("foo"));
  168. }
  169. public function testBuilderSetPrimaryKey()
  170. {
  171. $table = new Table("foo");
  172. $table->addColumn("bar", 'integer');
  173. $table->setPrimaryKey(array("bar"));
  174. $this->assertTrue($table->hasIndex("primary"));
  175. $this->assertInstanceOf('Doctrine\DBAL\Schema\Index', $table->getPrimaryKey());
  176. $this->assertTrue($table->getIndex("primary")->isUnique());
  177. $this->assertTrue($table->getIndex("primary")->isPrimary());
  178. }
  179. public function testBuilderAddUniqueIndex()
  180. {
  181. $table = new Table("foo");
  182. $table->addColumn("bar", 'integer');
  183. $table->addUniqueIndex(array("bar"), "my_idx");
  184. $this->assertTrue($table->hasIndex("my_idx"));
  185. $this->assertTrue($table->getIndex("my_idx")->isUnique());
  186. $this->assertFalse($table->getIndex("my_idx")->isPrimary());
  187. }
  188. public function testBuilderAddIndex()
  189. {
  190. $table = new Table("foo");
  191. $table->addColumn("bar", 'integer');
  192. $table->addIndex(array("bar"), "my_idx");
  193. $this->assertTrue($table->hasIndex("my_idx"));
  194. $this->assertFalse($table->getIndex("my_idx")->isUnique());
  195. $this->assertFalse($table->getIndex("my_idx")->isPrimary());
  196. }
  197. public function testBuilderAddIndexWithInvalidNameThrowsException()
  198. {
  199. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  200. $table = new Table("foo");
  201. $table->addColumn("bar",'integer');
  202. $table->addIndex(array("bar"), "invalid name %&/");
  203. }
  204. public function testBuilderAddIndexWithUnknownColumnThrowsException()
  205. {
  206. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  207. $table = new Table("foo");
  208. $table->addIndex(array("bar"), "invalidName");
  209. }
  210. public function testBuilderOptions()
  211. {
  212. $table = new Table("foo");
  213. $table->addOption("foo", "bar");
  214. $this->assertTrue($table->hasOption("foo"));
  215. $this->assertEquals("bar", $table->getOption("foo"));
  216. }
  217. public function testAddForeignKeyConstraint_UnknownLocalColumn_ThrowsException()
  218. {
  219. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  220. $table = new Table("foo");
  221. $table->addColumn("id", 'integer');
  222. $foreignTable = new Table("bar");
  223. $foreignTable->addColumn("id", 'integer');
  224. $table->addForeignKeyConstraint($foreignTable, array("foo"), array("id"));
  225. }
  226. public function testAddForeignKeyConstraint_UnknownForeignColumn_ThrowsException()
  227. {
  228. $this->setExpectedException("Doctrine\DBAL\Schema\SchemaException");
  229. $table = new Table("foo");
  230. $table->addColumn("id", 'integer');
  231. $foreignTable = new Table("bar");
  232. $foreignTable->addColumn("id", 'integer');
  233. $table->addForeignKeyConstraint($foreignTable, array("id"), array("foo"));
  234. }
  235. public function testAddForeignKeyConstraint()
  236. {
  237. $table = new Table("foo");
  238. $table->addColumn("id", 'integer');
  239. $foreignTable = new Table("bar");
  240. $foreignTable->addColumn("id", 'integer');
  241. $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
  242. $constraints = $table->getForeignKeys();
  243. $this->assertEquals(1, count($constraints));
  244. $constraint = current($constraints);
  245. $this->assertInstanceOf('Doctrine\DBAL\Schema\ForeignKeyConstraint', $constraint);
  246. $this->assertTrue($constraint->hasOption("foo"));
  247. $this->assertEquals("bar", $constraint->getOption("foo"));
  248. }
  249. public function testAddIndexWithCaseSensitiveColumnProblem()
  250. {
  251. $table = new Table("foo");
  252. $table->addColumn("id", 'integer');
  253. $table->addIndex(array("ID"), "my_idx");
  254. $this->assertTrue($table->hasIndex('my_idx'));
  255. $this->assertEquals(array("ID"), $table->getIndex("my_idx")->getColumns());
  256. $this->assertTrue($table->getIndex('my_idx')->spansColumns(array('id')));
  257. }
  258. public function testAddPrimaryKey_ColumnsAreExplicitlySetToNotNull()
  259. {
  260. $table = new Table("foo");
  261. $column = $table->addColumn("id", 'integer', array('notnull' => false));
  262. $this->assertFalse($column->getNotnull());
  263. $table->setPrimaryKey(array('id'));
  264. $this->assertTrue($column->getNotnull());
  265. }
  266. /**
  267. * @group DDC-133
  268. */
  269. public function testAllowImplicitSchemaTableInAutogeneratedIndexNames()
  270. {
  271. $table = new Table("foo.bar");
  272. $table->addColumn('baz', 'integer', array());
  273. $table->addIndex(array('baz'));
  274. $this->assertEquals(1, count($table->getIndexes()));
  275. }
  276. /**
  277. * @group DBAL-50
  278. */
  279. public function testAddIndexTwice_IgnoreSecond()
  280. {
  281. $table = new Table("foo.bar");
  282. $table->addColumn('baz', 'integer', array());
  283. $table->addIndex(array('baz'));
  284. $table->addIndex(array('baz'));
  285. $this->assertEquals(1, count($table->getIndexes()));
  286. }
  287. /**
  288. * @group DBAL-50
  289. */
  290. public function testAddForeignKeyIndexImplicitly()
  291. {
  292. $table = new Table("foo");
  293. $table->addColumn("id", 'integer');
  294. $foreignTable = new Table("bar");
  295. $foreignTable->addColumn("id", 'integer');
  296. $table->addForeignKeyConstraint($foreignTable, array("id"), array("id"), array("foo" => "bar"));
  297. $indexes = $table->getIndexes();
  298. $this->assertEquals(1, count($indexes));
  299. $index = current($indexes);
  300. $this->assertTrue($table->hasIndex($index->getName()));
  301. $this->assertEquals(array('id'), $index->getColumns());
  302. }
  303. /**
  304. * @group DBAL-50
  305. */
  306. public function testOverruleIndex()
  307. {
  308. $table = new Table("bar");
  309. $table->addColumn('baz', 'integer', array());
  310. $table->addIndex(array('baz'));
  311. $indexes = $table->getIndexes();
  312. $this->assertEquals(1, count($indexes));
  313. $index = current($indexes);
  314. $table->addUniqueIndex(array('baz'));
  315. $this->assertEquals(1, count($table->getIndexes()));
  316. $this->assertFalse($table->hasIndex($index->getName()));
  317. }
  318. public function testPrimaryKeyOverrulesUniqueIndex()
  319. {
  320. $table = new Table("bar");
  321. $table->addColumn('baz', 'integer', array());
  322. $table->addUniqueIndex(array('baz'));
  323. $table->setPrimaryKey(array('baz'));
  324. $indexes = $table->getIndexes();
  325. $this->assertEquals(1, count($indexes), "Table should only contain the primary key table index, not the unique one anymore, because it was overruled.");
  326. $index = current($indexes);
  327. $this->assertTrue($index->isPrimary());
  328. }
  329. /**
  330. * @group DBAL-64
  331. */
  332. public function testQuotedTableName()
  333. {
  334. $table = new Table("`bar`");
  335. $mysqlPlatform = new \Doctrine\DBAL\Platforms\MySqlPlatform();
  336. $sqlitePlatform = new \Doctrine\DBAL\Platforms\SqlitePlatform();
  337. $this->assertEquals('bar', $table->getName());
  338. $this->assertEquals('`bar`', $table->getQuotedName($mysqlPlatform));
  339. $this->assertEquals('"bar"', $table->getQuotedName($sqlitePlatform));
  340. }
  341. /**
  342. * @group DBAL-79
  343. */
  344. public function testTableHasPrimaryKey()
  345. {
  346. $table = new Table("test");
  347. $this->assertFalse($table->hasPrimaryKey());
  348. $table->addColumn("foo", "integer");
  349. $table->setPrimaryKey(array("foo"));
  350. $this->assertTrue($table->hasPrimaryKey());
  351. }
  352. /**
  353. * @group DBAL-91
  354. */
  355. public function testAddIndexWithQuotedColumns()
  356. {
  357. $table = new Table("test");
  358. $table->addColumn('"foo"', 'integer');
  359. $table->addColumn('bar', 'integer');
  360. $table->addIndex(array('"foo"', '"bar"'));
  361. }
  362. /**
  363. * @group DBAL-91
  364. */
  365. public function testAddForeignKeyWithQuotedColumnsAndTable()
  366. {
  367. $table = new Table("test");
  368. $table->addColumn('"foo"', 'integer');
  369. $table->addColumn('bar', 'integer');
  370. $table->addForeignKeyConstraint('"boing"', array('"foo"', '"bar"'), array("id"));
  371. }
  372. /**
  373. * @group DBAL-177
  374. */
  375. public function testQuoteSchemaPrefixed()
  376. {
  377. $table = new Table("`test`.`test`");
  378. $this->assertEquals("test.test", $table->getName());
  379. $this->assertEquals("`test`.`test`", $table->getQuotedName(new \Doctrine\DBAL\Platforms\MySqlPlatform));
  380. }
  381. /**
  382. * @group DBAL-204
  383. */
  384. public function testFullQualifiedTableName()
  385. {
  386. $table = new Table("`test`.`test`");
  387. $this->assertEquals('test.test', $table->getFullQualifiedName("test"));
  388. $this->assertEquals('test.test', $table->getFullQualifiedName("other"));
  389. $table = new Table("test");
  390. $this->assertEquals('test.test', $table->getFullQualifiedName("test"));
  391. $this->assertEquals('other.test', $table->getFullQualifiedName("other"));
  392. }
  393. /**
  394. * @group DBAL-224
  395. */
  396. public function testDropIndex()
  397. {
  398. $table = new Table("test");
  399. $table->addColumn('id', 'integer');
  400. $table->addIndex(array('id'), 'idx');
  401. $this->assertTrue($table->hasIndex('idx'));
  402. $table->dropIndex('idx');
  403. $this->assertFalse($table->hasIndex('idx'));
  404. }
  405. /**
  406. * @group DBAL-224
  407. */
  408. public function testDropPrimaryKey()
  409. {
  410. $table = new Table("test");
  411. $table->addColumn('id', 'integer');
  412. $table->setPrimaryKey(array('id'));
  413. $this->assertTrue($table->hasPrimaryKey());
  414. $table->dropPrimaryKey();
  415. $this->assertFalse($table->hasPrimaryKey());
  416. }
  417. }