/
ConnectionManagerTest.php
218 lines (195 loc) · 5.74 KB
/
ConnectionManagerTest.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
/**
* PHP 5
*
* 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://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @since CakePHP(tm) v 1.2.0.5550
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Datasource;
use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Database\Driver\Sqlite;
use Cake\TestSuite\TestCase;
class FakeConnection {
}
/**
* ConnectionManager Test
*/
class ConnectionManagerTest extends TestCase {
/**
* tearDown method
*
* @return void
*/
public function tearDown() {
parent::tearDown();
Plugin::unload();
ConnectionManager::drop('test_variant');
ConnectionManager::dropAlias('other_name');
}
/**
* Data provider for valid config data sets.
*
* @return array
*/
public static function configProvider() {
return [
'Array of data using classname key.' => [[
'className' => __NAMESPACE__ . '\FakeConnection',
'instance' => 'Sqlite',
'database' => ':memory:',
]],
'Direct instance' => [new FakeConnection],
];
}
/**
* Test the various valid config() calls.
*
* @dataProvider configProvider
* @return void
*/
public function testConfigVariants($settings) {
$this->assertNotContains('test_variant', ConnectionManager::configured(), 'test_variant config should not exist.');
ConnectionManager::config('test_variant', $settings);
$ds = ConnectionManager::get('test_variant');
$this->assertInstanceOf(__NAMESPACE__ . '\FakeConnection', $ds);
$this->assertContains('test_variant', ConnectionManager::configured());
}
/**
* Test invalid classes cause exceptions
*
* @expectedException Cake\Database\Exception\MissingDriverException
*/
public function testConfigInvalidOptions() {
ConnectionManager::config('test_variant', [
'className' => 'HerpDerp'
]);
ConnectionManager::get('test_variant');
}
/**
* Test for errors on duplicate config.
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage Cannot reconfigure existing key "test_variant"
* @return void
*/
public function testConfigDuplicateConfig() {
$settings = [
'className' => 'Sqlite',
'database' => ':memory:',
];
ConnectionManager::config('test_variant', $settings);
ConnectionManager::config('test_variant', $settings);
}
/**
* Test get() failing on missing config.
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage The datasource configuration "test_variant" was not found.
* @return void
*/
public function testGetFailOnMissingConfig() {
ConnectionManager::get('test_variant');
}
/**
* Test loading configured connections.
*
* @return void
*/
public function testGet() {
$config = ConnectionManager::config('test');
$this->skipIf(empty($config), 'No test config, skipping');
$ds = ConnectionManager::get('test');
$this->assertSame($ds, ConnectionManager::get('test'));
$this->assertInstanceOf('Cake\Database\Connection', $ds);
$this->assertEquals('test', $ds->configName());
}
/**
* Test loading connections without aliases
*
* @expectedException Cake\Error\Exception
* @expectedExceptionMessage The datasource configuration "other_name" was not found.
* @return void
*/
public function testGetNoAlias() {
$config = ConnectionManager::config('test');
$this->skipIf(empty($config), 'No test config, skipping');
ConnectionManager::alias('test', 'other_name');
ConnectionManager::get('other_name', false);
}
/**
* Test that configured() finds configured sources.
*
* @return void
*/
public function testConfigured() {
ConnectionManager::config('test_variant', [
'className' => 'Sqlite',
'database' => ':memory:'
]);
$results = ConnectionManager::configured();
$this->assertContains('test_variant', $results);
}
/**
* testGetPluginDataSource method
*
* @return void
*/
public function testGetPluginDataSource() {
Plugin::load('TestPlugin');
$name = 'test_variant';
$config = array('className' => 'TestPlugin.TestSource');
ConnectionManager::config($name, $config);
$connection = ConnectionManager::get($name);
$this->assertInstanceOf('Cake\Database\Connection', $connection);
$this->assertInstanceOf('TestPlugin\Database\Driver\TestSource', $connection->driver());
}
/**
* Tests that a connection configuration can be deleted in runtime
*
* @return void
*/
public function testDrop() {
ConnectionManager::config('test_variant', [
'className' => 'Sqlite',
'database' => ':memory:'
]);
$result = ConnectionManager::configured();
$this->assertContains('test_variant', $result);
$this->assertTrue(ConnectionManager::drop('test_variant'));
$result = ConnectionManager::configured();
$this->assertNotContains('test_variant', $result);
$this->assertFalse(ConnectionManager::drop('probably_does_not_exist'), 'Should return false on failure.');
}
/**
* Test aliasing connections.
*
* @return void
*/
public function testAlias() {
ConnectionManager::config('test_variant', [
'className' => 'Sqlite',
'database' => ':memory:'
]);
ConnectionManager::alias('test_variant', 'other_name');
$result = ConnectionManager::get('test_variant');
$this->assertSame($result, ConnectionManager::get('other_name'));
}
/**
* Test alias() raises an error when aliasing an undefined connection.
*
* @expectedException Cake\Error\MissingDatasourceConfigException
* @return void
*/
public function testAliasError() {
$this->assertNotContains('test_kaboom', ConnectionManager::configured());
ConnectionManager::alias('test_kaboom', 'other_name');
}
}