MysqlSchemaTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 CakePHP(tm) v 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\Core\Configure;
  17. use Cake\Database\ConnectionManager;
  18. use Cake\Database\Schema\Collection as SchemaCollection;
  19. use Cake\Database\Schema\MysqlSchema;
  20. use Cake\Database\Schema\Table;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Test case for Mysql Schema Dialect.
  24. */
  25. class MysqlSchemaTest extends TestCase {
  26. /**
  27. * Helper method for skipping tests that need a real connection.
  28. *
  29. * @return void
  30. */
  31. protected function _needsConnection() {
  32. $config = ConnectionManager::config('test');
  33. $this->skipIf(strpos($config['className'], 'Mysql') === false, 'Not using Mysql for test config');
  34. }
  35. /**
  36. * Data provider for convert column testing
  37. *
  38. * @return array
  39. */
  40. public static function convertColumnProvider() {
  41. return [
  42. [
  43. 'DATETIME',
  44. ['type' => 'datetime', 'length' => null]
  45. ],
  46. [
  47. 'DATE',
  48. ['type' => 'date', 'length' => null]
  49. ],
  50. [
  51. 'TIME',
  52. ['type' => 'time', 'length' => null]
  53. ],
  54. [
  55. 'TINYINT(1)',
  56. ['type' => 'boolean', 'length' => null]
  57. ],
  58. [
  59. 'TINYINT(2)',
  60. ['type' => 'integer', 'length' => 2, 'unsigned' => false]
  61. ],
  62. [
  63. 'INTEGER(11)',
  64. ['type' => 'integer', 'length' => 11, 'unsigned' => false]
  65. ],
  66. [
  67. 'INTEGER(11) UNSIGNED',
  68. ['type' => 'integer', 'length' => 11, 'unsigned' => true]
  69. ],
  70. [
  71. 'BIGINT',
  72. ['type' => 'biginteger', 'length' => null, 'unsigned' => false]
  73. ],
  74. [
  75. 'BIGINT UNSIGNED',
  76. ['type' => 'biginteger', 'length' => null, 'unsigned' => true]
  77. ],
  78. [
  79. 'VARCHAR(255)',
  80. ['type' => 'string', 'length' => 255]
  81. ],
  82. [
  83. 'CHAR(25)',
  84. ['type' => 'string', 'length' => 25, 'fixed' => true]
  85. ],
  86. [
  87. 'CHAR(36)',
  88. ['type' => 'uuid', 'length' => null]
  89. ],
  90. [
  91. 'TINYTEXT',
  92. ['type' => 'string', 'length' => null]
  93. ],
  94. [
  95. 'BLOB',
  96. ['type' => 'binary', 'length' => null]
  97. ],
  98. [
  99. 'MEDIUMBLOB',
  100. ['type' => 'binary', 'length' => null]
  101. ],
  102. [
  103. 'FLOAT',
  104. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
  105. ],
  106. [
  107. 'DOUBLE',
  108. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false]
  109. ],
  110. [
  111. 'DOUBLE UNSIGNED',
  112. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => true]
  113. ],
  114. [
  115. 'DECIMAL(11,2) UNSIGNED',
  116. ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => true]
  117. ],
  118. [
  119. 'DECIMAL(11,2)',
  120. ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => false]
  121. ],
  122. [
  123. 'FLOAT(11,2)',
  124. ['type' => 'float', 'length' => 11, 'precision' => 2, 'unsigned' => false]
  125. ],
  126. [
  127. 'FLOAT(11,2) UNSIGNED',
  128. ['type' => 'float', 'length' => 11, 'precision' => 2, 'unsigned' => true]
  129. ],
  130. [
  131. 'DOUBLE(10,4)',
  132. ['type' => 'float', 'length' => 10, 'precision' => 4, 'unsigned' => false]
  133. ],
  134. [
  135. 'DOUBLE(10,4) UNSIGNED',
  136. ['type' => 'float', 'length' => 10, 'precision' => 4, 'unsigned' => true]
  137. ],
  138. ];
  139. }
  140. /**
  141. * Test parsing MySQL column types form field description.
  142. *
  143. * @dataProvider convertColumnProvider
  144. * @return void
  145. */
  146. public function testConvertColumn($type, $expected) {
  147. $field = [
  148. 'Field' => 'field',
  149. 'Type' => $type,
  150. 'Null' => 'YES',
  151. 'Default' => 'Default value',
  152. 'Collation' => 'Collate information',
  153. 'Comment' => 'Comment section',
  154. ];
  155. $expected += [
  156. 'null' => true,
  157. 'default' => 'Default value',
  158. 'collate' => 'Collate information',
  159. 'comment' => 'Comment section',
  160. ];
  161. $driver = $this->getMock('Cake\Database\Driver\Mysql');
  162. $dialect = new MysqlSchema($driver);
  163. $table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
  164. $table->expects($this->at(0))->method('addColumn')->with('field', $expected);
  165. $dialect->convertFieldDescription($table, $field);
  166. }
  167. /**
  168. * Helper method for testing methods.
  169. *
  170. * @return void
  171. */
  172. protected function _createTables($connection) {
  173. $this->_needsConnection();
  174. $connection->execute('DROP TABLE IF EXISTS schema_articles');
  175. $connection->execute('DROP TABLE IF EXISTS schema_authors');
  176. $table = <<<SQL
  177. CREATE TABLE schema_authors (
  178. id INT(11) PRIMARY KEY AUTO_INCREMENT,
  179. name VARCHAR(50),
  180. bio TEXT,
  181. created DATETIME
  182. )ENGINE=InnoDB
  183. SQL;
  184. $connection->execute($table);
  185. $table = <<<SQL
  186. CREATE TABLE schema_articles (
  187. id BIGINT PRIMARY KEY AUTO_INCREMENT,
  188. title VARCHAR(20) COMMENT 'A title',
  189. body TEXT,
  190. author_id INT(11) NOT NULL,
  191. published BOOLEAN DEFAULT 0,
  192. allow_comments TINYINT(1) DEFAULT 0,
  193. created DATETIME,
  194. KEY `author_idx` (`author_id`),
  195. UNIQUE KEY `length_idx` (`title`(4)),
  196. FOREIGN KEY `author_idx` (`author_id`) REFERENCES `schema_authors`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT
  197. ) ENGINE=InnoDB COLLATE=utf8_general_ci
  198. SQL;
  199. $connection->execute($table);
  200. }
  201. /**
  202. * Integration test for SchemaCollection & MysqlDialect.
  203. *
  204. * @return void
  205. */
  206. public function testListTables() {
  207. $connection = ConnectionManager::get('test');
  208. $this->_createTables($connection);
  209. $schema = new SchemaCollection($connection);
  210. $result = $schema->listTables();
  211. $this->assertInternalType('array', $result);
  212. $this->assertCount(2, $result);
  213. $this->assertEquals('schema_articles', $result[0]);
  214. $this->assertEquals('schema_authors', $result[1]);
  215. }
  216. /**
  217. * Test describing a table with Mysql
  218. *
  219. * @return void
  220. */
  221. public function testDescribeTable() {
  222. $connection = ConnectionManager::get('test');
  223. $this->_createTables($connection);
  224. $schema = new SchemaCollection($connection);
  225. $result = $schema->describe('schema_articles');
  226. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  227. $expected = [
  228. 'id' => [
  229. 'type' => 'biginteger',
  230. 'null' => false,
  231. 'unsigned' => false,
  232. 'default' => null,
  233. 'length' => 20,
  234. 'precision' => null,
  235. 'comment' => null,
  236. 'autoIncrement' => true,
  237. ],
  238. 'title' => [
  239. 'type' => 'string',
  240. 'null' => true,
  241. 'default' => null,
  242. 'length' => 20,
  243. 'precision' => null,
  244. 'comment' => 'A title',
  245. 'fixed' => null,
  246. ],
  247. 'body' => [
  248. 'type' => 'text',
  249. 'null' => true,
  250. 'default' => null,
  251. 'length' => null,
  252. 'precision' => null,
  253. 'comment' => null,
  254. ],
  255. 'author_id' => [
  256. 'type' => 'integer',
  257. 'null' => false,
  258. 'unsigned' => false,
  259. 'default' => null,
  260. 'length' => 11,
  261. 'precision' => null,
  262. 'comment' => null,
  263. 'autoIncrement' => null,
  264. ],
  265. 'published' => [
  266. 'type' => 'boolean',
  267. 'null' => true,
  268. 'default' => 0,
  269. 'length' => null,
  270. 'precision' => null,
  271. 'comment' => null,
  272. ],
  273. 'allow_comments' => [
  274. 'type' => 'boolean',
  275. 'null' => true,
  276. 'default' => 0,
  277. 'length' => null,
  278. 'precision' => null,
  279. 'comment' => null,
  280. ],
  281. 'created' => [
  282. 'type' => 'datetime',
  283. 'null' => true,
  284. 'default' => null,
  285. 'length' => null,
  286. 'precision' => null,
  287. 'comment' => null,
  288. ],
  289. ];
  290. $this->assertEquals(['id'], $result->primaryKey());
  291. foreach ($expected as $field => $definition) {
  292. $this->assertEquals(
  293. $definition,
  294. $result->column($field),
  295. 'Field definition does not match for ' . $field
  296. );
  297. }
  298. }
  299. /**
  300. * Test describing a table with indexes in Mysql
  301. *
  302. * @return void
  303. */
  304. public function testDescribeTableIndexes() {
  305. $connection = ConnectionManager::get('test');
  306. $this->_createTables($connection);
  307. $schema = new SchemaCollection($connection);
  308. $result = $schema->describe('schema_articles');
  309. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  310. $this->assertCount(3, $result->constraints());
  311. $expected = [
  312. 'primary' => [
  313. 'type' => 'primary',
  314. 'columns' => ['id'],
  315. 'length' => []
  316. ],
  317. 'length_idx' => [
  318. 'type' => 'unique',
  319. 'columns' => ['title'],
  320. 'length' => [
  321. 'title' => 4,
  322. ]
  323. ],
  324. 'schema_articles_ibfk_1' => [
  325. 'type' => 'foreign',
  326. 'columns' => ['author_id'],
  327. 'references' => ['schema_authors', 'id'],
  328. 'length' => [],
  329. 'update' => 'cascade',
  330. 'delete' => 'restrict',
  331. ]
  332. ];
  333. $this->assertEquals($expected['primary'], $result->constraint('primary'));
  334. $this->assertEquals($expected['length_idx'], $result->constraint('length_idx'));
  335. $this->assertEquals($expected['schema_articles_ibfk_1'], $result->constraint('schema_articles_ibfk_1'));
  336. $this->assertCount(1, $result->indexes());
  337. $expected = [
  338. 'type' => 'index',
  339. 'columns' => ['author_id'],
  340. 'length' => []
  341. ];
  342. $this->assertEquals($expected, $result->index('author_idx'));
  343. }
  344. /**
  345. * Column provider for creating column sql
  346. *
  347. * @return array
  348. */
  349. public static function columnSqlProvider() {
  350. return [
  351. // strings
  352. [
  353. 'title',
  354. ['type' => 'string', 'length' => 25, 'null' => false],
  355. '`title` VARCHAR(25) NOT NULL'
  356. ],
  357. [
  358. 'title',
  359. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
  360. '`title` VARCHAR(25) DEFAULT NULL'
  361. ],
  362. [
  363. 'id',
  364. ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
  365. '`id` CHAR(32) NOT NULL'
  366. ],
  367. [
  368. 'role',
  369. ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
  370. '`role` VARCHAR(10) NOT NULL DEFAULT "admin"'
  371. ],
  372. [
  373. 'title',
  374. ['type' => 'string'],
  375. '`title` VARCHAR(255)'
  376. ],
  377. [
  378. 'id',
  379. ['type' => 'uuid'],
  380. '`id` CHAR(36)'
  381. ],
  382. // Text
  383. [
  384. 'body',
  385. ['type' => 'text', 'null' => false],
  386. '`body` TEXT NOT NULL'
  387. ],
  388. // Integers
  389. [
  390. 'post_id',
  391. ['type' => 'integer', 'length' => 11],
  392. '`post_id` INTEGER(11)'
  393. ],
  394. [
  395. 'post_id',
  396. ['type' => 'integer', 'length' => 11, 'unsigned' => true],
  397. '`post_id` INTEGER(11) UNSIGNED'
  398. ],
  399. [
  400. 'post_id',
  401. ['type' => 'biginteger', 'length' => 20],
  402. '`post_id` BIGINT'
  403. ],
  404. [
  405. 'post_id',
  406. ['type' => 'biginteger', 'length' => 20, 'unsigned' => true],
  407. '`post_id` BIGINT UNSIGNED'
  408. ],
  409. [
  410. 'post_id',
  411. ['type' => 'integer', 'length' => 20, 'autoIncrement' => true],
  412. '`post_id` INTEGER(20) AUTO_INCREMENT'
  413. ],
  414. [
  415. 'post_id',
  416. ['type' => 'biginteger', 'length' => 20, 'autoIncrement' => true],
  417. '`post_id` BIGINT AUTO_INCREMENT'
  418. ],
  419. // Decimal
  420. [
  421. 'value',
  422. ['type' => 'decimal'],
  423. '`value` DECIMAL'
  424. ],
  425. [
  426. 'value',
  427. ['type' => 'decimal', 'length' => 11, 'unsigned' => true],
  428. '`value` DECIMAL(11,0) UNSIGNED'
  429. ],
  430. [
  431. 'value',
  432. ['type' => 'decimal', 'length' => 12, 'precision' => 5],
  433. '`value` DECIMAL(12,5)'
  434. ],
  435. // Float
  436. [
  437. 'value',
  438. ['type' => 'float', 'unsigned'],
  439. '`value` FLOAT'
  440. ],
  441. [
  442. 'value',
  443. ['type' => 'float', 'unsigned' => true],
  444. '`value` FLOAT UNSIGNED'
  445. ],
  446. [
  447. 'value',
  448. ['type' => 'float', 'length' => 11, 'precision' => 3],
  449. '`value` FLOAT(11,3)'
  450. ],
  451. // Boolean
  452. [
  453. 'checked',
  454. ['type' => 'boolean', 'default' => false],
  455. '`checked` BOOLEAN DEFAULT FALSE'
  456. ],
  457. [
  458. 'checked',
  459. ['type' => 'boolean', 'default' => true, 'null' => false],
  460. '`checked` BOOLEAN NOT NULL DEFAULT TRUE'
  461. ],
  462. // datetimes
  463. [
  464. 'created',
  465. ['type' => 'datetime', 'comment' => 'Created timestamp'],
  466. '`created` DATETIME COMMENT "Created timestamp"'
  467. ],
  468. // Date & Time
  469. [
  470. 'start_date',
  471. ['type' => 'date'],
  472. '`start_date` DATE'
  473. ],
  474. [
  475. 'start_time',
  476. ['type' => 'time'],
  477. '`start_time` TIME'
  478. ],
  479. // timestamps
  480. [
  481. 'created',
  482. ['type' => 'timestamp', 'null' => true],
  483. '`created` TIMESTAMP NULL'
  484. ],
  485. [
  486. 'created',
  487. ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp'],
  488. '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
  489. ],
  490. ];
  491. }
  492. /**
  493. * Test generating column definitions
  494. *
  495. * @dataProvider columnSqlProvider
  496. * @return void
  497. */
  498. public function testColumnSql($name, $data, $expected) {
  499. $driver = $this->_getMockedDriver();
  500. $schema = new MysqlSchema($driver);
  501. $table = (new Table('articles'))->addColumn($name, $data);
  502. $this->assertEquals($expected, $schema->columnSql($table, $name));
  503. }
  504. /**
  505. * Provide data for testing constraintSql
  506. *
  507. * @return array
  508. */
  509. public static function constraintSqlProvider() {
  510. return [
  511. [
  512. 'primary',
  513. ['type' => 'primary', 'columns' => ['title']],
  514. 'PRIMARY KEY (`title`)'
  515. ],
  516. [
  517. 'unique_idx',
  518. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  519. 'UNIQUE KEY `unique_idx` (`title`, `author_id`)'
  520. ],
  521. [
  522. 'length_idx',
  523. [
  524. 'type' => 'unique',
  525. 'columns' => ['author_id', 'title'],
  526. 'length' => ['author_id' => 5, 'title' => 4]
  527. ],
  528. 'UNIQUE KEY `length_idx` (`author_id`(5), `title`(4))'
  529. ],
  530. [
  531. 'author_id_idx',
  532. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  533. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  534. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
  535. ],
  536. [
  537. 'author_id_idx',
  538. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  539. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  540. 'REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT'
  541. ],
  542. [
  543. 'author_id_idx',
  544. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
  545. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  546. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
  547. ],
  548. [
  549. 'author_id_idx',
  550. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  551. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  552. 'REFERENCES `authors` (`id`) ON UPDATE SET NULL ON DELETE RESTRICT'
  553. ],
  554. [
  555. 'author_id_idx',
  556. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  557. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  558. 'REFERENCES `authors` (`id`) ON UPDATE NO ACTION ON DELETE RESTRICT'
  559. ],
  560. ];
  561. }
  562. /**
  563. * Test the constraintSql method.
  564. *
  565. * @dataProvider constraintSqlProvider
  566. */
  567. public function testConstraintSql($name, $data, $expected) {
  568. $driver = $this->_getMockedDriver();
  569. $schema = new MysqlSchema($driver);
  570. $table = (new Table('articles'))->addColumn('title', [
  571. 'type' => 'string',
  572. 'length' => 255
  573. ])->addColumn('author_id', [
  574. 'type' => 'integer',
  575. ])->addConstraint($name, $data);
  576. $this->assertEquals($expected, $schema->constraintSql($table, $name));
  577. }
  578. /**
  579. * Test provider for indexSql()
  580. *
  581. * @return array
  582. */
  583. public static function indexSqlProvider() {
  584. return [
  585. [
  586. 'key_key',
  587. ['type' => 'index', 'columns' => ['author_id']],
  588. 'KEY `key_key` (`author_id`)'
  589. ],
  590. [
  591. 'full_text',
  592. ['type' => 'fulltext', 'columns' => ['title']],
  593. 'FULLTEXT KEY `full_text` (`title`)'
  594. ],
  595. ];
  596. }
  597. /**
  598. * Test the indexSql method.
  599. *
  600. * @dataProvider indexSqlProvider
  601. */
  602. public function testIndexSql($name, $data, $expected) {
  603. $driver = $this->_getMockedDriver();
  604. $schema = new MysqlSchema($driver);
  605. $table = (new Table('articles'))->addColumn('title', [
  606. 'type' => 'string',
  607. 'length' => 255
  608. ])->addColumn('author_id', [
  609. 'type' => 'integer',
  610. ])->addIndex($name, $data);
  611. $this->assertEquals($expected, $schema->indexSql($table, $name));
  612. }
  613. /**
  614. * Test generating a column that is a primary key.
  615. *
  616. * @return void
  617. */
  618. public function testColumnSqlPrimaryKey() {
  619. $driver = $this->_getMockedDriver();
  620. $schema = new MysqlSchema($driver);
  621. $table = new Table('articles');
  622. $table->addColumn('id', [
  623. 'type' => 'integer',
  624. 'null' => false
  625. ])
  626. ->addConstraint('primary', [
  627. 'type' => 'primary',
  628. 'columns' => ['id']
  629. ]);
  630. $result = $schema->columnSql($table, 'id');
  631. $this->assertEquals($result, '`id` INTEGER NOT NULL AUTO_INCREMENT');
  632. $table = new Table('articles');
  633. $table->addColumn('id', [
  634. 'type' => 'biginteger',
  635. 'null' => false
  636. ])
  637. ->addConstraint('primary', [
  638. 'type' => 'primary',
  639. 'columns' => ['id']
  640. ]);
  641. $result = $schema->columnSql($table, 'id');
  642. $this->assertEquals($result, '`id` BIGINT NOT NULL AUTO_INCREMENT');
  643. }
  644. /**
  645. * Integration test for converting a Schema\Table into MySQL table creates.
  646. *
  647. * @return void
  648. */
  649. public function testCreateSql() {
  650. $driver = $this->_getMockedDriver();
  651. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  652. $connection->expects($this->any())->method('driver')
  653. ->will($this->returnValue($driver));
  654. $table = (new Table('posts'))->addColumn('id', [
  655. 'type' => 'integer',
  656. 'null' => false
  657. ])
  658. ->addColumn('title', [
  659. 'type' => 'string',
  660. 'null' => false,
  661. 'comment' => 'The title'
  662. ])
  663. ->addColumn('body', ['type' => 'text'])
  664. ->addColumn('created', 'datetime')
  665. ->addConstraint('primary', [
  666. 'type' => 'primary',
  667. 'columns' => ['id']
  668. ])
  669. ->options([
  670. 'engine' => 'InnoDB',
  671. 'charset' => 'utf8',
  672. 'collate' => 'utf8_general_ci',
  673. ]);
  674. $expected = <<<SQL
  675. CREATE TABLE `posts` (
  676. `id` INTEGER NOT NULL AUTO_INCREMENT,
  677. `title` VARCHAR(255) NOT NULL COMMENT "The title",
  678. `body` TEXT,
  679. `created` DATETIME,
  680. PRIMARY KEY (`id`)
  681. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
  682. SQL;
  683. $result = $table->createSql($connection);
  684. $this->assertCount(1, $result);
  685. $this->assertEquals($expected, $result[0]);
  686. }
  687. /**
  688. * Test primary key generation & auto-increment.
  689. *
  690. * @return void
  691. */
  692. public function testCreateSqlCompositeIntegerKey() {
  693. $driver = $this->_getMockedDriver();
  694. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  695. $connection->expects($this->any())->method('driver')
  696. ->will($this->returnValue($driver));
  697. $table = (new Table('articles_tags'))
  698. ->addColumn('article_id', [
  699. 'type' => 'integer',
  700. 'null' => false
  701. ])
  702. ->addColumn('tag_id', [
  703. 'type' => 'integer',
  704. 'null' => false,
  705. ])
  706. ->addConstraint('primary', [
  707. 'type' => 'primary',
  708. 'columns' => ['article_id', 'tag_id']
  709. ]);
  710. $expected = <<<SQL
  711. CREATE TABLE `articles_tags` (
  712. `article_id` INTEGER NOT NULL,
  713. `tag_id` INTEGER NOT NULL,
  714. PRIMARY KEY (`article_id`, `tag_id`)
  715. )
  716. SQL;
  717. $result = $table->createSql($connection);
  718. $this->assertCount(1, $result);
  719. $this->assertEquals($expected, $result[0]);
  720. $table = (new Table('composite_key'))
  721. ->addColumn('id', [
  722. 'type' => 'integer',
  723. 'null' => false,
  724. 'autoIncrement' => true
  725. ])
  726. ->addColumn('account_id', [
  727. 'type' => 'integer',
  728. 'null' => false,
  729. ])
  730. ->addConstraint('primary', [
  731. 'type' => 'primary',
  732. 'columns' => ['id', 'account_id']
  733. ]);
  734. $expected = <<<SQL
  735. CREATE TABLE `composite_key` (
  736. `id` INTEGER NOT NULL AUTO_INCREMENT,
  737. `account_id` INTEGER NOT NULL,
  738. PRIMARY KEY (`id`, `account_id`)
  739. )
  740. SQL;
  741. $result = $table->createSql($connection);
  742. $this->assertCount(1, $result);
  743. $this->assertEquals($expected, $result[0]);
  744. }
  745. /**
  746. * test dropSql
  747. *
  748. * @return void
  749. */
  750. public function testDropSql() {
  751. $driver = $this->_getMockedDriver();
  752. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  753. $connection->expects($this->any())->method('driver')
  754. ->will($this->returnValue($driver));
  755. $table = new Table('articles');
  756. $result = $table->dropSql($connection);
  757. $this->assertCount(1, $result);
  758. $this->assertEquals('DROP TABLE `articles`', $result[0]);
  759. }
  760. /**
  761. * Test truncateSql()
  762. *
  763. * @return void
  764. */
  765. public function testTruncateSql() {
  766. $driver = $this->_getMockedDriver();
  767. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  768. $connection->expects($this->any())->method('driver')
  769. ->will($this->returnValue($driver));
  770. $table = new Table('articles');
  771. $result = $table->truncateSql($connection);
  772. $this->assertCount(1, $result);
  773. $this->assertEquals('TRUNCATE TABLE `articles`', $result[0]);
  774. }
  775. /**
  776. * Get a schema instance with a mocked driver/pdo instances
  777. *
  778. * @return MysqlSchema
  779. */
  780. protected function _getMockedDriver() {
  781. $driver = new \Cake\Database\Driver\Mysql();
  782. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  783. $mock->expects($this->any())
  784. ->method('quote')
  785. ->will($this->returnCallback(function ($value) {
  786. return '"' . $value . '"';
  787. }));
  788. $mock->expects($this->any())
  789. ->method('quoteIdentifier')
  790. ->will($this->returnCallback(function ($value) {
  791. return '`' . $value . '`';
  792. }));
  793. $driver->connection($mock);
  794. return $driver;
  795. }
  796. }