| 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 |
|
public function testConfig() |
| 44 |
|
{ |
| 45 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 46 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 47 |
|
|
| 48 |
|
$config = array( |
| 49 |
1 |
'api_version' => '0.6', |
| 50 |
1 |
'adapter' => $mock, |
| 51 |
1 |
'password' => null, |
| 52 |
1 |
'passwordfile' => null, |
| 53 |
1 |
'user' => null, |
| 54 |
1 |
'verbose' => false, |
| 55 |
1 |
'User-Agent' => 'Services_Openstreetmap', |
| 56 |
|
'server' => 'http://api06.dev.openstreetmap.org/' |
| 57 |
1 |
); |
| 58 |
|
|
| 59 |
1 |
$osm = new Services_Openstreetmap($config); |
| 60 |
|
|
| 61 |
1 |
$this->assertEquals( |
| 62 |
1 |
$osm->getConfig()->asArray(), |
| 63 |
|
array ( |
| 64 |
1 |
'api_version' => '0.6', |
| 65 |
1 |
'User-Agent' => 'Services_Openstreetmap', |
| 66 |
1 |
'adapter' => $mock, |
| 67 |
1 |
'server' => 'http://api06.dev.openstreetmap.org/', |
| 68 |
1 |
'verbose' => false, |
| 69 |
1 |
'user' => null, |
| 70 |
1 |
'password' => null, |
| 71 |
1 |
'passwordfile' => null, |
| 72 |
|
) |
| 73 |
1 |
); |
| 74 |
1 |
$this->assertEquals('0.6', $osm->getConfig()->getValue('api_version')); |
| 75 |
1 |
$osm->getConfig()->setValue('User-Agent', 'Acme 1.2'); |
| 76 |
1 |
$this->assertEquals($osm->getConfig()->getValue('User-Agent'), 'Acme 1.2'); |
| 77 |
1 |
$osm->getConfig()->setValue('api_version', '0.5'); |
| 78 |
1 |
$this->assertEquals($osm->getConfig()->getValue('api_version'), '0.5'); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
/** |
| 82 |
|
* Test unknown config detection. |
| 83 |
|
* |
| 84 |
|
* @expectedException Services_Openstreetmap_Exception |
| 85 |
|
* @expectedExceptionMessage Unknown config parameter 'api' |
| 86 |
|
* |
| 87 |
|
* @return void |
| 88 |
|
*/ |
| 89 |
|
public function testUnknownConfig() |
| 90 |
|
{ |
| 91 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 92 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 93 |
|
|
| 94 |
1 |
$config = array('adapter' => $mock); |
| 95 |
1 |
$osm = new Services_Openstreetmap($config); |
| 96 |
|
|
| 97 |
1 |
$osm->getConfig()->setValue('api', '0.5'); |
| 98 |
|
} |
| 99 |
|
|
| 100 |
|
/** |
| 101 |
|
* Test unknown config detection. |
| 102 |
|
* |
| 103 |
|
* @expectedException Services_Openstreetmap_Exception |
| 104 |
|
* @expectedExceptionMessage Unknown config parameter 'api' |
| 105 |
|
* |
| 106 |
|
* @return void |
| 107 |
|
*/ |
| 108 |
|
public function testConfig3() |
| 109 |
|
{ |
| 110 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 111 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 112 |
|
|
| 113 |
1 |
$config = array('adapter' => $mock); |
| 114 |
1 |
$osm = new Services_Openstreetmap($config); |
| 115 |
|
|
| 116 |
1 |
$osm->getConfig()->getValue('api'); |
| 117 |
|
} |
| 118 |
|
|
| 119 |
|
/** |
| 120 |
|
* @expectedException Services_Openstreetmap_Exception |
| 121 |
|
* @expectedExceptionMessage Unknown config parameter 'UserAgent' |
| 122 |
|
* |
| 123 |
|
* @return void |
| 124 |
|
*/ |
| 125 |
|
public function testUnrecognisedConfig() |
| 126 |
|
{ |
| 127 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 128 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 129 |
|
|
| 130 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 131 |
1 |
$osm->getConfig()->setValue('UserAgent', 'Acme/1.2'); |
| 132 |
|
} |
| 133 |
|
|
| 134 |
|
/** |
| 135 |
|
* @expectedException Services_Openstreetmap_Exception |
| 136 |
|
* @expectedExceptionMessage Unknown config parameter 'UserAgent' |
| 137 |
|
* |
| 138 |
|
* @return void |
| 139 |
|
*/ |
| 140 |
|
public function testUnrecognisedConfigByArray() |
| 141 |
|
{ |
| 142 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 143 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 144 |
|
|
| 145 |
1 |
$osm = new Services_Openstreetmap( |
| 146 |
|
array( |
| 147 |
1 |
'adapter' => $mock, |
| 148 |
|
'UserAgent'=> 'Acme/1.2' |
| 149 |
1 |
) |
| 150 |
1 |
); |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
/** |
| 154 |
|
* Set server value via the setValue method - with scenario of |
| 155 |
|
* something wrong with the API server. |
| 156 |
|
* |
| 157 |
|
* @return void |
| 158 |
|
*/ |
| 159 |
|
public function testSetServer() |
| 160 |
|
{ |
| 161 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 162 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/404', 'rb')); |
| 163 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/404', 'rb')); |
| 164 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 165 |
|
try { |
| 166 |
1 |
$osm->getConfig()->setValue('server', 'http://example.com'); |
| 167 |
1 |
} catch (Services_Openstreetmap_Exception $ex) { |
| 168 |
1 |
$this->assertEquals( |
| 169 |
1 |
$ex->getMessage(), |
| 170 |
|
'Could not get a valid response from server' |
| 171 |
1 |
); |
| 172 |
1 |
$this->assertEquals($ex->getCode(), 404); |
| 173 |
|
} |
| 174 |
1 |
$config = $osm->getConfig()->getValue('server'); |
| 175 |
1 |
$this->assertEquals($config, 'http://example.com'); |
| 176 |
|
} |
| 177 |
|
|
| 178 |
|
/** |
| 179 |
|
* Set server value via the explicit setServer method. |
| 180 |
|
* |
| 181 |
|
* @return void |
| 182 |
|
*/ |
| 183 |
|
public function testSetServerExplicitMethod() |
| 184 |
|
{ |
| 185 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 186 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 187 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 188 |
1 |
$osm->getConfig()->setServer('http://example.com'); |
| 189 |
1 |
$config = $osm->getConfig()->getValue('server'); |
| 190 |
1 |
$this->assertEquals($config, 'http://example.com'); |
| 191 |
|
} |
| 192 |
|
|
| 193 |
|
/** |
| 194 |
|
* Set passwordfile value using the setValue method. |
| 195 |
|
* |
| 196 |
|
* @return void |
| 197 |
|
*/ |
| 198 |
|
public function testSetPasswordFile() |
| 199 |
|
{ |
| 200 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 201 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 202 |
|
|
| 203 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 204 |
1 |
$cobj = $osm->getConfig(); |
| 205 |
1 |
$cobj->setValue('passwordfile', __DIR__ . '/files/pwd_1line'); |
| 206 |
1 |
$config = $cobj->getValue('passwordfile'); |
| 207 |
1 |
$this->assertEquals($config, __DIR__ . '/files/pwd_1line'); |
| 208 |
|
} |
| 209 |
|
|
| 210 |
|
/** |
| 211 |
|
* Set passwordfile value using the setPasswordfile method. |
| 212 |
|
* |
| 213 |
|
* @return void |
| 214 |
|
*/ |
| 215 |
|
public function testSetPasswordFileExplicitMethod() |
| 216 |
|
{ |
| 217 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 218 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 219 |
|
|
| 220 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 221 |
1 |
$cobj = $osm->getConfig(); |
| 222 |
1 |
$cobj->setPasswordfile( __DIR__ . '/files/pwd_1line'); |
| 223 |
1 |
$config = $cobj->getValue('passwordfile'); |
| 224 |
1 |
$this->assertEquals($config, __DIR__ . '/files/pwd_1line'); |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
/** |
| 228 |
|
* Exception should be thrown if the password file being set doesn't exist. |
| 229 |
|
* Do this via the setValue method. |
| 230 |
|
* |
| 231 |
|
* @expectedException Services_Openstreetmap_Exception |
| 232 |
|
* @expectedExceptionMessage Could not read password file |
| 233 |
|
* |
| 234 |
|
* @return void |
| 235 |
|
*/ |
| 236 |
|
public function testSetNonExistingPasswordFile() |
| 237 |
|
{ |
| 238 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 239 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 240 |
|
|
| 241 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 242 |
1 |
$osm->getConfig()->setValue('passwordfile', __DIR__ . '/files/credentels'); |
| 243 |
|
} |
| 244 |
|
|
| 245 |
|
/** |
| 246 |
|
* Exception should be thrown if the password file being set doesn't exist |
| 247 |
|
* Do this via the explicit setPasswordfile method. |
| 248 |
|
* |
| 249 |
|
* @expectedException Services_Openstreetmap_Exception |
| 250 |
|
* @expectedExceptionMessage Could not read password file |
| 251 |
|
* |
| 252 |
|
* @return void |
| 253 |
|
*/ |
| 254 |
|
public function testSetNonExistingPasswordFileExplicitMethod() |
| 255 |
|
{ |
| 256 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 257 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 258 |
|
|
| 259 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 260 |
1 |
$osm->getConfig()->setPasswordfile(__DIR__ . '/files/credentels'); |
| 261 |
|
} |
| 262 |
|
|
| 263 |
|
/** |
| 264 |
|
* Empty password file - set with setValue |
| 265 |
|
* |
| 266 |
|
* @return void |
| 267 |
|
*/ |
| 268 |
|
public function testEmptyPasswordFile() |
| 269 |
|
{ |
| 270 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 271 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 272 |
|
|
| 273 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 274 |
1 |
$osm->getConfig()->setValue('passwordfile', __DIR__ . '/files/pwd_empty'); |
| 275 |
1 |
$config = $osm->getConfig()->getValue('passwordfile'); |
| 276 |
1 |
$this->assertEquals($config, __DIR__ . '/files/pwd_empty'); |
| 277 |
1 |
$this->assertNull($osm->getConfig()->getValue('user')); |
| 278 |
1 |
$this->assertNull($osm->getConfig()->getValue('password')); |
| 279 |
|
} |
| 280 |
|
|
| 281 |
|
/** |
| 282 |
|
* Empty password file - set with setPasswordfile |
| 283 |
|
* |
| 284 |
|
* @return void |
| 285 |
|
*/ |
| 286 |
|
public function testEmptyPasswordFileExplicitMethod() |
| 287 |
|
{ |
| 288 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 289 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 290 |
|
|
| 291 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 292 |
1 |
$osm->getConfig()->setPasswordfile(__DIR__ . '/files/pwd_empty'); |
| 293 |
1 |
$config = $osm->getConfig()->getValue('passwordfile'); |
| 294 |
1 |
$this->assertEquals($config, __DIR__ . '/files/pwd_empty'); |
| 295 |
1 |
$this->assertNull($osm->getConfig()->getValue('user')); |
| 296 |
1 |
$this->assertNull($osm->getConfig()->getValue('password')); |
| 297 |
|
} |
| 298 |
|
|
| 299 |
|
/** |
| 300 |
|
* One line password file - set with setValue |
| 301 |
|
* |
| 302 |
|
* @return void |
| 303 |
|
*/ |
| 304 |
|
public function test1LinePasswordFile() |
| 305 |
|
{ |
| 306 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 307 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 308 |
|
|
| 309 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 310 |
1 |
$osm->getConfig()->setValue('passwordfile', __DIR__ . '/files/pwd_1line'); |
| 311 |
1 |
$config = $osm->getConfig(); |
| 312 |
1 |
$this->assertEquals($config->getValue('user'), 'fred@example.com'); |
| 313 |
1 |
$this->assertEquals($config->getValue('password'), 'Wilma4evah'); |
| 314 |
|
} |
| 315 |
|
|
| 316 |
|
/** |
| 317 |
|
* One line password file - set with setPasswordfile |
| 318 |
|
* |
| 319 |
|
* @return void |
| 320 |
|
*/ |
| 321 |
|
public function test1LinePasswordFileExplicitMethod() |
| 322 |
|
{ |
| 323 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 324 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 325 |
|
|
| 326 |
1 |
$osm = new Services_Openstreetmap(array('adapter' => $mock)); |
| 327 |
1 |
$osm->getConfig()->setPasswordfile(__DIR__ . '/files/pwd_1line'); |
| 328 |
1 |
$config = $osm->getConfig(); |
| 329 |
1 |
$this->assertEquals($config->getValue('user'), 'fred@example.com'); |
| 330 |
1 |
$this->assertEquals($config->getValue('password'), 'Wilma4evah'); |
| 331 |
|
} |
| 332 |
|
|
| 333 |
|
/** |
| 334 |
|
* One line password file - set with setValue |
| 335 |
|
* |
| 336 |
|
* @return void |
| 337 |
|
*/ |
| 338 |
|
public function testMultiLinedPasswordFile() |
| 339 |
|
{ |
| 340 |
1 |
$mock = new HTTP_Request2_Adapter_Mock(); |
| 341 |
1 |
$mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb')); |
| 342 |
|
|
| 343 |
1 |
$osm = new Services_Openstreetmap( |
| 344 |
|
array( |
| 345 |
1 |
'adapter' => $mock, |
| 346 |
|
'user' => 'fred@example.com' |
| 347 |
1 |
) |
| 348 |
1 |
); |
| 349 |
1 |
$config = $osm->getConfig(); |
| 350 |
1 |
$this->assertEquals($config->getValue('password'), null); |
| 351 |
1 |
$config->setValue('passwordfile', __DIR__ . '/files/pwd_multi'); |
| 352 |
1 |
$config = $osm->getConfig(); |
| 353 |
1 |
$this->assertEquals($config->getValue('password'), 'Wilma4evah'); |
| 354 |
|
} |
| 355 |
|
|
| 356 |
|
/** |
| 357 |
|
* One line password file - set with setPasswordfile |
| 358 |
|
* |
| 359 |
|
* @return void |
| 360 |
|
*/ |
| 361 |
|
public function testMultiLinedPasswordFileExplicitMethod() |
| 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->setPasswordfile(__DIR__ . '/files/pwd_multi'); |
| 375 |
1 |
$config = $osm->getConfig(); |
| 376 |
1 |
$this->assertEquals($config->getValue('password'), 'Wilma4evah'); |
| 377 |
|
} |
| 378 |
|
} |
| 379 |
|
// vim:set et ts=4 sw=4: |
| 380 |
|
?> |