TableTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Test\TestCase\Database\Schema;
  18. use Cake\Database\Schema\Table;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Test case for Table
  22. */
  23. class TableTest extends TestCase {
  24. /**
  25. * Test construction with columns
  26. *
  27. * @return void
  28. */
  29. public function testConstructWithColumns() {
  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. $table = new Table('articles');
  50. $result = $table->addColumn('title', [
  51. 'type' => 'string',
  52. 'length' => 25,
  53. 'null' => false
  54. ]);
  55. $this->assertSame($table, $result);
  56. $this->assertEquals(['title'], $table->columns());
  57. $result = $table->addColumn('body', 'text');
  58. $this->assertSame($table, $result);
  59. $this->assertEquals(['title', 'body'], $table->columns());
  60. }
  61. /**
  62. * Test columnType method
  63. *
  64. * @return void
  65. */
  66. public function testColumnType() {
  67. $table = new Table('articles');
  68. $table->addColumn('title', [
  69. 'type' => 'string',
  70. 'length' => 25,
  71. 'null' => false
  72. ]);
  73. $this->assertEquals('string', $table->columnType('title'));
  74. $this->assertNull($table->columnType('not there'));
  75. }
  76. /**
  77. * Attribute keys should be filtered and have defaults set.
  78. *
  79. * @return void
  80. */
  81. public function testAddColumnFiltersAttributes() {
  82. $table = new Table('articles');
  83. $table->addColumn('title', [
  84. 'type' => 'string'
  85. ]);
  86. $result = $table->column('title');
  87. $expected = [
  88. 'type' => 'string',
  89. 'length' => null,
  90. 'precision' => null,
  91. 'default' => null,
  92. 'null' => null,
  93. 'fixed' => null,
  94. 'comment' => null,
  95. ];
  96. $this->assertEquals($expected, $result);
  97. $table->addColumn('author_id', [
  98. 'type' => 'integer'
  99. ]);
  100. $result = $table->column('author_id');
  101. $expected = [
  102. 'type' => 'integer',
  103. 'length' => null,
  104. 'precision' => null,
  105. 'default' => null,
  106. 'null' => null,
  107. 'unsigned' => null,
  108. 'comment' => null,
  109. 'autoIncrement' => null,
  110. ];
  111. $this->assertEquals($expected, $result);
  112. $table->addColumn('amount', [
  113. 'type' => 'decimal'
  114. ]);
  115. $result = $table->column('amount');
  116. $expected = [
  117. 'type' => 'decimal',
  118. 'length' => null,
  119. 'precision' => null,
  120. 'default' => null,
  121. 'null' => null,
  122. 'unsigned' => null,
  123. 'comment' => null,
  124. ];
  125. $this->assertEquals($expected, $result);
  126. }
  127. /**
  128. * Test adding an constraint.
  129. *
  130. * @return void
  131. */
  132. public function testAddConstraint() {
  133. $table = new Table('articles');
  134. $table->addColumn('id', [
  135. 'type' => 'integer'
  136. ]);
  137. $result = $table->addConstraint('primary', [
  138. 'type' => 'primary',
  139. 'columns' => ['id']
  140. ]);
  141. $this->assertSame($result, $table);
  142. $this->assertEquals(['primary'], $table->constraints());
  143. }
  144. /**
  145. * Dataprovider for invalid addConstraint calls.
  146. *
  147. * @return array
  148. */
  149. public static function addConstaintErrorProvider() {
  150. return [
  151. // No properties
  152. [[]],
  153. // Empty columns
  154. [['columns' => '']],
  155. [['columns' => []]],
  156. // Missing column
  157. [['columns' => ['derp']]],
  158. // Invalid type
  159. [['columns' => 'author_id', 'type' => 'derp']],
  160. ];
  161. }
  162. /**
  163. * Test that an exception is raised when constraints
  164. * are added for fields that do not exist.
  165. *
  166. * @dataProvider addConstaintErrorProvider
  167. * @expectedException Cake\Database\Exception
  168. * @return void
  169. */
  170. public function testAddConstraintError($props) {
  171. $table = new Table('articles');
  172. $table->addColumn('author_id', 'integer');
  173. $table->addConstraint('author_idx', $props);
  174. }
  175. /**
  176. * Test adding an index.
  177. *
  178. * @return void
  179. */
  180. public function testAddIndex() {
  181. $table = new Table('articles');
  182. $table->addColumn('title', [
  183. 'type' => 'string'
  184. ]);
  185. $result = $table->addIndex('faster', [
  186. 'type' => 'index',
  187. 'columns' => ['title']
  188. ]);
  189. $this->assertSame($result, $table);
  190. $this->assertEquals(['faster'], $table->indexes());
  191. }
  192. /**
  193. * Dataprovider for invalid addIndex calls
  194. *
  195. * @return array
  196. */
  197. public static function addIndexErrorProvider() {
  198. return [
  199. // Empty
  200. [[]],
  201. // No columns
  202. [['columns' => '']],
  203. [['columns' => []]],
  204. // Missing column
  205. [['columns' => ['not_there']]],
  206. // Invalid type
  207. [['columns' => 'author_id', 'type' => 'derp']],
  208. ];
  209. }
  210. /**
  211. * Test that an exception is raised when indexes
  212. * are added for fields that do not exist.
  213. *
  214. * @dataProvider addIndexErrorProvider
  215. * @expectedException Cake\Database\Exception
  216. * @return void
  217. */
  218. public function testAddIndexError($props) {
  219. $table = new Table('articles');
  220. $table->addColumn('author_id', 'integer');
  221. $table->addIndex('author_idx', $props);
  222. }
  223. /**
  224. * Test adding different kinds of indexes.
  225. *
  226. * @return void
  227. */
  228. public function testAddIndexTypes() {
  229. $table = new Table('articles');
  230. $table->addColumn('id', 'integer')
  231. ->addColumn('title', 'string')
  232. ->addColumn('author_id', 'integer');
  233. $table->addIndex('author_idx', [
  234. 'columns' => ['author_id'],
  235. 'type' => 'index'
  236. ])->addIndex('texty', [
  237. 'type' => 'fulltext',
  238. 'columns' => ['title']
  239. ]);
  240. $this->assertEquals(
  241. ['author_idx', 'texty'],
  242. $table->indexes()
  243. );
  244. }
  245. /**
  246. * Test getting the primary key.
  247. *
  248. * @return void
  249. */
  250. public function testPrimaryKey() {
  251. $table = new Table('articles');
  252. $table->addColumn('id', 'integer')
  253. ->addColumn('title', 'string')
  254. ->addColumn('author_id', 'integer')
  255. ->addConstraint('author_idx', [
  256. 'columns' => ['author_id'],
  257. 'type' => 'unique'
  258. ])->addConstraint('primary', [
  259. 'type' => 'primary',
  260. 'columns' => ['id']
  261. ]);
  262. $this->assertEquals(['id'], $table->primaryKey());
  263. }
  264. /**
  265. * Test the options method.
  266. *
  267. * @return void
  268. */
  269. public function testOptions() {
  270. $table = new Table('articles');
  271. $options = [
  272. 'engine' => 'InnoDB'
  273. ];
  274. $return = $table->options($options);
  275. $this->assertInstanceOf('Cake\Database\Schema\Table', $return);
  276. $this->assertEquals($options, $table->options());
  277. }
  278. /**
  279. * Add a basic foreign key constraint.
  280. *
  281. * @return void
  282. */
  283. public function testAddConstraintForeignKey() {
  284. $table = new Table('articles');
  285. $table->addColumn('author_id', 'integer')
  286. ->addConstraint('author_id_idx', [
  287. 'type' => Table::CONSTRAINT_FOREIGN,
  288. 'columns' => ['author_id'],
  289. 'references' => ['authors', 'id'],
  290. 'update' => 'cascade',
  291. 'delete' => 'cascade',
  292. ]);
  293. $this->assertEquals(['author_id_idx'], $table->constraints());
  294. }
  295. /**
  296. * Provider for exceptionally bad foreign key data.
  297. *
  298. * @return array
  299. */
  300. public static function badForeignKeyProvider() {
  301. return [
  302. 'references is bad' => [[
  303. 'type' => Table::CONSTRAINT_FOREIGN,
  304. 'columns' => ['author_id'],
  305. 'references' => ['authors'],
  306. 'delete' => 'derp',
  307. ]],
  308. 'bad update value' => [[
  309. 'type' => Table::CONSTRAINT_FOREIGN,
  310. 'columns' => ['author_id'],
  311. 'references' => ['authors', 'id'],
  312. 'update' => 'derp',
  313. ]],
  314. 'bad delete value' => [[
  315. 'type' => Table::CONSTRAINT_FOREIGN,
  316. 'columns' => ['author_id'],
  317. 'references' => ['authors', 'id'],
  318. 'delete' => 'derp',
  319. ]],
  320. ];
  321. }
  322. /**
  323. * Add a foreign key constraint with bad data
  324. *
  325. * @dataProvider badForeignKeyProvider
  326. * @expectedException Cake\Database\Exception
  327. * @return void
  328. */
  329. public function testAddConstraintForeignKeyBadData($data) {
  330. $table = new Table('articles');
  331. $table->addColumn('author_id', 'integer')
  332. ->addConstraint('author_id_idx', $data);
  333. }
  334. }