MysqlSchemaTest.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Schema;
  16. use Cake\Database\Driver\Mysql;
  17. use Cake\Database\Schema\Collection as SchemaCollection;
  18. use Cake\Database\Schema\MysqlSchema;
  19. use Cake\Database\Schema\TableSchema;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. use PDO;
  23. /**
  24. * Test case for Mysql Schema Dialect.
  25. */
  26. class MysqlSchemaTest extends TestCase
  27. {
  28. /**
  29. * Helper method for skipping tests that need a real connection.
  30. *
  31. * @return void
  32. */
  33. protected function _needsConnection()
  34. {
  35. $config = ConnectionManager::getConfig('test');
  36. $this->skipIf(strpos($config['driver'], 'Mysql') === false, 'Not using Mysql for test config');
  37. }
  38. /**
  39. * Data provider for convert column testing
  40. *
  41. * @return array
  42. */
  43. public static function convertColumnProvider()
  44. {
  45. return [
  46. [
  47. 'DATETIME',
  48. ['type' => 'datetime', 'length' => null],
  49. ],
  50. [
  51. 'DATE',
  52. ['type' => 'date', 'length' => null],
  53. ],
  54. [
  55. 'TIME',
  56. ['type' => 'time', 'length' => null],
  57. ],
  58. [
  59. 'TIMESTAMP',
  60. ['type' => 'timestamp', 'length' => null],
  61. ],
  62. [
  63. 'TINYINT(1)',
  64. ['type' => 'boolean', 'length' => null],
  65. ],
  66. [
  67. 'TINYINT(2)',
  68. ['type' => 'tinyinteger', 'length' => 2, 'unsigned' => false],
  69. ],
  70. [
  71. 'TINYINT(3)',
  72. ['type' => 'tinyinteger', 'length' => 3, 'unsigned' => false],
  73. ],
  74. [
  75. 'TINYINT(3) UNSIGNED',
  76. ['type' => 'tinyinteger', 'length' => 3, 'unsigned' => true],
  77. ],
  78. [
  79. 'SMALLINT(4)',
  80. ['type' => 'smallinteger', 'length' => 4, 'unsigned' => false],
  81. ],
  82. [
  83. 'SMALLINT(4) UNSIGNED',
  84. ['type' => 'smallinteger', 'length' => 4, 'unsigned' => true],
  85. ],
  86. [
  87. 'INTEGER(11)',
  88. ['type' => 'integer', 'length' => 11, 'unsigned' => false],
  89. ],
  90. [
  91. 'MEDIUMINT(11)',
  92. ['type' => 'integer', 'length' => 11, 'unsigned' => false],
  93. ],
  94. [
  95. 'INTEGER(11) UNSIGNED',
  96. ['type' => 'integer', 'length' => 11, 'unsigned' => true],
  97. ],
  98. [
  99. 'BIGINT',
  100. ['type' => 'biginteger', 'length' => null, 'unsigned' => false],
  101. ],
  102. [
  103. 'BIGINT UNSIGNED',
  104. ['type' => 'biginteger', 'length' => null, 'unsigned' => true],
  105. ],
  106. [
  107. 'VARCHAR(255)',
  108. ['type' => 'string', 'length' => 255, 'collate' => 'utf8_general_ci'],
  109. ],
  110. [
  111. 'CHAR(25)',
  112. ['type' => 'string', 'length' => 25, 'fixed' => true, 'collate' => 'utf8_general_ci'],
  113. ],
  114. [
  115. 'CHAR(36)',
  116. ['type' => 'uuid', 'length' => null],
  117. ],
  118. [
  119. 'BINARY(16)',
  120. ['type' => 'binaryuuid', 'length' => null],
  121. ],
  122. [
  123. 'BINARY(1)',
  124. ['type' => 'binary', 'length' => 1],
  125. ],
  126. [
  127. 'TEXT',
  128. ['type' => 'text', 'length' => null, 'collate' => 'utf8_general_ci'],
  129. ],
  130. [
  131. 'TINYTEXT',
  132. ['type' => 'text', 'length' => TableSchema::LENGTH_TINY, 'collate' => 'utf8_general_ci'],
  133. ],
  134. [
  135. 'MEDIUMTEXT',
  136. ['type' => 'text', 'length' => TableSchema::LENGTH_MEDIUM, 'collate' => 'utf8_general_ci'],
  137. ],
  138. [
  139. 'LONGTEXT',
  140. ['type' => 'text', 'length' => TableSchema::LENGTH_LONG, 'collate' => 'utf8_general_ci'],
  141. ],
  142. [
  143. 'TINYBLOB',
  144. ['type' => 'binary', 'length' => TableSchema::LENGTH_TINY],
  145. ],
  146. [
  147. 'BLOB',
  148. ['type' => 'binary', 'length' => null],
  149. ],
  150. [
  151. 'MEDIUMBLOB',
  152. ['type' => 'binary', 'length' => TableSchema::LENGTH_MEDIUM],
  153. ],
  154. [
  155. 'LONGBLOB',
  156. ['type' => 'binary', 'length' => TableSchema::LENGTH_LONG],
  157. ],
  158. [
  159. 'FLOAT',
  160. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false],
  161. ],
  162. [
  163. 'DOUBLE',
  164. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => false],
  165. ],
  166. [
  167. 'DOUBLE UNSIGNED',
  168. ['type' => 'float', 'length' => null, 'precision' => null, 'unsigned' => true],
  169. ],
  170. [
  171. 'DECIMAL(11,2) UNSIGNED',
  172. ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => true],
  173. ],
  174. [
  175. 'DECIMAL(11,2)',
  176. ['type' => 'decimal', 'length' => 11, 'precision' => 2, 'unsigned' => false],
  177. ],
  178. [
  179. 'FLOAT(11,2)',
  180. ['type' => 'float', 'length' => 11, 'precision' => 2, 'unsigned' => false],
  181. ],
  182. [
  183. 'FLOAT(11,2) UNSIGNED',
  184. ['type' => 'float', 'length' => 11, 'precision' => 2, 'unsigned' => true],
  185. ],
  186. [
  187. 'DOUBLE(10,4)',
  188. ['type' => 'float', 'length' => 10, 'precision' => 4, 'unsigned' => false],
  189. ],
  190. [
  191. 'DOUBLE(10,4) UNSIGNED',
  192. ['type' => 'float', 'length' => 10, 'precision' => 4, 'unsigned' => true],
  193. ],
  194. [
  195. 'JSON',
  196. ['type' => 'json', 'length' => null],
  197. ],
  198. ];
  199. }
  200. /**
  201. * Test parsing MySQL column types from field description.
  202. *
  203. * @dataProvider convertColumnProvider
  204. * @return void
  205. */
  206. public function testConvertColumn($type, $expected)
  207. {
  208. $field = [
  209. 'Field' => 'field',
  210. 'Type' => $type,
  211. 'Null' => 'YES',
  212. 'Default' => 'Default value',
  213. 'Collation' => 'utf8_general_ci',
  214. 'Comment' => 'Comment section',
  215. ];
  216. $expected += [
  217. 'null' => true,
  218. 'default' => 'Default value',
  219. 'comment' => 'Comment section',
  220. ];
  221. $driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')->getMock();
  222. $dialect = new MysqlSchema($driver);
  223. $table = new TableSchema('table');
  224. $dialect->convertColumnDescription($table, $field);
  225. $actual = array_intersect_key($table->getColumn('field'), $expected);
  226. ksort($expected);
  227. ksort($actual);
  228. $this->assertSame($expected, $actual);
  229. }
  230. /**
  231. * Helper method for testing methods.
  232. *
  233. * @param \Cake\Datasource\ConnectionInterface $connection
  234. * @return void
  235. */
  236. protected function _createTables($connection)
  237. {
  238. $this->_needsConnection();
  239. $connection->execute('DROP TABLE IF EXISTS schema_articles');
  240. $connection->execute('DROP TABLE IF EXISTS schema_authors');
  241. $connection->execute('DROP TABLE IF EXISTS schema_json');
  242. $table = <<<SQL
  243. CREATE TABLE schema_authors (
  244. id INT(11) PRIMARY KEY AUTO_INCREMENT,
  245. name VARCHAR(50),
  246. bio TEXT,
  247. created DATETIME
  248. )ENGINE=InnoDB
  249. SQL;
  250. $connection->execute($table);
  251. $table = <<<SQL
  252. CREATE TABLE schema_articles (
  253. id BIGINT PRIMARY KEY AUTO_INCREMENT,
  254. title VARCHAR(20) COMMENT 'A title',
  255. body TEXT,
  256. author_id INT(11) NOT NULL,
  257. published BOOLEAN DEFAULT 0,
  258. allow_comments TINYINT(1) DEFAULT 0,
  259. created DATETIME,
  260. KEY `author_idx` (`author_id`),
  261. UNIQUE KEY `length_idx` (`title`(4)),
  262. FOREIGN KEY `author_idx` (`author_id`) REFERENCES `schema_authors`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT
  263. ) ENGINE=InnoDB COLLATE=utf8_general_ci
  264. SQL;
  265. $connection->execute($table);
  266. if ($connection->getDriver()->supportsNativeJson()) {
  267. $table = <<<SQL
  268. CREATE TABLE schema_json (
  269. id INT(11) PRIMARY KEY AUTO_INCREMENT,
  270. data JSON NOT NULL
  271. )
  272. SQL;
  273. $connection->execute($table);
  274. }
  275. }
  276. /**
  277. * Integration test for SchemaCollection & MysqlDialect.
  278. *
  279. * @return void
  280. */
  281. public function testListTables()
  282. {
  283. $connection = ConnectionManager::get('test');
  284. $this->_createTables($connection);
  285. $schema = new SchemaCollection($connection);
  286. $result = $schema->listTables();
  287. $this->assertInternalType('array', $result);
  288. $this->assertContains('schema_articles', $result);
  289. $this->assertContains('schema_authors', $result);
  290. }
  291. /**
  292. * Test describing a table with Mysql
  293. *
  294. * @return void
  295. */
  296. public function testDescribeTable()
  297. {
  298. $connection = ConnectionManager::get('test');
  299. $this->_createTables($connection);
  300. $schema = new SchemaCollection($connection);
  301. $result = $schema->describe('schema_articles');
  302. $this->assertInstanceOf('Cake\Database\Schema\TableSchema', $result);
  303. $expected = [
  304. 'id' => [
  305. 'type' => 'biginteger',
  306. 'null' => false,
  307. 'unsigned' => false,
  308. 'default' => null,
  309. 'length' => 20,
  310. 'precision' => null,
  311. 'comment' => null,
  312. 'autoIncrement' => true,
  313. ],
  314. 'title' => [
  315. 'type' => 'string',
  316. 'null' => true,
  317. 'default' => null,
  318. 'length' => 20,
  319. 'precision' => null,
  320. 'comment' => 'A title',
  321. 'fixed' => null,
  322. 'collate' => 'utf8_general_ci',
  323. ],
  324. 'body' => [
  325. 'type' => 'text',
  326. 'null' => true,
  327. 'default' => null,
  328. 'length' => null,
  329. 'precision' => null,
  330. 'comment' => null,
  331. 'collate' => 'utf8_general_ci',
  332. ],
  333. 'author_id' => [
  334. 'type' => 'integer',
  335. 'null' => false,
  336. 'unsigned' => false,
  337. 'default' => null,
  338. 'length' => 11,
  339. 'precision' => null,
  340. 'comment' => null,
  341. 'autoIncrement' => null,
  342. ],
  343. 'published' => [
  344. 'type' => 'boolean',
  345. 'null' => true,
  346. 'default' => 0,
  347. 'length' => null,
  348. 'precision' => null,
  349. 'comment' => null,
  350. ],
  351. 'allow_comments' => [
  352. 'type' => 'boolean',
  353. 'null' => true,
  354. 'default' => 0,
  355. 'length' => null,
  356. 'precision' => null,
  357. 'comment' => null,
  358. ],
  359. 'created' => [
  360. 'type' => 'datetime',
  361. 'null' => true,
  362. 'default' => null,
  363. 'length' => null,
  364. 'precision' => null,
  365. 'comment' => null,
  366. ],
  367. ];
  368. $this->assertEquals(['id'], $result->primaryKey());
  369. foreach ($expected as $field => $definition) {
  370. $this->assertEquals(
  371. $definition,
  372. $result->getColumn($field),
  373. 'Field definition does not match for ' . $field
  374. );
  375. }
  376. }
  377. /**
  378. * Test describing a table with indexes in Mysql
  379. *
  380. * @return void
  381. */
  382. public function testDescribeTableIndexes()
  383. {
  384. $connection = ConnectionManager::get('test');
  385. $this->_createTables($connection);
  386. $schema = new SchemaCollection($connection);
  387. $result = $schema->describe('schema_articles');
  388. $this->assertInstanceOf('Cake\Database\Schema\TableSchema', $result);
  389. $this->assertCount(3, $result->constraints());
  390. $expected = [
  391. 'primary' => [
  392. 'type' => 'primary',
  393. 'columns' => ['id'],
  394. 'length' => [],
  395. ],
  396. 'length_idx' => [
  397. 'type' => 'unique',
  398. 'columns' => ['title'],
  399. 'length' => [
  400. 'title' => 4,
  401. ],
  402. ],
  403. 'schema_articles_ibfk_1' => [
  404. 'type' => 'foreign',
  405. 'columns' => ['author_id'],
  406. 'references' => ['schema_authors', 'id'],
  407. 'length' => [],
  408. 'update' => 'cascade',
  409. 'delete' => 'restrict',
  410. ],
  411. ];
  412. $this->assertEquals($expected['primary'], $result->getConstraint('primary'));
  413. $this->assertEquals($expected['length_idx'], $result->getConstraint('length_idx'));
  414. $this->assertEquals($expected['schema_articles_ibfk_1'], $result->getConstraint('schema_articles_ibfk_1'));
  415. $this->assertCount(1, $result->indexes());
  416. $expected = [
  417. 'type' => 'index',
  418. 'columns' => ['author_id'],
  419. 'length' => [],
  420. ];
  421. $this->assertEquals($expected, $result->getIndex('author_idx'));
  422. }
  423. /**
  424. * Test describing a table creates options
  425. *
  426. * @return void
  427. */
  428. public function testDescribeTableOptions()
  429. {
  430. $connection = ConnectionManager::get('test');
  431. $this->_createTables($connection);
  432. $schema = new SchemaCollection($connection);
  433. $result = $schema->describe('schema_articles');
  434. $this->assertArrayHasKey('engine', $result->getOptions());
  435. $this->assertArrayHasKey('collation', $result->getOptions());
  436. }
  437. public function testDescribeNonPrimaryAutoIncrement()
  438. {
  439. $this->_needsConnection();
  440. $connection = ConnectionManager::get('test');
  441. $sql = <<<SQL
  442. CREATE TABLE `odd_primary_key` (
  443. `id` BIGINT UNSIGNED NOT NULL,
  444. `other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,
  445. PRIMARY KEY (`id`),
  446. UNIQUE KEY `other_field` (`other_field`)
  447. )
  448. SQL;
  449. $connection->execute($sql);
  450. $schema = new SchemaCollection($connection);
  451. $table = $schema->describe('odd_primary_key');
  452. $connection->execute('DROP TABLE odd_primary_key');
  453. $column = $table->getColumn('id');
  454. $this->assertNull($column['autoIncrement'], 'should not autoincrement');
  455. $this->assertTrue($column['unsigned'], 'should be unsigned');
  456. $column = $table->getColumn('other_field');
  457. $this->assertTrue($column['autoIncrement'], 'should not autoincrement');
  458. $this->assertFalse($column['unsigned'], 'should not be unsigned');
  459. $output = $table->createSql($connection);
  460. $this->assertContains('`id` BIGINT UNSIGNED NOT NULL,', $output[0]);
  461. $this->assertContains('`other_field` INTEGER(11) NOT NULL AUTO_INCREMENT,', $output[0]);
  462. }
  463. /**
  464. * Column provider for creating column sql
  465. *
  466. * @return array
  467. */
  468. public static function columnSqlProvider()
  469. {
  470. return [
  471. // strings
  472. [
  473. 'title',
  474. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => null],
  475. '`title` VARCHAR(25)',
  476. ],
  477. [
  478. 'title',
  479. ['type' => 'string', 'length' => 25, 'null' => false],
  480. '`title` VARCHAR(25) NOT NULL',
  481. ],
  482. [
  483. 'title',
  484. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
  485. '`title` VARCHAR(25) DEFAULT \'ignored\'',
  486. ],
  487. [
  488. 'title',
  489. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => ''],
  490. '`title` VARCHAR(25) DEFAULT \'\'',
  491. ],
  492. [
  493. 'role',
  494. ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
  495. '`role` VARCHAR(10) NOT NULL DEFAULT \'admin\'',
  496. ],
  497. [
  498. 'id',
  499. ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
  500. '`id` CHAR(32) NOT NULL',
  501. ],
  502. [
  503. 'title',
  504. ['type' => 'string'],
  505. '`title` VARCHAR(255)',
  506. ],
  507. [
  508. 'id',
  509. ['type' => 'uuid'],
  510. '`id` CHAR(36)',
  511. ],
  512. [
  513. 'id',
  514. ['type' => 'binaryuuid'],
  515. '`id` BINARY(16)',
  516. ],
  517. [
  518. 'title',
  519. ['type' => 'string', 'length' => 255, 'null' => false, 'collate' => 'utf8_unicode_ci'],
  520. '`title` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL',
  521. ],
  522. // Text
  523. [
  524. 'body',
  525. ['type' => 'text', 'null' => false],
  526. '`body` TEXT NOT NULL',
  527. ],
  528. [
  529. 'body',
  530. ['type' => 'text', 'length' => TableSchema::LENGTH_TINY, 'null' => false],
  531. '`body` TINYTEXT NOT NULL',
  532. ],
  533. [
  534. 'body',
  535. ['type' => 'text', 'length' => TableSchema::LENGTH_MEDIUM, 'null' => false],
  536. '`body` MEDIUMTEXT NOT NULL',
  537. ],
  538. [
  539. 'body',
  540. ['type' => 'text', 'length' => TableSchema::LENGTH_LONG, 'null' => false],
  541. '`body` LONGTEXT NOT NULL',
  542. ],
  543. [
  544. 'body',
  545. ['type' => 'text', 'null' => false, 'collate' => 'utf8_unicode_ci'],
  546. '`body` TEXT COLLATE utf8_unicode_ci NOT NULL',
  547. ],
  548. // Blob / binary
  549. [
  550. 'body',
  551. ['type' => 'binary', 'null' => false],
  552. '`body` BLOB NOT NULL',
  553. ],
  554. [
  555. 'body',
  556. ['type' => 'binary', 'length' => TableSchema::LENGTH_TINY, 'null' => false],
  557. '`body` TINYBLOB NOT NULL',
  558. ],
  559. [
  560. 'body',
  561. ['type' => 'binary', 'length' => TableSchema::LENGTH_MEDIUM, 'null' => false],
  562. '`body` MEDIUMBLOB NOT NULL',
  563. ],
  564. [
  565. 'body',
  566. ['type' => 'binary', 'length' => TableSchema::LENGTH_LONG, 'null' => false],
  567. '`body` LONGBLOB NOT NULL',
  568. ],
  569. [
  570. 'bytes',
  571. ['type' => 'binary', 'length' => 5],
  572. '`bytes` VARBINARY(5)',
  573. ],
  574. [
  575. 'bit',
  576. ['type' => 'binary', 'length' => 1],
  577. '`bit` BINARY(1)',
  578. ],
  579. // Integers
  580. [
  581. 'post_id',
  582. ['type' => 'tinyinteger', 'length' => 2],
  583. '`post_id` TINYINT(2)',
  584. ],
  585. [
  586. 'post_id',
  587. ['type' => 'tinyinteger', 'length' => 2, 'unsigned' => true],
  588. '`post_id` TINYINT(2) UNSIGNED',
  589. ],
  590. [
  591. 'post_id',
  592. ['type' => 'smallinteger', 'length' => 4],
  593. '`post_id` SMALLINT(4)',
  594. ],
  595. [
  596. 'post_id',
  597. ['type' => 'smallinteger', 'length' => 4, 'unsigned' => true],
  598. '`post_id` SMALLINT(4) UNSIGNED',
  599. ],
  600. [
  601. 'post_id',
  602. ['type' => 'integer', 'length' => 11],
  603. '`post_id` INTEGER(11)',
  604. ],
  605. [
  606. 'post_id',
  607. ['type' => 'integer', 'length' => 11, 'unsigned' => true],
  608. '`post_id` INTEGER(11) UNSIGNED',
  609. ],
  610. [
  611. 'post_id',
  612. ['type' => 'biginteger', 'length' => 20],
  613. '`post_id` BIGINT',
  614. ],
  615. [
  616. 'post_id',
  617. ['type' => 'biginteger', 'length' => 20, 'unsigned' => true],
  618. '`post_id` BIGINT UNSIGNED',
  619. ],
  620. [
  621. 'post_id',
  622. ['type' => 'integer', 'length' => 20, 'autoIncrement' => true],
  623. '`post_id` INTEGER(20) AUTO_INCREMENT',
  624. ],
  625. [
  626. 'post_id',
  627. ['type' => 'integer', 'length' => 20, 'null' => false, 'autoIncrement' => false],
  628. '`post_id` INTEGER(20) NOT NULL',
  629. ],
  630. [
  631. 'post_id',
  632. ['type' => 'biginteger', 'length' => 20, 'autoIncrement' => true],
  633. '`post_id` BIGINT AUTO_INCREMENT',
  634. ],
  635. // Decimal
  636. [
  637. 'value',
  638. ['type' => 'decimal'],
  639. '`value` DECIMAL',
  640. ],
  641. [
  642. 'value',
  643. ['type' => 'decimal', 'length' => 11, 'unsigned' => true],
  644. '`value` DECIMAL(11) UNSIGNED',
  645. ],
  646. [
  647. 'value',
  648. ['type' => 'decimal', 'length' => 12, 'precision' => 5],
  649. '`value` DECIMAL(12,5)',
  650. ],
  651. // Float
  652. [
  653. 'value',
  654. ['type' => 'float', 'unsigned'],
  655. '`value` FLOAT',
  656. ],
  657. [
  658. 'value',
  659. ['type' => 'float', 'unsigned' => true],
  660. '`value` FLOAT UNSIGNED',
  661. ],
  662. [
  663. 'latitude',
  664. ['type' => 'float', 'length' => 53, 'null' => true, 'default' => null, 'unsigned' => true],
  665. '`latitude` FLOAT(53) UNSIGNED',
  666. ],
  667. [
  668. 'value',
  669. ['type' => 'float', 'length' => 11, 'precision' => 3],
  670. '`value` FLOAT(11,3)',
  671. ],
  672. // Boolean
  673. [
  674. 'checked',
  675. ['type' => 'boolean', 'default' => false],
  676. '`checked` BOOLEAN DEFAULT FALSE',
  677. ],
  678. [
  679. 'checked',
  680. ['type' => 'boolean', 'default' => false, 'null' => false],
  681. '`checked` BOOLEAN NOT NULL DEFAULT FALSE',
  682. ],
  683. [
  684. 'checked',
  685. ['type' => 'boolean', 'default' => true, 'null' => false],
  686. '`checked` BOOLEAN NOT NULL DEFAULT TRUE',
  687. ],
  688. [
  689. 'checked',
  690. ['type' => 'boolean', 'default' => false, 'null' => true],
  691. '`checked` BOOLEAN DEFAULT FALSE',
  692. ],
  693. // datetimes
  694. [
  695. 'created',
  696. ['type' => 'datetime', 'comment' => 'Created timestamp'],
  697. '`created` DATETIME COMMENT \'Created timestamp\'',
  698. ],
  699. [
  700. 'created',
  701. ['type' => 'datetime', 'null' => false, 'default' => 'current_timestamp'],
  702. '`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP',
  703. ],
  704. [
  705. 'open_date',
  706. ['type' => 'datetime', 'null' => false, 'default' => '2016-12-07 23:04:00'],
  707. '`open_date` DATETIME NOT NULL DEFAULT \'2016-12-07 23:04:00\'',
  708. ],
  709. // Date & Time
  710. [
  711. 'start_date',
  712. ['type' => 'date'],
  713. '`start_date` DATE',
  714. ],
  715. [
  716. 'start_time',
  717. ['type' => 'time'],
  718. '`start_time` TIME',
  719. ],
  720. // timestamps
  721. [
  722. 'created',
  723. ['type' => 'timestamp', 'null' => true],
  724. '`created` TIMESTAMP NULL',
  725. ],
  726. [
  727. 'created',
  728. ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp'],
  729. '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP',
  730. ],
  731. [
  732. 'created',
  733. ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp()'],
  734. '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP',
  735. ],
  736. [
  737. 'open_date',
  738. ['type' => 'timestamp', 'null' => false, 'default' => '2016-12-07 23:04:00'],
  739. '`open_date` TIMESTAMP NOT NULL DEFAULT \'2016-12-07 23:04:00\'',
  740. ],
  741. ];
  742. }
  743. /**
  744. * Test generating column definitions
  745. *
  746. * @dataProvider columnSqlProvider
  747. * @return void
  748. */
  749. public function testColumnSql($name, $data, $expected)
  750. {
  751. $driver = $this->_getMockedDriver();
  752. $schema = new MysqlSchema($driver);
  753. $table = (new TableSchema('articles'))->addColumn($name, $data);
  754. $this->assertEquals($expected, $schema->columnSql($table, $name));
  755. }
  756. /**
  757. * Provide data for testing constraintSql
  758. *
  759. * @return array
  760. */
  761. public static function constraintSqlProvider()
  762. {
  763. return [
  764. [
  765. 'primary',
  766. ['type' => 'primary', 'columns' => ['title']],
  767. 'PRIMARY KEY (`title`)',
  768. ],
  769. [
  770. 'unique_idx',
  771. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  772. 'UNIQUE KEY `unique_idx` (`title`, `author_id`)',
  773. ],
  774. [
  775. 'length_idx',
  776. [
  777. 'type' => 'unique',
  778. 'columns' => ['author_id', 'title'],
  779. 'length' => ['author_id' => 5, 'title' => 4],
  780. ],
  781. 'UNIQUE KEY `length_idx` (`author_id`(5), `title`(4))',
  782. ],
  783. [
  784. 'author_id_idx',
  785. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  786. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  787. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT',
  788. ],
  789. [
  790. 'author_id_idx',
  791. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  792. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  793. 'REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT',
  794. ],
  795. [
  796. 'author_id_idx',
  797. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
  798. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  799. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT',
  800. ],
  801. [
  802. 'author_id_idx',
  803. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  804. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  805. 'REFERENCES `authors` (`id`) ON UPDATE SET NULL ON DELETE RESTRICT',
  806. ],
  807. [
  808. 'author_id_idx',
  809. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  810. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  811. 'REFERENCES `authors` (`id`) ON UPDATE NO ACTION ON DELETE RESTRICT',
  812. ],
  813. ];
  814. }
  815. /**
  816. * Test the constraintSql method.
  817. *
  818. * @dataProvider constraintSqlProvider
  819. */
  820. public function testConstraintSql($name, $data, $expected)
  821. {
  822. $driver = $this->_getMockedDriver();
  823. $schema = new MysqlSchema($driver);
  824. $table = (new TableSchema('articles'))->addColumn('title', [
  825. 'type' => 'string',
  826. 'length' => 255,
  827. ])->addColumn('author_id', [
  828. 'type' => 'integer',
  829. ])->addConstraint($name, $data);
  830. $this->assertEquals($expected, $schema->constraintSql($table, $name));
  831. }
  832. /**
  833. * Test provider for indexSql()
  834. *
  835. * @return array
  836. */
  837. public static function indexSqlProvider()
  838. {
  839. return [
  840. [
  841. 'key_key',
  842. ['type' => 'index', 'columns' => ['author_id']],
  843. 'KEY `key_key` (`author_id`)',
  844. ],
  845. [
  846. 'full_text',
  847. ['type' => 'fulltext', 'columns' => ['title']],
  848. 'FULLTEXT KEY `full_text` (`title`)',
  849. ],
  850. ];
  851. }
  852. /**
  853. * Test the indexSql method.
  854. *
  855. * @dataProvider indexSqlProvider
  856. */
  857. public function testIndexSql($name, $data, $expected)
  858. {
  859. $driver = $this->_getMockedDriver();
  860. $schema = new MysqlSchema($driver);
  861. $table = (new TableSchema('articles'))->addColumn('title', [
  862. 'type' => 'string',
  863. 'length' => 255,
  864. ])->addColumn('author_id', [
  865. 'type' => 'integer',
  866. ])->addIndex($name, $data);
  867. $this->assertEquals($expected, $schema->indexSql($table, $name));
  868. }
  869. /**
  870. * Test the addConstraintSql method.
  871. *
  872. * @return void
  873. */
  874. public function testAddConstraintSql()
  875. {
  876. $driver = $this->_getMockedDriver();
  877. $connection = $this->getMockBuilder('Cake\Database\Connection')
  878. ->disableOriginalConstructor()
  879. ->getMock();
  880. $connection->expects($this->any())->method('getDriver')
  881. ->will($this->returnValue($driver));
  882. $table = (new TableSchema('posts'))
  883. ->addColumn('author_id', [
  884. 'type' => 'integer',
  885. 'null' => false,
  886. ])
  887. ->addColumn('category_id', [
  888. 'type' => 'integer',
  889. 'null' => false,
  890. ])
  891. ->addColumn('category_name', [
  892. 'type' => 'integer',
  893. 'null' => false,
  894. ])
  895. ->addConstraint('author_fk', [
  896. 'type' => 'foreign',
  897. 'columns' => ['author_id'],
  898. 'references' => ['authors', 'id'],
  899. 'update' => 'cascade',
  900. 'delete' => 'cascade',
  901. ])
  902. ->addConstraint('category_fk', [
  903. 'type' => 'foreign',
  904. 'columns' => ['category_id', 'category_name'],
  905. 'references' => ['categories', ['id', 'name']],
  906. 'update' => 'cascade',
  907. 'delete' => 'cascade',
  908. ]);
  909. $expected = [
  910. 'ALTER TABLE `posts` ADD CONSTRAINT `author_fk` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;',
  911. 'ALTER TABLE `posts` ADD CONSTRAINT `category_fk` FOREIGN KEY (`category_id`, `category_name`) REFERENCES `categories` (`id`, `name`) ON UPDATE CASCADE ON DELETE CASCADE;',
  912. ];
  913. $result = $table->addConstraintSql($connection);
  914. $this->assertCount(2, $result);
  915. $this->assertEquals($expected, $result);
  916. }
  917. /**
  918. * Test the dropConstraintSql method.
  919. *
  920. * @return void
  921. */
  922. public function testDropConstraintSql()
  923. {
  924. $driver = $this->_getMockedDriver();
  925. $connection = $this->getMockBuilder('Cake\Database\Connection')
  926. ->disableOriginalConstructor()
  927. ->getMock();
  928. $connection->expects($this->any())->method('getDriver')
  929. ->will($this->returnValue($driver));
  930. $table = (new TableSchema('posts'))
  931. ->addColumn('author_id', [
  932. 'type' => 'integer',
  933. 'null' => false,
  934. ])
  935. ->addColumn('category_id', [
  936. 'type' => 'integer',
  937. 'null' => false,
  938. ])
  939. ->addColumn('category_name', [
  940. 'type' => 'integer',
  941. 'null' => false,
  942. ])
  943. ->addConstraint('author_fk', [
  944. 'type' => 'foreign',
  945. 'columns' => ['author_id'],
  946. 'references' => ['authors', 'id'],
  947. 'update' => 'cascade',
  948. 'delete' => 'cascade',
  949. ])
  950. ->addConstraint('category_fk', [
  951. 'type' => 'foreign',
  952. 'columns' => ['category_id', 'category_name'],
  953. 'references' => ['categories', ['id', 'name']],
  954. 'update' => 'cascade',
  955. 'delete' => 'cascade',
  956. ]);
  957. $expected = [
  958. 'ALTER TABLE `posts` DROP FOREIGN KEY `author_fk`;',
  959. 'ALTER TABLE `posts` DROP FOREIGN KEY `category_fk`;',
  960. ];
  961. $result = $table->dropConstraintSql($connection);
  962. $this->assertCount(2, $result);
  963. $this->assertEquals($expected, $result);
  964. }
  965. /**
  966. * Test generating a column that is a primary key.
  967. *
  968. * @return void
  969. */
  970. public function testColumnSqlPrimaryKey()
  971. {
  972. $driver = $this->_getMockedDriver();
  973. $schema = new MysqlSchema($driver);
  974. $table = new TableSchema('articles');
  975. $table->addColumn('id', [
  976. 'type' => 'integer',
  977. 'null' => false,
  978. ])
  979. ->addConstraint('primary', [
  980. 'type' => 'primary',
  981. 'columns' => ['id'],
  982. ]);
  983. $result = $schema->columnSql($table, 'id');
  984. $this->assertEquals($result, '`id` INTEGER NOT NULL AUTO_INCREMENT');
  985. $table = new TableSchema('articles');
  986. $table->addColumn('id', [
  987. 'type' => 'biginteger',
  988. 'null' => false,
  989. ])
  990. ->addConstraint('primary', [
  991. 'type' => 'primary',
  992. 'columns' => ['id'],
  993. ]);
  994. $result = $schema->columnSql($table, 'id');
  995. $this->assertEquals($result, '`id` BIGINT NOT NULL AUTO_INCREMENT');
  996. }
  997. /**
  998. * Integration test for converting a Schema\Table into MySQL table creates.
  999. *
  1000. * @return void
  1001. */
  1002. public function testCreateSql()
  1003. {
  1004. $driver = $this->_getMockedDriver();
  1005. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1006. ->disableOriginalConstructor()
  1007. ->getMock();
  1008. $connection->expects($this->any())->method('getDriver')
  1009. ->will($this->returnValue($driver));
  1010. $driver->getConnection()
  1011. ->expects($this->any())
  1012. ->method('getAttribute')
  1013. ->will($this->returnValue('5.6.0'));
  1014. $table = (new TableSchema('posts'))->addColumn('id', [
  1015. 'type' => 'integer',
  1016. 'null' => false,
  1017. ])
  1018. ->addColumn('title', [
  1019. 'type' => 'string',
  1020. 'null' => false,
  1021. 'comment' => 'The title',
  1022. ])
  1023. ->addColumn('body', [
  1024. 'type' => 'text',
  1025. 'comment' => '',
  1026. ])
  1027. ->addColumn('data', [
  1028. 'type' => 'json',
  1029. ])
  1030. ->addColumn('hash', [
  1031. 'type' => 'string',
  1032. 'fixed' => true,
  1033. 'length' => 40,
  1034. 'collate' => 'latin1_bin',
  1035. 'null' => false,
  1036. ])
  1037. ->addColumn('created', 'datetime')
  1038. ->addConstraint('primary', [
  1039. 'type' => 'primary',
  1040. 'columns' => ['id'],
  1041. ])
  1042. ->setOptions([
  1043. 'engine' => 'InnoDB',
  1044. 'charset' => 'utf8',
  1045. 'collate' => 'utf8_general_ci',
  1046. ]);
  1047. $expected = <<<SQL
  1048. CREATE TABLE `posts` (
  1049. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1050. `title` VARCHAR(255) NOT NULL COMMENT 'The title',
  1051. `body` TEXT,
  1052. `data` LONGTEXT,
  1053. `hash` CHAR(40) COLLATE latin1_bin NOT NULL,
  1054. `created` DATETIME,
  1055. PRIMARY KEY (`id`)
  1056. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
  1057. SQL;
  1058. $result = $table->createSql($connection);
  1059. $this->assertCount(1, $result);
  1060. $this->assertTextEquals($expected, $result[0]);
  1061. }
  1062. /**
  1063. * Integration test for converting a Schema\Table with native JSON
  1064. *
  1065. * @return void
  1066. */
  1067. public function testCreateSqlJson()
  1068. {
  1069. $driver = $this->_getMockedDriver();
  1070. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1071. ->disableOriginalConstructor()
  1072. ->getMock();
  1073. $connection->expects($this->any())
  1074. ->method('getDriver')
  1075. ->will($this->returnValue($driver));
  1076. $driver->getConnection()
  1077. ->expects($this->any())
  1078. ->method('getAttribute')
  1079. ->will($this->returnValue('5.7.0'));
  1080. $table = (new TableSchema('posts'))->addColumn('id', [
  1081. 'type' => 'integer',
  1082. 'null' => false,
  1083. ])
  1084. ->addColumn('data', [
  1085. 'type' => 'json',
  1086. ])
  1087. ->addConstraint('primary', [
  1088. 'type' => 'primary',
  1089. 'columns' => ['id'],
  1090. ])
  1091. ->setOptions([
  1092. 'engine' => 'InnoDB',
  1093. 'charset' => 'utf8',
  1094. 'collate' => 'utf8_general_ci',
  1095. ]);
  1096. $expected = <<<SQL
  1097. CREATE TABLE `posts` (
  1098. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1099. `data` JSON,
  1100. PRIMARY KEY (`id`)
  1101. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
  1102. SQL;
  1103. $result = $table->createSql($connection);
  1104. $this->assertCount(1, $result);
  1105. $this->assertTextEquals($expected, $result[0]);
  1106. }
  1107. /**
  1108. * Tests creating temporary tables
  1109. *
  1110. * @return void
  1111. */
  1112. public function testCreateTemporary()
  1113. {
  1114. $driver = $this->_getMockedDriver();
  1115. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1116. ->disableOriginalConstructor()
  1117. ->getMock();
  1118. $connection->expects($this->any())->method('getDriver')
  1119. ->will($this->returnValue($driver));
  1120. $table = (new TableSchema('schema_articles'))->addColumn('id', [
  1121. 'type' => 'integer',
  1122. 'null' => false,
  1123. ]);
  1124. $table->setTemporary(true);
  1125. $sql = $table->createSql($connection);
  1126. $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]);
  1127. }
  1128. /**
  1129. * Test primary key generation & auto-increment.
  1130. *
  1131. * @return void
  1132. */
  1133. public function testCreateSqlCompositeIntegerKey()
  1134. {
  1135. $driver = $this->_getMockedDriver();
  1136. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1137. ->disableOriginalConstructor()
  1138. ->getMock();
  1139. $connection->expects($this->any())->method('getDriver')
  1140. ->will($this->returnValue($driver));
  1141. $table = (new TableSchema('articles_tags'))
  1142. ->addColumn('article_id', [
  1143. 'type' => 'integer',
  1144. 'null' => false,
  1145. ])
  1146. ->addColumn('tag_id', [
  1147. 'type' => 'integer',
  1148. 'null' => false,
  1149. ])
  1150. ->addConstraint('primary', [
  1151. 'type' => 'primary',
  1152. 'columns' => ['article_id', 'tag_id'],
  1153. ]);
  1154. $expected = <<<SQL
  1155. CREATE TABLE `articles_tags` (
  1156. `article_id` INTEGER NOT NULL,
  1157. `tag_id` INTEGER NOT NULL,
  1158. PRIMARY KEY (`article_id`, `tag_id`)
  1159. )
  1160. SQL;
  1161. $result = $table->createSql($connection);
  1162. $this->assertCount(1, $result);
  1163. $this->assertTextEquals($expected, $result[0]);
  1164. $table = (new TableSchema('composite_key'))
  1165. ->addColumn('id', [
  1166. 'type' => 'integer',
  1167. 'null' => false,
  1168. 'autoIncrement' => true,
  1169. ])
  1170. ->addColumn('account_id', [
  1171. 'type' => 'integer',
  1172. 'null' => false,
  1173. ])
  1174. ->addConstraint('primary', [
  1175. 'type' => 'primary',
  1176. 'columns' => ['id', 'account_id'],
  1177. ]);
  1178. $expected = <<<SQL
  1179. CREATE TABLE `composite_key` (
  1180. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1181. `account_id` INTEGER NOT NULL,
  1182. PRIMARY KEY (`id`, `account_id`)
  1183. )
  1184. SQL;
  1185. $result = $table->createSql($connection);
  1186. $this->assertCount(1, $result);
  1187. $this->assertTextEquals($expected, $result[0]);
  1188. }
  1189. /**
  1190. * test dropSql
  1191. *
  1192. * @return void
  1193. */
  1194. public function testDropSql()
  1195. {
  1196. $driver = $this->_getMockedDriver();
  1197. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1198. ->disableOriginalConstructor()
  1199. ->getMock();
  1200. $connection->expects($this->any())->method('getDriver')
  1201. ->will($this->returnValue($driver));
  1202. $table = new TableSchema('articles');
  1203. $result = $table->dropSql($connection);
  1204. $this->assertCount(1, $result);
  1205. $this->assertEquals('DROP TABLE `articles`', $result[0]);
  1206. }
  1207. /**
  1208. * Test truncateSql()
  1209. *
  1210. * @return void
  1211. */
  1212. public function testTruncateSql()
  1213. {
  1214. $driver = $this->_getMockedDriver();
  1215. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1216. ->disableOriginalConstructor()
  1217. ->getMock();
  1218. $connection->expects($this->any())->method('getDriver')
  1219. ->will($this->returnValue($driver));
  1220. $table = new TableSchema('articles');
  1221. $result = $table->truncateSql($connection);
  1222. $this->assertCount(1, $result);
  1223. $this->assertEquals('TRUNCATE TABLE `articles`', $result[0]);
  1224. }
  1225. /**
  1226. * Test that constructing a schema dialect connects the driver.
  1227. *
  1228. * @return void
  1229. */
  1230. public function testConstructConnectsDriver()
  1231. {
  1232. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  1233. $driver->expects($this->once())
  1234. ->method('connect');
  1235. $schema = new MysqlSchema($driver);
  1236. }
  1237. /**
  1238. * Tests json column parsing on Mysql 5.7+
  1239. *
  1240. * @return void
  1241. */
  1242. public function testDescribeJson()
  1243. {
  1244. $connection = ConnectionManager::get('test');
  1245. $this->_createTables($connection);
  1246. $this->skipIf(!$connection->getDriver()->supportsNativeJson(), 'Does not support native json');
  1247. $schema = new SchemaCollection($connection);
  1248. $result = $schema->describe('schema_json');
  1249. $this->assertInstanceOf('Cake\Database\Schema\TableSchema', $result);
  1250. $expected = [
  1251. 'type' => 'json',
  1252. 'null' => false,
  1253. 'default' => null,
  1254. 'length' => null,
  1255. 'precision' => null,
  1256. 'comment' => null,
  1257. ];
  1258. $this->assertEquals(
  1259. $expected,
  1260. $result->getColumn('data'),
  1261. 'Field definition does not match for data'
  1262. );
  1263. }
  1264. /**
  1265. * Get a schema instance with a mocked driver/pdo instances
  1266. *
  1267. * @return MysqlSchema
  1268. */
  1269. protected function _getMockedDriver()
  1270. {
  1271. $driver = new Mysql();
  1272. $mock = $this->getMockBuilder(PDO::class)
  1273. ->setMethods(['quote', 'quoteIdentifier', 'getAttribute'])
  1274. ->disableOriginalConstructor()
  1275. ->getMock();
  1276. $mock->expects($this->any())
  1277. ->method('quote')
  1278. ->will($this->returnCallback(function ($value) {
  1279. return "'$value'";
  1280. }));
  1281. $driver->setConnection($mock);
  1282. return $driver;
  1283. }
  1284. }