MysqlSchemaTest.php 21 KB

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