MysqlSchemaTest.php 43 KB

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