MysqlSchemaTest.php 43 KB

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