PostgresTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * PHP Version 5.4
  4. *
  5. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice.
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://cakephp.org CakePHP(tm) Project
  14. * @since CakePHP(tm) v 3.0.0
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. namespace Cake\Test\TestCase\Database\Driver;
  18. use Cake\Core\Configure;
  19. use Cake\Database\Connection;
  20. use Cake\Database\Driver\Postgres;
  21. use Cake\Database\Query;
  22. use \PDO;
  23. /**
  24. * Tests Postgres driver
  25. */
  26. class PostgresTest extends \Cake\TestSuite\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' => 'UTC',
  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->at(5))->method('exec')->with('SET timezone = UTC');
  63. $connection->expects($this->exactly(3))->method('exec');
  64. $driver->expects($this->once())->method('_connect')
  65. ->with($expected);
  66. $driver->expects($this->any())->method('connection')
  67. ->will($this->returnValue($connection));
  68. $driver->connect();
  69. }
  70. /**
  71. * Test connecting to Postgres with custom configuration
  72. *
  73. * @return void
  74. */
  75. public function testConnectionConfigCustom() {
  76. $config = [
  77. 'persistent' => false,
  78. 'host' => 'foo',
  79. 'database' => 'bar',
  80. 'login' => 'user',
  81. 'password' => 'pass',
  82. 'port' => 3440,
  83. 'flags' => [1 => true, 2 => false],
  84. 'encoding' => 'a-language',
  85. 'timezone' => 'Antartica',
  86. 'schema' => 'fooblic',
  87. 'init' => ['Execute this', 'this too']
  88. ];
  89. $driver = $this->getMock(
  90. 'Cake\Database\Driver\Postgres',
  91. ['_connect', 'connection'],
  92. [$config]
  93. );
  94. $expected = $config;
  95. $expected['dsn'] = 'pgsql:host=foo;port=3440;dbname=bar';
  96. $expected['flags'] += [
  97. PDO::ATTR_PERSISTENT => 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($expected);
  117. $driver->expects($this->any())->method('connection')
  118. ->will($this->returnValue($connection));
  119. $driver->connect();
  120. }
  121. }