MysqlSchemaTest.php 43 KB

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