/
PostgresTest.php
139 lines (123 loc) · 3.98 KB
/
PostgresTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* PHP Version 5.4
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package Cake.Model
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Database\Driver;
use Cake\Core\Configure;
use Cake\Database\Connection;
use Cake\Database\Driver\Postgres;
use Cake\Database\Query;
use \PDO;
/**
* Tests Postgres driver
*
*/
class PostgresTest extends \Cake\TestSuite\TestCase {
/**
* Test connecting to Postgres with default configuration
*
* @return void
*/
public function testConnectionConfigDefault() {
$driver = $this->getMock('Cake\Database\Driver\Postgres', ['_connect', 'connection']);
$expected = [
'persistent' => true,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'cake',
'schema' => 'public',
'port' => 5432,
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'init' => [],
'dsn' => 'pgsql:host=localhost;port=5432;dbname=cake'
];
$expected['flags'] += [
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$connection = $this->getMock('stdClass', ['exec', 'quote']);
$connection->expects($this->any())
->method('quote')
->will($this->onConsecutiveCalls(
$this->returnArgument(0),
$this->returnArgument(0),
$this->returnArgument(0)
));
$connection->expects($this->at(1))->method('exec')->with('SET NAMES utf8');
$connection->expects($this->at(3))->method('exec')->with('SET search_path TO public');
$connection->expects($this->at(5))->method('exec')->with('SET timezone = UTC');
$connection->expects($this->exactly(3))->method('exec');
$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->expects($this->any())->method('connection')
->will($this->returnValue($connection));
$driver->connect();
}
/**
* Test connecting to Postgres with custom configuration
*
* @return void
*/
public function testConnectionConfigCustom() {
$config = [
'persistent' => false,
'host' => 'foo',
'database' => 'bar',
'login' => 'user',
'password' => 'pass',
'port' => 3440,
'flags' => [1 => true, 2 => false],
'encoding' => 'a-language',
'timezone' => 'Antartica',
'schema' => 'fooblic',
'init' => ['Execute this', 'this too']
];
$driver = $this->getMock(
'Cake\Database\Driver\Postgres',
['_connect', 'connection'],
[$config]
);
$expected = $config;
$expected['dsn'] = 'pgsql:host=foo;port=3440;dbname=bar';
$expected['flags'] += [
PDO::ATTR_PERSISTENT => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
];
$connection = $this->getMock('stdClass', ['exec', 'quote']);
$connection->expects($this->any())
->method('quote')
->will($this->onConsecutiveCalls(
$this->returnArgument(0),
$this->returnArgument(0),
$this->returnArgument(0)
));
$connection->expects($this->at(1))->method('exec')->with('SET NAMES a-language');
$connection->expects($this->at(3))->method('exec')->with('SET search_path TO fooblic');
$connection->expects($this->at(5))->method('exec')->with('Execute this');
$connection->expects($this->at(6))->method('exec')->with('this too');
$connection->expects($this->at(7))->method('exec')->with('SET timezone = Antartica');
$connection->expects($this->exactly(5))->method('exec');
$driver->connection($connection);
$driver->expects($this->once())->method('_connect')
->with($expected);
$driver->expects($this->any())->method('connection')
->will($this->returnValue($connection));
$driver->connect();
}
}