PostgresTest.php 4.8 KB

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