TableTest.php 9.6 KB

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