Mercurial > hg > Members > shoshi > webvirt
comparison cake/tests/cases/libs/model/connection_manager.test.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * Connection Manager tests | |
4 * | |
5 * | |
6 * PHP versions 4 and 5 | |
7 * | |
8 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
9 * | |
10 * Licensed under The Open Group Test Suite License | |
11 * Redistributions of files must retain the above copyright notice. | |
12 * | |
13 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
14 * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests | |
15 * @package cake | |
16 * @subpackage cake.tests.cases.libs | |
17 * @since CakePHP(tm) v 1.2.0.5550 | |
18 * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License | |
19 */ | |
20 App::import('Core', 'ConnectionManager'); | |
21 | |
22 /** | |
23 * ConnectionManagerTest | |
24 * | |
25 * @package cake | |
26 * @subpackage cake.tests.cases.models | |
27 */ | |
28 class ConnectionManagerTest extends CakeTestCase { | |
29 | |
30 /** | |
31 * setUp method | |
32 * | |
33 * @access public | |
34 * @return void | |
35 */ | |
36 function setUp() { | |
37 $this->ConnectionManager =& ConnectionManager::getInstance(); | |
38 } | |
39 | |
40 /** | |
41 * tearDown method | |
42 * | |
43 * @access public | |
44 * @return void | |
45 */ | |
46 function tearDown() { | |
47 unset($this->ConnectionManager); | |
48 } | |
49 | |
50 /** | |
51 * testInstantiation method | |
52 * | |
53 * @access public | |
54 * @return void | |
55 */ | |
56 function testInstantiation() { | |
57 $this->assertTrue(is_a($this->ConnectionManager, 'ConnectionManager')); | |
58 } | |
59 | |
60 /** | |
61 * testEnumConnectionObjects method | |
62 * | |
63 * @access public | |
64 * @return void | |
65 */ | |
66 function testEnumConnectionObjects() { | |
67 $sources = ConnectionManager::enumConnectionObjects(); | |
68 $this->assertTrue(count($sources) >= 1); | |
69 | |
70 $connections = array('default', 'test', 'test_suite'); | |
71 $this->assertTrue(count(array_intersect(array_keys($sources), $connections)) >= 1); | |
72 } | |
73 | |
74 /** | |
75 * testGetDataSource method | |
76 * | |
77 * @access public | |
78 * @return void | |
79 */ | |
80 function testGetDataSource() { | |
81 $connections = ConnectionManager::enumConnectionObjects(); | |
82 $this->assertTrue(count(array_keys($connections) >= 1)); | |
83 | |
84 $source = ConnectionManager::getDataSource(key($connections)); | |
85 $this->assertTrue(is_object($source)); | |
86 | |
87 $this->expectError(new PatternExpectation('/Non-existent data source/i')); | |
88 | |
89 $source = ConnectionManager::getDataSource('non_existent_source'); | |
90 $this->assertEqual($source, null); | |
91 | |
92 } | |
93 | |
94 /** | |
95 * testGetPluginDataSource method | |
96 * | |
97 * @access public | |
98 * @return void | |
99 */ | |
100 function testGetPluginDataSource() { | |
101 App::build(array( | |
102 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) | |
103 )); | |
104 | |
105 $name = 'test_source'; | |
106 $config = array('datasource' => 'TestPlugin.TestSource'); | |
107 $connection = ConnectionManager::create($name, $config); | |
108 | |
109 $this->assertTrue(class_exists('TestSource')); | |
110 $this->assertEqual($connection->configKeyName, $name); | |
111 $this->assertEqual($connection->config, $config); | |
112 | |
113 App::build(); | |
114 } | |
115 | |
116 /** | |
117 * testGetPluginDataSourceAndPluginDriver method | |
118 * | |
119 * @access public | |
120 * @return void | |
121 */ | |
122 function testGetPluginDataSourceAndPluginDriver() { | |
123 App::build(array( | |
124 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) | |
125 )); | |
126 | |
127 $name = 'test_plugin_source_and_driver'; | |
128 $config = array('datasource' => 'TestPlugin.TestSource', 'driver' => 'TestPlugin.TestDriver'); | |
129 | |
130 $connection = ConnectionManager::create($name, $config); | |
131 | |
132 $this->assertTrue(class_exists('TestSource')); | |
133 $this->assertTrue(class_exists('TestDriver')); | |
134 $this->assertEqual($connection->configKeyName, $name); | |
135 $this->assertEqual($connection->config, $config); | |
136 | |
137 App::build(); | |
138 } | |
139 | |
140 /** | |
141 * testGetLocalDataSourceAndPluginDriver method | |
142 * | |
143 * @access public | |
144 * @return void | |
145 */ | |
146 function testGetLocalDataSourceAndPluginDriver() { | |
147 App::build(array( | |
148 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS) | |
149 )); | |
150 | |
151 $name = 'test_local_source_and_plugin_driver'; | |
152 $config = array('datasource' => 'dbo', 'driver' => 'TestPlugin.DboDummy'); | |
153 | |
154 $connection = ConnectionManager::create($name, $config); | |
155 | |
156 $this->assertTrue(class_exists('DboSource')); | |
157 $this->assertTrue(class_exists('DboDummy')); | |
158 $this->assertEqual($connection->configKeyName, $name); | |
159 | |
160 App::build(); | |
161 } | |
162 | |
163 /** | |
164 * testGetPluginDataSourceAndLocalDriver method | |
165 * | |
166 * @access public | |
167 * @return void | |
168 */ | |
169 function testGetPluginDataSourceAndLocalDriver() { | |
170 App::build(array( | |
171 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS), | |
172 'datasources' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS . 'datasources' . DS) | |
173 )); | |
174 | |
175 $name = 'test_plugin_source_and_local_driver'; | |
176 $config = array('datasource' => 'TestPlugin.TestSource', 'driver' => 'local_driver'); | |
177 | |
178 $connection = ConnectionManager::create($name, $config); | |
179 | |
180 $this->assertTrue(class_exists('TestSource')); | |
181 $this->assertTrue(class_exists('TestLocalDriver')); | |
182 $this->assertEqual($connection->configKeyName, $name); | |
183 $this->assertEqual($connection->config, $config); | |
184 App::build(); | |
185 } | |
186 | |
187 /** | |
188 * testSourceList method | |
189 * | |
190 * @access public | |
191 * @return void | |
192 */ | |
193 function testSourceList() { | |
194 $sources = ConnectionManager::sourceList(); | |
195 $this->assertTrue(count($sources) >= 1); | |
196 | |
197 $connections = array('default', 'test', 'test_suite'); | |
198 $this->assertTrue(count(array_intersect($sources, $connections)) >= 1); | |
199 } | |
200 | |
201 /** | |
202 * testGetSourceName method | |
203 * | |
204 * @access public | |
205 * @return void | |
206 */ | |
207 function testGetSourceName() { | |
208 $connections = ConnectionManager::enumConnectionObjects(); | |
209 $name = key($connections); | |
210 $source = ConnectionManager::getDataSource($name); | |
211 $result = ConnectionManager::getSourceName($source); | |
212 | |
213 $this->assertEqual($result, $name); | |
214 | |
215 $source =& new StdClass(); | |
216 $result = ConnectionManager::getSourceName($source); | |
217 $this->assertEqual($result, null); | |
218 } | |
219 | |
220 /** | |
221 * testLoadDataSource method | |
222 * | |
223 * @access public | |
224 * @return void | |
225 */ | |
226 function testLoadDataSource() { | |
227 $connections = array( | |
228 array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql'), | |
229 array('classname' => 'DboMysqli', 'filename' => 'dbo' . DS . 'dbo_mysqli'), | |
230 array('classname' => 'DboMssql', 'filename' => 'dbo' . DS . 'dbo_mssql'), | |
231 array('classname' => 'DboOracle', 'filename' => 'dbo' . DS . 'dbo_oracle'), | |
232 ); | |
233 | |
234 foreach ($connections as $connection) { | |
235 $exists = class_exists($connection['classname']); | |
236 $loaded = ConnectionManager::loadDataSource($connection); | |
237 $this->assertEqual($loaded, !$exists, "%s Failed loading the {$connection['classname']} datasource"); | |
238 } | |
239 | |
240 $connection = array('classname' => 'NonExistentDataSource', 'filename' => 'non_existent'); | |
241 $this->expectError(new PatternExpectation('/Unable to import DataSource class/i')); | |
242 | |
243 $loaded = ConnectionManager::loadDataSource($connection); | |
244 $this->assertEqual($loaded, null); | |
245 } | |
246 | |
247 /** | |
248 * testCreateDataSource method | |
249 * | |
250 * @access public | |
251 * @return void | |
252 */ | |
253 function testCreateDataSourceWithIntegrationTests() { | |
254 $name = 'test_created_connection'; | |
255 | |
256 $connections = ConnectionManager::enumConnectionObjects(); | |
257 $this->assertTrue(count(array_keys($connections) >= 1)); | |
258 | |
259 $source = ConnectionManager::getDataSource(key($connections)); | |
260 $this->assertTrue(is_object($source)); | |
261 | |
262 $config = $source->config; | |
263 $connection = ConnectionManager::create($name, $config); | |
264 | |
265 $this->assertTrue(is_object($connection)); | |
266 $this->assertEqual($name, $connection->configKeyName); | |
267 $this->assertEqual($name, ConnectionManager::getSourceName($connection)); | |
268 | |
269 $source = ConnectionManager::create(null, array()); | |
270 $this->assertEqual($source, null); | |
271 | |
272 $source = ConnectionManager::create('another_test', array()); | |
273 $this->assertEqual($source, null); | |
274 | |
275 $config = array('classname' => 'DboMysql', 'filename' => 'dbo' . DS . 'dbo_mysql'); | |
276 $source = ConnectionManager::create(null, $config); | |
277 $this->assertEqual($source, null); | |
278 } | |
279 | |
280 /** | |
281 * testConnectionData method | |
282 * | |
283 * @access public | |
284 * @return void | |
285 */ | |
286 function testConnectionData() { | |
287 App::build(array( | |
288 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS), | |
289 'datasources' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'models' . DS . 'datasources' . DS) | |
290 )); | |
291 | |
292 $expected = array( | |
293 'filename' => 'test2_source', | |
294 'classname' => 'Test2Source', | |
295 'parent' => '', | |
296 'plugin' => '' | |
297 ); | |
298 | |
299 ConnectionManager::create('connection1', array('datasource' => 'Test2')); | |
300 $connections = ConnectionManager::enumConnectionObjects(); | |
301 $this->assertEqual($expected, $connections['connection1']); | |
302 | |
303 ConnectionManager::create('connection2', array('datasource' => 'Test2Source')); | |
304 $connections = ConnectionManager::enumConnectionObjects(); | |
305 $this->assertEqual($expected, $connections['connection2']); | |
306 | |
307 ConnectionManager::create('connection3', array('datasource' => 'TestPlugin.Test')); | |
308 $connections = ConnectionManager::enumConnectionObjects(); | |
309 $expected['filename'] = 'test_source'; | |
310 $expected['classname'] = 'TestSource'; | |
311 $expected['plugin'] = 'TestPlugin'; | |
312 $this->assertEqual($expected, $connections['connection3']); | |
313 | |
314 ConnectionManager::create('connection4', array('datasource' => 'TestPlugin.TestSource')); | |
315 $connections = ConnectionManager::enumConnectionObjects(); | |
316 $this->assertEqual($expected, $connections['connection4']); | |
317 | |
318 ConnectionManager::create('connection5', array('datasource' => 'Test2Other')); | |
319 $connections = ConnectionManager::enumConnectionObjects(); | |
320 $expected['filename'] = 'test2_other_source'; | |
321 $expected['classname'] = 'Test2OtherSource'; | |
322 $expected['plugin'] = ''; | |
323 $this->assertEqual($expected, $connections['connection5']); | |
324 | |
325 ConnectionManager::create('connection6', array('datasource' => 'Test2OtherSource')); | |
326 $connections = ConnectionManager::enumConnectionObjects(); | |
327 $this->assertEqual($expected, $connections['connection6']); | |
328 | |
329 ConnectionManager::create('connection7', array('datasource' => 'TestPlugin.TestOther')); | |
330 $connections = ConnectionManager::enumConnectionObjects(); | |
331 $expected['filename'] = 'test_other_source'; | |
332 $expected['classname'] = 'TestOtherSource'; | |
333 $expected['plugin'] = 'TestPlugin'; | |
334 $this->assertEqual($expected, $connections['connection7']); | |
335 | |
336 ConnectionManager::create('connection8', array('datasource' => 'TestPlugin.TestOtherSource')); | |
337 $connections = ConnectionManager::enumConnectionObjects(); | |
338 $this->assertEqual($expected, $connections['connection8']); | |
339 } | |
340 } |