統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Model / ConnectionManagerTest.php @ 0b1b8047

履歴 | 表示 | アノテート | ダウンロード (10.637 KB)

1 635eef61 spyder1211
<?php
2
/**
3
 * Connection Manager tests
4
 *
5
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
6
 *
7
 * Licensed under The MIT License
8
 * For full copyright and license information, please see the LICENSE.txt
9
 * Redistributions of files must retain the above copyright notice
10
 *
11
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
12
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
13
 * @package       Cake.Test.Case.Model
14
 * @since         CakePHP(tm) v 1.2.0.5550
15
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
16
 */
17
18
App::uses('ConnectionManager', 'Model');
19
20
/**
21
 * ConnectionManagerTest
22
 *
23
 * @package       Cake.Test.Case.Model
24
 */
25
class ConnectionManagerTest extends CakeTestCase {
26
27
/**
28
 * tearDown method
29
 *
30
 * @return void
31
 */
32
        public function tearDown() {
33
                parent::tearDown();
34
                CakePlugin::unload();
35
        }
36
37
/**
38
 * testEnumConnectionObjects method
39
 *
40
 * @return void
41
 */
42
        public function testEnumConnectionObjects() {
43
                $sources = ConnectionManager::enumConnectionObjects();
44
                $this->assertTrue(count($sources) >= 1);
45
46
                $connections = array('default', 'test', 'test');
47
                $this->assertTrue(count(array_intersect(array_keys($sources), $connections)) >= 1);
48
        }
49
50
/**
51
 * testGetDataSource method
52
 *
53
 * @return void
54
 */
55
        public function testGetDataSource() {
56
                App::build(array(
57
                        'Model/Datasource' => array(
58
                                CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS
59
                        )
60
                ));
61
62
                $name = 'test_get_datasource';
63
                $config = array('datasource' => 'Test2Source');
64
65
                ConnectionManager::create($name, $config);
66
                $connections = ConnectionManager::enumConnectionObjects();
67
                $this->assertTrue((bool)(count(array_keys($connections) >= 1)));
68
69
                $source = ConnectionManager::getDataSource('test_get_datasource');
70
                $this->assertTrue(is_object($source));
71
                ConnectionManager::drop('test_get_datasource');
72
        }
73
74
/**
75
 * testGetDataSourceException() method
76
 *
77
 * @return void
78
 * @expectedException MissingDatasourceConfigException
79
 */
80
        public function testGetDataSourceException() {
81
                ConnectionManager::getDataSource('non_existent_source');
82
        }
83
84
/**
85
 * testGetPluginDataSource method
86
 *
87
 * @return void
88
 */
89
        public function testGetPluginDataSource() {
90
                App::build(array(
91
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
92
                ), App::RESET);
93
                CakePlugin::load('TestPlugin');
94
                $name = 'test_source';
95
                $config = array('datasource' => 'TestPlugin.TestSource');
96
                $connection = ConnectionManager::create($name, $config);
97
98
                $this->assertTrue(class_exists('TestSource'));
99
                $this->assertEquals($connection->configKeyName, $name);
100
                $this->assertEquals($connection->config, $config);
101
102
                ConnectionManager::drop($name);
103
        }
104
105
/**
106
 * testGetPluginDataSourceAndPluginDriver method
107
 *
108
 * @return void
109
 */
110
        public function testGetPluginDataSourceAndPluginDriver() {
111
                App::build(array(
112
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
113
                ), App::RESET);
114
                CakePlugin::load('TestPlugin');
115
                $name = 'test_plugin_source_and_driver';
116
                $config = array('datasource' => 'TestPlugin.Database/TestDriver');
117
118
                $connection = ConnectionManager::create($name, $config);
119
120
                $this->assertTrue(class_exists('TestSource'));
121
                $this->assertTrue(class_exists('TestDriver'));
122
                $this->assertEquals($connection->configKeyName, $name);
123
                $this->assertEquals($connection->config, $config);
124
125
                ConnectionManager::drop($name);
126
        }
127
128
/**
129
 * testGetLocalDataSourceAndPluginDriver method
130
 *
131
 * @return void
132
 */
133
        public function testGetLocalDataSourceAndPluginDriver() {
134
                App::build(array(
135
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
136
                ));
137
                CakePlugin::load('TestPlugin');
138
                $name = 'test_local_source_and_plugin_driver';
139
                $config = array('datasource' => 'TestPlugin.Database/DboDummy');
140
141
                $connection = ConnectionManager::create($name, $config);
142
143
                $this->assertTrue(class_exists('DboSource'));
144
                $this->assertTrue(class_exists('DboDummy'));
145
                $this->assertEquals($connection->configKeyName, $name);
146
147
                ConnectionManager::drop($name);
148
        }
149
150
/**
151
 * testGetPluginDataSourceAndLocalDriver method
152
 *
153
 * @return void
154
 */
155
        public function testGetPluginDataSourceAndLocalDriver() {
156
                App::build(array(
157
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
158
                        'Model/Datasource/Database' => array(
159
                                CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS . 'Database' . DS
160
                        )
161
                ));
162
163
                $name = 'test_plugin_source_and_local_driver';
164
                $config = array('datasource' => 'Database/TestLocalDriver');
165
166
                $connection = ConnectionManager::create($name, $config);
167
168
                $this->assertTrue(class_exists('TestSource'));
169
                $this->assertTrue(class_exists('TestLocalDriver'));
170
                $this->assertEquals($connection->configKeyName, $name);
171
                $this->assertEquals($connection->config, $config);
172
                ConnectionManager::drop($name);
173
        }
174
175
/**
176
 * testSourceList method
177
 *
178
 * @return void
179
 */
180
        public function testSourceList() {
181
                ConnectionManager::getDataSource('test');
182
                $sources = ConnectionManager::sourceList();
183
                $this->assertTrue(count($sources) >= 1);
184
                $this->assertTrue(in_array('test', array_keys($sources)));
185
        }
186
187
/**
188
 * testGetSourceName method
189
 *
190
 * @return void
191
 */
192
        public function testGetSourceName() {
193
                $source = ConnectionManager::getDataSource('test');
194
                $result = ConnectionManager::getSourceName($source);
195
196
                $this->assertEquals('test', $result);
197
198
                $source = new StdClass();
199
                $result = ConnectionManager::getSourceName($source);
200
                $this->assertNull($result);
201
        }
202
203
/**
204
 * testLoadDataSource method
205
 *
206
 * @return void
207
 */
208
        public function testLoadDataSource() {
209
                $connections = array(
210
                        array('classname' => 'Mysql', 'filename' => 'Mysql', 'package' => 'Database'),
211
                        array('classname' => 'Postgres', 'filename' => 'Postgres', 'package' => 'Database'),
212
                        array('classname' => 'Sqlite', 'filename' => 'Sqlite', 'package' => 'Database'),
213
                );
214
215
                foreach ($connections as $connection) {
216
                        $exists = class_exists($connection['classname']);
217
                        $loaded = ConnectionManager::loadDataSource($connection);
218
                        $this->assertEquals($loaded, !$exists, "Failed loading the {$connection['classname']} datasource");
219
                }
220
        }
221
222
/**
223
 * testLoadDataSourceException() method
224
 *
225
 * @return void
226
 * @expectedException MissingDatasourceException
227
 */
228
        public function testLoadDataSourceException() {
229
                $connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent');
230
                ConnectionManager::loadDataSource($connection);
231
        }
232
233
/**
234
 * testCreateDataSource method
235
 *
236
 * @return void
237
 */
238
        public function testCreateDataSourceWithIntegrationTests() {
239
                $name = 'test_created_connection';
240
241
                $connections = ConnectionManager::enumConnectionObjects();
242
                $this->assertTrue((bool)(count(array_keys($connections) >= 1)));
243
244
                $source = ConnectionManager::getDataSource('test');
245
                $this->assertTrue(is_object($source));
246
247
                $config = $source->config;
248
                $connection = ConnectionManager::create($name, $config);
249
250
                $this->assertTrue(is_object($connection));
251
                $this->assertEquals($name, $connection->configKeyName);
252
                $this->assertEquals($name, ConnectionManager::getSourceName($connection));
253
254
                $source = ConnectionManager::create(null, array());
255
                $this->assertEquals(null, $source);
256
257
                $source = ConnectionManager::create('another_test', array());
258
                $this->assertEquals(null, $source);
259
260
                $config = array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql');
261
                $source = ConnectionManager::create(null, $config);
262
                $this->assertEquals(null, $source);
263
        }
264
265
/**
266
 * testConnectionData method
267
 *
268
 * @return void
269
 */
270
        public function testConnectionData() {
271
                App::build(array(
272
                        'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
273
                        'Model/Datasource' => array(
274
                                CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS
275
                        )
276
                ), App::RESET);
277
                CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
278
                $expected = array(
279
                        'datasource' => 'Test2Source'
280
                );
281
282
                ConnectionManager::create('connection1', array('datasource' => 'Test2Source'));
283
                $connections = ConnectionManager::enumConnectionObjects();
284
                $this->assertEquals($expected, $connections['connection1']);
285
                ConnectionManager::drop('connection1');
286
287
                ConnectionManager::create('connection2', array('datasource' => 'Test2Source'));
288
                $connections = ConnectionManager::enumConnectionObjects();
289
                $this->assertEquals($expected, $connections['connection2']);
290
                ConnectionManager::drop('connection2');
291
292
                ConnectionManager::create('connection3', array('datasource' => 'TestPlugin.TestSource'));
293
                $connections = ConnectionManager::enumConnectionObjects();
294
                $expected['datasource'] = 'TestPlugin.TestSource';
295
                $this->assertEquals($expected, $connections['connection3']);
296
                ConnectionManager::drop('connection3');
297
298
                ConnectionManager::create('connection4', array('datasource' => 'TestPlugin.TestSource'));
299
                $connections = ConnectionManager::enumConnectionObjects();
300
                $this->assertEquals($expected, $connections['connection4']);
301
                ConnectionManager::drop('connection4');
302
303
                ConnectionManager::create('connection5', array('datasource' => 'Test2OtherSource'));
304
                $connections = ConnectionManager::enumConnectionObjects();
305
                $expected['datasource'] = 'Test2OtherSource';
306
                $this->assertEquals($expected, $connections['connection5']);
307
                ConnectionManager::drop('connection5');
308
309
                ConnectionManager::create('connection6', array('datasource' => 'Test2OtherSource'));
310
                $connections = ConnectionManager::enumConnectionObjects();
311
                $this->assertEquals($expected, $connections['connection6']);
312
                ConnectionManager::drop('connection6');
313
314
                ConnectionManager::create('connection7', array('datasource' => 'TestPlugin.TestOtherSource'));
315
                $connections = ConnectionManager::enumConnectionObjects();
316
                $expected['datasource'] = 'TestPlugin.TestOtherSource';
317
                $this->assertEquals($expected, $connections['connection7']);
318
                ConnectionManager::drop('connection7');
319
320
                ConnectionManager::create('connection8', array('datasource' => 'TestPlugin.TestOtherSource'));
321
                $connections = ConnectionManager::enumConnectionObjects();
322
                $this->assertEquals($expected, $connections['connection8']);
323
                ConnectionManager::drop('connection8');
324
        }
325
326
/**
327
 * Tests that a connection configuration can be deleted in runtime
328
 *
329
 * @return void
330
 */
331
        public function testDrop() {
332
                App::build(array(
333
                        'Model/Datasource' => array(
334
                                CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS . 'Datasource' . DS
335
                        )
336
                ));
337
                ConnectionManager::create('droppable', array('datasource' => 'Test2Source'));
338
                $connections = ConnectionManager::enumConnectionObjects();
339
                $this->assertEquals(array('datasource' => 'Test2Source'), $connections['droppable']);
340
341
                $this->assertTrue(ConnectionManager::drop('droppable'));
342
                $connections = ConnectionManager::enumConnectionObjects();
343
                $this->assertFalse(isset($connections['droppable']));
344
        }
345
}