TableTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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\TestSuite\TestCase;
  18. /**
  19. * Test case for Table
  20. */
  21. class TableTest extends TestCase
  22. {
  23. /**
  24. * Test construction with columns
  25. *
  26. * @return void
  27. */
  28. public function testConstructWithColumns()
  29. {
  30. $columns = [
  31. 'id' => [
  32. 'type' => 'integer',
  33. 'length' => 11,
  34. ],
  35. 'title' => [
  36. 'type' => 'string',
  37. 'length' => 255
  38. ]
  39. ];
  40. $table = new Table('articles', $columns);
  41. $this->assertEquals(['id', 'title'], $table->columns());
  42. }
  43. /**
  44. * Test adding columns.
  45. *
  46. * @return void
  47. */
  48. public function testAddColumn()
  49. {
  50. $table = new Table('articles');
  51. $result = $table->addColumn('title', [
  52. 'type' => 'string',
  53. 'length' => 25,
  54. 'null' => false
  55. ]);
  56. $this->assertSame($table, $result);
  57. $this->assertEquals(['title'], $table->columns());
  58. $result = $table->addColumn('body', 'text');
  59. $this->assertSame($table, $result);
  60. $this->assertEquals(['title', 'body'], $table->columns());
  61. }
  62. /**
  63. * Test isNullable method
  64. *
  65. * @return void
  66. */
  67. public function testIsNullable()
  68. {
  69. $table = new Table('articles');
  70. $table->addColumn('title', [
  71. 'type' => 'string',
  72. 'length' => 25,
  73. 'null' => false
  74. ])->addColumn('tagline', [
  75. 'type' => 'string',
  76. 'length' => 25,
  77. 'null' => true
  78. ]);
  79. $this->assertFalse($table->isNullable('title'));
  80. $this->assertTrue($table->isNullable('tagline'));
  81. $this->assertTrue($table->isNullable('missing'));
  82. }
  83. /**
  84. * Test columnType method
  85. *
  86. * @return void
  87. */
  88. public function testColumnType()
  89. {
  90. $table = new Table('articles');
  91. $table->addColumn('title', [
  92. 'type' => 'string',
  93. 'length' => 25,
  94. 'null' => false
  95. ]);
  96. $this->assertEquals('string', $table->columnType('title'));
  97. $this->assertNull($table->columnType('not there'));
  98. }
  99. /**
  100. * Test columnType setter method
  101. *
  102. * @return void
  103. */
  104. public function testColumnTypeSet()
  105. {
  106. $table = new Table('articles');
  107. $table->addColumn('title', [
  108. 'type' => 'string',
  109. 'length' => 25,
  110. 'null' => false
  111. ]);
  112. $this->assertEquals('string', $table->columnType('title'));
  113. $table->columnType('title', 'json');
  114. $this->assertEquals('json', $table->columnType('title'));
  115. }
  116. /**
  117. * Attribute keys should be filtered and have defaults set.
  118. *
  119. * @return void
  120. */
  121. public function testAddColumnFiltersAttributes()
  122. {
  123. $table = new Table('articles');
  124. $table->addColumn('title', [
  125. 'type' => 'string'
  126. ]);
  127. $result = $table->column('title');
  128. $expected = [
  129. 'type' => 'string',
  130. 'length' => null,
  131. 'precision' => null,
  132. 'default' => null,
  133. 'null' => null,
  134. 'fixed' => null,
  135. 'comment' => null,
  136. ];
  137. $this->assertEquals($expected, $result);
  138. $table->addColumn('author_id', [
  139. 'type' => 'integer'
  140. ]);
  141. $result = $table->column('author_id');
  142. $expected = [
  143. 'type' => 'integer',
  144. 'length' => null,
  145. 'precision' => null,
  146. 'default' => null,
  147. 'null' => null,
  148. 'unsigned' => null,
  149. 'comment' => null,
  150. 'autoIncrement' => null,
  151. ];
  152. $this->assertEquals($expected, $result);
  153. $table->addColumn('amount', [
  154. 'type' => 'decimal'
  155. ]);
  156. $result = $table->column('amount');
  157. $expected = [
  158. 'type' => 'decimal',
  159. 'length' => null,
  160. 'precision' => null,
  161. 'default' => null,
  162. 'null' => null,
  163. 'unsigned' => null,
  164. 'comment' => null,
  165. ];
  166. $this->assertEquals($expected, $result);
  167. }
  168. /**
  169. * Test reading default values.
  170. *
  171. * @return void
  172. */
  173. public function testDefaultValues()
  174. {
  175. $table = new Table('articles');
  176. $table->addColumn('id', [
  177. 'type' => 'integer',
  178. 'default' => 0
  179. ])->addColumn('title', [
  180. 'type' => 'string',
  181. 'default' => 'A title'
  182. ])->addColumn('name', [
  183. 'type' => 'string',
  184. 'null' => false,
  185. 'default' => null,
  186. ])->addColumn('body', [
  187. 'type' => 'text',
  188. 'null' => true,
  189. 'default' => null,
  190. ]);
  191. $result = $table->defaultValues();
  192. $expected = [
  193. 'id' => 0,
  194. 'title' => 'A title',
  195. 'body' => null
  196. ];
  197. $this->assertEquals($expected, $result);
  198. }
  199. /**
  200. * Test adding an constraint.
  201. * >
  202. * @return void
  203. */
  204. public function testAddConstraint()
  205. {
  206. $table = new Table('articles');
  207. $table->addColumn('id', [
  208. 'type' => 'integer'
  209. ]);
  210. $result = $table->addConstraint('primary', [
  211. 'type' => 'primary',
  212. 'columns' => ['id']
  213. ]);
  214. $this->assertSame($result, $table);
  215. $this->assertEquals(['primary'], $table->constraints());
  216. }
  217. /**
  218. * Dataprovider for invalid addConstraint calls.
  219. *
  220. * @return array
  221. */
  222. public static function addConstaintErrorProvider()
  223. {
  224. return [
  225. // No properties
  226. [[]],
  227. // Empty columns
  228. [['columns' => '', 'type' => Table::CONSTRAINT_UNIQUE]],
  229. [['columns' => [], 'type' => Table::CONSTRAINT_UNIQUE]],
  230. // Missing column
  231. [['columns' => ['derp'], 'type' => Table::CONSTRAINT_UNIQUE]],
  232. // Invalid type
  233. [['columns' => 'author_id', 'type' => 'derp']],
  234. ];
  235. }
  236. /**
  237. * Test that an exception is raised when constraints
  238. * are added for fields that do not exist.
  239. *
  240. * @dataProvider addConstaintErrorProvider
  241. * @expectedException \Cake\Database\Exception
  242. * @return void
  243. */
  244. public function testAddConstraintError($props)
  245. {
  246. $table = new Table('articles');
  247. $table->addColumn('author_id', 'integer');
  248. $table->addConstraint('author_idx', $props);
  249. }
  250. /**
  251. * Test adding an index.
  252. *
  253. * @return void
  254. */
  255. public function testAddIndex()
  256. {
  257. $table = new Table('articles');
  258. $table->addColumn('title', [
  259. 'type' => 'string'
  260. ]);
  261. $result = $table->addIndex('faster', [
  262. 'type' => 'index',
  263. 'columns' => ['title']
  264. ]);
  265. $this->assertSame($result, $table);
  266. $this->assertEquals(['faster'], $table->indexes());
  267. }
  268. /**
  269. * Dataprovider for invalid addIndex calls
  270. *
  271. * @return array
  272. */
  273. public static function addIndexErrorProvider()
  274. {
  275. return [
  276. // Empty
  277. [[]],
  278. // Invalid type
  279. [['columns' => 'author_id', 'type' => 'derp']],
  280. // No columns
  281. [['columns' => ''], 'type' => Table::INDEX_INDEX],
  282. [['columns' => [], 'type' => Table::INDEX_INDEX]],
  283. // Missing column
  284. [['columns' => ['not_there'], 'type' => Table::INDEX_INDEX]],
  285. ];
  286. }
  287. /**
  288. * Test that an exception is raised when indexes
  289. * are added for fields that do not exist.
  290. *
  291. * @dataProvider addIndexErrorProvider
  292. * @expectedException \Cake\Database\Exception
  293. * @return void
  294. */
  295. public function testAddIndexError($props)
  296. {
  297. $table = new Table('articles');
  298. $table->addColumn('author_id', 'integer');
  299. $table->addIndex('author_idx', $props);
  300. }
  301. /**
  302. * Test adding different kinds of indexes.
  303. *
  304. * @return void
  305. */
  306. public function testAddIndexTypes()
  307. {
  308. $table = new Table('articles');
  309. $table->addColumn('id', 'integer')
  310. ->addColumn('title', 'string')
  311. ->addColumn('author_id', 'integer');
  312. $table->addIndex('author_idx', [
  313. 'columns' => ['author_id'],
  314. 'type' => 'index'
  315. ])->addIndex('texty', [
  316. 'type' => 'fulltext',
  317. 'columns' => ['title']
  318. ]);
  319. $this->assertEquals(
  320. ['author_idx', 'texty'],
  321. $table->indexes()
  322. );
  323. }
  324. /**
  325. * Test getting the primary key.
  326. *
  327. * @return void
  328. */
  329. public function testPrimaryKey()
  330. {
  331. $table = new Table('articles');
  332. $table->addColumn('id', 'integer')
  333. ->addColumn('title', 'string')
  334. ->addColumn('author_id', 'integer')
  335. ->addConstraint('author_idx', [
  336. 'columns' => ['author_id'],
  337. 'type' => 'unique'
  338. ])->addConstraint('primary', [
  339. 'type' => 'primary',
  340. 'columns' => ['id']
  341. ]);
  342. $this->assertEquals(['id'], $table->primaryKey());
  343. $table = new Table('articles');
  344. $table->addColumn('id', 'integer')
  345. ->addColumn('title', 'string')
  346. ->addColumn('author_id', 'integer');
  347. $this->assertEquals([], $table->primaryKey());
  348. }
  349. /**
  350. * Test the options method.
  351. *
  352. * @return void
  353. */
  354. public function testOptions()
  355. {
  356. $table = new Table('articles');
  357. $options = [
  358. 'engine' => 'InnoDB'
  359. ];
  360. $return = $table->options($options);
  361. $this->assertInstanceOf('Cake\Database\Schema\Table', $return);
  362. $this->assertEquals($options, $table->options());
  363. }
  364. /**
  365. * Add a basic foreign key constraint.
  366. *
  367. * @return void
  368. */
  369. public function testAddConstraintForeignKey()
  370. {
  371. $table = new Table('articles');
  372. $table->addColumn('author_id', 'integer')
  373. ->addConstraint('author_id_idx', [
  374. 'type' => Table::CONSTRAINT_FOREIGN,
  375. 'columns' => ['author_id'],
  376. 'references' => ['authors', 'id'],
  377. 'update' => 'cascade',
  378. 'delete' => 'cascade',
  379. ]);
  380. $this->assertEquals(['author_id_idx'], $table->constraints());
  381. }
  382. /**
  383. * Provider for exceptionally bad foreign key data.
  384. *
  385. * @return array
  386. */
  387. public static function badForeignKeyProvider()
  388. {
  389. return [
  390. 'references is bad' => [[
  391. 'type' => Table::CONSTRAINT_FOREIGN,
  392. 'columns' => ['author_id'],
  393. 'references' => ['authors'],
  394. 'delete' => 'derp',
  395. ]],
  396. 'bad update value' => [[
  397. 'type' => Table::CONSTRAINT_FOREIGN,
  398. 'columns' => ['author_id'],
  399. 'references' => ['authors', 'id'],
  400. 'update' => 'derp',
  401. ]],
  402. 'bad delete value' => [[
  403. 'type' => Table::CONSTRAINT_FOREIGN,
  404. 'columns' => ['author_id'],
  405. 'references' => ['authors', 'id'],
  406. 'delete' => 'derp',
  407. ]],
  408. ];
  409. }
  410. /**
  411. * Add a foreign key constraint with bad data
  412. *
  413. * @dataProvider badForeignKeyProvider
  414. * @expectedException \Cake\Database\Exception
  415. * @return void
  416. */
  417. public function testAddConstraintForeignKeyBadData($data)
  418. {
  419. $table = new Table('articles');
  420. $table->addColumn('author_id', 'integer')
  421. ->addConstraint('author_id_idx', $data);
  422. }
  423. /**
  424. * Tests the temporary() method
  425. *
  426. * @return void
  427. */
  428. public function testTemporary()
  429. {
  430. $table = new Table('articles');
  431. $this->assertFalse($table->temporary());
  432. $this->assertSame($table, $table->temporary(true));
  433. $this->assertTrue($table->temporary());
  434. $table->temporary(false);
  435. $this->assertFalse($table->temporary());
  436. }
  437. }