PostgresSchemaTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since CakePHP(tm) v 3.0.0
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Test\TestCase\Database\Schema;
  16. use Cake\Core\Configure;
  17. use Cake\Database\Schema\Collection as SchemaCollection;
  18. use Cake\Database\Schema\PostgresSchema;
  19. use Cake\Database\Schema\Table;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. /**
  23. * Postgres schema test case.
  24. */
  25. class PostgresSchemaTest extends TestCase {
  26. /**
  27. * Helper method for skipping tests that need a real connection.
  28. *
  29. * @return void
  30. */
  31. protected function _needsConnection() {
  32. $config = ConnectionManager::config('test');
  33. $this->skipIf(strpos($config['className'], 'Postgres') === false, 'Not using Postgres for test config');
  34. }
  35. /**
  36. * Helper method for testing methods.
  37. *
  38. * @return void
  39. */
  40. protected function _createTables($connection) {
  41. $this->_needsConnection();
  42. $connection->execute('DROP TABLE IF EXISTS schema_articles');
  43. $connection->execute('DROP TABLE IF EXISTS schema_authors');
  44. $table = <<<SQL
  45. CREATE TABLE schema_authors (
  46. id SERIAL,
  47. name VARCHAR(50),
  48. bio DATE,
  49. created TIMESTAMP,
  50. PRIMARY KEY (id)
  51. )
  52. SQL;
  53. $connection->execute($table);
  54. $table = <<<SQL
  55. CREATE TABLE schema_articles (
  56. id BIGINT PRIMARY KEY,
  57. title VARCHAR(20),
  58. body TEXT,
  59. author_id INTEGER NOT NULL,
  60. published BOOLEAN DEFAULT false,
  61. views SMALLINT DEFAULT 0,
  62. created TIMESTAMP,
  63. CONSTRAINT "content_idx" UNIQUE ("title", "body"),
  64. CONSTRAINT "author_idx" FOREIGN KEY ("author_id") REFERENCES "schema_authors" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
  65. )
  66. SQL;
  67. $connection->execute($table);
  68. $connection->execute('COMMENT ON COLUMN "schema_articles"."title" IS \'a title\'');
  69. $connection->execute('CREATE INDEX "author_idx" ON "schema_articles" ("author_id")');
  70. }
  71. /**
  72. * Data provider for convert column testing
  73. *
  74. * @return array
  75. */
  76. public static function convertColumnProvider() {
  77. return [
  78. // Timestamp
  79. [
  80. 'TIMESTAMP',
  81. ['type' => 'datetime', 'length' => null]
  82. ],
  83. [
  84. 'TIMESTAMP WITHOUT TIME ZONE',
  85. ['type' => 'datetime', 'length' => null]
  86. ],
  87. // Date & time
  88. [
  89. 'DATE',
  90. ['type' => 'date', 'length' => null]
  91. ],
  92. [
  93. 'TIME',
  94. ['type' => 'time', 'length' => null]
  95. ],
  96. // Integer
  97. [
  98. 'SMALLINT',
  99. ['type' => 'integer', 'length' => 5]
  100. ],
  101. [
  102. 'INTEGER',
  103. ['type' => 'integer', 'length' => 10]
  104. ],
  105. [
  106. 'SERIAL',
  107. ['type' => 'integer', 'length' => 10]
  108. ],
  109. [
  110. 'BIGINT',
  111. ['type' => 'biginteger', 'length' => 20]
  112. ],
  113. [
  114. 'BIGSERIAL',
  115. ['type' => 'biginteger', 'length' => 20]
  116. ],
  117. // Decimal
  118. [
  119. 'NUMERIC',
  120. ['type' => 'decimal', 'length' => null]
  121. ],
  122. [
  123. 'DECIMAL(10,2)',
  124. ['type' => 'decimal', 'length' => null]
  125. ],
  126. [
  127. 'MONEY',
  128. ['type' => 'decimal', 'length' => null]
  129. ],
  130. // String
  131. [
  132. 'VARCHAR',
  133. ['type' => 'string', 'length' => null]
  134. ],
  135. [
  136. 'VARCHAR(10)',
  137. ['type' => 'string', 'length' => 10]
  138. ],
  139. [
  140. 'CHARACTER VARYING',
  141. ['type' => 'string', 'length' => null]
  142. ],
  143. [
  144. 'CHARACTER VARYING(10)',
  145. ['type' => 'string', 'length' => 10]
  146. ],
  147. [
  148. 'CHAR(10)',
  149. ['type' => 'string', 'fixed' => true, 'length' => 10]
  150. ],
  151. [
  152. 'CHARACTER(10)',
  153. ['type' => 'string', 'fixed' => true, 'length' => 10]
  154. ],
  155. // UUID
  156. [
  157. 'UUID',
  158. ['type' => 'uuid', 'length' => null]
  159. ],
  160. [
  161. 'INET',
  162. ['type' => 'string', 'length' => 39]
  163. ],
  164. // Text
  165. [
  166. 'TEXT',
  167. ['type' => 'text', 'length' => null]
  168. ],
  169. // Blob
  170. [
  171. 'BYTEA',
  172. ['type' => 'binary', 'length' => null]
  173. ],
  174. // Float
  175. [
  176. 'REAL',
  177. ['type' => 'float', 'length' => null]
  178. ],
  179. [
  180. 'DOUBLE PRECISION',
  181. ['type' => 'float', 'length' => null]
  182. ],
  183. ];
  184. }
  185. /**
  186. * Test parsing Postgres column types from field description.
  187. *
  188. * @dataProvider convertColumnProvider
  189. * @return void
  190. */
  191. public function testConvertColumn($type, $expected) {
  192. $field = [
  193. 'name' => 'field',
  194. 'type' => $type,
  195. 'null' => 'YES',
  196. 'default' => 'Default value',
  197. 'comment' => 'Comment section',
  198. 'char_length' => null,
  199. ];
  200. $expected += [
  201. 'null' => true,
  202. 'default' => 'Default value',
  203. 'comment' => 'Comment section',
  204. ];
  205. $driver = $this->getMock('Cake\Database\Driver\Postgres');
  206. $dialect = new PostgresSchema($driver);
  207. $table = $this->getMock('Cake\Database\Schema\Table', [], ['table']);
  208. $table->expects($this->at(0))->method('addColumn')->with('field', $expected);
  209. $dialect->convertFieldDescription($table, $field);
  210. }
  211. /**
  212. * Test listing tables with Postgres
  213. *
  214. * @return void
  215. */
  216. public function testListTables() {
  217. $connection = ConnectionManager::get('test');
  218. $this->_createTables($connection);
  219. $schema = new SchemaCollection($connection);
  220. $result = $schema->listTables();
  221. $this->assertInternalType('array', $result);
  222. $this->assertCount(2, $result);
  223. $this->assertEquals('schema_articles', $result[0]);
  224. $this->assertEquals('schema_authors', $result[1]);
  225. }
  226. /**
  227. * Test describing a table with Postgres
  228. *
  229. * @return void
  230. */
  231. public function testDescribeTable() {
  232. $connection = ConnectionManager::get('test');
  233. $this->_createTables($connection);
  234. $schema = new SchemaCollection($connection);
  235. $result = $schema->describe('schema_articles');
  236. $expected = [
  237. 'id' => [
  238. 'type' => 'biginteger',
  239. 'null' => false,
  240. 'default' => null,
  241. 'length' => 20,
  242. 'precision' => null,
  243. 'unsigned' => null,
  244. 'comment' => null,
  245. 'autoIncrement' => true,
  246. ],
  247. 'title' => [
  248. 'type' => 'string',
  249. 'null' => true,
  250. 'default' => null,
  251. 'length' => 20,
  252. 'precision' => null,
  253. 'comment' => 'a title',
  254. 'fixed' => null,
  255. ],
  256. 'body' => [
  257. 'type' => 'text',
  258. 'null' => true,
  259. 'default' => null,
  260. 'length' => null,
  261. 'precision' => null,
  262. 'comment' => null,
  263. ],
  264. 'author_id' => [
  265. 'type' => 'integer',
  266. 'null' => false,
  267. 'default' => null,
  268. 'length' => 10,
  269. 'precision' => null,
  270. 'unsigned' => null,
  271. 'comment' => null,
  272. 'autoIncrement' => null,
  273. ],
  274. 'published' => [
  275. 'type' => 'boolean',
  276. 'null' => true,
  277. 'default' => 0,
  278. 'length' => null,
  279. 'precision' => null,
  280. 'comment' => null,
  281. ],
  282. 'views' => [
  283. 'type' => 'integer',
  284. 'null' => true,
  285. 'default' => 0,
  286. 'length' => 5,
  287. 'precision' => null,
  288. 'unsigned' => null,
  289. 'comment' => null,
  290. 'autoIncrement' => null,
  291. ],
  292. 'created' => [
  293. 'type' => 'datetime',
  294. 'null' => true,
  295. 'default' => null,
  296. 'length' => null,
  297. 'precision' => null,
  298. 'comment' => null,
  299. ],
  300. ];
  301. $this->assertEquals(['id'], $result->primaryKey());
  302. foreach ($expected as $field => $definition) {
  303. $this->assertEquals($definition, $result->column($field));
  304. }
  305. }
  306. /**
  307. * Test describing a table with indexes
  308. *
  309. * @return void
  310. */
  311. public function testDescribeTableIndexes() {
  312. $connection = ConnectionManager::get('test');
  313. $this->_createTables($connection);
  314. $schema = new SchemaCollection($connection);
  315. $result = $schema->describe('schema_articles');
  316. $this->assertInstanceOf('Cake\Database\Schema\Table', $result);
  317. $expected = [
  318. 'primary' => [
  319. 'type' => 'primary',
  320. 'columns' => ['id'],
  321. 'length' => []
  322. ],
  323. 'content_idx' => [
  324. 'type' => 'unique',
  325. 'columns' => ['title', 'body'],
  326. 'length' => []
  327. ]
  328. ];
  329. $this->assertCount(3, $result->constraints());
  330. $expected = [
  331. 'primary' => [
  332. 'type' => 'primary',
  333. 'columns' => ['id'],
  334. 'length' => []
  335. ],
  336. 'content_idx' => [
  337. 'type' => 'unique',
  338. 'columns' => ['title', 'body'],
  339. 'length' => []
  340. ],
  341. 'author_idx' => [
  342. 'type' => 'foreign',
  343. 'columns' => ['author_id'],
  344. 'references' => ['schema_authors', 'id'],
  345. 'length' => [],
  346. 'update' => 'cascade',
  347. 'delete' => 'restrict',
  348. ]
  349. ];
  350. $this->assertEquals($expected['primary'], $result->constraint('primary'));
  351. $this->assertEquals($expected['content_idx'], $result->constraint('content_idx'));
  352. $this->assertEquals($expected['author_idx'], $result->constraint('author_idx'));
  353. $this->assertCount(1, $result->indexes());
  354. $expected = [
  355. 'type' => 'index',
  356. 'columns' => ['author_id'],
  357. 'length' => []
  358. ];
  359. $this->assertEquals($expected, $result->index('author_idx'));
  360. }
  361. /**
  362. * Column provider for creating column sql
  363. *
  364. * @return array
  365. */
  366. public static function columnSqlProvider() {
  367. return [
  368. // strings
  369. [
  370. 'title',
  371. ['type' => 'string', 'length' => 25, 'null' => false],
  372. '"title" VARCHAR(25) NOT NULL'
  373. ],
  374. [
  375. 'title',
  376. ['type' => 'string', 'length' => 25, 'null' => true, 'default' => 'ignored'],
  377. '"title" VARCHAR(25) DEFAULT NULL'
  378. ],
  379. [
  380. 'id',
  381. ['type' => 'string', 'length' => 32, 'fixed' => true, 'null' => false],
  382. '"id" CHAR(32) NOT NULL'
  383. ],
  384. [
  385. 'id',
  386. ['type' => 'uuid', 'length' => 36, 'null' => false],
  387. '"id" UUID NOT NULL'
  388. ],
  389. [
  390. 'role',
  391. ['type' => 'string', 'length' => 10, 'null' => false, 'default' => 'admin'],
  392. '"role" VARCHAR(10) NOT NULL DEFAULT "admin"'
  393. ],
  394. [
  395. 'title',
  396. ['type' => 'string'],
  397. '"title" VARCHAR'
  398. ],
  399. // Text
  400. [
  401. 'body',
  402. ['type' => 'text', 'null' => false],
  403. '"body" TEXT NOT NULL'
  404. ],
  405. // Integers
  406. [
  407. 'post_id',
  408. ['type' => 'integer', 'length' => 11],
  409. '"post_id" INTEGER'
  410. ],
  411. [
  412. 'post_id',
  413. ['type' => 'biginteger', 'length' => 20],
  414. '"post_id" BIGINT'
  415. ],
  416. [
  417. 'post_id',
  418. ['type' => 'integer', 'autoIncrement' => true, 'length' => 11],
  419. '"post_id" SERIAL'
  420. ],
  421. [
  422. 'post_id',
  423. ['type' => 'biginteger', 'autoIncrement' => true, 'length' => 20],
  424. '"post_id" BIGSERIAL'
  425. ],
  426. // Decimal
  427. [
  428. 'value',
  429. ['type' => 'decimal'],
  430. '"value" DECIMAL'
  431. ],
  432. [
  433. 'value',
  434. ['type' => 'decimal', 'length' => 11],
  435. '"value" DECIMAL(11,0)'
  436. ],
  437. [
  438. 'value',
  439. ['type' => 'decimal', 'length' => 12, 'precision' => 5],
  440. '"value" DECIMAL(12,5)'
  441. ],
  442. // Float
  443. [
  444. 'value',
  445. ['type' => 'float'],
  446. '"value" FLOAT'
  447. ],
  448. [
  449. 'value',
  450. ['type' => 'float', 'length' => 11, 'precision' => 3],
  451. '"value" FLOAT(3)'
  452. ],
  453. // Binary
  454. [
  455. 'img',
  456. ['type' => 'binary'],
  457. '"img" BYTEA'
  458. ],
  459. // Boolean
  460. [
  461. 'checked',
  462. ['type' => 'boolean', 'default' => false],
  463. '"checked" BOOLEAN DEFAULT FALSE'
  464. ],
  465. [
  466. 'checked',
  467. ['type' => 'boolean', 'default' => true, 'null' => false],
  468. '"checked" BOOLEAN NOT NULL DEFAULT TRUE'
  469. ],
  470. // datetimes
  471. [
  472. 'created',
  473. ['type' => 'datetime'],
  474. '"created" TIMESTAMP'
  475. ],
  476. // Date & Time
  477. [
  478. 'start_date',
  479. ['type' => 'date'],
  480. '"start_date" DATE'
  481. ],
  482. [
  483. 'start_time',
  484. ['type' => 'time'],
  485. '"start_time" TIME'
  486. ],
  487. // timestamps
  488. [
  489. 'created',
  490. ['type' => 'timestamp', 'null' => true],
  491. '"created" TIMESTAMP DEFAULT NULL'
  492. ],
  493. ];
  494. }
  495. /**
  496. * Test generating column definitions
  497. *
  498. * @dataProvider columnSqlProvider
  499. * @return void
  500. */
  501. public function testColumnSql($name, $data, $expected) {
  502. $driver = $this->_getMockedDriver();
  503. $schema = new PostgresSchema($driver);
  504. $table = (new Table('schema_articles'))->addColumn($name, $data);
  505. $this->assertEquals($expected, $schema->columnSql($table, $name));
  506. }
  507. /**
  508. * Test generating a column that is a primary key.
  509. *
  510. * @return void
  511. */
  512. public function testColumnSqlPrimaryKey() {
  513. $driver = $this->_getMockedDriver();
  514. $schema = new PostgresSchema($driver);
  515. $table = new Table('schema_articles');
  516. $table->addColumn('id', [
  517. 'type' => 'integer',
  518. 'null' => false
  519. ])
  520. ->addConstraint('primary', [
  521. 'type' => 'primary',
  522. 'columns' => ['id']
  523. ]);
  524. $result = $schema->columnSql($table, 'id');
  525. $this->assertEquals($result, '"id" SERIAL');
  526. }
  527. /**
  528. * Provide data for testing constraintSql
  529. *
  530. * @return array
  531. */
  532. public static function constraintSqlProvider() {
  533. return [
  534. [
  535. 'primary',
  536. ['type' => 'primary', 'columns' => ['title']],
  537. 'PRIMARY KEY ("title")'
  538. ],
  539. [
  540. 'unique_idx',
  541. ['type' => 'unique', 'columns' => ['title', 'author_id']],
  542. 'CONSTRAINT "unique_idx" UNIQUE ("title", "author_id")'
  543. ],
  544. [
  545. 'author_id_idx',
  546. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
  547. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  548. 'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT'
  549. ],
  550. [
  551. 'author_id_idx',
  552. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
  553. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  554. 'REFERENCES "authors" ("id") ON UPDATE CASCADE ON DELETE RESTRICT'
  555. ],
  556. [
  557. 'author_id_idx',
  558. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
  559. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  560. 'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT'
  561. ],
  562. [
  563. 'author_id_idx',
  564. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
  565. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  566. 'REFERENCES "authors" ("id") ON UPDATE SET NULL ON DELETE RESTRICT'
  567. ],
  568. [
  569. 'author_id_idx',
  570. ['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
  571. 'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
  572. 'REFERENCES "authors" ("id") ON UPDATE NO ACTION ON DELETE RESTRICT'
  573. ],
  574. ];
  575. }
  576. /**
  577. * Test the constraintSql method.
  578. *
  579. * @dataProvider constraintSqlProvider
  580. */
  581. public function testConstraintSql($name, $data, $expected) {
  582. $driver = $this->_getMockedDriver();
  583. $schema = new PostgresSchema($driver);
  584. $table = (new Table('schema_articles'))->addColumn('title', [
  585. 'type' => 'string',
  586. 'length' => 255
  587. ])->addColumn('author_id', [
  588. 'type' => 'integer',
  589. ])->addConstraint($name, $data);
  590. $this->assertEquals($expected, $schema->constraintSql($table, $name));
  591. }
  592. /**
  593. * Integration test for converting a Schema\Table into MySQL table creates.
  594. *
  595. * @return void
  596. */
  597. public function testCreateSql() {
  598. $driver = $this->_getMockedDriver();
  599. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  600. $connection->expects($this->any())->method('driver')
  601. ->will($this->returnValue($driver));
  602. $table = (new Table('schema_articles'))->addColumn('id', [
  603. 'type' => 'integer',
  604. 'null' => false
  605. ])
  606. ->addColumn('title', [
  607. 'type' => 'string',
  608. 'null' => false,
  609. 'comment' => 'This is the title',
  610. ])
  611. ->addColumn('body', ['type' => 'text'])
  612. ->addColumn('created', 'datetime')
  613. ->addConstraint('primary', [
  614. 'type' => 'primary',
  615. 'columns' => ['id'],
  616. ])
  617. ->addIndex('title_idx', [
  618. 'type' => 'index',
  619. 'columns' => ['title'],
  620. ]);
  621. $expected = <<<SQL
  622. CREATE TABLE "schema_articles" (
  623. "id" SERIAL,
  624. "title" VARCHAR NOT NULL,
  625. "body" TEXT,
  626. "created" TIMESTAMP,
  627. PRIMARY KEY ("id")
  628. )
  629. SQL;
  630. $result = $table->createSql($connection);
  631. $this->assertCount(3, $result);
  632. $this->assertEquals($expected, $result[0]);
  633. $this->assertEquals(
  634. 'CREATE INDEX "title_idx" ON "schema_articles" ("title")',
  635. $result[1]
  636. );
  637. $this->assertEquals(
  638. 'COMMENT ON COLUMN "schema_articles"."title" IS "This is the title"',
  639. $result[2]
  640. );
  641. }
  642. /**
  643. * Test primary key generation & auto-increment.
  644. *
  645. * @return void
  646. */
  647. public function testCreateSqlCompositeIntegerKey() {
  648. $driver = $this->_getMockedDriver();
  649. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  650. $connection->expects($this->any())->method('driver')
  651. ->will($this->returnValue($driver));
  652. $table = (new Table('articles_tags'))
  653. ->addColumn('article_id', [
  654. 'type' => 'integer',
  655. 'null' => false
  656. ])
  657. ->addColumn('tag_id', [
  658. 'type' => 'integer',
  659. 'null' => false,
  660. ])
  661. ->addConstraint('primary', [
  662. 'type' => 'primary',
  663. 'columns' => ['article_id', 'tag_id']
  664. ]);
  665. $expected = <<<SQL
  666. CREATE TABLE "articles_tags" (
  667. "article_id" INTEGER NOT NULL,
  668. "tag_id" INTEGER NOT NULL,
  669. PRIMARY KEY ("article_id", "tag_id")
  670. )
  671. SQL;
  672. $result = $table->createSql($connection);
  673. $this->assertCount(1, $result);
  674. $this->assertEquals($expected, $result[0]);
  675. $table = (new Table('composite_key'))
  676. ->addColumn('id', [
  677. 'type' => 'integer',
  678. 'null' => false,
  679. 'autoIncrement' => true
  680. ])
  681. ->addColumn('account_id', [
  682. 'type' => 'integer',
  683. 'null' => false,
  684. ])
  685. ->addConstraint('primary', [
  686. 'type' => 'primary',
  687. 'columns' => ['id', 'account_id']
  688. ]);
  689. $expected = <<<SQL
  690. CREATE TABLE "composite_key" (
  691. "id" SERIAL,
  692. "account_id" INTEGER NOT NULL,
  693. PRIMARY KEY ("id", "account_id")
  694. )
  695. SQL;
  696. $result = $table->createSql($connection);
  697. $this->assertCount(1, $result);
  698. $this->assertEquals($expected, $result[0]);
  699. }
  700. /**
  701. * test dropSql
  702. *
  703. * @return void
  704. */
  705. public function testDropSql() {
  706. $driver = $this->_getMockedDriver();
  707. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  708. $connection->expects($this->any())->method('driver')
  709. ->will($this->returnValue($driver));
  710. $table = new Table('schema_articles');
  711. $result = $table->dropSql($connection);
  712. $this->assertCount(1, $result);
  713. $this->assertEquals('DROP TABLE "schema_articles"', $result[0]);
  714. }
  715. /**
  716. * Test truncateSql()
  717. *
  718. * @return void
  719. */
  720. public function testTruncateSql() {
  721. $driver = $this->_getMockedDriver();
  722. $connection = $this->getMock('Cake\Database\Connection', [], [], '', false);
  723. $connection->expects($this->any())->method('driver')
  724. ->will($this->returnValue($driver));
  725. $table = new Table('schema_articles');
  726. $table->addColumn('id', 'integer')
  727. ->addConstraint('primary', [
  728. 'type' => 'primary',
  729. 'columns' => ['id']
  730. ]);
  731. $result = $table->truncateSql($connection);
  732. $this->assertCount(1, $result);
  733. $this->assertEquals('TRUNCATE "schema_articles" RESTART IDENTITY', $result[0]);
  734. }
  735. /**
  736. * Get a schema instance with a mocked driver/pdo instances
  737. *
  738. * @return Driver
  739. */
  740. protected function _getMockedDriver() {
  741. $driver = new \Cake\Database\Driver\Postgres();
  742. $mock = $this->getMock('FakePdo', ['quote', 'quoteIdentifier']);
  743. $mock->expects($this->any())
  744. ->method('quote')
  745. ->will($this->returnCallback(function ($value) {
  746. return '"' . $value . '"';
  747. }));
  748. $mock->expects($this->any())
  749. ->method('quoteIdentifier')
  750. ->will($this->returnCallback(function ($value) {
  751. return '"' . $value . '"';
  752. }));
  753. $driver->connection($mock);
  754. return $driver;
  755. }
  756. }