http://www.phing.info/

Source Code Coverage

Designed for use with PHPUnit, Xdebug and Phing.

Methods: 11 LOC: 349 Statements: 152
Legend: executednot executeddead code
Source file Statements Methods Total coverage
WayTest.php 100.0% 100.0% 100.0%
 
1
<?php
2
/**
3
 * Unit test class for Way related functionality.
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       WayTest.php
14
 * @todo
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
27
/**
28
 * Unit test class for manipulation of Services_OpenStreetMap_Way.
29
 *
30
 * @category   Services
31
 * @package    Services_OpenStreetMap
32
 * @subpackage UnitTesting
33
 * @author     Ken Guest <kguest@php.net>
34
 * @license    BSD http://www.opensource.org/licenses/bsd-license.php
35
 * @link       WayTest.php
36
 */
37
class WayTest extends PHPUnit_Framework_TestCase
38
{
39
40
    /**
41
     * Test retrieving a way and some tags and attributes of it too.
42
     *
43
     * @return void
44
     */
45
    public function testGetWay()
46
    {
47 1
        $id = 25978036;
48
49 1
        $mock = new HTTP_Request2_Adapter_Mock();
50 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
51 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way.xml', 'rb'));
52
53
        $config = array(
54 1
            'adapter' => $mock,
55
            'server' => 'http://api06.dev.openstreetmap.org/'
56 1
        );
57 1
        $osm = new Services_OpenStreetMap($config);
58 1
        $way = $osm->getWay($id);
59 1
        $getTags = $way->getTags();
60 1
        $this->assertEquals($id, (int) $way->getAttributes()->id);
61 1
        $this->assertEquals($getTags['highway'], 'service');
62 1
        $this->assertEquals($way->getUid(), 1379);
63 1
        $this->assertEquals($way->getVersion(), 1);
64 1
        $this->assertEquals($way->getUser(), 'AndrewMcCarthy');
65 1
        $this->assertEquals($way->getNodes(), array('283393706','283393707'));
66
    }
67
68
    /**
69
     * Test setting multiple tags to a way (or any other object)
70
     */
71
    public function testGetAddMultipleTagsToWay()
72
    {
73 1
        $id = 25978036;
74
75 1
        $mock = new HTTP_Request2_Adapter_Mock();
76 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
77 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way.xml', 'rb'));
78
79
        $config = array(
80 1
            'adapter' => $mock,
81
            'server' => 'http://api06.dev.openstreetmap.org/'
82 1
        );
83 1
        $osm = new Services_OpenStreetMap($config);
84 1
        $way = $osm->getWay($id);
85
86 1
        $getTags = $way->getTags();
87 1
        $this->assertEquals($getTags['highway'], 'service');
88 1
        $this->assertEquals($getTags, array ('highway' => 'service'));
89
90 1
        $way->setTags(array( 'service' => 'driveway' , 'surface' => 'gravel') ) ;
91 1
        $this->assertEquals(
92 1
            $way->getTags(),
93
            array (
94 1
            'highway' => 'service',
95 1
            'service' => 'driveway',
96 1
            'surface' => 'gravel',
97
            )
98 1
        );
99
100
    }
101
102
    /**
103
     * Test the isClosed method against a closed way.
104
     *
105
     * Check the 'building' tag, and id attribute too.
106
     *
107
     * @return void
108
     */
109
    public function testGetClosedWay()
110
    {
111 1
        $id = 18197393;
112
113 1
        $mock = new HTTP_Request2_Adapter_Mock();
114 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
115 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_closed.xml', 'rb'));
116
117
        $config = array(
118 1
            'adapter' => $mock,
119
            'server' => 'http://api06.dev.openstreetmap.org/'
120 1
        );
121 1
        $osm = new Services_OpenStreetMap($config);
122 1
        $way = $osm->getWay($id);
123 1
        $tags = $way->getTags();
124 1
        $this->assertEquals($id, (int) $way->getAttributes()->id);
125 1
        $this->assertEquals($tags['building'], 'yes');
126 1
        $this->assertTrue($way->isClosed());
127
    }
128
129
    /**
130
     * Test the isClosed method against an open way.
131
     *
132
     * @return void
133
     */
134
    public function testOpenWay()
135
    {
136 1
        $id = 23010474;
137
138 1
        $mock = new HTTP_Request2_Adapter_Mock();
139 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
140 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_open.xml', 'rb'));
141
142
        $config = array(
143 1
            'adapter' => $mock,
144
            'server' => 'http://api06.dev.openstreetmap.org'
145 1
        );
146 1
        $osm = new Services_OpenStreetMap($config);
147 1
        $way = $osm->getWay($id);
148 1
        $getTags = $way->getTags();
149 1
        $this->assertEquals($id, (int) $way->getAttributes()->id);
150 1
        $this->assertFalse($way->isClosed());
151
    }
152
153
    /**
154
     * A way with just one node can't be deemed closed.
155
     *
156
     * @return void
157
     */
158
    public function testWayWithOneNode()
159
    {
160 1
        $id = 23010475;
161
162 1
        $mock = new HTTP_Request2_Adapter_Mock();
163 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
164 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_one_node.xml', 'rb'));
165
166
        $config = array(
167 1
            'adapter' => $mock,
168
            'server' => 'http://api06.dev.openstreetmap.org'
169 1
        );
170 1
        $osm = new Services_OpenStreetMap($config);
171 1
        $way = $osm->getWay($id);
172 1
        $getTags = $way->getTags();
173 1
        $this->assertEquals($id, (int) $way->getAttributes()->id);
174 1
        $this->assertFalse($way->isClosed());
175
    }
176
177
    /**
178
     * Test adding nodes to a way.
179
     *
180
     * @return void
181
     */
182
    public function testAddNodeToWay()
183
    {
184 1
        $id = 23010474;
185
186 1
        $mock = new HTTP_Request2_Adapter_Mock();
187 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
188 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_open.xml', 'rb'));
189
190
        $config = array(
191 1
            'adapter' => $mock,
192
            'server' => 'http://api06.dev.openstreetmap.org'
193 1
        );
194 1
        $osm = new Services_OpenStreetMap($config);
195 1
        $way = $osm->getWay($id);
196
197 1
        $lat = 52.8638729;
198 1
        $lon = -8.1983611;
199 1
        $nodes = $way->getNodes();
200 1
        $node = $osm->createNode($lat, $lon);
201 1
        $way->addNode($node);
202 1
        $lat = $lat + 0.00002;
203 1
        $node = $osm->createNode($lat, $lon);
204 1
        $way->addNode($node);
205 1
        $this->assertEquals(sizeof($nodes) + 2, sizeof($way->getNodes()));
206
    }
207
208
    /**
209
     * Check that an exception is thrown when an incorrect identifier is used
210
     * to specify a node to remove from a way.
211
     *
212
     * @expectedException        InvalidArgumentException
213
     * @expectedExceptionMessage $node must be either an instance of
214
     *                           Services_OpenStreetMap_Node or a numeric id
215
     *
216
     * @return void
217
     */
218
    public function testIncorrectTypeToRemoveNode()
219
    {
220 1
        $id = 23010474;
221
222 1
        $mock = new HTTP_Request2_Adapter_Mock();
223 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
224 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_open.xml', 'rb'));
225
226
        $config = array(
227 1
            'adapter' => $mock,
228
            'server' => 'http://api06.dev.openstreetmap.org'
229 1
        );
230 1
        $osm = new Services_OpenStreetMap($config);
231 1
        $way = $osm->getWay($id);
232 1
        $way->removeNode('way5432456');
233
    }
234
235
    /**
236
     * Remove a node from a way.
237
     *
238
     * @return void
239
     */
240
    public function testRemoveNode()
241
    {
242 1
        $id = 23010474;
243
244 1
        $mock = new HTTP_Request2_Adapter_Mock();
245 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
246 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_open.xml', 'rb'));
247
248
        $config = array(
249 1
            'adapter' => $mock,
250
            'server' => 'http://api06.dev.openstreetmap.org'
251 1
        );
252 1
        $osm = new Services_OpenStreetMap($config);
253 1
        $way = $osm->getWay($id);
254 1
        $nb = count($way->getNodes());
255 1
        $way->removeNode(248081798);
256 1
        $way->setTag('note', 'testing...');
257 1
        $na = count($way->getNodes());
258 1
        $this->assertEquals($na, $nb - 1);
259
    }
260
261
    /**
262
     * Retrieve multiple (2) ways.
263
     *
264
     * @return void
265
     */
266
    public function testGetWays()
267
    {
268 1
        $wayId = 30357328;
269 1
        $way2Id = 30357329;
270
271 1
        $mock = new HTTP_Request2_Adapter_Mock();
272 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
273 1
        $mock->addResponse(
274 1
            fopen(__DIR__ . '/responses/way_30357328_30357329.xml', 'rb')
275 1
        );
276
        $config = array(
277 1
            'adapter'  => $mock,
278 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
279 1
        );
280 1
        $osm = new Services_OpenStreetMap($config);
281 1
        $ways = $osm->getWays($wayId, $way2Id);
282 1
        foreach ($ways as $key=>$way) {
283 1
            $this->assertEquals($way, $ways[$key]);
284 1
        }
285
    }
286
287
    /**
288
     * Test retrieving address tags from a way.
289
     *
290
     * @return void
291
     */
292
    public function testWayWithAddressSet()
293
    {
294 1
        $id = 75490756;
295
296 1
        $mock = new HTTP_Request2_Adapter_Mock();
297 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
298 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_75490756.xml', 'rb'));
299
300
        $config = array(
301 1
            'adapter' => $mock,
302
            'server' => 'http://api06.dev.openstreetmap.org'
303 1
        );
304 1
        $osm = new Services_OpenStreetMap($config);
305 1
        $way = $osm->getWay($id);
306
        $address = array(
307 1
            'addr_housename' => null,
308 1
            'addr_housenumber' => '20-21',
309 1
            'addr_street' => 'Pearse Street',
310 1
            'addr_city' => 'Nenagh',
311 1
            'addr_country' => 'IE',
312 1
        );
313 1
        $this->assertEquals($address, $way->getAddress());
314
    }
315
316
    /**
317
     * Test retrieving relations which refer to a specific way.
318
     *
319
     * @return void
320
     */
321
    public function testWayBackRelations()
322
    {
323 1
        $mock = new HTTP_Request2_Adapter_Mock();
324 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
325 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_5850969.xml', 'rb'));
326 1
        $mock->addResponse(
327 1
            fopen(
328 1
                __DIR__ . '/responses/way_5850969_relations.xml',
329
                'rb'
330 1
            )
331 1
        );
332
333
        $config = array(
334 1
            'adapter' => $mock,
335
            'server' => 'http://api06.dev.openstreetmap.org'
336 1
        );
337
338 1
        $osm = new Services_OpenStreetMap($config);
339
340 1
        $relations = $osm->getWay(5850969)->getRelations();
341 1
        $this->assertInstanceOf('Services_OpenStreetMap_Relations', $relations);
342 1
        $this->assertEquals(2, sizeof($relations));
343 1
        $this->assertEquals($relations[0]->getTag('name'), 'Dublin Bus route 14');
344 1
        $this->assertEquals($relations[1]->getTag('name'), 'Dublin Bus route 75');
345
    }
346
}
347
348
// vim:set et ts=4 sw=4:
349
?>


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