Completed
Push — master ( d97cbc...a4ad66 )
by Tobias
06:24
created

NominatimTest::testReverseWithCoordinatesGetsError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Provider\Nominatim\Tests;
14
15
use Geocoder\Collection;
16
use Geocoder\IntegrationTest\BaseTestCase;
17
use Geocoder\Location;
18
use Geocoder\Query\GeocodeQuery;
19
use Geocoder\Query\ReverseQuery;
20
use Geocoder\Provider\Nominatim\Nominatim;
21
22
class NominatimTest extends BaseTestCase
23
{
24
    protected function getCacheDir()
25
    {
26
        return __DIR__.'/.cached_responses';
27
    }
28
29
30
    public function testGeocodeWithLocalhostIPv4()
31
    {
32
        $provider = Nominatim::withOpenStreetMapServer($this->getMockedHttpClient());
33
        $results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
34
35
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
36
        $this->assertCount(1, $results);
37
38
        /** @var Location $result */
39
        $result = $results->first();
40
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
41
        $this->assertEquals('localhost', $result->getLocality());
42
        $this->assertEmpty($result->getAdminLevels());
43
        $this->assertEquals('localhost', $result->getCountry()->getName());
44
    }
45
46
    /**
47
     * @expectedException \Geocoder\Exception\UnsupportedOperation
48
     * @expectedExceptionMessage The Nominatim provider does not support IPv6 addresses.
49
     */
50
    public function testGeocodeWithLocalhostIPv6()
51
    {
52
        $provider = Nominatim::withOpenStreetMapServer($this->getMockedHttpClient());
53
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
54
    }
55
56
    /**
57
     * @expectedException \Geocoder\Exception\UnsupportedOperation
58
     * @expectedExceptionMessage The Nominatim provider does not support IPv6 addresses.
59
     */
60
    public function testGeocodeWithRealIPv6()
61
    {
62
        $provider = Nominatim::withOpenStreetMapServer($this->getHttpClient());
63
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:88.188.221.14'));
64
    }
65
66
    /**
67
     * @expectedException \Geocoder\Exception\InvalidServerResponse
68
     */
69
    public function testGeocodeWithAddressGetsEmptyContent()
70
    {
71
        $provider = Nominatim::withOpenStreetMapServer($this->getMockedHttpClient('<foo></foo>'));
72
        $provider->geocodeQuery(GeocodeQuery::create('Läntinen Pitkäkatu 35, Turku'));
73
    }
74
75
    /**
76
     * @expectedException \Geocoder\Exception\InvalidServerResponse
77
     */
78
    public function testGeocodeWithAddressGetsEmptyXML()
79
    {
80
        $emptyXML = <<<'XML'
81
<?xml version="1.0" encoding="utf-8"?><searchresults_empty></searchresults_empty>
82
XML;
83
        $provider = Nominatim::withOpenStreetMapServer($this->getMockedHttpClient($emptyXML));
84
        $provider->geocodeQuery(GeocodeQuery::create('Läntinen Pitkäkatu 35, Turku'));
85
    }
86
87
    public function testReverseWithCoordinatesGetsError()
88
    {
89
        $errorXml = <<<'XML'
90
<?xml version="1.0" encoding="UTF-8" ?>
91
<reversegeocode querystring='format=xml&amp;lat=-80.000000&amp;lon=-170.000000&amp;addressdetails=1'>
92
    <error>Unable to geocode</error>
93
</reversegeocode>
94
XML;
95
        $provider = Nominatim::withOpenStreetMapServer($this->getMockedHttpClient($errorXml));
96
        $result = $provider->reverseQuery(ReverseQuery::fromCoordinates(-80.000000, -170.000000));
97
98
        $this->assertInstanceOf(Collection::class, $result);
99
        $this->assertEquals(0, $result->count());
100
    }
101
102
    public function testGetNodeStreetName()
103
    {
104
        $provider = Nominatim::withOpenStreetMapServer($this->getHttpClient());
105
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.86, 2.35));
106
107
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
108
        $this->assertCount(1, $results);
109
110
        /** @var Location $result */
111
        $result = $results->first();
112
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
113
        $this->assertEquals('Rue Quincampoix', $result->getStreetName());
114
    }
115
}
116