/
ConnectionManagerTest.php
343 lines (310 loc) · 9.64 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* 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
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Datasource;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\TestSuite\TestCase;
class FakeConnection
{
protected $_config = [];
/**
* Constructor.
*
* @param array $config configuration for connecting to database
*/
public function __construct($config)
{
$this->_config = $config;
}
/**
* Returns the set config
*
* @return array
*/
public function config()
{
return $this->_config;
}
/**
* Returns the set name
*
* @return string
*/
public function configName()
{
if (empty($this->_config['name'])) {
return '';
}
return $this->_config['name'];
}
}
/**
* 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\Datasource\Exception\MissingDatasourceException
*/
public function testConfigInvalidOptions()
{
ConnectionManager::config('test_variant', [
'className' => 'Herp\Derp'
]);
ConnectionManager::get('test_variant');
}
/**
* Test for errors on duplicate config.
*
* @expectedException \BadMethodCallException
* @expectedExceptionMessage Cannot reconfigure existing key "test_variant"
* @return void
*/
public function testConfigDuplicateConfig()
{
$settings = [
'className' => __NAMESPACE__ . '\FakeConnection',
'database' => ':memory:',
];
ConnectionManager::config('test_variant', $settings);
ConnectionManager::config('test_variant', $settings);
}
/**
* Test get() failing on missing config.
*
* @expectedException \Cake\Core\Exception\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\Core\Exception\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' => __NAMESPACE__ . '\FakeConnection',
'database' => ':memory:'
]);
$results = ConnectionManager::configured();
$this->assertContains('test_variant', $results);
}
/**
* testGetPluginDataSource method
*
* @return void
*/
public function testGetPluginDataSource()
{
Plugin::load('TestPlugin');
$name = 'test_variant';
$config = ['className' => 'TestPlugin.TestSource', 'foo' => 'bar'];
ConnectionManager::config($name, $config);
$connection = ConnectionManager::get($name);
$this->assertInstanceOf('TestPlugin\Datasource\TestSource', $connection);
unset($config['className']);
$this->assertSame($config + ['name' => 'test_variant'], $connection->config());
}
/**
* Tests that a connection configuration can be deleted in runtime
*
* @return void
*/
public function testDrop()
{
ConnectionManager::config('test_variant', [
'className' => __NAMESPACE__ . '\FakeConnection',
'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' => __NAMESPACE__ . '\FakeConnection',
'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\Datasource\Exception\MissingDatasourceConfigException
* @return void
*/
public function testAliasError()
{
$this->assertNotContains('test_kaboom', ConnectionManager::configured());
ConnectionManager::alias('test_kaboom', 'other_name');
}
/**
* Test parseDsn method.
*
* @return void
*/
public function testParseDsn()
{
$result = ConnectionManager::parseDsn('mysql://root:secret@localhost:3306/database?log=1');
$expected = [
'scheme' => 'mysql',
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'secret',
'port' => 3306,
'database' => 'database',
'log' => '1'
];
$this->assertEquals($expected, $result);
}
/**
* Tests that directly setting an instance in a config, will not return a different
* instance later on
*
* @return void
*/
public function testConfigWithObject()
{
$connection = new FakeConnection;
ConnectionManager::config('test_variant', $connection);
$this->assertSame($connection, ConnectionManager::get('test_variant'));
}
/**
* Tests configuring an instance with a callable
*
* @return void
*/
public function testConfigWithCallable()
{
$connection = new FakeConnection;
$callable = function ($alias) use ($connection) {
$this->assertEquals('test_variant', $alias);
return $connection;
};
ConnectionManager::config('test_variant', $callable);
$this->assertSame($connection, ConnectionManager::get('test_variant'));
}
/**
* Tests that setting a config will also correctly set the name for the connection
*
* @return void
*/
public function testSetConfigName()
{
//Set with explicit name
ConnectionManager::config('test_variant', [
'className' => __NAMESPACE__ . '\FakeConnection',
'database' => ':memory:'
]);
$result = ConnectionManager::get('test_variant');
$this->assertSame('test_variant', $result->configName());
ConnectionManager::drop('test_variant');
ConnectionManager::config([
'test_variant' => [
'className' => __NAMESPACE__ . '\FakeConnection',
'database' => ':memory:'
]
]);
$result = ConnectionManager::get('test_variant');
$this->assertSame('test_variant', $result->configName());
}
}