MysqlSchemaTest.php 28 KB

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