TableTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Schema;
  16. use Cake\Database\Schema\Table;
  17. use Cake\Database\Type;
  18. use Cake\Datasource\ConnectionManager;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Mock class for testing baseType inheritance
  23. *
  24. */
  25. class FooType extends Type
  26. {
  27. public function getBaseType()
  28. {
  29. return 'integer';
  30. }
  31. }
  32. /**
  33. * Test case for Table
  34. */
  35. class TableTest extends TestCase
  36. {
  37. public $fixtures = [
  38. 'core.articles',
  39. 'core.tags',
  40. 'core.articles_tags',
  41. 'core.orders',
  42. 'core.products'
  43. ];
  44. protected $_map;
  45. public function setUp()
  46. {
  47. $this->_map = Type::map();
  48. parent::setUp();
  49. }
  50. public function tearDown()
  51. {
  52. TableRegistry::clear();
  53. Type::clear();
  54. Type::map($this->_map);
  55. parent::tearDown();
  56. }
  57. /**
  58. * Test construction with columns
  59. *
  60. * @return void
  61. */
  62. public function testConstructWithColumns()
  63. {
  64. $columns = [
  65. 'id' => [
  66. 'type' => 'integer',
  67. 'length' => 11,
  68. ],
  69. 'title' => [
  70. 'type' => 'string',
  71. 'length' => 255
  72. ]
  73. ];
  74. $table = new Table('articles', $columns);
  75. $this->assertEquals(['id', 'title'], $table->columns());
  76. }
  77. /**
  78. * Test adding columns.
  79. *
  80. * @return void
  81. */
  82. public function testAddColumn()
  83. {
  84. $table = new Table('articles');
  85. $result = $table->addColumn('title', [
  86. 'type' => 'string',
  87. 'length' => 25,
  88. 'null' => false
  89. ]);
  90. $this->assertSame($table, $result);
  91. $this->assertEquals(['title'], $table->columns());
  92. $result = $table->addColumn('body', 'text');
  93. $this->assertSame($table, $result);
  94. $this->assertEquals(['title', 'body'], $table->columns());
  95. }
  96. /**
  97. * Test isNullable method
  98. *
  99. * @return void
  100. */
  101. public function testIsNullable()
  102. {
  103. $table = new Table('articles');
  104. $table->addColumn('title', [
  105. 'type' => 'string',
  106. 'length' => 25,
  107. 'null' => false
  108. ])->addColumn('tagline', [
  109. 'type' => 'string',
  110. 'length' => 25,
  111. 'null' => true
  112. ]);
  113. $this->assertFalse($table->isNullable('title'));
  114. $this->assertTrue($table->isNullable('tagline'));
  115. $this->assertTrue($table->isNullable('missing'));
  116. }
  117. /**
  118. * Test columnType method
  119. *
  120. * @return void
  121. */
  122. public function testColumnType()
  123. {
  124. $table = new Table('articles');
  125. $table->addColumn('title', [
  126. 'type' => 'string',
  127. 'length' => 25,
  128. 'null' => false
  129. ]);
  130. $this->assertEquals('string', $table->columnType('title'));
  131. $this->assertNull($table->columnType('not there'));
  132. }
  133. /**
  134. * Test columnType setter method
  135. *
  136. * @return void
  137. */
  138. public function testColumnTypeSet()
  139. {
  140. $table = new Table('articles');
  141. $table->addColumn('title', [
  142. 'type' => 'string',
  143. 'length' => 25,
  144. 'null' => false
  145. ]);
  146. $this->assertEquals('string', $table->columnType('title'));
  147. $table->columnType('title', 'json');
  148. $this->assertEquals('json', $table->columnType('title'));
  149. }
  150. /**
  151. * Tests getting the baseType as configured when creating the column
  152. *
  153. * @return void
  154. */
  155. public function testBaseColumnType()
  156. {
  157. $table = new Table('articles');
  158. $table->addColumn('title', [
  159. 'type' => 'json',
  160. 'baseType' => 'text',
  161. 'length' => 25,
  162. 'null' => false
  163. ]);
  164. $this->assertEquals('json', $table->columnType('title'));
  165. $this->assertEquals('text', $table->baseColumnType('title'));
  166. }
  167. /**
  168. * Tests getting the base type as it is retuned by the Type class
  169. *
  170. * @return void
  171. */
  172. public function testBaseColumnTypeInherited()
  173. {
  174. Type::map('foo', __NAMESPACE__ . '\FooType');
  175. $table = new Table('articles');
  176. $table->addColumn('thing', [
  177. 'type' => 'foo',
  178. 'null' => false
  179. ]);
  180. $this->assertEquals('foo', $table->columnType('thing'));
  181. $this->assertEquals('integer', $table->baseColumnType('thing'));
  182. }
  183. /**
  184. * Attribute keys should be filtered and have defaults set.
  185. *
  186. * @return void
  187. */
  188. public function testAddColumnFiltersAttributes()
  189. {
  190. $table = new Table('articles');
  191. $table->addColumn('title', [
  192. 'type' => 'string'
  193. ]);
  194. $result = $table->column('title');
  195. $expected = [
  196. 'type' => 'string',
  197. 'length' => null,
  198. 'precision' => null,
  199. 'default' => null,
  200. 'null' => null,
  201. 'fixed' => null,
  202. 'comment' => null,
  203. ];
  204. $this->assertEquals($expected, $result);
  205. $table->addColumn('author_id', [
  206. 'type' => 'integer'
  207. ]);
  208. $result = $table->column('author_id');
  209. $expected = [
  210. 'type' => 'integer',
  211. 'length' => null,
  212. 'precision' => null,
  213. 'default' => null,
  214. 'null' => null,
  215. 'unsigned' => null,
  216. 'comment' => null,
  217. 'autoIncrement' => null,
  218. ];
  219. $this->assertEquals($expected, $result);
  220. $table->addColumn('amount', [
  221. 'type' => 'decimal'
  222. ]);
  223. $result = $table->column('amount');
  224. $expected = [
  225. 'type' => 'decimal',
  226. 'length' => null,
  227. 'precision' => null,
  228. 'default' => null,
  229. 'null' => null,
  230. 'unsigned' => null,
  231. 'comment' => null,
  232. ];
  233. $this->assertEquals($expected, $result);
  234. }
  235. /**
  236. * Test reading default values.
  237. *
  238. * @return void
  239. */
  240. public function testDefaultValues()
  241. {
  242. $table = new Table('articles');
  243. $table->addColumn('id', [
  244. 'type' => 'integer',
  245. 'default' => 0
  246. ])->addColumn('title', [
  247. 'type' => 'string',
  248. 'default' => 'A title'
  249. ])->addColumn('name', [
  250. 'type' => 'string',
  251. 'null' => false,
  252. 'default' => null,
  253. ])->addColumn('body', [
  254. 'type' => 'text',
  255. 'null' => true,
  256. 'default' => null,
  257. ]);
  258. $result = $table->defaultValues();
  259. $expected = [
  260. 'id' => 0,
  261. 'title' => 'A title',
  262. 'body' => null
  263. ];
  264. $this->assertEquals($expected, $result);
  265. }
  266. /**
  267. * Test adding an constraint.
  268. * >
  269. * @return void
  270. */
  271. public function testAddConstraint()
  272. {
  273. $table = new Table('articles');
  274. $table->addColumn('id', [
  275. 'type' => 'integer'
  276. ]);
  277. $result = $table->addConstraint('primary', [
  278. 'type' => 'primary',
  279. 'columns' => ['id']
  280. ]);
  281. $this->assertSame($result, $table);
  282. $this->assertEquals(['primary'], $table->constraints());
  283. }
  284. /**
  285. * Test adding an constraint with an overlapping unique index
  286. * >
  287. * @return void
  288. */
  289. public function testAddConstraintOverwriteUniqueIndex()
  290. {
  291. $table = new Table('articles');
  292. $table->addColumn('project_id', [
  293. 'type' => 'integer',
  294. 'default' => null,
  295. 'limit' => 11,
  296. 'null' => false,
  297. ])->addColumn('id', [
  298. 'type' => 'integer',
  299. 'autoIncrement' => true,
  300. 'limit' => 11
  301. ])->addColumn('user_id', [
  302. 'type' => 'integer',
  303. 'default' => null,
  304. 'limit' => 11,
  305. 'null' => false,
  306. ])->addConstraint('users_idx', [
  307. 'type' => 'unique',
  308. 'columns' => ['project_id', 'user_id']
  309. ])->addConstraint('users_idx', [
  310. 'type' => 'foreign',
  311. 'references' => ['users', 'project_id', 'id'],
  312. 'columns' => ['project_id', 'user_id']
  313. ]);
  314. $this->assertEquals(['users_idx'], $table->constraints());
  315. }
  316. /**
  317. * Dataprovider for invalid addConstraint calls.
  318. *
  319. * @return array
  320. */
  321. public static function addConstaintErrorProvider()
  322. {
  323. return [
  324. // No properties
  325. [[]],
  326. // Empty columns
  327. [['columns' => '', 'type' => Table::CONSTRAINT_UNIQUE]],
  328. [['columns' => [], 'type' => Table::CONSTRAINT_UNIQUE]],
  329. // Missing column
  330. [['columns' => ['derp'], 'type' => Table::CONSTRAINT_UNIQUE]],
  331. // Invalid type
  332. [['columns' => 'author_id', 'type' => 'derp']],
  333. ];
  334. }
  335. /**
  336. * Test that an exception is raised when constraints
  337. * are added for fields that do not exist.
  338. *
  339. * @dataProvider addConstaintErrorProvider
  340. * @expectedException \Cake\Database\Exception
  341. * @return void
  342. */
  343. public function testAddConstraintError($props)
  344. {
  345. $table = new Table('articles');
  346. $table->addColumn('author_id', 'integer');
  347. $table->addConstraint('author_idx', $props);
  348. }
  349. /**
  350. * Test adding an index.
  351. *
  352. * @return void
  353. */
  354. public function testAddIndex()
  355. {
  356. $table = new Table('articles');
  357. $table->addColumn('title', [
  358. 'type' => 'string'
  359. ]);
  360. $result = $table->addIndex('faster', [
  361. 'type' => 'index',
  362. 'columns' => ['title']
  363. ]);
  364. $this->assertSame($result, $table);
  365. $this->assertEquals(['faster'], $table->indexes());
  366. }
  367. /**
  368. * Dataprovider for invalid addIndex calls
  369. *
  370. * @return array
  371. */
  372. public static function addIndexErrorProvider()
  373. {
  374. return [
  375. // Empty
  376. [[]],
  377. // Invalid type
  378. [['columns' => 'author_id', 'type' => 'derp']],
  379. // No columns
  380. [['columns' => ''], 'type' => Table::INDEX_INDEX],
  381. [['columns' => [], 'type' => Table::INDEX_INDEX]],
  382. // Missing column
  383. [['columns' => ['not_there'], 'type' => Table::INDEX_INDEX]],
  384. ];
  385. }
  386. /**
  387. * Test that an exception is raised when indexes
  388. * are added for fields that do not exist.
  389. *
  390. * @dataProvider addIndexErrorProvider
  391. * @expectedException \Cake\Database\Exception
  392. * @return void
  393. */
  394. public function testAddIndexError($props)
  395. {
  396. $table = new Table('articles');
  397. $table->addColumn('author_id', 'integer');
  398. $table->addIndex('author_idx', $props);
  399. }
  400. /**
  401. * Test adding different kinds of indexes.
  402. *
  403. * @return void
  404. */
  405. public function testAddIndexTypes()
  406. {
  407. $table = new Table('articles');
  408. $table->addColumn('id', 'integer')
  409. ->addColumn('title', 'string')
  410. ->addColumn('author_id', 'integer');
  411. $table->addIndex('author_idx', [
  412. 'columns' => ['author_id'],
  413. 'type' => 'index'
  414. ])->addIndex('texty', [
  415. 'type' => 'fulltext',
  416. 'columns' => ['title']
  417. ]);
  418. $this->assertEquals(
  419. ['author_idx', 'texty'],
  420. $table->indexes()
  421. );
  422. }
  423. /**
  424. * Test getting the primary key.
  425. *
  426. * @return void
  427. */
  428. public function testPrimaryKey()
  429. {
  430. $table = new Table('articles');
  431. $table->addColumn('id', 'integer')
  432. ->addColumn('title', 'string')
  433. ->addColumn('author_id', 'integer')
  434. ->addConstraint('author_idx', [
  435. 'columns' => ['author_id'],
  436. 'type' => 'unique'
  437. ])->addConstraint('primary', [
  438. 'type' => 'primary',
  439. 'columns' => ['id']
  440. ]);
  441. $this->assertEquals(['id'], $table->primaryKey());
  442. $table = new Table('articles');
  443. $table->addColumn('id', 'integer')
  444. ->addColumn('title', 'string')
  445. ->addColumn('author_id', 'integer');
  446. $this->assertEquals([], $table->primaryKey());
  447. }
  448. /**
  449. * Test the options method.
  450. *
  451. * @return void
  452. */
  453. public function testOptions()
  454. {
  455. $table = new Table('articles');
  456. $options = [
  457. 'engine' => 'InnoDB'
  458. ];
  459. $return = $table->options($options);
  460. $this->assertInstanceOf('Cake\Database\Schema\Table', $return);
  461. $this->assertEquals($options, $table->options());
  462. }
  463. /**
  464. * Add a basic foreign key constraint.
  465. *
  466. * @return void
  467. */
  468. public function testAddConstraintForeignKey()
  469. {
  470. $table = new Table('articles');
  471. $table->addColumn('author_id', 'integer')
  472. ->addConstraint('author_id_idx', [
  473. 'type' => Table::CONSTRAINT_FOREIGN,
  474. 'columns' => ['author_id'],
  475. 'references' => ['authors', 'id'],
  476. 'update' => 'cascade',
  477. 'delete' => 'cascade',
  478. ]);
  479. $this->assertEquals(['author_id_idx'], $table->constraints());
  480. }
  481. /**
  482. * Test single column foreign keys constraint support
  483. *
  484. * @return void
  485. */
  486. public function testConstraintForeignKey()
  487. {
  488. $table = TableRegistry::get('ArticlesTags');
  489. $compositeConstraint = $table->schema()->constraint('tag_id_fk');
  490. $expected = [
  491. 'type' => 'foreign',
  492. 'columns' => ['tag_id'],
  493. 'references' => ['tags', 'id'],
  494. 'update' => 'cascade',
  495. 'delete' => 'cascade',
  496. 'length' => []
  497. ];
  498. $this->assertEquals($expected, $compositeConstraint);
  499. $expectedSubstring = 'CONSTRAINT <tag_id_fk> FOREIGN KEY \(<tag_id>\) REFERENCES <tags> \(<id>\)';
  500. $this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('test'))[0]);
  501. }
  502. /**
  503. * Test composite foreign keys support
  504. *
  505. * @return void
  506. */
  507. public function testConstraintForeignKeyTwoColumns()
  508. {
  509. $table = TableRegistry::get('Orders');
  510. $compositeConstraint = $table->schema()->constraint('product_category_fk');
  511. $expected = [
  512. 'type' => 'foreign',
  513. 'columns' => [
  514. 'product_category',
  515. 'product_id'
  516. ],
  517. 'references' => [
  518. 'products',
  519. ['category', 'id']
  520. ],
  521. 'update' => 'cascade',
  522. 'delete' => 'cascade',
  523. 'length' => []
  524. ];
  525. $this->assertEquals($expected, $compositeConstraint);
  526. $expectedSubstring = 'CONSTRAINT <product_category_fk> FOREIGN KEY \(<product_category>, <product_id>\)' .
  527. ' REFERENCES <products> \(<category>, <id>\)';
  528. $this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('test'))[0]);
  529. }
  530. /**
  531. * Provider for exceptionally bad foreign key data.
  532. *
  533. * @return array
  534. */
  535. public static function badForeignKeyProvider()
  536. {
  537. return [
  538. 'references is bad' => [[
  539. 'type' => Table::CONSTRAINT_FOREIGN,
  540. 'columns' => ['author_id'],
  541. 'references' => ['authors'],
  542. 'delete' => 'derp',
  543. ]],
  544. 'bad update value' => [[
  545. 'type' => Table::CONSTRAINT_FOREIGN,
  546. 'columns' => ['author_id'],
  547. 'references' => ['authors', 'id'],
  548. 'update' => 'derp',
  549. ]],
  550. 'bad delete value' => [[
  551. 'type' => Table::CONSTRAINT_FOREIGN,
  552. 'columns' => ['author_id'],
  553. 'references' => ['authors', 'id'],
  554. 'delete' => 'derp',
  555. ]],
  556. ];
  557. }
  558. /**
  559. * Add a foreign key constraint with bad data
  560. *
  561. * @dataProvider badForeignKeyProvider
  562. * @expectedException \Cake\Database\Exception
  563. * @return void
  564. */
  565. public function testAddConstraintForeignKeyBadData($data)
  566. {
  567. $table = new Table('articles');
  568. $table->addColumn('author_id', 'integer')
  569. ->addConstraint('author_id_idx', $data);
  570. }
  571. /**
  572. * Tests the temporary() method
  573. *
  574. * @return void
  575. */
  576. public function testTemporary()
  577. {
  578. $table = new Table('articles');
  579. $this->assertFalse($table->temporary());
  580. $this->assertSame($table, $table->temporary(true));
  581. $this->assertTrue($table->temporary());
  582. $table->temporary(false);
  583. $this->assertFalse($table->temporary());
  584. }
  585. /**
  586. * Assertion for comparing a regex pattern against a query having its identifiers
  587. * quoted. It accepts queries quoted with the characters `<` and `>`. If the third
  588. * parameter is set to true, it will alter the pattern to both accept quoted and
  589. * unquoted queries
  590. *
  591. * @param string $pattern
  592. * @param string $query the result to compare against
  593. * @param bool $optional
  594. * @return void
  595. */
  596. public function assertQuotedQuery($pattern, $query, $optional = false)
  597. {
  598. if ($optional) {
  599. $optional = '?';
  600. }
  601. $pattern = str_replace('<', '[`"\[]' . $optional, $pattern);
  602. $pattern = str_replace('>', '[`"\]]' . $optional, $pattern);
  603. $this->assertRegExp('#' . $pattern . '#', $query);
  604. }
  605. }