PostgresSchemaTest.php 20 KB

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