PostgresTest.php 4.7 KB

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