MysqlSchemaTest.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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]
  109. ],
  110. [
  111. 'CHAR(25)',
  112. ['type' => 'string', 'length' => 25, 'fixed' => true]
  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]
  129. ],
  130. [
  131. 'TINYTEXT',
  132. ['type' => 'text', 'length' => TableSchema::LENGTH_TINY]
  133. ],
  134. [
  135. 'MEDIUMTEXT',
  136. ['type' => 'text', 'length' => TableSchema::LENGTH_MEDIUM]
  137. ],
  138. [
  139. 'LONGTEXT',
  140. ['type' => 'text', 'length' => TableSchema::LENGTH_LONG]
  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. 'collate' => 'utf8_general_ci',
  220. 'comment' => 'Comment section',
  221. ];
  222. $driver = $this->getMockBuilder('Cake\Database\Driver\Mysql')->getMock();
  223. $dialect = new MysqlSchema($driver);
  224. $table = $this->getMockBuilder(TableSchema::class)
  225. ->setConstructorArgs(['table'])
  226. ->getMock();
  227. $table->expects($this->at(0))->method('addColumn')->with('field', $expected);
  228. $dialect->convertColumnDescription($table, $field);
  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,0) 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. 'value',
  664. ['type' => 'float', 'length' => 11, 'precision' => 3],
  665. '`value` FLOAT(11,3)'
  666. ],
  667. // Boolean
  668. [
  669. 'checked',
  670. ['type' => 'boolean', 'default' => false],
  671. '`checked` BOOLEAN DEFAULT FALSE'
  672. ],
  673. [
  674. 'checked',
  675. ['type' => 'boolean', 'default' => false, 'null' => false],
  676. '`checked` BOOLEAN NOT NULL DEFAULT FALSE'
  677. ],
  678. [
  679. 'checked',
  680. ['type' => 'boolean', 'default' => true, 'null' => false],
  681. '`checked` BOOLEAN NOT NULL DEFAULT TRUE'
  682. ],
  683. [
  684. 'checked',
  685. ['type' => 'boolean', 'default' => false, 'null' => true],
  686. '`checked` BOOLEAN DEFAULT FALSE'
  687. ],
  688. // datetimes
  689. [
  690. 'created',
  691. ['type' => 'datetime', 'comment' => 'Created timestamp'],
  692. '`created` DATETIME COMMENT \'Created timestamp\''
  693. ],
  694. [
  695. 'created',
  696. ['type' => 'datetime', 'null' => false, 'default' => 'current_timestamp'],
  697. '`created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP'
  698. ],
  699. [
  700. 'open_date',
  701. ['type' => 'datetime', 'null' => false, 'default' => '2016-12-07 23:04:00'],
  702. '`open_date` DATETIME NOT NULL DEFAULT \'2016-12-07 23:04:00\''
  703. ],
  704. // Date & Time
  705. [
  706. 'start_date',
  707. ['type' => 'date'],
  708. '`start_date` DATE'
  709. ],
  710. [
  711. 'start_time',
  712. ['type' => 'time'],
  713. '`start_time` TIME'
  714. ],
  715. // timestamps
  716. [
  717. 'created',
  718. ['type' => 'timestamp', 'null' => true],
  719. '`created` TIMESTAMP NULL'
  720. ],
  721. [
  722. 'created',
  723. ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp'],
  724. '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
  725. ],
  726. [
  727. 'created',
  728. ['type' => 'timestamp', 'null' => false, 'default' => 'current_timestamp()'],
  729. '`created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'
  730. ],
  731. [
  732. 'open_date',
  733. ['type' => 'timestamp', 'null' => false, 'default' => '2016-12-07 23:04:00'],
  734. '`open_date` TIMESTAMP NOT NULL DEFAULT \'2016-12-07 23:04:00\''
  735. ],
  736. ];
  737. }
  738. /**
  739. * Test generating column definitions
  740. *
  741. * @dataProvider columnSqlProvider
  742. * @return void
  743. */
  744. public function testColumnSql($name, $data, $expected)
  745. {
  746. $driver = $this->_getMockedDriver();
  747. $schema = new MysqlSchema($driver);
  748. $table = (new TableSchema('articles'))->addColumn($name, $data);
  749. $this->assertEquals($expected, $schema->columnSql($table, $name));
  750. }
  751. /**
  752. * Provide data for testing constraintSql
  753. *
  754. * @return array
  755. */
  756. public static function constraintSqlProvider()
  757. {
  758. return [
  759. [
  760. 'primary',
  761. ['type' => 'primary', 'columns' => ['title']],
  762. 'PRIMARY KEY (`title`)'
  763. ],
  764. [
  765. 'unique_idx',
  766. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  767. 'UNIQUE KEY `unique_idx` (`title`, `author_id`)'
  768. ],
  769. [
  770. 'length_idx',
  771. [
  772. 'type' => 'unique',
  773. 'columns' => ['author_id', 'title'],
  774. 'length' => ['author_id' => 5, 'title' => 4]
  775. ],
  776. 'UNIQUE KEY `length_idx` (`author_id`(5), `title`(4))'
  777. ],
  778. [
  779. 'author_id_idx',
  780. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  781. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  782. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
  783. ],
  784. [
  785. 'author_id_idx',
  786. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  787. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  788. 'REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE RESTRICT'
  789. ],
  790. [
  791. 'author_id_idx',
  792. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
  793. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  794. 'REFERENCES `authors` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT'
  795. ],
  796. [
  797. 'author_id_idx',
  798. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  799. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  800. 'REFERENCES `authors` (`id`) ON UPDATE SET NULL ON DELETE RESTRICT'
  801. ],
  802. [
  803. 'author_id_idx',
  804. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  805. 'CONSTRAINT `author_id_idx` FOREIGN KEY (`author_id`) ' .
  806. 'REFERENCES `authors` (`id`) ON UPDATE NO ACTION ON DELETE RESTRICT'
  807. ],
  808. ];
  809. }
  810. /**
  811. * Test the constraintSql method.
  812. *
  813. * @dataProvider constraintSqlProvider
  814. */
  815. public function testConstraintSql($name, $data, $expected)
  816. {
  817. $driver = $this->_getMockedDriver();
  818. $schema = new MysqlSchema($driver);
  819. $table = (new TableSchema('articles'))->addColumn('title', [
  820. 'type' => 'string',
  821. 'length' => 255
  822. ])->addColumn('author_id', [
  823. 'type' => 'integer',
  824. ])->addConstraint($name, $data);
  825. $this->assertEquals($expected, $schema->constraintSql($table, $name));
  826. }
  827. /**
  828. * Test provider for indexSql()
  829. *
  830. * @return array
  831. */
  832. public static function indexSqlProvider()
  833. {
  834. return [
  835. [
  836. 'key_key',
  837. ['type' => 'index', 'columns' => ['author_id']],
  838. 'KEY `key_key` (`author_id`)'
  839. ],
  840. [
  841. 'full_text',
  842. ['type' => 'fulltext', 'columns' => ['title']],
  843. 'FULLTEXT KEY `full_text` (`title`)'
  844. ],
  845. ];
  846. }
  847. /**
  848. * Test the indexSql method.
  849. *
  850. * @dataProvider indexSqlProvider
  851. */
  852. public function testIndexSql($name, $data, $expected)
  853. {
  854. $driver = $this->_getMockedDriver();
  855. $schema = new MysqlSchema($driver);
  856. $table = (new TableSchema('articles'))->addColumn('title', [
  857. 'type' => 'string',
  858. 'length' => 255
  859. ])->addColumn('author_id', [
  860. 'type' => 'integer',
  861. ])->addIndex($name, $data);
  862. $this->assertEquals($expected, $schema->indexSql($table, $name));
  863. }
  864. /**
  865. * Test the addConstraintSql method.
  866. *
  867. * @return void
  868. */
  869. public function testAddConstraintSql()
  870. {
  871. $driver = $this->_getMockedDriver();
  872. $connection = $this->getMockBuilder('Cake\Database\Connection')
  873. ->disableOriginalConstructor()
  874. ->getMock();
  875. $connection->expects($this->any())->method('getDriver')
  876. ->will($this->returnValue($driver));
  877. $table = (new TableSchema('posts'))
  878. ->addColumn('author_id', [
  879. 'type' => 'integer',
  880. 'null' => false
  881. ])
  882. ->addColumn('category_id', [
  883. 'type' => 'integer',
  884. 'null' => false
  885. ])
  886. ->addColumn('category_name', [
  887. 'type' => 'integer',
  888. 'null' => false
  889. ])
  890. ->addConstraint('author_fk', [
  891. 'type' => 'foreign',
  892. 'columns' => ['author_id'],
  893. 'references' => ['authors', 'id'],
  894. 'update' => 'cascade',
  895. 'delete' => 'cascade'
  896. ])
  897. ->addConstraint('category_fk', [
  898. 'type' => 'foreign',
  899. 'columns' => ['category_id', 'category_name'],
  900. 'references' => ['categories', ['id', 'name']],
  901. 'update' => 'cascade',
  902. 'delete' => 'cascade'
  903. ]);
  904. $expected = [
  905. 'ALTER TABLE `posts` ADD CONSTRAINT `author_fk` FOREIGN KEY (`author_id`) REFERENCES `authors` (`id`) ON UPDATE CASCADE ON DELETE CASCADE;',
  906. 'ALTER TABLE `posts` ADD CONSTRAINT `category_fk` FOREIGN KEY (`category_id`, `category_name`) REFERENCES `categories` (`id`, `name`) ON UPDATE CASCADE ON DELETE CASCADE;'
  907. ];
  908. $result = $table->addConstraintSql($connection);
  909. $this->assertCount(2, $result);
  910. $this->assertEquals($expected, $result);
  911. }
  912. /**
  913. * Test the dropConstraintSql method.
  914. *
  915. * @return void
  916. */
  917. public function testDropConstraintSql()
  918. {
  919. $driver = $this->_getMockedDriver();
  920. $connection = $this->getMockBuilder('Cake\Database\Connection')
  921. ->disableOriginalConstructor()
  922. ->getMock();
  923. $connection->expects($this->any())->method('getDriver')
  924. ->will($this->returnValue($driver));
  925. $table = (new TableSchema('posts'))
  926. ->addColumn('author_id', [
  927. 'type' => 'integer',
  928. 'null' => false
  929. ])
  930. ->addColumn('category_id', [
  931. 'type' => 'integer',
  932. 'null' => false
  933. ])
  934. ->addColumn('category_name', [
  935. 'type' => 'integer',
  936. 'null' => false
  937. ])
  938. ->addConstraint('author_fk', [
  939. 'type' => 'foreign',
  940. 'columns' => ['author_id'],
  941. 'references' => ['authors', 'id'],
  942. 'update' => 'cascade',
  943. 'delete' => 'cascade'
  944. ])
  945. ->addConstraint('category_fk', [
  946. 'type' => 'foreign',
  947. 'columns' => ['category_id', 'category_name'],
  948. 'references' => ['categories', ['id', 'name']],
  949. 'update' => 'cascade',
  950. 'delete' => 'cascade'
  951. ]);
  952. $expected = [
  953. 'ALTER TABLE `posts` DROP FOREIGN KEY `author_fk`;',
  954. 'ALTER TABLE `posts` DROP FOREIGN KEY `category_fk`;'
  955. ];
  956. $result = $table->dropConstraintSql($connection);
  957. $this->assertCount(2, $result);
  958. $this->assertEquals($expected, $result);
  959. }
  960. /**
  961. * Test generating a column that is a primary key.
  962. *
  963. * @return void
  964. */
  965. public function testColumnSqlPrimaryKey()
  966. {
  967. $driver = $this->_getMockedDriver();
  968. $schema = new MysqlSchema($driver);
  969. $table = new TableSchema('articles');
  970. $table->addColumn('id', [
  971. 'type' => 'integer',
  972. 'null' => false,
  973. ])
  974. ->addConstraint('primary', [
  975. 'type' => 'primary',
  976. 'columns' => ['id']
  977. ]);
  978. $result = $schema->columnSql($table, 'id');
  979. $this->assertEquals($result, '`id` INTEGER NOT NULL AUTO_INCREMENT');
  980. $table = new TableSchema('articles');
  981. $table->addColumn('id', [
  982. 'type' => 'biginteger',
  983. 'null' => false
  984. ])
  985. ->addConstraint('primary', [
  986. 'type' => 'primary',
  987. 'columns' => ['id']
  988. ]);
  989. $result = $schema->columnSql($table, 'id');
  990. $this->assertEquals($result, '`id` BIGINT NOT NULL AUTO_INCREMENT');
  991. }
  992. /**
  993. * Integration test for converting a Schema\Table into MySQL table creates.
  994. *
  995. * @return void
  996. */
  997. public function testCreateSql()
  998. {
  999. $driver = $this->_getMockedDriver();
  1000. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1001. ->disableOriginalConstructor()
  1002. ->getMock();
  1003. $connection->expects($this->any())->method('getDriver')
  1004. ->will($this->returnValue($driver));
  1005. $driver->getConnection()
  1006. ->expects($this->any())
  1007. ->method('getAttribute')
  1008. ->will($this->returnValue('5.6.0'));
  1009. $table = (new TableSchema('posts'))->addColumn('id', [
  1010. 'type' => 'integer',
  1011. 'null' => false
  1012. ])
  1013. ->addColumn('title', [
  1014. 'type' => 'string',
  1015. 'null' => false,
  1016. 'comment' => 'The title'
  1017. ])
  1018. ->addColumn('body', [
  1019. 'type' => 'text',
  1020. 'comment' => ''
  1021. ])
  1022. ->addColumn('data', [
  1023. 'type' => 'json'
  1024. ])
  1025. ->addColumn('hash', [
  1026. 'type' => 'string',
  1027. 'fixed' => true,
  1028. 'length' => 40,
  1029. 'collate' => 'latin1_bin',
  1030. 'null' => false,
  1031. ])
  1032. ->addColumn('created', 'datetime')
  1033. ->addConstraint('primary', [
  1034. 'type' => 'primary',
  1035. 'columns' => ['id']
  1036. ])
  1037. ->setOptions([
  1038. 'engine' => 'InnoDB',
  1039. 'charset' => 'utf8',
  1040. 'collate' => 'utf8_general_ci',
  1041. ]);
  1042. $expected = <<<SQL
  1043. CREATE TABLE `posts` (
  1044. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1045. `title` VARCHAR(255) NOT NULL COMMENT 'The title',
  1046. `body` TEXT,
  1047. `data` LONGTEXT,
  1048. `hash` CHAR(40) COLLATE latin1_bin NOT NULL,
  1049. `created` DATETIME,
  1050. PRIMARY KEY (`id`)
  1051. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
  1052. SQL;
  1053. $result = $table->createSql($connection);
  1054. $this->assertCount(1, $result);
  1055. $this->assertTextEquals($expected, $result[0]);
  1056. }
  1057. /**
  1058. * Integration test for converting a Schema\Table with native JSON
  1059. *
  1060. * @return void
  1061. */
  1062. public function testCreateSqlJson()
  1063. {
  1064. $driver = $this->_getMockedDriver();
  1065. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1066. ->disableOriginalConstructor()
  1067. ->getMock();
  1068. $connection->expects($this->any())
  1069. ->method('getDriver')
  1070. ->will($this->returnValue($driver));
  1071. $driver->getConnection()
  1072. ->expects($this->any())
  1073. ->method('getAttribute')
  1074. ->will($this->returnValue('5.7.0'));
  1075. $table = (new TableSchema('posts'))->addColumn('id', [
  1076. 'type' => 'integer',
  1077. 'null' => false
  1078. ])
  1079. ->addColumn('data', [
  1080. 'type' => 'json'
  1081. ])
  1082. ->addConstraint('primary', [
  1083. 'type' => 'primary',
  1084. 'columns' => ['id']
  1085. ])
  1086. ->setOptions([
  1087. 'engine' => 'InnoDB',
  1088. 'charset' => 'utf8',
  1089. 'collate' => 'utf8_general_ci',
  1090. ]);
  1091. $expected = <<<SQL
  1092. CREATE TABLE `posts` (
  1093. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1094. `data` JSON,
  1095. PRIMARY KEY (`id`)
  1096. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci
  1097. SQL;
  1098. $result = $table->createSql($connection);
  1099. $this->assertCount(1, $result);
  1100. $this->assertTextEquals($expected, $result[0]);
  1101. }
  1102. /**
  1103. * Tests creating temporary tables
  1104. *
  1105. * @return void
  1106. */
  1107. public function testCreateTemporary()
  1108. {
  1109. $driver = $this->_getMockedDriver();
  1110. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1111. ->disableOriginalConstructor()
  1112. ->getMock();
  1113. $connection->expects($this->any())->method('getDriver')
  1114. ->will($this->returnValue($driver));
  1115. $table = (new TableSchema('schema_articles'))->addColumn('id', [
  1116. 'type' => 'integer',
  1117. 'null' => false
  1118. ]);
  1119. $table->setTemporary(true);
  1120. $sql = $table->createSql($connection);
  1121. $this->assertContains('CREATE TEMPORARY TABLE', $sql[0]);
  1122. }
  1123. /**
  1124. * Test primary key generation & auto-increment.
  1125. *
  1126. * @return void
  1127. */
  1128. public function testCreateSqlCompositeIntegerKey()
  1129. {
  1130. $driver = $this->_getMockedDriver();
  1131. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1132. ->disableOriginalConstructor()
  1133. ->getMock();
  1134. $connection->expects($this->any())->method('getDriver')
  1135. ->will($this->returnValue($driver));
  1136. $table = (new TableSchema('articles_tags'))
  1137. ->addColumn('article_id', [
  1138. 'type' => 'integer',
  1139. 'null' => false
  1140. ])
  1141. ->addColumn('tag_id', [
  1142. 'type' => 'integer',
  1143. 'null' => false,
  1144. ])
  1145. ->addConstraint('primary', [
  1146. 'type' => 'primary',
  1147. 'columns' => ['article_id', 'tag_id']
  1148. ]);
  1149. $expected = <<<SQL
  1150. CREATE TABLE `articles_tags` (
  1151. `article_id` INTEGER NOT NULL,
  1152. `tag_id` INTEGER NOT NULL,
  1153. PRIMARY KEY (`article_id`, `tag_id`)
  1154. )
  1155. SQL;
  1156. $result = $table->createSql($connection);
  1157. $this->assertCount(1, $result);
  1158. $this->assertTextEquals($expected, $result[0]);
  1159. $table = (new TableSchema('composite_key'))
  1160. ->addColumn('id', [
  1161. 'type' => 'integer',
  1162. 'null' => false,
  1163. 'autoIncrement' => true
  1164. ])
  1165. ->addColumn('account_id', [
  1166. 'type' => 'integer',
  1167. 'null' => false,
  1168. ])
  1169. ->addConstraint('primary', [
  1170. 'type' => 'primary',
  1171. 'columns' => ['id', 'account_id']
  1172. ]);
  1173. $expected = <<<SQL
  1174. CREATE TABLE `composite_key` (
  1175. `id` INTEGER NOT NULL AUTO_INCREMENT,
  1176. `account_id` INTEGER NOT NULL,
  1177. PRIMARY KEY (`id`, `account_id`)
  1178. )
  1179. SQL;
  1180. $result = $table->createSql($connection);
  1181. $this->assertCount(1, $result);
  1182. $this->assertTextEquals($expected, $result[0]);
  1183. }
  1184. /**
  1185. * test dropSql
  1186. *
  1187. * @return void
  1188. */
  1189. public function testDropSql()
  1190. {
  1191. $driver = $this->_getMockedDriver();
  1192. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1193. ->disableOriginalConstructor()
  1194. ->getMock();
  1195. $connection->expects($this->any())->method('getDriver')
  1196. ->will($this->returnValue($driver));
  1197. $table = new TableSchema('articles');
  1198. $result = $table->dropSql($connection);
  1199. $this->assertCount(1, $result);
  1200. $this->assertEquals('DROP TABLE `articles`', $result[0]);
  1201. }
  1202. /**
  1203. * Test truncateSql()
  1204. *
  1205. * @return void
  1206. */
  1207. public function testTruncateSql()
  1208. {
  1209. $driver = $this->_getMockedDriver();
  1210. $connection = $this->getMockBuilder('Cake\Database\Connection')
  1211. ->disableOriginalConstructor()
  1212. ->getMock();
  1213. $connection->expects($this->any())->method('getDriver')
  1214. ->will($this->returnValue($driver));
  1215. $table = new TableSchema('articles');
  1216. $result = $table->truncateSql($connection);
  1217. $this->assertCount(1, $result);
  1218. $this->assertEquals('TRUNCATE TABLE `articles`', $result[0]);
  1219. }
  1220. /**
  1221. * Test that constructing a schema dialect connects the driver.
  1222. *
  1223. * @return void
  1224. */
  1225. public function testConstructConnectsDriver()
  1226. {
  1227. $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock();
  1228. $driver->expects($this->once())
  1229. ->method('connect');
  1230. $schema = new MysqlSchema($driver);
  1231. }
  1232. /**
  1233. * Tests json column parsing on Mysql 5.7+
  1234. *
  1235. * @return void
  1236. */
  1237. public function testDescribeJson()
  1238. {
  1239. $connection = ConnectionManager::get('test');
  1240. $this->_createTables($connection);
  1241. $this->skipIf(!$connection->getDriver()->supportsNativeJson(), 'Does not support native json');
  1242. $schema = new SchemaCollection($connection);
  1243. $result = $schema->describe('schema_json');
  1244. $this->assertInstanceOf('Cake\Database\Schema\TableSchema', $result);
  1245. $expected = [
  1246. 'type' => 'json',
  1247. 'null' => false,
  1248. 'default' => null,
  1249. 'length' => null,
  1250. 'precision' => null,
  1251. 'comment' => null,
  1252. ];
  1253. $this->assertEquals(
  1254. $expected,
  1255. $result->getColumn('data'),
  1256. 'Field definition does not match for data'
  1257. );
  1258. }
  1259. /**
  1260. * Get a schema instance with a mocked driver/pdo instances
  1261. *
  1262. * @return MysqlSchema
  1263. */
  1264. protected function _getMockedDriver()
  1265. {
  1266. $driver = new Mysql();
  1267. $mock = $this->getMockBuilder(PDO::class)
  1268. ->setMethods(['quote', 'quoteIdentifier', 'getAttribute'])
  1269. ->disableOriginalConstructor()
  1270. ->getMock();
  1271. $mock->expects($this->any())
  1272. ->method('quote')
  1273. ->will($this->returnCallback(function ($value) {
  1274. return "'$value'";
  1275. }));
  1276. $driver->setConnection($mock);
  1277. return $driver;
  1278. }
  1279. }