http://www.phing.info/

Source Code Coverage

Designed for use with PHPUnit, Xdebug and Phing.

Methods: 15 LOC: 540 Statements: 287
Legend: executednot executeddead code
Source file Statements Methods Total coverage
OSMTest.php 100.0% 100.0% 100.0%
 
1
<?php
2
/**
3
 * OSMTest.php
4
 * 25-Apr-2011
5
 *
6
 * PHP Version 5
7
 *
8
 * @category   Services
9
 * @package    Services_OpenStreetMap
10
 * @subpackage UnitTesting
11
 * @author     Ken Guest <kguest@php.net>
12
 * @license    BSD http://www.opensource.org/licenses/bsd-license.php
13
 * @version    Release: @package_version@
14
 * @link       OSMTest.php
15
 */
16
17
$version = '@package_version@';
18
if (strstr($version, 'package_version')) {
19
    set_include_path(dirname(dirname(__FILE__)) . ':' . get_include_path());
20
}
21
22
require_once 'Services/OpenStreetMap.php';
23
24
require_once 'HTTP/Request2.php';
25
require_once 'HTTP/Request2/Adapter/Mock.php';
26
require_once 'PHPUnit/Framework/TestCase.php';
27
28
29
/**
30
 * Test Services_OpenStreetMap functionality specific only to that class.
31
 *
32
 * @category   Services
33
 * @package    Services_OpenStreetMap
34
 * @subpackage UnitTesting
35
 * @author     Ken Guest <kguest@php.net>
36
 * @license    BSD http://www.opensource.org/licenses/bsd-license.php
37
 * @link       OSMTest.php
38
 */
