http://www.phing.info/

Source Code Coverage

Designed for use with PHPUnit, Xdebug and Phing.

Methods: 12 LOC: 567 Statements: 313
Legend: executednot executeddead code
Source file Statements Methods Total coverage
ChangesetTest.php 100.0% 25.0% 97.2%
   
1
<?php
2
/**
3
 * Unit test class for Changeset 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       ChangesetTest.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
/**
29
 * Unit test class for Changeset related functionality.
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       ChangesetTest.php
37
 */
38
class ChangesetTest extends PHPUnit_Framework_TestCase
39
{
40
    protected $credentialsFile = '/credentials';
41
42
    /**
43
     * Setup - if there is a 'credentials' file, use it. Otherwise fall back to
44
     * using the credentials.dist file.
45
     *
46
     * @return void
47
     */
48
    public function setup()
49
    {
50 11
        if (file_exists(__DIR__ . $this->credentialsFile)) {
51 11
            $this->credentialsFile = __DIR__ . $this->credentialsFile;
52 11
        } else {
53
            $this->credentialsFile = __DIR__ . '/credentials.dist';
54
        }
55
    }
56
57
    /**
58
     * Retrieve a changeset and check its attributes are as expected.
59
     *
60
     * @return void
61
     */
62
    public function testGetChangeset()
63
    {
64 1
        $mock = new HTTP_Request2_Adapter_Mock();
65 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
66 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset.xml', 'rb'));
67
68 1
        $cId = 2217466;
69
70
        $config = array(
71 1
            'adapter' => $mock,
72
            'server' => 'http://api06.dev.openstreetmap.org'
73 1
        );
74 1
        $osm = new Services_OpenStreetMap($config);
75 1
        $changeset = $osm->getChangeSet($cId);
76 1
        $this->assertEquals($cId, (int) $changeset->getId());
77 1
        $this->assertEquals("2009-08-20T22:31:06Z", $changeset->getCreatedAt());
78 1
        $this->assertEquals("2009-08-20T22:31:08Z", $changeset->getClosedAt());
79 1
        $this->assertEquals(false, $changeset->isOpen());
80 1
        $this->assertEquals("-8.2205445", $changeset->getMinLon());
81 1
        $this->assertEquals("52.857758", $changeset->getMinLat());
82 1
        $this->assertEquals("-8.2055278", $changeset->getMaxLon());
83 1
        $this->assertEquals("52.8634333", $changeset->getMaxLat());
84
    }
85
86
    /**
87
     * An exception should be thrown when starting a changeset without having a
88
     * password set.
89
     *
90
     * @expectedException        Services_OpenStreetMap_RuntimeException
91
     * @expectedExceptionMessage Password must be set
92
     *
93
     * @return void
94
     */
95
    public function testPasswordNotSet()
96
    {
97 1
        $mock = new HTTP_Request2_Adapter_Mock();
98 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
99 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
100
        $config = array(
101 1
            'adapter'  => $mock,
102 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
103
            'user' => 'fred@example.com'
104 1
        );
105 1
        $osm = new Services_OpenStreetMap($config);
106
        try {
107 1
            $changeset = $osm->createChangeset();
108 1
        } catch (Services_OpenStreetMap_Exception $e) {
109
        }
110 1
        $this->assertEquals(false, $changeset->isOpen());
111 1
        $changeset->begin('Start a changeset');
112
    }
113
114
    /**
115
     * An exception should be thrown when starting a changeset without having a
116
     * [valid] username set.
117
     *
118
     * @expectedException        Services_OpenStreetMap_RuntimeException
119
     * @expectedExceptionMessage User must be set
120
     *
121
     * @return void
122
     */
123
    public function testUserNotSet()
124
    {
125 1
        $mock = new HTTP_Request2_Adapter_Mock();
126 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
127 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
128
        $config = array(
129 1
            'adapter'  => $mock,
130 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
131
            'password' => 'wilma4evah'
132 1
        );
133 1
        $osm = new Services_OpenStreetMap($config);
134
        try {
135 1
            $changeset = $osm->createChangeset();
136 1
        } catch (Services_OpenStreetMap_Exception $e) {
137
        }
138 1
        $this->assertEquals(false, $changeset->isOpen());
139 1
        $changeset->begin('Undo accidental highway change');
140
    }
141
142
    /**
143
     * A successful run through making changes to some ways and committing
144
     * them.
145
     *
146
     * @return void
147
     */
148
    public function testChange()
149
    {
150 1
        $wayId = 30357328;
151 1
        $way2Id = 30357329;
152
153 1
        if (!file_exists($this->credentialsFile)) {
154
            $this->markTestSkipped('Credentials file does not exist.');
155
        }
156
157 1
        $mock = new HTTP_Request2_Adapter_Mock();
158 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
159 1
        $mock->addResponse(
160 1
            fopen(__DIR__ . '/responses/way_30357328_30357329.xml', 'rb')
161 1
        );
162 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
163 1
        $mock->addResponse(
164 1
            fopen(__DIR__ . '/responses/diff_30357328_30357329.xml', 'rb')
165 1
        );
166 1
        $mock->addResponse(
167 1
            fopen(__DIR__ . '/responses/changeset_closed', 'rb')
168 1
        );
169
170
        $config = array(
171 1
            'adapter'  => $mock,
172 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
173 1
            'passwordfile' => $this->credentialsFile
174 1
        );
175 1
        $osm = new Services_OpenStreetMap($config);
176
        try {
177 1
            $changeset = $osm->createChangeset();
178 1
        } catch (Services_OpenStreetMap_Exception $e) {
179
        }
180 1
        $this->assertEquals(false, $changeset->isOpen());
181 1
        $ways = $osm->getWays($wayId, $way2Id);
182 1
        foreach ($ways as $way) {
183 1
            $tags = $way->getTags();
184 1
            if ($tags['highway'] == 'residential') {
185 1
                return;
186 1
            }
187
        }
188
        $this->assertEquals(2, count($ways));
189
        $changeset->begin(
190
            'Undo accidental highway change from residential to service'
191
        );
192
        foreach ($ways as $way) {
193
            $way->setTag('highway', 'residential');
194
            $way->setTag('lit', 'yes');
195
            $changeset->add($way);
196
        }
197
        $this->assertEquals(true, $changeset->isOpen());
198
        $changeset->commit();
199
    }
200
201
    /**
202
     * Test that an object can not be added to a closed changeset.
203
     * A changeset is closed after it has been committed.
204
     *
205
     * @expectedException Services_OpenStreetMap_RuntimeException
206
     * @expectedExceptionMessage Object added to closed changeset
207
     *
208
     * @return void
209
     */
210
    public function testObjectAddedToChangesetAfterCommit()
211
    {
212 1
        if (!file_exists($this->credentialsFile)) {
213
            $this->markTestSkipped('Credentials file does not exist.');
214
        }
215
216 1
        $wayId = 30357328;
217 1
        $way2Id = 30357329;
218
219 1
        $mock = new HTTP_Request2_Adapter_Mock();
220 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
221 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
222 1
        $mock->addResponse(
223 1
            fopen(__DIR__ . '/responses/way_30357328_30357329.xml', 'rb')
224 1
        );
225 1
        $mock->addResponse(
226 1
            fopen(__DIR__ . '/responses/diff_30357328_30357329.xml', 'rb')
227 1
        );
228 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_closed', 'rb'));
229
230
        $config = array(
231 1
            'adapter'  => $mock,
232 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
233 1
            'passwordfile' => $this->credentialsFile
234 1
        );
235 1
        $osm = new Services_OpenStreetMap($config);
236
        try {
237 1
            $changeset = $osm->createChangeset();
238 1
        } catch (Services_OpenStreetMap_Exception $e) {
239
        }
240 1
        $this->assertEquals(false, $changeset->isOpen());
241 1
        $ways = $osm->getWays($wayId, $way2Id);
242 1
        $changeset->begin(
243
            'Undo accidental highway change from residential to service'
244 1
        );
245 1
        foreach ($ways as $way) {
246
            $way->setTag('highway', 'residential');
247
            $way->setTag('lit', 'yes');
248
            $changeset->add($way);
249 1
        }
250 1
        $this->assertEquals(true, $changeset->isOpen());
251 1
        $changeset->commit();
252 1
        $lat = 52.8638729;
253 1
        $lon = -8.1983611;
254 1
        $tags = array('building' => 'yes', 'amenity' => 'vet');
255 1
        $node = $osm->createNode($lat, $lon, $tags);
256 1
        $changeset->add($node);
257
    }
258
259
    /**
260
     * Test that the same object can not be added to the same changeset.
261
     *
262
     * @expectedException Services_OpenStreetMap_RuntimeException
263
     * @expectedExceptionMessage Object added to changeset already
264
     *
265
     * @return void
266
     */
267
    public function testSameObjectAddedToChangeset()
268
    {
269 1
        if (!file_exists($this->credentialsFile)) {
270
            $this->markTestSkipped('Credentials file does not exist.');
271
        }
272
273 1
        $wayId = 30357328;
274
275 1
        $mock = new HTTP_Request2_Adapter_Mock();
276 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
277 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_30357328.xml', 'rb'));
278 1
        $mock->addResponse(fopen(__DIR__ . '/responses/way_30357328.xml', 'rb'));
279 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
280 1
        $mock->addResponse(
281 1
            fopen(__DIR__ . '/responses/diff_30357328_30357329.xml', 'rb')
282 1
        );
283 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_closed', 'rb'));
284
285
        $config = array(
286 1
            'adapter'  => $mock,
287 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
288 1
            'passwordfile' => $this->credentialsFile
289 1
        );
290 1
        $osm = new Services_OpenStreetMap($config);
291
        try {
292 1
            $changeset = $osm->createChangeset();
293 1
        } catch (Services_OpenStreetMap_Exception $e) {
294
        }
295 1
        $way = $osm->getWay($wayId);
296 1
        $way->setTag('highway', 'residential');
297 1
        $way->setTag('lit', 'yes');
298 1
        $this->assertNotEquals('' . $way, '');
299 1
        $way2 = $osm->getWay($wayId);
300 1
        $way2->setTag('highway', 'residential');
301 1
        $way2->setTag('lit', 'yes');
302 1
        $this->assertNotEquals('' . $way2, '');
303 1
        $this->assertEquals(false, $changeset->isOpen());
304 1
        $changeset->begin(
305
            'Undo accidental highway change from residential to service'
306 1
        );
307 1
        $changeset->add($way);
308 1
        $changeset->add($way2);
309
    }
310
311
    /**
312
     * Test deleting a node - including an 'accidental' second commit...
313
     *
314
     * @expectedException Services_OpenStreetMap_Exception
315
     * @expectedExceptionMessage Attempt to commit a closed changeset
316
     *
317
     * @return void
318
     */
319
    public function testDeleteNode()
320
    {
321 1
        if (!file_exists($this->credentialsFile)) {
322
            $this->markTestSkipped('Credentials file does not exist.');
323
        }
324
325 1
        $nodeID = 1436433375;
326
327 1
        $mock = new HTTP_Request2_Adapter_Mock();
328 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
329 1
        $mock->addResponse(fopen(__DIR__ . '/responses/node_1436433375.xml', 'rb'));
330 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
331 1
        $mock->addResponse(
332 1
            fopen(__DIR__ . '/responses/diff_1436433375_deleted.xml', 'rb')
333 1
        );
334 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_closed', 'rb'));
335 1
        $mock->addResponse(fopen(__DIR__ . '/responses/410', 'rb'));
336
337
        $config = array(
338 1
            'adapter'  => $mock,
339 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
340 1
            'passwordfile' => $this->credentialsFile
341 1
        );
342 1
        $osm = new Services_OpenStreetMap($config);
343
        try {
344 1
            $changeset = $osm->createChangeset();
345 1
        } catch (Services_OpenStreetMap_Exception $e) {
346
            echo  $e->getMessage();
347
            return;
348
        }
349 1
        $node = $osm->getNode($nodeID);
350 1
        $this->assertTrue($node != false);
351 1
        $changeset->begin("Delete unrequired node.");
352 1
        $changeset->add($node->delete());
353 1
        $this->assertEquals(true, $changeset->isOpen());
354 1
        $changeset->commit();
355 1
        $changeset->commit();
356
        $node = $osm->getNode($nodeID);
357
        $this->assertFalse($node);
358
    }
359
360
    /**
361
     * If a 404 error occurs while closing a changeset [during a commit] then
362
     * an exception should be thrown. Test for this.
363
     *
364
     * @expectedException        Services_OpenStreetMap_Exception
365
     * @expectedExceptionMessage Error closing changeset
366
     *
367
     * @return void
368
     */
369
    public function testDeleteNodeClosingError404()
370
    {
371 1
        if (!file_exists($this->credentialsFile)) {
372
            $this->markTestSkipped('Credentials file does not exist.');
373
        }
374
375 1
        $nodeID = 1436433375;
376
377 1
        $mock = new HTTP_Request2_Adapter_Mock();
378 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
379 1
        $mock->addResponse(fopen(__DIR__ . '/responses/node_1436433375.xml', 'rb'));
380 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
381 1
        $mock->addResponse(
382 1
            fopen(__DIR__ . '/responses/diff_1436433375_deleted.xml', 'rb')
383 1
        );
384 1
        $mock->addResponse(fopen(__DIR__ . '/responses/404', 'rb'));
385
386
        $config = array(
387 1
            'adapter'  => $mock,
388 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
389 1
            'passwordfile' => $this->credentialsFile
390 1
        );
391 1
        $osm = new Services_OpenStreetMap($config);
392
        try {
393 1
            $changeset = $osm->createChangeset();
394 1
        } catch (Services_OpenStreetMap_Exception $e) {
395
            echo  $e->getMessage();
396
            return;
397
        }
398 1
        $node = $osm->getNode($nodeID);
399 1
        $this->assertTrue($node != false);
400 1
        $changeset->begin("Delete unrequired node.");
401 1
        $changeset->add($node->delete());
402 1
        $this->assertEquals(true, $changeset->isOpen());
403 1
        $changeset->commit();
404
    }
405
406
    /**
407
     * If an error occurs while closing a changeset [during a commit] then
408
     * an exception should be thrown. Test for this.
409
     *
410
     * @expectedException Services_OpenStreetMap_Exception
411
     * @xpectedExceptionMessage Error closing changeset
412
     *
413
     * @return void
414
     */
415
    public function testDeleteNodeClosingError400()
416
    {
417 1
        if (!file_exists($this->credentialsFile)) {
418
            $this->markTestSkipped('Credentials file does not exist.');
419
        }
420
421 1
        $nodeID = 1436433375;
422
423 1
        $mock = new HTTP_Request2_Adapter_Mock();
424 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
425 1
        $mock->addResponse(
426 1
            fopen(
427 1
                __DIR__ . '/responses/node_1436433375.xml',
428
                'rb'
429 1
            )
430 1
        );
431 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
432 1
        $mock->addResponse(
433 1
            fopen(
434 1
                __DIR__ . '/responses/diff_1436433375_deleted.xml',
435
                'rb'
436 1
            )
437 1
        );
438 1
        $mock->addResponse(fopen(__DIR__ . '/responses/400', 'rb'));
439
440
        $config = array(
441 1
            'adapter'  => $mock,
442 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
443 1
            'passwordfile' => $this->credentialsFile
444 1
        );
445 1
        $osm = new Services_OpenStreetMap($config);
446
        try {
447 1
            $changeset = $osm->createChangeset();
448 1
        } catch (Services_OpenStreetMap_Exception $e) {
449
            echo  $e->getMessage();
450
            return;
451
        }
452 1
        $node = $osm->getNode($nodeID);
453 1
        $this->assertTrue($node != false);
454 1
        $changeset->begin("Delete unrequired node.");
455 1
        $changeset->add($node->delete());
456 1
        $this->assertEquals(true, $changeset->isOpen());
457 1
        $changeset->commit();
458
    }
459
460
    /**
461
     * If an error occurs while posting changeset information to the server an
462
     * exception should be thrown.
463
     *
464
     * @expectedException        Services_OpenStreetMap_Exception
465
     * @expectedExceptionMessage Error posting changeset
466
     *
467
     * @return void
468
     */
469
    public function testDeleteNodeDiffError400()
470
    {
471 1
        if (!file_exists($this->credentialsFile)) {
472
            $this->markTestSkipped('Credentials file does not exist.');
473
        }
474
475 1
        $nodeID = 1436433375;
476
477 1
        $mock = new HTTP_Request2_Adapter_Mock();
478 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
479 1
        $mock->addResponse(fopen(__DIR__ . '/responses/node_1436433375.xml', 'rb'));
480 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
481 1
        $mock->addResponse(fopen(__DIR__ . '/responses/400', 'rb'));
482
483
        $config = array(
484 1
            'adapter'  => $mock,
485 1
            'server'   => 'http://api06.dev.openstreetmap.org/',
486 1
            'passwordfile' => $this->credentialsFile
487 1
        );
488 1
        $osm = new Services_OpenStreetMap($config);
489
        try {
490 1
            $changeset = $osm->createChangeset();
491 1
        } catch (Services_OpenStreetMap_Exception $e) {
492
            echo  $e->getMessage();
493
            return;
494
        }
495 1
        $node = $osm->getNode($nodeID);
496 1
        $this->assertTrue($node != false);
497 1
        $changeset->begin("Delete unrequired node.");
498 1
        $changeset->add($node->delete());
499 1
        $this->assertEquals(true, $changeset->isOpen());
500 1
        $changeset->commit();
501
    }
502
503
    /**
504
     * Open a new changeset, create a node and save it by associating it with
505
     * that changeset and then committing.
506
     * The id of the node should change from -1 to a positive integer/double.
507
     *
508
     * @return void
509
     */
510
    public function testSaveNode()
511
    {
512 1
        if (!file_exists($this->credentialsFile)) {
513
            $this->markTestSkipped('Credentials file does not exist.');
514
        }
515
516 1
        $mock = new HTTP_Request2_Adapter_Mock();
517 1
        $mock->addResponse(fopen(__DIR__ . '/responses/capabilities.xml', 'rb'));
518 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_id', 'rb'));
519 1
        $mock->addResponse(fopen(__DIR__ . '/responses/diff_create_node.xml', 'rb'));
520 1
        $mock->addResponse(fopen(__DIR__ . '/responses/changeset_closed', 'rb'));
521
        $config = array(
522 1
                'adapter'  => $mock,
523 1
                'server'   => 'http://api06.dev.openstreetmap.org/',
524 1
                'passwordfile' => $this->credentialsFile
525 1
                );
526 1
        $osm = new Services_OpenStreetMap($config);
527 1
        $lat = 52.8638729;
528 1
        $lon = -8.1983611;
529 1
        $node = $osm->createNode(
530 1
            $lat,
531 1
            $lon,
532
            array(
533 1
                'building' => 'yes',
534
                'amenity' => 'vet'
535 1
        )
536 1
        );
537 1
        $this->assertEquals(
538 1
            $node->getTags(),
539
            array(
540 1
                'created_by' => 'Services_OpenStreetMap',
541 1
                'building' => 'yes',
542 1
                'amenity' => 'vet',
543
            )
544 1
        );
545 1
        $this->assertEquals($lat, $node->getlat());
546 1
        $this->assertEquals($lon, $node->getlon());
547 1
        $node->setTag('amenity', 'veterinary')->setTag('name', 'O\'Kennedy');
548 1
        $this->assertEquals(
549 1
            $node->getTags(),
550
            array(
551 1
                'created_by' => 'Services_OpenStreetMap',
552 1
                'building' => 'yes',
553 1
                'amenity' => 'veterinary',
554
                'name' => 'O\'Kennedy'
555 1
            )
556 1
        );
557 1
        $this->assertEquals(-1, $node->getId());
558
559 1
        $changeset = $osm->createChangeset();
560 1
        $changeset->begin("Add O'Kennedy vets in Nenagh");
561 1
        $changeset->add($node);
562 1
        $changeset->commit();
563 1
        $this->assertEquals($node->getId(), 1448499623);
564
    }
565
}
566
// vim:set et ts=4 sw=4:
567
?>


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