TableTest.php 8.9 KB

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