TableTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. * Dataprovider for invalid addConstraint calls.
  286. *
  287. * @return array
  288. */
  289. public static function addConstaintErrorProvider()
  290. {
  291. return [
  292. // No properties
  293. [[]],
  294. // Empty columns
  295. [['columns' => '', 'type' => Table::CONSTRAINT_UNIQUE]],
  296. [['columns' => [], 'type' => Table::CONSTRAINT_UNIQUE]],
  297. // Missing column
  298. [['columns' => ['derp'], 'type' => Table::CONSTRAINT_UNIQUE]],
  299. // Invalid type
  300. [['columns' => 'author_id', 'type' => 'derp']],
  301. ];
  302. }
  303. /**
  304. * Test that an exception is raised when constraints
  305. * are added for fields that do not exist.
  306. *
  307. * @dataProvider addConstaintErrorProvider
  308. * @expectedException \Cake\Database\Exception
  309. * @return void
  310. */
  311. public function testAddConstraintError($props)
  312. {
  313. $table = new Table('articles');
  314. $table->addColumn('author_id', 'integer');
  315. $table->addConstraint('author_idx', $props);
  316. }
  317. /**
  318. * Test adding an index.
  319. *
  320. * @return void
  321. */
  322. public function testAddIndex()
  323. {
  324. $table = new Table('articles');
  325. $table->addColumn('title', [
  326. 'type' => 'string'
  327. ]);
  328. $result = $table->addIndex('faster', [
  329. 'type' => 'index',
  330. 'columns' => ['title']
  331. ]);
  332. $this->assertSame($result, $table);
  333. $this->assertEquals(['faster'], $table->indexes());
  334. }
  335. /**
  336. * Dataprovider for invalid addIndex calls
  337. *
  338. * @return array
  339. */
  340. public static function addIndexErrorProvider()
  341. {
  342. return [
  343. // Empty
  344. [[]],
  345. // Invalid type
  346. [['columns' => 'author_id', 'type' => 'derp']],
  347. // No columns
  348. [['columns' => ''], 'type' => Table::INDEX_INDEX],
  349. [['columns' => [], 'type' => Table::INDEX_INDEX]],
  350. // Missing column
  351. [['columns' => ['not_there'], 'type' => Table::INDEX_INDEX]],
  352. ];
  353. }
  354. /**
  355. * Test that an exception is raised when indexes
  356. * are added for fields that do not exist.
  357. *
  358. * @dataProvider addIndexErrorProvider
  359. * @expectedException \Cake\Database\Exception
  360. * @return void
  361. */
  362. public function testAddIndexError($props)
  363. {
  364. $table = new Table('articles');
  365. $table->addColumn('author_id', 'integer');
  366. $table->addIndex('author_idx', $props);
  367. }
  368. /**
  369. * Test adding different kinds of indexes.
  370. *
  371. * @return void
  372. */
  373. public function testAddIndexTypes()
  374. {
  375. $table = new Table('articles');
  376. $table->addColumn('id', 'integer')
  377. ->addColumn('title', 'string')
  378. ->addColumn('author_id', 'integer');
  379. $table->addIndex('author_idx', [
  380. 'columns' => ['author_id'],
  381. 'type' => 'index'
  382. ])->addIndex('texty', [
  383. 'type' => 'fulltext',
  384. 'columns' => ['title']
  385. ]);
  386. $this->assertEquals(
  387. ['author_idx', 'texty'],
  388. $table->indexes()
  389. );
  390. }
  391. /**
  392. * Test getting the primary key.
  393. *
  394. * @return void
  395. */
  396. public function testPrimaryKey()
  397. {
  398. $table = new Table('articles');
  399. $table->addColumn('id', 'integer')
  400. ->addColumn('title', 'string')
  401. ->addColumn('author_id', 'integer')
  402. ->addConstraint('author_idx', [
  403. 'columns' => ['author_id'],
  404. 'type' => 'unique'
  405. ])->addConstraint('primary', [
  406. 'type' => 'primary',
  407. 'columns' => ['id']
  408. ]);
  409. $this->assertEquals(['id'], $table->primaryKey());
  410. $table = new Table('articles');
  411. $table->addColumn('id', 'integer')
  412. ->addColumn('title', 'string')
  413. ->addColumn('author_id', 'integer');
  414. $this->assertEquals([], $table->primaryKey());
  415. }
  416. /**
  417. * Test the options method.
  418. *
  419. * @return void
  420. */
  421. public function testOptions()
  422. {
  423. $table = new Table('articles');
  424. $options = [
  425. 'engine' => 'InnoDB'
  426. ];
  427. $return = $table->options($options);
  428. $this->assertInstanceOf('Cake\Database\Schema\Table', $return);
  429. $this->assertEquals($options, $table->options());
  430. }
  431. /**
  432. * Add a basic foreign key constraint.
  433. *
  434. * @return void
  435. */
  436. public function testAddConstraintForeignKey()
  437. {
  438. $table = new Table('articles');
  439. $table->addColumn('author_id', 'integer')
  440. ->addConstraint('author_id_idx', [
  441. 'type' => Table::CONSTRAINT_FOREIGN,
  442. 'columns' => ['author_id'],
  443. 'references' => ['authors', 'id'],
  444. 'update' => 'cascade',
  445. 'delete' => 'cascade',
  446. ]);
  447. $this->assertEquals(['author_id_idx'], $table->constraints());
  448. }
  449. /**
  450. * Test single column foreign keys constraint support
  451. *
  452. * @return void
  453. */
  454. public function testConstraintForeignKey()
  455. {
  456. $table = TableRegistry::get('ArticlesTags');
  457. $compositeConstraint = $table->schema()->constraint('tag_id_fk');
  458. $expected = [
  459. 'type' => 'foreign',
  460. 'columns' => ['tag_id'],
  461. 'references' => ['tags', 'id'],
  462. 'update' => 'cascade',
  463. 'delete' => 'cascade',
  464. 'length' => []
  465. ];
  466. $this->assertEquals($expected, $compositeConstraint);
  467. $expectedSubstring = 'CONSTRAINT <tag_id_fk> FOREIGN KEY \(<tag_id>\) REFERENCES <tags> \(<id>\)';
  468. $this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('test'))[0]);
  469. }
  470. /**
  471. * Test composite foreign keys support
  472. *
  473. * @return void
  474. */
  475. public function testConstraintForeignKeyTwoColumns()
  476. {
  477. $table = TableRegistry::get('Orders');
  478. $compositeConstraint = $table->schema()->constraint('product_category_fk');
  479. $expected = [
  480. 'type' => 'foreign',
  481. 'columns' => [
  482. 'product_category',
  483. 'product_id'
  484. ],
  485. 'references' => [
  486. 'products',
  487. ['category', 'id']
  488. ],
  489. 'update' => 'cascade',
  490. 'delete' => 'cascade',
  491. 'length' => []
  492. ];
  493. $this->assertEquals($expected, $compositeConstraint);
  494. $expectedSubstring = 'CONSTRAINT <product_category_fk> FOREIGN KEY \(<product_category>, <product_id>\)' .
  495. ' REFERENCES <products> \(<category>, <id>\)';
  496. $this->assertQuotedQuery($expectedSubstring, $table->schema()->createSql(ConnectionManager::get('test'))[0]);
  497. }
  498. /**
  499. * Provider for exceptionally bad foreign key data.
  500. *
  501. * @return array
  502. */
  503. public static function badForeignKeyProvider()
  504. {
  505. return [
  506. 'references is bad' => [[
  507. 'type' => Table::CONSTRAINT_FOREIGN,
  508. 'columns' => ['author_id'],
  509. 'references' => ['authors'],
  510. 'delete' => 'derp',
  511. ]],
  512. 'bad update value' => [[
  513. 'type' => Table::CONSTRAINT_FOREIGN,
  514. 'columns' => ['author_id'],
  515. 'references' => ['authors', 'id'],
  516. 'update' => 'derp',
  517. ]],
  518. 'bad delete value' => [[
  519. 'type' => Table::CONSTRAINT_FOREIGN,
  520. 'columns' => ['author_id'],
  521. 'references' => ['authors', 'id'],
  522. 'delete' => 'derp',
  523. ]],
  524. ];
  525. }
  526. /**
  527. * Add a foreign key constraint with bad data
  528. *
  529. * @dataProvider badForeignKeyProvider
  530. * @expectedException \Cake\Database\Exception
  531. * @return void
  532. */
  533. public function testAddConstraintForeignKeyBadData($data)
  534. {
  535. $table = new Table('articles');
  536. $table->addColumn('author_id', 'integer')
  537. ->addConstraint('author_id_idx', $data);
  538. }
  539. /**
  540. * Tests the temporary() method
  541. *
  542. * @return void
  543. */
  544. public function testTemporary()
  545. {
  546. $table = new Table('articles');
  547. $this->assertFalse($table->temporary());
  548. $this->assertSame($table, $table->temporary(true));
  549. $this->assertTrue($table->temporary());
  550. $table->temporary(false);
  551. $this->assertFalse($table->temporary());
  552. }
  553. /**
  554. * Assertion for comparing a regex pattern against a query having its identifiers
  555. * quoted. It accepts queries quoted with the characters `<` and `>`. If the third
  556. * parameter is set to true, it will alter the pattern to both accept quoted and
  557. * unquoted queries
  558. *
  559. * @param string $pattern
  560. * @param string $query the result to compare against
  561. * @param bool $optional
  562. * @return void
  563. */
  564. public function assertQuotedQuery($pattern, $query, $optional = false)
  565. {
  566. if ($optional) {
  567. $optional = '?';
  568. }
  569. $pattern = str_replace('<', '[`"\[]' . $optional, $pattern);
  570. $pattern = str_replace('>', '[`"\]]' . $optional, $pattern);
  571. $this->assertRegExp('#' . $pattern . '#', $query);
  572. }
  573. }