1 |
|
<?php |
2 |
|
/** |
3 |
|
* Unit testing for Services_OpenStreetMap_Config class. |
4 |
|
* |
5 |
|
* PHP Version 5 |
6 |
|
* |
7 |
|
* @category Services |
8 |
|
* @package Services_OpenStreetMap |
9 |
|
* @subpackage UnitTesting |
10 |
|
* @author Ken Guest <kguest@php.net> |
11 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
12 |
|
* @version Release: @package_version@ |
13 |
|
* @link ConfigTest.php |
14 |
|
*/ |
15 |
|
|
16 |
|
$version = '@package_version@'; |
17 |
|
if (strstr($version, 'package_version')) { |
18 |
|
set_include_path(dirname(dirname(__FILE__)) . ':' . get_include_path()); |
19 |
|
} |
20 |
|
|
21 |
|
require_once 'Services/OpenStreetMap.php'; |
22 |
|
|
23 |
|
require_once 'HTTP/Request2.php'; |
24 |
|
require_once 'HTTP/Request2/Adapter/Mock.php'; |
25 |
|
require_once 'PHPUnit/Framework/TestCase.php'; |
26 |
|
|
27 |
|
/** |
28 |
|
* Test Services_OpenStreetMap_Config functionality and how it's used |
29 |
|
* throughout the Services_OpenStreetMap package. |
30 |
|
* |
31 |
|
* @category Services |
32 |
|
* @package Services_OpenStreetMap |
33 |
|
* @subpackage UnitTesting |
34 |
|
* @author Ken Guest <kguest@php.net> |
35 |
|
* @license BSD http://www.opensource.org/licenses/bsd-license.php |
36 |
|
* @link ConfigTest.php |
37 |
|
*/ |
38 |
|
class ConfigTest extends PHPUnit_Framework_TestCase |
39 |
|
{ |
40 |
|
/** |
41 |
|
* General test of the Config class |
42 |
|
* |
43 |
|
* @return void |
44 |
|
*/ |
45 |
|
public function testConfig() |
46 |
|
{ |
47 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
48 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
49 |
|
|
50 |
|
$config = array( |
51 |
1 |
'api_version' => '0.6', |
52 |
1 |
'adapter' => $mock, |
53 |
1 |
'password' => null, |
54 |
1 |
'passwordfile' => null, |
55 |
1 |
'user' => null, |
56 |
1 |
'verbose' => false, |
57 |
1 |
'User-Agent' => 'Services_OpenStreetMap', |
58 |
|
'server' => 'http://api06.dev.openstreetmap.org/' |
59 |
1 |
); |
60 |
|
|
61 |
1 |
$osm = new Services_OpenStreetMap($config); |
62 |
|
|
63 |
1 |
$this->assertEquals( |
64 |
1 |
$osm->getConfig()->asArray(), |
65 |
|
array ( |
66 |
1 |
'api_version' => '0.6', |
67 |
1 |
'User-Agent' => 'Services_OpenStreetMap', |
68 |
1 |
'adapter' => $mock, |
69 |
1 |
'server' => 'http://api06.dev.openstreetmap.org/', |
70 |
1 |
'verbose' => false, |
71 |
1 |
'user' => null, |
72 |
1 |
'password' => null, |
73 |
1 |
'passwordfile' => null, |
74 |
|
) |
75 |
1 |
); |
76 |
1 |
$this->assertEquals('0.6', $osm->getConfig()->getValue('api_version')); |
77 |
1 |
$osm->getConfig()->setValue('User-Agent', 'Acme 1.2'); |
78 |
1 |
$this->assertEquals($osm->getConfig()->getValue('User-Agent'), 'Acme 1.2'); |
79 |
1 |
$osm->getConfig()->setValue('api_version', '0.5'); |
80 |
1 |
$this->assertEquals($osm->getConfig()->getValue('api_version'), '0.5'); |
81 |
|
} |
82 |
|
|
83 |
|
/** |
84 |
|
* Test unknown config detection. |
85 |
|
* |
86 |
|
* @expectedException Services_OpenStreetMap_InvalidArgumentException |
87 |
|
* @expectedExceptionMessage Unknown config parameter 'api' |
88 |
|
* |
89 |
|
* @return void |
90 |
|
*/ |
91 |
|
public function testUnknownConfig() |
92 |
|
{ |
93 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
94 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
95 |
|
|
96 |
1 |
$config = array('adapter' => $mock); |
97 |
1 |
$osm = new Services_OpenStreetMap($config); |
98 |
|
|
99 |
1 |
$osm->getConfig()->setValue('api', '0.5'); |
100 |
|
} |
101 |
|
|
102 |
|
/** |
103 |
|
* Test unknown config detection. |
104 |
|
* |
105 |
|
* @expectedException Services_OpenStreetMap_InvalidArgumentException |
106 |
|
* @expectedExceptionMessage Unknown config parameter 'api' |
107 |
|
* |
108 |
|
* @return void |
109 |
|
*/ |
110 |
|
public function testConfig3() |
111 |
|
{ |
112 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
113 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
114 |
|
|
115 |
1 |
$config = array('adapter' => $mock); |
116 |
1 |
$osm = new Services_OpenStreetMap($config); |
117 |
|
|
118 |
1 |
$osm->getConfig()->getValue('api'); |
119 |
|
} |
120 |
|
|
121 |
|
public function testGetValueEmptyParameter() |
122 |
|
{ |
123 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
124 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
125 |
|
|
126 |
1 |
$config = array('adapter' => $mock); |
127 |
1 |
$osm = new Services_OpenStreetMap($config); |
128 |
|
|
129 |
1 |
$configValues = $osm->getConfig()->getValue(); |
130 |
1 |
$this->assertEquals($configValues['server'], 'http://api.openstreetmap.org/'); |
131 |
1 |
$this->assertEquals($configValues['api_version'], '0.6'); |
132 |
1 |
$this->assertEquals($configValues['User-Agent'], 'Services_OpenStreetMap'); |
133 |
1 |
$this->assertNull($configValues['user']); |
134 |
1 |
$this->assertNull($configValues['password']); |
135 |
1 |
$this->assertNull($configValues['passwordfile']); |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
|
* Setting an unrecognised config setting should raise an exception. |
140 |
|
* |
141 |
|
* @expectedException Services_OpenStreetMap_InvalidArgumentException |
142 |
|
* @expectedExceptionMessage Unknown config parameter 'UserAgent' |
143 |
|
* |
144 |
|
* @return void |
145 |
|
*/ |
146 |
|
public function testUnrecognisedConfig() |
147 |
|
{ |
148 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
149 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
150 |
|
|
151 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
152 |
1 |
$osm->getConfig()->setValue('UserAgent', 'Acme/1.2'); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
|
* Try the same, this time with the config settings in an array. |
157 |
|
* |
158 |
|
* @expectedException Services_OpenStreetMap_InvalidArgumentException |
159 |
|
* @expectedExceptionMessage Unknown config parameter 'UserAgent' |
160 |
|
* |
161 |
|
* @return void |
162 |
|
*/ |
163 |
|
public function testUnrecognisedConfigByArray() |
164 |
|
{ |
165 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
166 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
167 |
|
|
168 |
1 |
$osm = new Services_OpenStreetMap( |
169 |
|
array( |
170 |
1 |
'adapter' => $mock, |
171 |
|
'UserAgent'=> 'Acme/1.2' |
172 |
1 |
) |
173 |
1 |
); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
|
* Set server value via the setValue method - with scenario of |
178 |
|
* something wrong with the API server. |
179 |
|
* |
180 |
|
* @return void |
181 |
|
*/ |
182 |
|
public function testSetServer() |
183 |
|
{ |
184 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
185 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/404', 'rb')); |
186 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/404', 'rb')); |
187 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
188 |
|
try { |
189 |
1 |
$osm->getConfig()->setValue('server', 'http://example.com'); |
190 |
1 |
} catch (Services_OpenStreetMap_Exception $ex) { |
191 |
1 |
$this->assertEquals( |
192 |
1 |
$ex->getMessage(), |
193 |
|
'Could not get a valid response from server' |
194 |
1 |
); |
195 |
1 |
$this->assertEquals($ex->getCode(), 404); |
196 |
|
} |
197 |
1 |
$config = $osm->getConfig()->getValue('server'); |
198 |
1 |
$this->assertEquals($config, 'http://example.com'); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
|
* Set server value via the explicit setServer method. |
203 |
|
* |
204 |
|
* @return void |
205 |
|
*/ |
206 |
|
public function testSetServerExplicitMethod() |
207 |
|
{ |
208 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
209 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
210 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
211 |
1 |
$osm->getConfig()->setServer('http://example.com'); |
212 |
1 |
$config = $osm->getConfig()->getValue('server'); |
213 |
1 |
$this->assertEquals($config, 'http://example.com'); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
217 |
|
* Set passwordfile value using the setValue method. |
218 |
|
* |
219 |
|
* @return void |
220 |
|
*/ |
221 |
|
public function testSetPasswordFile() |
222 |
|
{ |
223 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
224 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
225 |
|
|
226 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
227 |
1 |
$cobj = $osm->getConfig(); |
228 |
1 |
$cobj->setValue('passwordfile', __DIR__ . '/files/pwd_1line'); |
229 |
1 |
$config = $cobj->getValue('passwordfile'); |
230 |
1 |
$this->assertEquals($config, __DIR__ . '/files/pwd_1line'); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
|
* Set passwordfile value using the setPasswordfile method. |
235 |
|
* |
236 |
|
* @return void |
237 |
|
*/ |
238 |
|
public function testSetPasswordFileExplicitMethod() |
239 |
|
{ |
240 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
241 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
242 |
|
|
243 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
244 |
1 |
$cobj = $osm->getConfig(); |
245 |
1 |
$cobj->setPasswordfile(__DIR__ . '/files/pwd_1line'); |
246 |
1 |
$config = $cobj->getValue('passwordfile'); |
247 |
1 |
$this->assertEquals($config, __DIR__ . '/files/pwd_1line'); |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
|
* Exception should be thrown if the password file being set doesn't exist. |
252 |
|
* Do this via the setValue method. |
253 |
|
* |
254 |
|
* @expectedException Services_OpenStreetMap_Exception |
255 |
|
* @expectedExceptionMessage Could not read password file |
256 |
|
* |
257 |
|
* @return void |
258 |
|
*/ |
259 |
|
public function testSetNonExistingPasswordFile() |
260 |
|
{ |
261 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
262 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
263 |
|
|
264 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
265 |
1 |
$osm->getConfig()->setValue('passwordfile', __DIR__ . '/files/credentels'); |
266 |
|
} |
267 |
|
|
268 |
|
/** |
269 |
|
* Exception should be thrown if the password file being set doesn't exist |
270 |
|
* Do this via the explicit setPasswordfile method. |
271 |
|
* |
272 |
|
* @expectedException Services_OpenStreetMap_Exception |
273 |
|
* @expectedExceptionMessage Could not read password file |
274 |
|
* |
275 |
|
* @return void |
276 |
|
*/ |
277 |
|
public function testSetNonExistingPasswordFileExplicitMethod() |
278 |
|
{ |
279 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
280 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
281 |
|
|
282 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
283 |
1 |
$osm->getConfig()->setPasswordfile(__DIR__ . '/files/credentels'); |
284 |
|
} |
285 |
|
|
286 |
|
/** |
287 |
|
* Empty password file - set with setValue |
288 |
|
* |
289 |
|
* @return void |
290 |
|
*/ |
291 |
|
public function testEmptyPasswordFile() |
292 |
|
{ |
293 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
294 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
295 |
|
|
296 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
297 |
1 |
$osm->getConfig()->setValue('passwordfile', __DIR__ . '/files/pwd_empty'); |
298 |
1 |
$config = $osm->getConfig()->getValue('passwordfile'); |
299 |
1 |
$this->assertEquals($config, __DIR__ . '/files/pwd_empty'); |
300 |
1 |
$this->assertNull($osm->getConfig()->getValue('user')); |
301 |
1 |
$this->assertNull($osm->getConfig()->getValue('password')); |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
|
* Empty password file - set with setPasswordfile |
306 |
|
* |
307 |
|
* @return void |
308 |
|
*/ |
309 |
|
public function testEmptyPasswordFileExplicitMethod() |
310 |
|
{ |
311 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
312 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
313 |
|
|
314 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
315 |
1 |
$osm->getConfig()->setPasswordfile(__DIR__ . '/files/pwd_empty'); |
316 |
1 |
$config = $osm->getConfig()->getValue('passwordfile'); |
317 |
1 |
$this->assertEquals($config, __DIR__ . '/files/pwd_empty'); |
318 |
1 |
$this->assertNull($osm->getConfig()->getValue('user')); |
319 |
1 |
$this->assertNull($osm->getConfig()->getValue('password')); |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
|
* One line password file - set with setValue |
324 |
|
* |
325 |
|
* @return void |
326 |
|
*/ |
327 |
|
public function test1LinePasswordFile() |
328 |
|
{ |
329 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
330 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
331 |
|
|
332 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
333 |
1 |
$osm->getConfig()->setValue('passwordfile', __DIR__ . '/files/pwd_1line'); |
334 |
1 |
$config = $osm->getConfig(); |
335 |
1 |
$this->assertEquals($config->getValue('user'), 'fred@example.com'); |
336 |
1 |
$this->assertEquals($config->getValue('password'), 'Wilma4evah'); |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
|
* One line password file - set with setPasswordfile |
341 |
|
* |
342 |
|
* @return void |
343 |
|
*/ |
344 |
|
public function test1LinePasswordFileExplicitMethod() |
345 |
|
{ |
346 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
347 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
348 |
|
|
349 |
1 |
$osm = new Services_OpenStreetMap(array('adapter' => $mock)); |
350 |
1 |
$osm->getConfig()->setPasswordfile(__DIR__ . '/files/pwd_1line'); |
351 |
1 |
$config = $osm->getConfig(); |
352 |
1 |
$this->assertEquals($config->getValue('user'), 'fred@example.com'); |
353 |
1 |
$this->assertEquals($config->getValue('password'), 'Wilma4evah'); |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
|
* One line password file - set with setValue |
358 |
|
* |
359 |
|
* @return void |
360 |
|
*/ |
361 |
|
public function testMultiLinedPasswordFile() |
362 |
|
{ |
363 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
364 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
365 |
|
|
366 |
1 |
$osm = new Services_OpenStreetMap( |
367 |
|
array( |
368 |
1 |
'adapter' => $mock, |
369 |
|
'user' => 'fred@example.com' |
370 |
1 |
) |
371 |
1 |
); |
372 |
1 |
$config = $osm->getConfig(); |
373 |
1 |
$this->assertEquals($config->getValue('password'), null); |
374 |
1 |
$config->setValue('passwordfile', __DIR__ . '/files/pwd_multi'); |
375 |
1 |
$config = $osm->getConfig(); |
376 |
1 |
$this->assertEquals($config->getValue('password'), 'Wilma4evah'); |
377 |
|
} |
378 |
|
|
379 |
|
/** |
380 |
|
* One line password file - set with setPasswordfile |
381 |
|
* |
382 |
|
* @return void |
383 |
|
*/ |
384 |
|
public function testMultiLinedPasswordFileExplicitMethod() |
385 |
|
{ |
386 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
387 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
388 |
|
|
389 |
1 |
$osm = new Services_OpenStreetMap( |
390 |
|
array( |
391 |
1 |
'adapter' => $mock, |
392 |
|
'user' => 'fred@example.com' |
393 |
1 |
) |
394 |
1 |
); |
395 |
1 |
$config = $osm->getConfig(); |
396 |
1 |
$this->assertEquals($config->getValue('password'), null); |
397 |
1 |
$config->setPasswordfile(__DIR__ . '/files/pwd_multi'); |
398 |
1 |
$config = $osm->getConfig(); |
399 |
1 |
$this->assertEquals($config->getValue('password'), 'Wilma4evah'); |
400 |
|
} |
401 |
|
|
402 |
|
public function testGenerator() |
403 |
|
{ |
404 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
405 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
406 |
|
|
407 |
|
$config = array( |
408 |
1 |
'api_version' => '0.6', |
409 |
1 |
'adapter' => $mock, |
410 |
1 |
'password' => null, |
411 |
1 |
'passwordfile' => null, |
412 |
1 |
'user' => null, |
413 |
1 |
'verbose' => false, |
414 |
1 |
'User-Agent' => 'Services_OpenStreetMap', |
415 |
|
'server' => 'http://api06.dev.openstreetmap.org/' |
416 |
1 |
); |
417 |
1 |
$osm = new Services_OpenStreetMap($config); |
418 |
1 |
$generator = $osm->getConfig()->getGenerator(); |
419 |
1 |
$this->assertEquals($generator, 'OpenStreetMap server'); |
420 |
|
|
421 |
1 |
$mock2 = new HTTP_Request2_Adapter_Mock(); |
422 |
1 |
$mock2->addResponse(fopen(__DIR__ . '/responses/capabilities_jxapi.xml', 'rb')); |
423 |
|
|
424 |
1 |
$config['adapter'] = $mock2; |
425 |
1 |
$osm = new Services_OpenStreetMap($config); |
426 |
1 |
$generator = $osm->getConfig()->getGenerator(); |
427 |
1 |
$this->assertEquals($generator, 'Java XAPI Server'); |
428 |
|
} |
429 |
|
} |
430 |
|
// vim:set et ts=4 sw=4: |
431 |
|
?> |