TableTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  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. * Attribute keys should be filtered and have defaults set.
  76. *
  77. * @return void
  78. */
  79. public function testAddColumnFiltersAttributes() {
  80. $table = new Table('articles');
  81. $table->addColumn('title', [
  82. 'type' => 'string'
  83. ]);
  84. $result = $table->column('title');
  85. $expected = [
  86. 'type' => 'string',
  87. 'length' => null,
  88. 'precision' => null,
  89. 'default' => null,
  90. 'null' => null,
  91. 'fixed' => null,
  92. 'comment' => null,
  93. ];
  94. $this->assertEquals($expected, $result);
  95. $table->addColumn('author_id', [
  96. 'type' => 'integer'
  97. ]);
  98. $result = $table->column('author_id');
  99. $expected = [
  100. 'type' => 'integer',
  101. 'length' => null,
  102. 'precision' => null,
  103. 'default' => null,
  104. 'null' => null,
  105. 'unsigned' => null,
  106. 'comment' => null,
  107. 'autoIncrement' => null,
  108. ];
  109. $this->assertEquals($expected, $result);
  110. $table->addColumn('amount', [
  111. 'type' => 'decimal'
  112. ]);
  113. $result = $table->column('amount');
  114. $expected = [
  115. 'type' => 'decimal',
  116. 'length' => null,
  117. 'precision' => null,
  118. 'default' => null,
  119. 'null' => null,
  120. 'unsigned' => null,
  121. 'comment' => null,
  122. ];
  123. $this->assertEquals($expected, $result);
  124. }
  125. /**
  126. * Test adding an constraint.
  127. *
  128. * @return void
  129. */
  130. public function testAddConstraint() {
  131. $table = new Table('articles');
  132. $table->addColumn('id', [
  133. 'type' => 'integer'
  134. ]);
  135. $result = $table->addConstraint('primary', [
  136. 'type' => 'primary',
  137. 'columns' => ['id']
  138. ]);
  139. $this->assertSame($result, $table);
  140. $this->assertEquals(['primary'], $table->constraints());
  141. }
  142. /**
  143. * Dataprovider for invalid addConstraint calls.
  144. *
  145. * @return array
  146. */
  147. public static function addConstaintErrorProvider() {
  148. return [
  149. // No properties
  150. [[]],
  151. // Empty columns
  152. [['columns' => '']],
  153. [['columns' => []]],
  154. // Missing column
  155. [['columns' => ['derp']]],
  156. // Invalid type
  157. [['columns' => 'author_id', 'type' => 'derp']],
  158. ];
  159. }
  160. /**
  161. * Test that an exception is raised when constraints
  162. * are added for fields that do not exist.
  163. *
  164. * @dataProvider addConstaintErrorProvider
  165. * @expectedException \Cake\Database\Exception
  166. * @return void
  167. */
  168. public function testAddConstraintError($props) {
  169. $table = new Table('articles');
  170. $table->addColumn('author_id', 'integer');
  171. $table->addConstraint('author_idx', $props);
  172. }
  173. /**
  174. * Test adding an index.
  175. *
  176. * @return void
  177. */
  178. public function testAddIndex() {
  179. $table = new Table('articles');
  180. $table->addColumn('title', [
  181. 'type' => 'string'
  182. ]);
  183. $result = $table->addIndex('faster', [
  184. 'type' => 'index',
  185. 'columns' => ['title']
  186. ]);
  187. $this->assertSame($result, $table);
  188. $this->assertEquals(['faster'], $table->indexes());
  189. }
  190. /**
  191. * Dataprovider for invalid addIndex calls
  192. *
  193. * @return array
  194. */
  195. public static function addIndexErrorProvider() {
  196. return [
  197. // Empty
  198. [[]],
  199. // No columns
  200. [['columns' => '']],
  201. [['columns' => []]],
  202. // Missing column
  203. [['columns' => ['not_there']]],
  204. // Invalid type
  205. [['columns' => 'author_id', 'type' => 'derp']],
  206. ];
  207. }
  208. /**
  209. * Test that an exception is raised when indexes
  210. * are added for fields that do not exist.
  211. *
  212. * @dataProvider addIndexErrorProvider
  213. * @expectedException \Cake\Database\Exception
  214. * @return void
  215. */
  216. public function testAddIndexError($props) {
  217. $table = new Table('articles');
  218. $table->addColumn('author_id', 'integer');
  219. $table->addIndex('author_idx', $props);
  220. }
  221. /**
  222. * Test adding different kinds of indexes.
  223. *
  224. * @return void
  225. */
  226. public function testAddIndexTypes() {
  227. $table = new Table('articles');
  228. $table->addColumn('id', 'integer')
  229. ->addColumn('title', 'string')
  230. ->addColumn('author_id', 'integer');
  231. $table->addIndex('author_idx', [
  232. 'columns' => ['author_id'],
  233. 'type' => 'index'
  234. ])->addIndex('texty', [
  235. 'type' => 'fulltext',
  236. 'columns' => ['title']
  237. ]);
  238. $this->assertEquals(
  239. ['author_idx', 'texty'],
  240. $table->indexes()
  241. );
  242. }
  243. /**
  244. * Test getting the primary key.
  245. *
  246. * @return void
  247. */
  248. public function testPrimaryKey() {
  249. $table = new Table('articles');
  250. $table->addColumn('id', 'integer')
  251. ->addColumn('title', 'string')
  252. ->addColumn('author_id', 'integer')
  253. ->addConstraint('author_idx', [
  254. 'columns' => ['author_id'],
  255. 'type' => 'unique'
  256. ])->addConstraint('primary', [
  257. 'type' => 'primary',
  258. 'columns' => ['id']
  259. ]);
  260. $this->assertEquals(['id'], $table->primaryKey());
  261. }
  262. /**
  263. * Test the options method.
  264. *
  265. * @return void
  266. */
  267. public function testOptions() {
  268. $table = new Table('articles');
  269. $options = [
  270. 'engine' => 'InnoDB'
  271. ];
  272. $return = $table->options($options);
  273. $this->assertInstanceOf('Cake\Database\Schema\Table', $return);
  274. $this->assertEquals($options, $table->options());
  275. }
  276. /**
  277. * Add a basic foreign key constraint.
  278. *
  279. * @return void
  280. */
  281. public function testAddConstraintForeignKey() {
  282. $table = new Table('articles');
  283. $table->addColumn('author_id', 'integer')
  284. ->addConstraint('author_id_idx', [
  285. 'type' => Table::CONSTRAINT_FOREIGN,
  286. 'columns' => ['author_id'],
  287. 'references' => ['authors', 'id'],
  288. 'update' => 'cascade',
  289. 'delete' => 'cascade',
  290. ]);
  291. $this->assertEquals(['author_id_idx'], $table->constraints());
  292. }
  293. /**
  294. * Provider for exceptionally bad foreign key data.
  295. *
  296. * @return array
  297. */
  298. public static function badForeignKeyProvider() {
  299. return [
  300. 'references is bad' => [[
  301. 'type' => Table::CONSTRAINT_FOREIGN,
  302. 'columns' => ['author_id'],
  303. 'references' => ['authors'],
  304. 'delete' => 'derp',
  305. ]],
  306. 'bad update value' => [[
  307. 'type' => Table::CONSTRAINT_FOREIGN,
  308. 'columns' => ['author_id'],
  309. 'references' => ['authors', 'id'],
  310. 'update' => 'derp',
  311. ]],
  312. 'bad delete value' => [[
  313. 'type' => Table::CONSTRAINT_FOREIGN,
  314. 'columns' => ['author_id'],
  315. 'references' => ['authors', 'id'],
  316. 'delete' => 'derp',
  317. ]],
  318. ];
  319. }
  320. /**
  321. * Add a foreign key constraint with bad data
  322. *
  323. * @dataProvider badForeignKeyProvider
  324. * @expectedException \Cake\Database\Exception
  325. * @return void
  326. */
  327. public function testAddConstraintForeignKeyBadData($data) {
  328. $table = new Table('articles');
  329. $table->addColumn('author_id', 'integer')
  330. ->addConstraint('author_id_idx', $data);
  331. }
  332. /**
  333. * Tests the temporary() method
  334. *
  335. * @return void
  336. */
  337. public function testTemporary() {
  338. $table = new Table('articles');
  339. $this->assertFalse($table->temporary());
  340. $this->assertSame($table, $table->temporary(true));
  341. $this->assertTrue($table->temporary());
  342. $table->temporary(false);
  343. $this->assertFalse($table->temporary());
  344. }
  345. }