39
class OSMTest extends PHPUnit_Framework_TestCase
40
{
41
    /**
42
     * Check that a Services_OpenStreetMap object can be created ok.
43
     *
44
     * @return void
45
     */
46
    public function testCreateObject()
47
    {
48 1
        $mock = new HTTP_Request2_Adapter_Mock();
49 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
50
51 1
        $osm = new Services_OpenStreetMap(array('adapter' => $mock));
52 1
        $this->assertInstanceOf('Services_OpenStreetMap', $osm);
53
    }
54
55
    /**
56
     * Test that an OpenStreetMap XML datafile can be loaded via the loadXml method.
57
     *
58
     * @return void
59
     */
60
    public function testLoadXml()
61
    {
62 1
        $mock = new HTTP_Request2_Adapter_Mock();
63 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
64
65 1
        $osm = new Services_OpenStreetMap(array('adapter' => $mock));
66 1
        $this->assertEquals($osm->getXml(), null);
67 1
        $osm->loadXml(__DIR__ . '/files/osm.osm');
68 1
        $this->assertNotEquals($osm->getXml(), null);
69
    }
70
71
    /**
72
     * Test parsing of capability data.
73
     *
74
     * @return void
75
     */
76
    public function testCapabilities()
77
    {
78 1
        $mock = new HTTP_Request2_Adapter_Mock();
79 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
80
81
        $config = array(
82 1
            'adapter' => $mock,
83 1
            'server' => 'http://api06.dev.openstreetmap.org/',
84 1
        );
85 1
        $osm = new Services_OpenStreetMap($config);
86 1
        $this->assertEquals($osm->getTimeout(), 300);
87
    }
88
89
    /**
90
     * Test parsing of capability data.
91
     *
92
     * @return void
93
     */
94
    public function testCapabilities2()
95
    {
96 1
        $mock = new HTTP_Request2_Adapter_Mock();
97 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities2.xml', 'rb'));
98
99
        $config = array(
100 1
            'adapter' => $mock,
101 1
            'server' => 'http://api06.dev.openstreetmap.org/',
102 1
        );
103 1
        $osm = new Services_OpenStreetMap($config);
104 1
        $this->assertEquals($osm->getMinVersion(), 0.5);
105 1
        $this->assertEquals($osm->getMaxVersion(), 0.6);
106 1
        $this->assertEquals($osm->getMaxArea(), 0.25);
107 1
        $this->assertEquals($osm->getTracepointsPerPage(), 5000);
108 1
        $this->assertEquals($osm->getMaxNodes(), 2000);
109 1
        $this->assertEquals($osm->getMaxElements(), 50000);
110
    }
111
112
    /**
113
     * If the minimum version supported by the server is greater than what this
114
     * package supports then an exception should be thrown.
115
     *
116
     * @expectedException Services_OpenStreetMap_Exception
117
     * @expectedExceptionMessage Specified API Version 0.6 not supported.
118
     *
119
     * @return void
120
     */
121
    public function testCapabilitiesMin()
122
    {
123 1
        $mock = new HTTP_Request2_Adapter_Mock();
124 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities_min.xml', 'rb'));
125
126
        $config = array(
127 1
            'adapter' => $mock,
128 1
            'server' => 'http://api06.dev.openstreetmap.org/',
129 1
        );
130 1
        $osm = new Services_OpenStreetMap($config);
131
    }
132
133
    /**
134
     * If the maximum version supported by the server is lower than a version
135
     * supported by this package, then an exception should be thrown.
136
     *
137
     * @expectedException        Services_OpenStreetMap_Exception
138
     * @expectedExceptionMessage Specified API Version 0.6 not supported.
139
     *
140
     * @return void
141
     */
142
    public function testCapabilitiesMax()
143
    {
144 1
        $mock = new HTTP_Request2_Adapter_Mock();
145 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities_max.xml', 'rb'));
146
147
        $config = array(
148 1
            'adapter' => $mock,
149 1
            'server' => 'http://api06.dev.openstreetmap.org/',
150 1
        );
151 1
        $osm = new Services_OpenStreetMap($config);
152
    }
153
154
    /**
155
     * If invalid/no capabilities are retrieving an exception should be thrown.
156
     *
157
     * @expectedException        Services_OpenStreetMap_Exception
158
     * @expectedExceptionMessage Problem checking server capabilities
159
     *
160
     * @return void
161
     */
162
    public function testCapabilitiesInvalid()
163
    {
164 1
        $mock = new HTTP_Request2_Adapter_Mock();
165 1
        $mock->addResponse(
166 1
            fopen(__DIR__ . '/responses/capabilities_invalid.xml', 'rb')
167 1
        );
168
169
        $config = array(
170 1
            'adapter' => $mock,
171 1
            'server' => 'http://api06.dev.openstreetmap.org/',
172 1
        );
173 1
        $osm = new Services_OpenStreetMap($config);
174
    }
175
176
    /**
177
     * Test retrieving data covering an area.
178
     *
179
     * @return void
180
     */
181
    public function testGetArea()
182
    {
183 1
        $mock = new HTTP_Request2_Adapter_Mock();
184 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
185 1
        $mock->addResponse(fopen(__DIR__ . '/responses/area.xml', 'rb'));
186
187
        $config = array(
188 1
            'adapter' => $mock,
189
            'server' => 'http://api06.dev.openstreetmap.org/'
190 1
        );
191 1
        $osm = new Services_OpenStreetMap($config);
192 1
        $results = $osm->search(array('amenity' => 'pharmacy'));
193 1
        $this->AssertTrue(empty($results));
194 1
        $osm->get(
195 1
            52.84824191354071, -8.247245026639696,
196 1
            52.89957825532213, -8.174161478654796
197 1
        );
198 1
        $results = $osm->search(array('amenity' => 'pharmacy'));
199
200 1
        $tags = array();
201 1
        foreach ($results as $result) {
202 1
            $tags[] = $result->getTags();
203 1
        }
204
205 1
        $this->assertEquals(
206 1
            $tags,
207
            array (
208
                0 => array (
209 1
                    'addr:city' => 'Nenagh',
210 1
                    'addr:country' => 'IE',
211 1
                    'addr:housename' => '20-21',
212 1
                    'addr:street' => 'Pearse Street',
213 1
                    'amenity' => 'pharmacy',
214 1
                    'building' => 'yes',
215 1
                    'building:levels' => '3',
216 1
                    'building:use' => 'retail',
217 1
                    'dispensing' => 'yes',
218 1
                    'fax' => '+353 67 34540',
219 1
                    'name' => 'Ryans Pharmacy and Beauty Salon',
220 1
                    'phone' => '+353 67 31464',
221 1
                ),
222
                1 => array (
223 1
                    'addr:city' => 'Nenagh',
224 1
                    'addr:country' => 'IE',
225 1
                    'addr:housename' => '7',
226 1
                    'addr:street' => 'Pearse Street',
227 1
                    'amenity' => 'pharmacy',
228 1
                    'building' => 'yes',
229 1
                    'dispensing' => 'yes',
230 1
                    'name' => 'Ray Walsh',
231 1
                    'opening_hours' => 'Mo-Fr 09:30-19:00',
232 1
                    'phone' => '+353 67 31249',
233 1
                    'shop' => 'chemist',
234 1
                ),
235
                2 => array (
236 1
                    'addr:city' => 'Nenagh',
237 1
                    'addr:country' => 'IE',
238 1
                    'addr:housename' => '20-21',
239 1
                    'addr:street' => 'Pearse Street',
240 1
                    'amenity' => 'pharmacy',
241 1
                    'building' => 'yes',
242 1
                    'building:levels' => '3',
243 1
                    'building:use' => 'retail',
244 1
                    'dispensing' => 'yes',
245 1
                    'fax' => '+353 67 34540',
246 1
                    'name' => 'Ryans Pharmacy and Beauty Salon',
247 1
                    'phone' => '+353 67 31464',
248 1
                ),
249
                3 => array (
250 1
                    'addr:city' => 'Nenagh',
251 1
                    'addr:country' => 'IE',
252 1
                    'addr:housenumber' => 'Unit 1A',
253 1
                    'addr:street' => 'O\'Connors Shopping Centre',
254 1
                    'amenity' => 'pharmacy',
255 1
                    'name' => 'Ann Kelly\'s',
256
                    'opening_hours' =>
257 1
                        'Mo-Th 09:00-18:00; Fr 09:00-19:30; Sa 09:00-18:00',
258 1
                    'phone' => '+353 67 34244',
259 1
                ),
260
                4 => array (
261 1
                    'addr:city' => 'Nenagh',
262 1
                    'addr:country' => 'IE',
263 1
                    'addr:housename' => '7',
264 1
                    'addr:street' => 'Mitchell Street',
265 1
                    'amenity' => 'pharmacy',
266 1
                    'dispensing' => 'yes',
267 1
                    'name' => 'Guierins',
268 1
                    'phone' => '+353 67 31447',
269 1
                    ),
270
                5 => array (
271 1
                    'addr:city' => 'Nenagh',
272 1
                    'addr:country' => 'IE',
273 1
                    'addr:housenumber' => '69',
274 1
                    'addr:street' => 'Kenyon Street',
275 1
                    'amenity' => 'pharmacy',
276 1
                    'dispensing' => 'yes',
277 1
                    'name' => 'Finnerty\'s',
278 1
                    'phone' => '+353 67 34155',
279 1
                ),
280
                6 => array (
281 1
                    'addr:city' => 'Nenagh',
282 1
                    'addr:country' => 'IE',
283 1
                    'addr:housenumber' => '67',
284 1
                    'addr:street' => 'Kenyon Street',
285 1
                    'amenity' => 'pharmacy',
286 1
                    'name' => 'Cuddys',
287 1
                    'phone' => '+353 67 31262',
288 1
                ),
289
                7 => array (
290 1
                    'addr:city' => 'Nenagh',
291 1
                    'addr:country' => 'IE',
292 1
                    'addr:street' => 'Clare Street',
293 1
                    'amenity' => 'pharmacy',
294 1
                    'dispensing' => 'yes',
295 1
                    'fax' => '+3536742775',
296 1
                    'name' => 'Clare Street Pharmacy',
297 1
                    'opening_hours' => 'Mo-Sa 09:15-18:00',
298 1
                    'phone' => '+3536742775',
299 1
                ),
300
            )
301 1
        );
302
    }
303
304
    public function testGetReturnValue()
305
    {
306 1
        $mock = new HTTP_Request2_Adapter_Mock();
307 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
308 1
        $mock->addResponse(fopen(__DIR__ . '/responses/area.xml', 'rb'));
309
310
        $config = array(
311 1
            'adapter' => $mock,
312
            'server' => 'http://api06.dev.openstreetmap.org/'
313 1
        );
314 1
        $osm = new Services_OpenStreetMap($config);
315 1
        $results = $osm->search(array('amenity' => 'pharmacy'));
316 1
        $this->AssertTrue(empty($results));
317 1
        $xml = $osm->get(
318 1
            52.84824191354071, -8.247245026639696,
319 1
            52.89957825532213, -8.174161478654796
320 1
        );
321 1
        $xml1 = $osm->getXml();
322 1
        $this->assertEquals($xml, $xml1);
323
    }
324
325
    /**
326
     * Test searching for a value where it is part of a semicolon delimited
327
     * string.
328
     *
329
     * @return void
330
     */
331
    public function testSearchDelimited()
332
    {
333 1
        $mock = new HTTP_Request2_Adapter_Mock();
334 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
335 1
        $mock->addResponse(fopen(__DIR__ . '/responses/area.xml', 'rb'));
336
337
        $config = array(
338 1
            'adapter' => $mock,
339
            'server' => 'http://api06.dev.openstreetmap.org/'
340 1
        );
341 1
        $osm = new Services_OpenStreetMap($config);
342 1
        $results = $osm->search(array('amenity' => 'pharmacy'));
343 1
        $this->AssertTrue(empty($results));
344 1
        $osm->get(
345 1
            52.84824191354071, -8.247245026639696,
346 1
            52.89957825532213, -8.174161478654796
347 1
        );
348 1
        $results = $osm->search(array('amenity' => 'restaurant'));
349
350 1
        $tags = array();
351 1
        foreach ($results as $result) {
352 1
            $tags[] = $result->getTags();
353 1
        }
354
355 1
        $this->assertEquals(
356 1
            $tags,
357
            array (
358
                0 =>
359
                array (
360 1
                    'addr:city' => 'Nenagh',
361 1
                    'addr:country' => 'IE',
362 1
                    'addr:housenumber' => '19',
363 1
                    'addr:street' => 'Pearse Street',
364 1
                    'amenity' => 'restaurant',
365 1
                    'building' => 'yes',
366 1
                    'building:levels' => '3',
367 1
                ),
368
                1 =>
369
                array (
370 1
                    'addr:city' => 'Nenagh',
371 1
                    'addr:country' => 'IE',
372 1
                    'addr:housenumber' => '26',
373 1
                    'addr:street' => 'Kenyon Street',
374 1
                    'amenity' => 'restaurant',
375 1
                    'name' => 'The Peppermill',
376 1
                ),
377
                2 =>
378
                array (
379 1
                    'amenity' => 'restaurant',
380 1
                    'cuisine' => 'italian',
381 1
                    'name' => 'Pepe\'s Restaurant',
382 1
                ),
383
                3 =>
384
                array (
385 1
                    'addr:city' => 'Nenagh',
386 1
                    'addr:country' => 'IE',
387 1
                    'addr:housenumber' => '19',
388 1
                    'addr:street' => 'Kenyon Street',
389 1
                    'amenity' => 'restaurant',
390 1
                    'name' => 'Simply Food',
391 1
                ),
392
                4 =>
393
                array (
394 1
                    'amenity' => 'restaurant',
395 1
                    'cuisine' => 'chinese',
396 1
                    'name' => 'Jin\'s',
397 1
                ),
398
                5 =>
399
                array (
400 1
                    'addr:city' => 'Nenagh',
401 1
                    'addr:country' => 'IE',
402 1
                    'addr:housenumber' => '23',
403 1
                    'addr:street' => 'Sarsfield Street',
404 1
                    'amenity' => 'pub;restaurant',
405 1
                    'name' => 'Andy\'s',
406 1
                    'phone' => '+353 67 32494',
407 1
                    'tourism' => 'guest_house',
408 1
                    'website' => 'http://www.andysnenagh.com',
409 1
                ),
410
                6 =>
411
                array (
412 1
                    'amenity' => 'restaurant',
413 1
                    'cuisine' => 'chinese',
414 1
                    'name' => 'Golden Star',
415 1
                    'opening_hours' => 'Mo-Su 17:00-24:00',
416 1
                ),
417
                7 =>
418
                array (
419 1
                    'amenity' => 'restaurant',
420 1
                    'cuisine' => 'indian',
421 1
                    'email' => 'turbanrest@gmail.com',
422 1
                    'name' => 'Turban',
423 1
                    'opening_hours' => 'Mo-Su 16:30-23:00; Fr,Sa 16:30-23:30',
424 1
                    'phone' => '+353 67 42794',
425 1
                ),
426
            )
427 1
        );
428
    }
429
430
    /**
431
     * test the  getCoordsOfPlace method.
432
     *
433
     * @return void
434
     */
435
    public function testGetCoordsOfPlace()
436
    {
437 1
        $mock = new HTTP_Request2_Adapter_Mock();
438 1
        $mock->addResponse(
439 1
            fopen(__DIR__ . '/responses/nominatim_search_limerick.xml', 'rb')
440 1
        );
441
442 1
        $osm = new Services_OpenStreetMap(array('adapter' => $mock));
443 1
        $this->AssertEquals(
444 1
            $osm->getCoordsOfPlace('Limerick, Ireland'),
445 1
            array('lat'=> '52.6612577', 'lon'=> '-8.6302084')
446 1
        );
447
    }
448
449
    /**
450
     * An exception should be thrown if the place of interest can not be
451
     * found.
452
     *
453
     * @expectedException Services_OpenStreetMap_Exception
454
     * @expectedExceptionMessage Could not get coords for Neeenaaa, Ireland
455
     *
456
     * @return void
457
     */
458
    public function testGetCoordsOfNonExistentPlace()
459
    {
460 1
        $mock = new HTTP_Request2_Adapter_Mock();
461 1
        $mock->addResponse(
462 1
            fopen(
463 1
                __DIR__ . '/responses/nominatim_search_neeenaaa.xml',
464
                'rb'
465 1
            )
466 1
        );
467
468 1
        $osm = new Services_OpenStreetMap(array('adapter' => $mock));
469 1
        $osm->getCoordsOfPlace('Neeenaaa, Ireland');
470
    }
471
472
    /**
473
     * test retrieving the history of an object.
474
     *
475
     * @return void
476
     */
477
    public function testGetHistory()
478
    {
479 1
        $id = 52245107;
480
481 1
        $mock = new HTTP_Request2_Adapter_Mock();
482 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
483 1
        $mock->addResponse(fopen(__DIR__ . '/responses/node.xml', 'rb'));
484 1
        $mock->addResponse(fopen(__DIR__ . '/responses/node_history.xml', 'rb'));
485
486
        $config = array(
487 1
            'adapter' => $mock,
488
            'server' => 'http://api06.dev.openstreetmap.org'
489 1
        );
490 1
        $osm = new Services_OpenStreetMap($config);
491 1
        $node = $osm->getNode($id);
492 1
        $history = $node->history();
493 1
        foreach ($history as $key=>$version) {
494 1
            $this->assertEquals($version, $history[$key]);
495 1
            $this->assertEquals($id, $version->getId());
496 1
        }
497
    }
498
499
    /**
500
     * test the bboxToMinMax method
501
     *
502
     * @return void
503
     */
504
    public function testBboxToMinMax()
505
    {
506 1
        $mock = new HTTP_Request2_Adapter_Mock();
507 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
508
509 1
        $config = array('adapter' => $mock);
510 1
        $osm = new Services_OpenStreetMap($config);
511 1
        $this->assertEquals(
512 1
            $osm->bboxToMinMax(
513 1
                '0.0327873', '52.260074599999996',
514 1
                '0.0767326', '52.282047299999995'
515 1
            ),
516
            array(
517 1
                '52.260074599999996', '0.0327873',
518 1
                '52.282047299999995', '0.0767326',
519
            )
520 1
        );
521
    }
522
523
524
    /**
525
     * Test default value of attributes when creating an object.
526
     *
527
     * @return void
528
     */
529
    public function testAttribsNotSet()
530
    {
531 1
        $node = new Services_OpenStreetMap_Node();
532 1
        $this->assertEquals($node->getVersion(), null);
533 1
        $this->assertEquals($node->getUser(), null);
534 1
        $this->assertEquals($node->getUid(), null);
535 1
        $this->assertEquals($node->getId(), null);
536 1
        $this->assertEquals('' . $node, '');
537
    }
538
}
539
// vim:set et ts=4 sw=4:
540
?>


Report generated at 2012-10-02T18:40:35+01:00