PostgresTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\Driver;
  17. use Cake\Database\Driver;
  18. use Cake\Database\Driver\Postgres;
  19. use Cake\Database\Query;
  20. use Cake\Datasource\ConnectionManager;
  21. use Cake\TestSuite\TestCase;
  22. use PDO;
  23. /**
  24. * Tests Postgres driver
  25. */
  26. class PostgresTest extends TestCase
  27. {
  28. /**
  29. * Test connecting to Postgres with default configuration
  30. */
  31. public function testConnectionConfigDefault(): void
  32. {
  33. $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
  34. ->onlyMethods(['createPdo'])
  35. ->getMock();
  36. $dsn = 'pgsql:host=localhost;port=5432;dbname=cake';
  37. $expected = [
  38. 'persistent' => true,
  39. 'host' => 'localhost',
  40. 'username' => 'root',
  41. 'password' => '',
  42. 'database' => 'cake',
  43. 'schema' => 'public',
  44. 'port' => 5432,
  45. 'encoding' => 'utf8',
  46. 'timezone' => null,
  47. 'flags' => [],
  48. 'init' => [],
  49. 'log' => false,
  50. ];
  51. $expected['flags'] += [
  52. PDO::ATTR_PERSISTENT => true,
  53. PDO::ATTR_EMULATE_PREPARES => false,
  54. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  55. ];
  56. $connection = $this->getMockBuilder('PDO')
  57. ->disableOriginalConstructor()
  58. ->onlyMethods(['exec', 'quote'])
  59. ->getMock();
  60. $connection->expects($this->any())
  61. ->method('quote')
  62. ->will($this->onConsecutiveCalls(
  63. $this->returnArgument(0),
  64. $this->returnArgument(0),
  65. $this->returnArgument(0)
  66. ));
  67. $connection->expects($this->exactly(2))
  68. ->method('exec')
  69. ->withConsecutive(
  70. ['SET NAMES utf8'],
  71. ['SET search_path TO public']
  72. );
  73. $driver->expects($this->once())->method('createPdo')
  74. ->with($dsn, $expected)
  75. ->will($this->returnValue($connection));
  76. $driver->connect();
  77. }
  78. /**
  79. * Test connecting to Postgres with custom configuration
  80. */
  81. public function testConnectionConfigCustom(): void
  82. {
  83. $config = [
  84. 'persistent' => false,
  85. 'host' => 'foo',
  86. 'database' => 'bar',
  87. 'username' => 'user',
  88. 'password' => 'pass',
  89. 'port' => 3440,
  90. 'flags' => [1 => true, 2 => false],
  91. 'encoding' => 'a-language',
  92. 'timezone' => 'Antarctica',
  93. 'schema' => 'fooblic',
  94. 'init' => ['Execute this', 'this too'],
  95. 'log' => false,
  96. ];
  97. $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
  98. ->onlyMethods(['createPdo'])
  99. ->setConstructorArgs([$config])
  100. ->getMock();
  101. $dsn = 'pgsql:host=foo;port=3440;dbname=bar';
  102. $expected = $config;
  103. $expected['flags'] += [
  104. PDO::ATTR_PERSISTENT => false,
  105. PDO::ATTR_EMULATE_PREPARES => false,
  106. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  107. ];
  108. $connection = $this->getMockBuilder('PDO')
  109. ->disableOriginalConstructor()
  110. ->onlyMethods(['exec', 'quote'])
  111. ->getMock();
  112. $connection->expects($this->any())
  113. ->method('quote')
  114. ->will($this->onConsecutiveCalls(
  115. $this->returnArgument(0),
  116. $this->returnArgument(0),
  117. $this->returnArgument(0)
  118. ));
  119. $connection->expects($this->exactly(5))
  120. ->method('exec')
  121. ->withConsecutive(
  122. ['SET NAMES a-language'],
  123. ['SET search_path TO fooblic'],
  124. ['Execute this'],
  125. ['this too'],
  126. ['SET timezone = Antarctica']
  127. );
  128. $driver->expects($this->once())->method('createPdo')
  129. ->with($dsn, $expected)
  130. ->will($this->returnValue($connection));
  131. $driver->connect();
  132. }
  133. /**
  134. * Tests that insert queries get a "RETURNING *" string at the end
  135. */
  136. public function testInsertReturning(): void
  137. {
  138. $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
  139. ->onlyMethods(['createPdo', 'getPdo'])
  140. ->setConstructorArgs([[]])
  141. ->getMock();
  142. $connection = $this
  143. ->getMockBuilder('Cake\Database\Connection')
  144. ->onlyMethods(['connect'])
  145. ->disableOriginalConstructor()
  146. ->getMock();
  147. $query = new Query($connection);
  148. $query->insert(['id', 'title'])
  149. ->into('articles')
  150. ->values([1, 'foo']);
  151. $translator = $driver->queryTranslator('insert');
  152. $query = $translator($query);
  153. $this->assertSame('RETURNING *', $query->clause('epilog'));
  154. $query = new Query($connection);
  155. $query->insert(['id', 'title'])
  156. ->into('articles')
  157. ->values([1, 'foo'])
  158. ->epilog('FOO');
  159. $query = $translator($query);
  160. $this->assertSame('FOO', $query->clause('epilog'));
  161. }
  162. /**
  163. * Test that having queries replace the aggregated alias field.
  164. */
  165. public function testHavingReplacesAlias(): void
  166. {
  167. $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
  168. ->onlyMethods(['connect', 'getPdo', 'version', 'enabled'])
  169. ->setConstructorArgs([[]])
  170. ->getMock();
  171. $driver->method('enabled')
  172. ->will($this->returnValue(true));
  173. $connection = $this->getMockBuilder('\Cake\Database\Connection')
  174. ->onlyMethods(['connect'])
  175. ->setConstructorArgs([['driver' => $driver, 'log' => false]])
  176. ->getMock();
  177. $query = new Query($connection);
  178. $query
  179. ->select([
  180. 'posts.author_id',
  181. 'post_count' => $query->func()->count('posts.id'),
  182. ])
  183. ->group(['posts.author_id'])
  184. ->having([$query->newExpr()->gte('post_count', 2, 'integer')]);
  185. $expected = 'SELECT posts.author_id, (COUNT(posts.id)) AS "post_count" ' .
  186. 'GROUP BY posts.author_id HAVING COUNT(posts.id) >= :c0';
  187. $this->assertSame($expected, $query->sql());
  188. }
  189. /**
  190. * Test that having queries replaces nothing if no alias is used.
  191. */
  192. public function testHavingWhenNoAliasIsUsed(): void
  193. {
  194. $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
  195. ->onlyMethods(['connect', 'getPdo', 'version', 'enabled'])
  196. ->setConstructorArgs([[]])
  197. ->getMock();
  198. $driver->method('enabled')
  199. ->will($this->returnValue(true));
  200. $connection = $this->getMockBuilder('\Cake\Database\Connection')
  201. ->onlyMethods(['connect'])
  202. ->setConstructorArgs([['driver' => $driver, 'log' => false]])
  203. ->getMock();
  204. $query = new Query($connection);
  205. $query
  206. ->select([
  207. 'posts.author_id',
  208. 'post_count' => $query->func()->count('posts.id'),
  209. ])
  210. ->group(['posts.author_id'])
  211. ->having([$query->newExpr()->gte('posts.author_id', 2, 'integer')]);
  212. $expected = 'SELECT posts.author_id, (COUNT(posts.id)) AS "post_count" ' .
  213. 'GROUP BY posts.author_id HAVING posts.author_id >= :c0';
  214. $this->assertSame($expected, $query->sql());
  215. }
  216. /**
  217. * Tests driver-specific feature support check.
  218. */
  219. public function testSupports(): void
  220. {
  221. $driver = ConnectionManager::get('test')->getDriver();
  222. $this->skipIf(!$driver instanceof Postgres);
  223. $this->assertTrue($driver->supports(Driver::FEATURE_TRUNCATE_WITH_CONSTRAINTS));
  224. $this->assertTrue($driver->supports(Driver::FEATURE_CTE));
  225. $this->assertTrue($driver->supports(Driver::FEATURE_JSON));
  226. $this->assertTrue($driver->supports(Driver::FEATURE_SAVEPOINT));
  227. $this->assertTrue($driver->supports(Driver::FEATURE_QUOTE));
  228. $this->assertTrue($driver->supports(Driver::FEATURE_WINDOW));
  229. $this->assertFalse($driver->supports(Driver::FEATURE_DISABLE_CONSTRAINT_WITHOUT_TRANSACTION));
  230. $this->assertFalse($driver->supports('this-is-fake'));
  231. }
  232. }