PostgresTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\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->getMock('Cake\Database\Driver\Postgres', ['_connect', 'connection']);
  32. $dsn = 'pgsql:host=localhost;port=5432;dbname=cake';
  33. $expected = [
  34. 'persistent' => true,
  35. 'host' => 'localhost',
  36. 'username' => 'root',
  37. 'password' => '',
  38. 'database' => 'cake',
  39. 'schema' => 'public',
  40. 'port' => 5432,
  41. 'encoding' => 'utf8',
  42. 'timezone' => null,
  43. 'flags' => [],
  44. 'init' => [],
  45. ];
  46. $expected['flags'] += [
  47. PDO::ATTR_PERSISTENT => true,
  48. PDO::ATTR_EMULATE_PREPARES => false,
  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($dsn, $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. {
  75. $config = [
  76. 'persistent' => false,
  77. 'host' => 'foo',
  78. 'database' => 'bar',
  79. 'username' => '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. $dsn = 'pgsql:host=foo;port=3440;dbname=bar';
  94. $expected = $config;
  95. $expected['flags'] += [
  96. PDO::ATTR_PERSISTENT => false,
  97. PDO::ATTR_EMULATE_PREPARES => false,
  98. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  99. ];
  100. $connection = $this->getMock('stdClass', ['exec', 'quote']);
  101. $connection->expects($this->any())
  102. ->method('quote')
  103. ->will($this->onConsecutiveCalls(
  104. $this->returnArgument(0),
  105. $this->returnArgument(0),
  106. $this->returnArgument(0)
  107. ));
  108. $connection->expects($this->at(1))->method('exec')->with('SET NAMES a-language');
  109. $connection->expects($this->at(3))->method('exec')->with('SET search_path TO fooblic');
  110. $connection->expects($this->at(5))->method('exec')->with('Execute this');
  111. $connection->expects($this->at(6))->method('exec')->with('this too');
  112. $connection->expects($this->at(7))->method('exec')->with('SET timezone = Antartica');
  113. $connection->expects($this->exactly(5))->method('exec');
  114. $driver->connection($connection);
  115. $driver->expects($this->once())->method('_connect')
  116. ->with($dsn, $expected);
  117. $driver->expects($this->any())->method('connection')
  118. ->will($this->returnValue($connection));
  119. $driver->connect();
  120. }
  121. /**
  122. * Tests that insert queries get a "RETURNING *" string at the end
  123. *
  124. * @return void
  125. */
  126. public function testInsertReturning()
  127. {
  128. $driver = $this->getMock(
  129. 'Cake\Database\Driver\Postgres',
  130. ['_connect', 'connection'],
  131. [[]]
  132. );
  133. $connection = $this
  134. ->getMockBuilder('\Cake\Database\Connection')
  135. ->setMethods(['connect'])
  136. ->disableOriginalConstructor()
  137. ->getMock();
  138. $query = new \Cake\Database\Query($connection);
  139. $query->insert(['id', 'title'])
  140. ->into('articles')
  141. ->values([1, 'foo']);
  142. $translator = $driver->queryTranslator('insert');
  143. $query = $translator($query);
  144. $this->assertEquals('RETURNING *', $query->clause('epilog'));
  145. $query = new \Cake\Database\Query($connection);
  146. $query->insert(['id', 'title'])
  147. ->into('articles')
  148. ->values([1, 'foo'])
  149. ->epilog('FOO');
  150. $query = $translator($query);
  151. $this->assertEquals('FOO', $query->clause('epilog'));
  152. }
  153. }