PostgresTest.php 8.2 KB

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