PostgresTest.php 5.9 KB

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