MysqlSchemaTest.php 21 KB

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