TableTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. ]);
  157. $result = $table->defaultValues();
  158. $expected = [
  159. 'id' => 0,
  160. 'title' => 'A title'
  161. ];
  162. $this->assertEquals($expected, $result);
  163. }
  164. /**
  165. * Test adding an constraint.
  166. *>
  167. * @return void
  168. */
  169. public function testAddConstraint() {
  170. $table = new Table('articles');
  171. $table->addColumn('id', [
  172. 'type' => 'integer'
  173. ]);
  174. $result = $table->addConstraint('primary', [
  175. 'type' => 'primary',
  176. 'columns' => ['id']
  177. ]);
  178. $this->assertSame($result, $table);
  179. $this->assertEquals(['primary'], $table->constraints());
  180. }
  181. /**
  182. * Dataprovider for invalid addConstraint calls.
  183. *
  184. * @return array
  185. */
  186. public static function addConstaintErrorProvider() {
  187. return [
  188. // No properties
  189. [[]],
  190. // Empty columns
  191. [['columns' => '', 'type' => Table::CONSTRAINT_UNIQUE]],
  192. [['columns' => [], 'type' => Table::CONSTRAINT_UNIQUE]],
  193. // Missing column
  194. [['columns' => ['derp'], 'type' => Table::CONSTRAINT_UNIQUE]],
  195. // Invalid type
  196. [['columns' => 'author_id', 'type' => 'derp']],
  197. ];
  198. }
  199. /**
  200. * Test that an exception is raised when constraints
  201. * are added for fields that do not exist.
  202. *
  203. * @dataProvider addConstaintErrorProvider
  204. * @expectedException \Cake\Database\Exception
  205. * @return void
  206. */
  207. public function testAddConstraintError($props) {
  208. $table = new Table('articles');
  209. $table->addColumn('author_id', 'integer');
  210. $table->addConstraint('author_idx', $props);
  211. }
  212. /**
  213. * Test adding an index.
  214. *
  215. * @return void
  216. */
  217. public function testAddIndex() {
  218. $table = new Table('articles');
  219. $table->addColumn('title', [
  220. 'type' => 'string'
  221. ]);
  222. $result = $table->addIndex('faster', [
  223. 'type' => 'index',
  224. 'columns' => ['title']
  225. ]);
  226. $this->assertSame($result, $table);
  227. $this->assertEquals(['faster'], $table->indexes());
  228. }
  229. /**
  230. * Dataprovider for invalid addIndex calls
  231. *
  232. * @return array
  233. */
  234. public static function addIndexErrorProvider() {
  235. return [
  236. // Empty
  237. [[]],
  238. // Invalid type
  239. [['columns' => 'author_id', 'type' => 'derp']],
  240. // No columns
  241. [['columns' => ''], 'type' => Table::INDEX_INDEX],
  242. [['columns' => [], 'type' => Table::INDEX_INDEX]],
  243. // Missing column
  244. [['columns' => ['not_there'], 'type' => Table::INDEX_INDEX]],
  245. ];
  246. }
  247. /**
  248. * Test that an exception is raised when indexes
  249. * are added for fields that do not exist.
  250. *
  251. * @dataProvider addIndexErrorProvider
  252. * @expectedException \Cake\Database\Exception
  253. * @return void
  254. */
  255. public function testAddIndexError($props) {
  256. $table = new Table('articles');
  257. $table->addColumn('author_id', 'integer');
  258. $table->addIndex('author_idx', $props);
  259. }
  260. /**
  261. * Test adding different kinds of indexes.
  262. *
  263. * @return void
  264. */
  265. public function testAddIndexTypes() {
  266. $table = new Table('articles');
  267. $table->addColumn('id', 'integer')
  268. ->addColumn('title', 'string')
  269. ->addColumn('author_id', 'integer');
  270. $table->addIndex('author_idx', [
  271. 'columns' => ['author_id'],
  272. 'type' => 'index'
  273. ])->addIndex('texty', [
  274. 'type' => 'fulltext',
  275. 'columns' => ['title']
  276. ]);
  277. $this->assertEquals(
  278. ['author_idx', 'texty'],
  279. $table->indexes()
  280. );
  281. }
  282. /**
  283. * Test getting the primary key.
  284. *
  285. * @return void
  286. */
  287. public function testPrimaryKey() {
  288. $table = new Table('articles');
  289. $table->addColumn('id', 'integer')
  290. ->addColumn('title', 'string')
  291. ->addColumn('author_id', 'integer')
  292. ->addConstraint('author_idx', [
  293. 'columns' => ['author_id'],
  294. 'type' => 'unique'
  295. ])->addConstraint('primary', [
  296. 'type' => 'primary',
  297. 'columns' => ['id']
  298. ]);
  299. $this->assertEquals(['id'], $table->primaryKey());
  300. }
  301. /**
  302. * Test the options method.
  303. *
  304. * @return void
  305. */
  306. public function testOptions() {
  307. $table = new Table('articles');
  308. $options = [
  309. 'engine' => 'InnoDB'
  310. ];
  311. $return = $table->options($options);
  312. $this->assertInstanceOf('Cake\Database\Schema\Table', $return);
  313. $this->assertEquals($options, $table->options());
  314. }
  315. /**
  316. * Add a basic foreign key constraint.
  317. *
  318. * @return void
  319. */
  320. public function testAddConstraintForeignKey() {
  321. $table = new Table('articles');
  322. $table->addColumn('author_id', 'integer')
  323. ->addConstraint('author_id_idx', [
  324. 'type' => Table::CONSTRAINT_FOREIGN,
  325. 'columns' => ['author_id'],
  326. 'references' => ['authors', 'id'],
  327. 'update' => 'cascade',
  328. 'delete' => 'cascade',
  329. ]);
  330. $this->assertEquals(['author_id_idx'], $table->constraints());
  331. }
  332. /**
  333. * Provider for exceptionally bad foreign key data.
  334. *
  335. * @return array
  336. */
  337. public static function badForeignKeyProvider() {
  338. return [
  339. 'references is bad' => [[
  340. 'type' => Table::CONSTRAINT_FOREIGN,
  341. 'columns' => ['author_id'],
  342. 'references' => ['authors'],
  343. 'delete' => 'derp',
  344. ]],
  345. 'bad update value' => [[
  346. 'type' => Table::CONSTRAINT_FOREIGN,
  347. 'columns' => ['author_id'],
  348. 'references' => ['authors', 'id'],
  349. 'update' => 'derp',
  350. ]],
  351. 'bad delete value' => [[
  352. 'type' => Table::CONSTRAINT_FOREIGN,
  353. 'columns' => ['author_id'],
  354. 'references' => ['authors', 'id'],
  355. 'delete' => 'derp',
  356. ]],
  357. ];
  358. }
  359. /**
  360. * Add a foreign key constraint with bad data
  361. *
  362. * @dataProvider badForeignKeyProvider
  363. * @expectedException \Cake\Database\Exception
  364. * @return void
  365. */
  366. public function testAddConstraintForeignKeyBadData($data) {
  367. $table = new Table('articles');
  368. $table->addColumn('author_id', 'integer')
  369. ->addConstraint('author_id_idx', $data);
  370. }
  371. /**
  372. * Tests the temporary() method
  373. *
  374. * @return void
  375. */
  376. public function testTemporary() {
  377. $table = new Table('articles');
  378. $this->assertFalse($table->temporary());
  379. $this->assertSame($table, $table->temporary(true));
  380. $this->assertTrue($table->temporary());
  381. $table->temporary(false);
  382. $this->assertFalse($table->temporary());
  383. }
  384. }