PostgresTest.php 8.3 KB

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