SqliteSchemaTest.php 44 KB

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