PostgresTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database\Driver;
  16. use Cake\Database\Query;
  17. use Cake\TestSuite\TestCase;
  18. use PDO;
  19. /**
  20. * Tests Postgres driver
  21. */
  22. class PostgresTest extends TestCase
  23. {
  24. /**
  25. * Test connecting to Postgres with default configuration
  26. *
  27. * @return void
  28. */
  29. public function testConnectionConfigDefault()
  30. {
  31. $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
  32. ->setMethods(['_connect', 'getConnection'])
  33. ->getMock();
  34. $dsn = 'pgsql:host=localhost;port=5432;dbname=cake';
  35. $expected = [
  36. 'persistent' => true,
  37. 'host' => 'localhost',
  38. 'username' => 'root',
  39. 'password' => '',
  40. 'database' => 'cake',
  41. 'schema' => 'public',
  42. 'port' => 5432,
  43. 'encoding' => 'utf8',
  44. 'timezone' => null,
  45. 'flags' => [],
  46. 'init' => [],
  47. ];
  48. $expected['flags'] += [
  49. PDO::ATTR_PERSISTENT => true,
  50. PDO::ATTR_EMULATE_PREPARES => false,
  51. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  52. ];
  53. $connection = $this->getMockBuilder('stdClass')
  54. ->setMethods(['exec', 'quote'])
  55. ->getMock();
  56. $connection->expects($this->any())
  57. ->method('quote')
  58. ->will($this->onConsecutiveCalls(
  59. $this->returnArgument(0),
  60. $this->returnArgument(0),
  61. $this->returnArgument(0)
  62. ));
  63. $connection->expects($this->at(1))->method('exec')->with('SET NAMES utf8');
  64. $connection->expects($this->at(3))->method('exec')->with('SET search_path TO public');
  65. $connection->expects($this->exactly(2))->method('exec');
  66. $driver->expects($this->once())->method('_connect')
  67. ->with($dsn, $expected);
  68. $driver->expects($this->any())->method('getConnection')
  69. ->will($this->returnValue($connection));
  70. $driver->connect();
  71. }
  72. /**
  73. * Test connecting to Postgres with custom configuration
  74. *
  75. * @return void
  76. */
  77. public function testConnectionConfigCustom()
  78. {
  79. $config = [
  80. 'persistent' => false,
  81. 'host' => 'foo',
  82. 'database' => 'bar',
  83. 'username' => 'user',
  84. 'password' => 'pass',
  85. 'port' => 3440,
  86. 'flags' => [1 => true, 2 => false],
  87. 'encoding' => 'a-language',
  88. 'timezone' => 'Antarctica',
  89. 'schema' => 'fooblic',
  90. 'init' => ['Execute this', 'this too'],
  91. ];
  92. $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
  93. ->setMethods(['_connect', 'getConnection', 'setConnection'])
  94. ->setConstructorArgs([$config])
  95. ->getMock();
  96. $dsn = 'pgsql:host=foo;port=3440;dbname=bar';
  97. $expected = $config;
  98. $expected['flags'] += [
  99. PDO::ATTR_PERSISTENT => false,
  100. PDO::ATTR_EMULATE_PREPARES => false,
  101. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
  102. ];
  103. $connection = $this->getMockBuilder('stdClass')
  104. ->setMethods(['exec', 'quote'])
  105. ->getMock();
  106. $connection->expects($this->any())
  107. ->method('quote')
  108. ->will($this->onConsecutiveCalls(
  109. $this->returnArgument(0),
  110. $this->returnArgument(0),
  111. $this->returnArgument(0)
  112. ));
  113. $connection->expects($this->at(1))->method('exec')->with('SET NAMES a-language');
  114. $connection->expects($this->at(3))->method('exec')->with('SET search_path TO fooblic');
  115. $connection->expects($this->at(5))->method('exec')->with('Execute this');
  116. $connection->expects($this->at(6))->method('exec')->with('this too');
  117. $connection->expects($this->at(7))->method('exec')->with('SET timezone = Antarctica');
  118. $connection->expects($this->exactly(5))->method('exec');
  119. $driver->setConnection($connection);
  120. $driver->expects($this->once())->method('_connect')
  121. ->with($dsn, $expected);
  122. $driver->expects($this->any())->method('getConnection')
  123. ->will($this->returnValue($connection));
  124. $driver->connect();
  125. }
  126. /**
  127. * Tests that insert queries get a "RETURNING *" string at the end
  128. *
  129. * @return void
  130. */
  131. public function testInsertReturning()
  132. {
  133. $driver = $this->getMockBuilder('Cake\Database\Driver\Postgres')
  134. ->setMethods(['_connect', 'getConnection'])
  135. ->setConstructorArgs([[]])
  136. ->getMock();
  137. $connection = $this
  138. ->getMockBuilder('\Cake\Database\Connection')
  139. ->setMethods(['connect'])
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $query = new Query($connection);
  143. $query->insert(['id', 'title'])
  144. ->into('articles')
  145. ->values([1, 'foo']);
  146. $translator = $driver->queryTranslator('insert');
  147. $query = $translator($query);
  148. $this->assertEquals('RETURNING *', $query->clause('epilog'));
  149. $query = new Query($connection);
  150. $query->insert(['id', 'title'])
  151. ->into('articles')
  152. ->values([1, 'foo'])
  153. ->epilog('FOO');
  154. $query = $translator($query);
  155. $this->assertEquals('FOO', $query->clause('epilog'));
  156. }
  157. }