MysqlSchemaTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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' => 'text', '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->convertColumnDescription($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. * Test describing a table creates options
  350. *
  351. * @return void
  352. */
  353. public function testDescribeTableOptions() {
  354. $connection = ConnectionManager::get('test');
  355. $this->_createTables($connection);
  356. $schema = new SchemaCollection($connection);
  357. $result = $schema->describe('schema_articles');
  358. $this->assertArrayHasKey('engine', $result->options());
  359. $this->assertArrayHasKey('collation', $result->options());
  360. }
  361. /**
  362. * Column provider for creating column sql
  363. *
  364. * @return array
  365. */
  366. public static function columnSqlProvider() {
  367. return [
  368. // strings
  369. [
  370. 'title',
  371. ['type' => 'string', 'length' => 25, 'null' => false],
  372. '`title` VARCHAR(25) NOT NULL'
  373. ],
  374. [
  375. 'title',
  376. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
  377. '`title` VARCHAR(25) DEFAULT NULL'
  378. ],
  379. [
  380. 'id',
  381. ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
  382. '`id` CHAR(32) NOT NULL'
  383. ],
  384. [
  385. 'role',
  386. ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
  387. '`role` VARCHAR(10) NOT NULL DEFAULT "admin"'
  388. ],
  389. [
  390. 'title',
  391. ['type' => 'string'],
  392. '`title` VARCHAR(255)'
  393. ],
  394. [
  395. 'id',
  396. ['type' => 'uuid'],
  397. '`id` CHAR(36)'
  398. ],
  399. // Text
  400. [
  401. 'body',
  402. ['type' => 'text', 'null' => false],
  403. '`body` TEXT NOT NULL'
  404. ],
  405. // Integers
  406. [
  407. 'post_id',
  408. ['type' => 'integer', 'length' => 11],
  409. '`post_id` INTEGER(11)'
  410. ],
  411. [
  412. 'post_id',
  413. ['type' => 'integer', 'length' => 11, 'unsigned' => true],
  414. '`post_id` INTEGER(11) UNSIGNED'
  415. ],
  416. [
  417. 'post_id',
  418. ['type' => 'biginteger', 'length' => 20],
  419. '`post_id` BIGINT'
  420. ],
  421. [
  422. 'post_id',
  423. ['type' => 'biginteger', 'length' => 20, 'unsigned' => true],
  424. '`post_id` BIGINT UNSIGNED'
  425. ],
  426. [
  427. 'post_id',
  428. ['type' => 'integer', 'length' => 20, 'autoIncrement' => true],
  429. '`post_id` INTEGER(20) AUTO_INCREMENT'
  430. ],
  431. [
  432. 'post_id',
  433. ['type' => 'biginteger', 'length' => 20, 'autoIncrement' => true],
  434. '`post_id` BIGINT AUTO_INCREMENT'
  435. ],
  436. // Decimal
  437. [
  438. 'value',
  439. ['type' => 'decimal'],
  440. '`value` DECIMAL'
  441. ],
  442. [
  443. 'value',
  444. ['type' => 'decimal', 'length' => 11, 'unsigned' => true],
  445. '`value` DECIMAL(11,0) UNSIGNED'
  446. ],
  447. [
  448. 'value',
  449. ['type' => 'decimal', 'length' => 12, 'precision' => 5],
  450. '`value` DECIMAL(12,5)'
  451. ],
  452. // Float
  453. [
  454. 'value',
  455. ['type' => 'float', 'unsigned'],
  456. '`value` FLOAT'
  457. ],
  458. [
  459. 'value',
  460. ['type' => 'float', 'unsigned' => true],
  461. '`value` FLOAT UNSIGNED'
  462. ],
  463. [
  464. 'value',
  465. ['type' => 'float', 'length' => 11, 'precision' => 3],
  466. '`value` FLOAT(11,3)'
  467. ],
  468. // Boolean
  469. [
  470. 'checked',
  471. ['type' => 'boolean', 'default' => false],
  472. '`checked` BOOLEAN DEFAULT FALSE'
  473. ],
  474. [
  475. 'checked',
  476. ['type' => 'boolean', 'default' => true, 'null' => false],
  477. '`checked` BOOLEAN NOT NULL DEFAULT TRUE'
  478. ],
  479. // datetimes
  480. [
  481. 'created',
  482. ['type' => 'datetime', 'comment' => 'Created timestamp'],
  483. '`created` DATETIME COMMENT "Created timestamp"'
  484. ],
  485. // Date & Time
  486. [
  487. 'start_date',
  488. ['type' => 'date'],
  489. '`start_date` DATE'
  490. ],
  491. [
  492. 'start_time',
  493. ['type' => 'time'],
  494. '`start_time` TIME'
  495. ],
  496. // timestamps
  497. [
  498. 'created',
  499. ['type' => 'timestamp', 'null' => true],
  500. '`created` TIMESTAMP NULL'
  501. ],
  502. [
  503. 'created',
  504. ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp'],
  505. '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
  506. ],
  507. ];
  508. }
  509. /**
  510. * Test generating column definitions
  511. *
  512. * @dataProvider columnSqlProvider
  513. * @return void
  514. */
  515. public function testColumnSql($name, $data, $expected) {
  516. $driver = $this->_getMockedDriver();
  517. $schema = new MysqlSchema($driver);
  518. $table = (new Table('articles'))->addColumn($name, $data);
  519. $this->assertEquals($expected, $schema->columnSql($table, $name));
  520. }
  521. /**
  522. * Provide data for testing constraintSql
  523. *
  524. * @return array
  525. */
  526. public static function constraintSqlProvider() {
  527. return [
  528. [
  529. 'primary',
  530. ['type' => 'primary', 'columns' => ['title']],
  531. 'PRIMARY KEY (`title`)'
  532. ],
  533. [
  534. 'unique_idx',
  535. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  536. 'UNIQUE KEY `unique_idx` (`title`, `author_id`)'
  537. ],
  538. [
  539. 'length_idx',
  540. [
  541. 'type' => 'unique',
  542. 'columns' => ['author_id', 'title'],
  543. 'length' => ['author_id' => 5, 'title' => 4]
  544. ],
  545. 'UNIQUE KEY `length_idx` (`author_id`(5), `title`(4))'
  546. ],
  547. [
  548. 'author_id_idx',
  549. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  550. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  551. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
  552. ],
  553. [
  554. 'author_id_idx',
  555. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  556. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  557. 'REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT'
  558. ],
  559. [
  560. 'author_id_idx',
  561. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
  562. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  563. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
  564. ],
  565. [
  566. 'author_id_idx',
  567. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  568. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  569. 'REFERENCES `authors` (`id`) ON UPDATE SET NULL ON DELETE RESTRICT'
  570. ],
  571. [
  572. 'author_id_idx',
  573. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  574. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  575. 'REFERENCES `authors` (`id`) ON UPDATE NO ACTION ON DELETE RESTRICT'
  576. ],
  577. ];
  578. }
  579. /**
  580. * Test the constraintSql method.
  581. *
  582. * @dataProvider constraintSqlProvider
  583. */
  584. public function testConstraintSql($name, $data, $expected) {
  585. $driver = $this->_getMockedDriver();
  586. $schema = new MysqlSchema($driver);
  587. $table = (new Table('articles'))->addColumn('title', [
  588. 'type' => 'string',
  589. 'length' => 255
  590. ])->addColumn('author_id', [
  591. 'type' => 'integer',
  592. ])->addConstraint($name, $data);
  593. $this->assertEquals($expected, $schema->constraintSql($table, $name));
  594. }
  595. /**
  596. * Test provider for indexSql()
  597. *
  598. * @return array
  599. */
  600. public static function indexSqlProvider() {
  601. return [
  602. [
  603. 'key_key',
  604. ['type' => 'index', 'columns' => ['author_id']],
  605. 'KEY `key_key` (`author_id`)'
  606. ],
  607. [
  608. 'full_text',
  609. ['type' => 'fulltext', 'columns' => ['title']],
  610. 'FULLTEXT KEY `full_text` (`title`)'
  611. ],
  612. ];
  613. }
  614. /**
  615. * Test the indexSql method.
  616. *
  617. * @dataProvider indexSqlProvider
  618. */
  619. public function testIndexSql($name, $data, $expected) {
  620. $driver = $this->_getMockedDriver();
  621. $schema = new MysqlSchema($driver);
  622. $table = (new Table('articles'))->addColumn('title', [
  623. 'type' => 'string',
  624. 'length' => 255
  625. ])->addColumn('author_id', [
  626. 'type' => 'integer',
  627. ])->addIndex($name, $data);
  628. $this->assertEquals($expected, $schema->indexSql($table, $name));
  629. }
  630. /**
  631. * Test generating a column that is a primary key.
  632. *
  633. * @return void
  634. */
  635. public function testColumnSqlPrimaryKey() {
  636. $driver = $this->_getMockedDriver();
  637. $schema = new MysqlSchema($driver);
  638. $table = new Table('articles');
  639. $table->addColumn('id', [
  640. 'type' => 'integer',
  641. 'null' => false
  642. ])
  643. ->addConstraint('primary', [
  644. 'type' => 'primary',
  645. 'columns' => ['id']
  646. ]);
  647. $result = $schema->columnSql($table, 'id');
  648. $this->assertEquals($result, '`id` INTEGER NOT NULL AUTO_INCREMENT');
  649. $table = new Table('articles');
  650. $table->addColumn('id', [
  651. 'type' => 'biginteger',
  652. 'null' => false
  653. ])
  654. ->addConstraint('primary', [
  655. 'type' => 'primary',
  656. 'columns' => ['id']
  657. ]);
  658. $result = $schema->columnSql($table, 'id');
  659. $this->assertEquals($result, '`id` BIGINT NOT NULL AUTO_INCREMENT');
  660. }
  661. /**
  662. * Integration test for converting a Schema\Table into MySQL table creates.
  663. *
  664. * @return void
  665. */
  666. public function testCreateSql() {
  667. $driver = $this->_getMockedDriver();
  668. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  669. $connection->expects($this->any())->method('driver')
  670. ->will($this->returnValue($driver));
  671. $table = (new Table('posts'))->addColumn('id', [
  672. 'type' => 'integer',
  673. 'null' => false
  674. ])
  675. ->addColumn('title', [
  676. 'type' => 'string',
  677. 'null' => false,
  678. 'comment' => 'The title'
  679. ])
  680. ->addColumn('body', [
  681. 'type' => 'text',
  682. 'comment' => ''
  683. ])
  684. ->addColumn('created', 'datetime')
  685. ->addConstraint('primary', [
  686. 'type' => 'primary',
  687. 'columns' => ['id']
  688. ])
  689. ->options([
  690. 'engine' => 'InnoDB',
  691. 'charset' => 'utf8',
  692. 'collate' => 'utf8_general_ci',
  693. ]);
  694. $expected = <<<SQL
  695. CREATE TABLE `posts` (
  696. `id` INTEGER NOT NULL AUTO_INCREMENT,
  697. `title` VARCHAR(255) NOT NULL COMMENT "The title",
  698. `body` TEXT,
  699. `created` DATETIME,
  700. PRIMARY KEY (`id`)
  701. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
  702. SQL;
  703. $result = $table->createSql($connection);
  704. $this->assertCount(1, $result);
  705. $this->assertTextEquals($expected, $result[0]);
  706. }
  707. /**
  708. * Tests creating temporary tables
  709. *
  710. * @return void
  711. */
  712. public function testCreateTemporary() {
  713. $driver = $this->_getMockedDriver();
  714. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  715. $connection->expects($this->any())->method('driver')
  716. ->will($this->returnValue($driver));
  717. $table = (new Table('schema_articles'))->addColumn('id', [
  718. 'type' => 'integer',
  719. 'null' => false
  720. ]);
  721. $table->temporary(true);
  722. $sql = $table->createSql($connection);
  723. $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]);
  724. }
  725. /**
  726. * Test primary key generation & auto-increment.
  727. *
  728. * @return void
  729. */
  730. public function testCreateSqlCompositeIntegerKey() {
  731. $driver = $this->_getMockedDriver();
  732. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  733. $connection->expects($this->any())->method('driver')
  734. ->will($this->returnValue($driver));
  735. $table = (new Table('articles_tags'))
  736. ->addColumn('article_id', [
  737. 'type' => 'integer',
  738. 'null' => false
  739. ])
  740. ->addColumn('tag_id', [
  741. 'type' => 'integer',
  742. 'null' => false,
  743. ])
  744. ->addConstraint('primary', [
  745. 'type' => 'primary',
  746. 'columns' => ['article_id', 'tag_id']
  747. ]);
  748. $expected = <<<SQL
  749. CREATE TABLE `articles_tags` (
  750. `article_id` INTEGER NOT NULL,
  751. `tag_id` INTEGER NOT NULL,
  752. PRIMARY KEY (`article_id`, `tag_id`)
  753. )
  754. SQL;
  755. $result = $table->createSql($connection);
  756. $this->assertCount(1, $result);
  757. $this->assertTextEquals($expected, $result[0]);
  758. $table = (new Table('composite_key'))
  759. ->addColumn('id', [
  760. 'type' => 'integer',
  761. 'null' => false,
  762. 'autoIncrement' => true
  763. ])
  764. ->addColumn('account_id', [
  765. 'type' => 'integer',
  766. 'null' => false,
  767. ])
  768. ->addConstraint('primary', [
  769. 'type' => 'primary',
  770. 'columns' => ['id', 'account_id']
  771. ]);
  772. $expected = <<<SQL
  773. CREATE TABLE `composite_key` (
  774. `id` INTEGER NOT NULL AUTO_INCREMENT,
  775. `account_id` INTEGER NOT NULL,
  776. PRIMARY KEY (`id`, `account_id`)
  777. )
  778. SQL;
  779. $result = $table->createSql($connection);
  780. $this->assertCount(1, $result);
  781. $this->assertTextEquals($expected, $result[0]);
  782. }
  783. /**
  784. * test dropSql
  785. *
  786. * @return void
  787. */
  788. public function testDropSql() {
  789. $driver = $this->_getMockedDriver();
  790. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  791. $connection->expects($this->any())->method('driver')
  792. ->will($this->returnValue($driver));
  793. $table = new Table('articles');
  794. $result = $table->dropSql($connection);
  795. $this->assertCount(1, $result);
  796. $this->assertEquals('DROP TABLE `articles`', $result[0]);
  797. }
  798. /**
  799. * Test truncateSql()
  800. *
  801. * @return void
  802. */
  803. public function testTruncateSql() {
  804. $driver = $this->_getMockedDriver();
  805. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  806. $connection->expects($this->any())->method('driver')
  807. ->will($this->returnValue($driver));
  808. $table = new Table('articles');
  809. $result = $table->truncateSql($connection);
  810. $this->assertCount(1, $result);
  811. $this->assertEquals('TRUNCATE TABLE `articles`', $result[0]);
  812. }
  813. /**
  814. * Test that constructing a schema dialect connects the driver.
  815. *
  816. * @return void
  817. */
  818. public function testConstructConnectsDriver() {
  819. $driver = $this->getMock('Cake\Database\Driver');
  820. $driver->expects($this->once())
  821. ->method('connect');
  822. $schema = new MysqlSchema($driver);
  823. }
  824. /**
  825. * Get a schema instance with a mocked driver/pdo instances
  826. *
  827. * @return MysqlSchema
  828. */
  829. protected function _getMockedDriver() {
  830. $driver = new \Cake\Database\Driver\Mysql();
  831. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  832. $mock->expects($this->any())
  833. ->method('quote')
  834. ->will($this->returnCallback(function ($value) {
  835. return '"' . $value . '"';
  836. }));
  837. $mock->expects($this->any())
  838. ->method('quoteIdentifier')
  839. ->will($this->returnCallback(function ($value) {
  840. return '`' . $value . '`';
  841. }));
  842. $driver->connection($mock);
  843. return $driver;
  844. }
  845. }