GraphHopperTest::testGeocodeWithRealIPv6()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
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\GraphHopper\Tests;
14
15
use Geocoder\IntegrationTest\BaseTestCase;
16
use Geocoder\Query\GeocodeQuery;
17
use Geocoder\Query\ReverseQuery;
18
use Geocoder\Provider\GraphHopper\GraphHopper;
19
20
/**
21
 * @author Gary Gale <[email protected]>
22
 */
23
class GraphHopperTest extends BaseTestCase
24
{
25
    protected function getCacheDir()
26
    {
27
        return __DIR__.'/.cached_responses';
28
    }
29
30
    public function testGetName()
31
    {
32
        $provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
33
        $this->assertEquals('graphhopper', $provider->getName());
34
    }
35
36
    public function testGeocodeWithRealAddress()
37
    {
38
        if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
39
            $this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
40
        }
41
42
        $provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
43
        $results = $provider->geocodeQuery(GeocodeQuery::create('242 Acklam Road, London, United Kingdom'));
44
45
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
46
        $this->assertCount(1, $results);
47
48
        /** @var \Geocoder\Model\Address $result */
49
        $result = $results->first();
50
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
51
        $this->assertEqualsWithDelta(51.521124, $result->getCoordinates()->getLatitude(), 0.01);
52
        $this->assertEqualsWithDelta(-0.20360200000000001, $result->getCoordinates()->getLongitude(), 0.01);
53
        $this->assertEquals('Acklam Road', $result->getStreetName());
54
        $this->assertEquals('London', $result->getLocality());
55
        $this->assertEquals('United Kingdom', $result->getCountry()->getName());
56
    }
57
58
    public function testGeocodeWithRealAddressAndLocale()
59
    {
60
        if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
61
            $this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
62
        }
63
64
        $provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
65
        $results = $provider->geocodeQuery(GeocodeQuery::create('242 Acklam Road, London, United Kingdom')->withLocale('fr'));
66
67
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
68
        $this->assertCount(1, $results);
69
70
        /** @var \Geocoder\Model\Address $result */
71
        $result = $results->first();
72
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
73
        $this->assertEqualsWithDelta(51.521124, $result->getCoordinates()->getLatitude(), 0.01);
74
        $this->assertEqualsWithDelta(-0.20360200000000001, $result->getCoordinates()->getLongitude(), 0.01);
75
        $this->assertEquals('Acklam Road', $result->getStreetName());
76
        $this->assertEquals('Londres', $result->getLocality());
77
        $this->assertEquals('Royaume-Uni', $result->getCountry()->getName());
78
    }
79
80
    public function testReverseWithRealCoordinates()
81
    {
82
        if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
83
            $this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
84
        }
85
86
        $provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
87
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(54.0484068, -2.7990345));
88
89
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
90
        $this->assertCount(5, $results);
91
92
        /** @var \Geocoder\Model\Address $result */
93
        $result = $results->get(1);
94
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
95
        $this->assertEqualsWithDelta(54.048411999999999, $result->getCoordinates()->getLatitude(), 0.001);
96
        $this->assertEqualsWithDelta(-2.7989549999999999, $result->getCoordinates()->getLongitude(), 0.001);
97
        $this->assertEquals('1', $result->getStreetNumber());
98
        $this->assertEquals('Gage Street', $result->getStreetName());
99
        $this->assertEquals('LA1 1UH', $result->getPostalCode());
100
        $this->assertEquals('Lancaster', $result->getLocality());
101
        $this->assertEquals('United Kingdom', $result->getCountry()->getName());
102
    }
103
104
    public function testGeocodeWithLocalhostIPv4()
105
    {
106
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
107
        $this->expectExceptionMessage('The GraphHopper provider does not support IP addresses, only street addresses.');
108
109
        $provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
110
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
111
    }
112
113
    public function testGeocodeWithLocalhostIPv6()
114
    {
115
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
116
        $this->expectExceptionMessage('The GraphHopper provider does not support IP addresses, only street addresses.');
117
118
        $provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
119
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
120
    }
121
122
    public function testGeocodeWithRealIPv4()
123
    {
124
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
125
        $this->expectExceptionMessage('The GraphHopper provider does not support IP addresses, only street addresses.');
126
127
        $provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
128
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
129
    }
130
131
    public function testGeocodeWithRealIPv6()
132
    {
133
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
134
        $this->expectExceptionMessage('The GraphHopper provider does not support IP addresses, only street addresses.');
135
136
        $provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
137
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59'));
138
    }
139
}
140