-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy pathGraphHopperTest.php
212 lines (176 loc) · 9 KB
/
GraphHopperTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider\GraphHopper\Tests;
use Geocoder\Exception\InvalidServerResponse;
use Geocoder\IntegrationTest\BaseTestCase;
use Geocoder\Model\Bounds;
use Geocoder\Provider\GraphHopper\GraphHopper;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Psr\Http\Message\RequestInterface;
/**
* @author Gary Gale <[email protected]>
*/
class GraphHopperTest extends BaseTestCase
{
protected function getCacheDir(): string
{
return __DIR__.'/.cached_responses';
}
public function testGetName(): void
{
$provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
$this->assertEquals('graphhopper', $provider->getName());
}
public function testGeocodeWithRealAddress(): void
{
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
}
$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
$results = $provider->geocodeQuery(GeocodeQuery::create('242 Acklam Road, London, United Kingdom'));
$this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results);
$this->assertCount(1, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf(\Geocoder\Model\Address::class, $result);
$this->assertEqualsWithDelta(51.521124, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-0.20360200000000001, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Acklam Road', $result->getStreetName());
$this->assertEquals('London', $result->getLocality());
$this->assertEquals('United Kingdom', $result->getCountry()->getName());
}
public function testGeocodeWithRealAddressAndLocale(): void
{
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
}
$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
$results = $provider->geocodeQuery(GeocodeQuery::create('242 Acklam Road, London, United Kingdom')->withLocale('fr'));
$this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results);
$this->assertCount(1, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf(\Geocoder\Model\Address::class, $result);
$this->assertEqualsWithDelta(51.521124, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-0.20360200000000001, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Acklam Road', $result->getStreetName());
$this->assertEquals('Londres', $result->getLocality());
$this->assertEquals('Royaume-Uni', $result->getCountry()->getName());
}
public function testGeocodeInsideBounds(): void
{
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
}
$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
$results = $provider->geocodeQuery(
GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
->withLocale('fr')
->withBounds(new Bounds(50, -10, 55, 10))
);
$this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results);
$this->assertCount(1, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf(\Geocoder\Model\Address::class, $result);
$this->assertEqualsWithDelta(51.521124, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-0.20360200000000001, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Acklam Road', $result->getStreetName());
$this->assertEquals('Londres', $result->getLocality());
$this->assertEquals('Royaume-Uni', $result->getCountry()->getName());
}
public function testGeocodeOutsideBounds(): void
{
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
}
$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
$results = $provider->geocodeQuery(
GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
->withLocale('fr')
->withBounds(new Bounds(20, 10, 30, 20))
);
$this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results);
$this->assertCount(0, $results);
}
public function testCorrectlyAppendsProvider(): void
{
$uri = '';
$provider = new GraphHopper(
$this->getMockedHttpClientCallback(
function (RequestInterface $request) use (&$uri) {
$uri = (string) $request->getUri();
}
),
'api_key'
);
$query = GeocodeQuery::create('242 Acklam Road, London, United Kingdom')
->withLocale('fr')
->withData('provider', 'default');
try {
$provider->geocodeQuery($query);
} catch (InvalidServerResponse $e) {
}
$this->assertEquals('https://graphhopper.com/api/1/geocode'.
'?q=242+Acklam+Road%2C+London%2C+United+Kingdom'.
'&key=api_key&locale=fr&limit=5&provider=default',
$uri
);
}
public function testReverseWithRealCoordinates(): void
{
if (!isset($_SERVER['GRAPHHOPPER_API_KEY'])) {
$this->markTestSkipped('You need to configure the GRAPHHOPPER_API_KEY value in phpunit.xml.');
}
$provider = new GraphHopper($this->getHttpClient($_SERVER['GRAPHHOPPER_API_KEY']), $_SERVER['GRAPHHOPPER_API_KEY']);
$results = $provider->reverseQuery(ReverseQuery::fromCoordinates(54.0484068, -2.7990345));
$this->assertInstanceOf(\Geocoder\Model\AddressCollection::class, $results);
$this->assertCount(5, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->get(1);
$this->assertInstanceOf(\Geocoder\Model\Address::class, $result);
$this->assertEqualsWithDelta(54.048411999999999, $result->getCoordinates()->getLatitude(), 0.001);
$this->assertEqualsWithDelta(-2.7989549999999999, $result->getCoordinates()->getLongitude(), 0.001);
$this->assertEquals('1', $result->getStreetNumber());
$this->assertEquals('Gage Street', $result->getStreetName());
$this->assertEquals('LA1 1UH', $result->getPostalCode());
$this->assertEquals('Lancaster', $result->getLocality());
$this->assertEquals('United Kingdom', $result->getCountry()->getName());
}
public function testGeocodeWithLocalhostIPv4(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The GraphHopper provider does not support IP addresses, only street addresses.');
$provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
$provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
}
public function testGeocodeWithLocalhostIPv6(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The GraphHopper provider does not support IP addresses, only street addresses.');
$provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
$provider->geocodeQuery(GeocodeQuery::create('::1'));
}
public function testGeocodeWithRealIPv4(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The GraphHopper provider does not support IP addresses, only street addresses.');
$provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
$provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
}
public function testGeocodeWithRealIPv6(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The GraphHopper provider does not support IP addresses, only street addresses.');
$provider = new GraphHopper($this->getMockedHttpClient(), 'api_key');
$provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59'));
}
}