PostgresTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\Driver;
  16. use Cake\Core\Configure;
  17. use Cake\Database\Connection;
  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. * @return void
  32. */
  33. public function testConnectionConfigDefault()
  34. {
  35. $driver = $this->getMock('Cake\Database\Driver\Postgres', ['_connect', 'connection']);
  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. ];
  50. $expected['flags'] += [
  51. PDO::ATTR_PERSISTENT => true,
  52. PDO::ATTR_EMULATE_PREPARES => false,
  53. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  54. ];
  55. $connection = $this->getMock('stdClass', ['exec', 'quote']);
  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('connection')
  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' => 'Antartica',
  89. 'schema' => 'fooblic',
  90. 'init' => ['Execute this', 'this too']
  91. ];
  92. $driver = $this->getMock(
  93. 'Cake\Database\Driver\Postgres',
  94. ['_connect', 'connection'],
  95. [$config]
  96. );
  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->getMock('stdClass', ['exec', 'quote']);
  105. $connection->expects($this->any())
  106. ->method('quote')
  107. ->will($this->onConsecutiveCalls(
  108. $this->returnArgument(0),
  109. $this->returnArgument(0),
  110. $this->returnArgument(0)
  111. ));
  112. $connection->expects($this->at(1))->method('exec')->with('SET NAMES a-language');
  113. $connection->expects($this->at(3))->method('exec')->with('SET search_path TO fooblic');
  114. $connection->expects($this->at(5))->method('exec')->with('Execute this');
  115. $connection->expects($this->at(6))->method('exec')->with('this too');
  116. $connection->expects($this->at(7))->method('exec')->with('SET timezone = Antartica');
  117. $connection->expects($this->exactly(5))->method('exec');
  118. $driver->connection($connection);
  119. $driver->expects($this->once())->method('_connect')
  120. ->with($dsn, $expected);
  121. $driver->expects($this->any())->method('connection')
  122. ->will($this->returnValue($connection));
  123. $driver->connect();
  124. }
  125. /**
  126. * Tests that insert queries get a "RETURNING *" string at the end
  127. *
  128. * @return void
  129. */
  130. public function testInsertReturning()
  131. {
  132. $driver = $this->getMock(
  133. 'Cake\Database\Driver\Postgres',
  134. ['_connect', 'connection'],
  135. [[]]
  136. );
  137. $connection = $this
  138. ->getMockBuilder('\Cake\Database\Connection')
  139. ->setMethods(['connect'])
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $query = new \Cake\Database\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 \Cake\Database\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. }