MysqlSchemaTest.php 43 KB

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