PostgresTest.php 4.8 KB

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