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\OpenRouteService\Tests; |
14
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
16
|
|
|
use Geocoder\IntegrationTest\BaseTestCase; |
17
|
|
|
use Geocoder\Provider\OpenRouteService\OpenRouteService; |
18
|
|
|
use Geocoder\Query\GeocodeQuery; |
19
|
|
|
use Geocoder\Query\ReverseQuery; |
20
|
|
|
|
21
|
|
|
class OpenRouteServiceTest extends BaseTestCase |
22
|
|
|
{ |
23
|
|
|
protected function getCacheDir() |
24
|
|
|
{ |
25
|
|
|
return __DIR__.'/.cached_responses'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testGetName() |
29
|
|
|
{ |
30
|
|
|
$provider = new OpenRouteService($this->getMockedHttpClient(), 'api_key'); |
31
|
|
|
$this->assertEquals('openrouteservice', $provider->getName()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGeocode() |
35
|
|
|
{ |
36
|
|
|
$provider = new OpenRouteService($this->getMockedHttpClient('{}'), 'api_key'); |
37
|
|
|
$result = $provider->geocodeQuery(GeocodeQuery::create('foobar')); |
38
|
|
|
|
39
|
|
|
$this->assertInstanceOf(Collection::class, $result); |
40
|
|
|
$this->assertEquals(0, $result->count()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGeocodeWithRealAddress() |
44
|
|
|
{ |
45
|
|
|
if (!isset($_SERVER['OPEN_ROUTE_SERVICE_API_KEY'])) { |
46
|
|
|
$this->markTestSkipped('You need to configure the OPEN_ROUTE_SERVICE_API_KEY value in phpunit.xml'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$provider = new OpenRouteService($this->getHttpClient($_SERVER['OPEN_ROUTE_SERVICE_API_KEY']), $_SERVER['OPEN_ROUTE_SERVICE_API_KEY']); |
50
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('242 Acklam Road, London, United Kingdom')); |
51
|
|
|
|
52
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
53
|
|
|
$this->assertCount(1, $results); |
54
|
|
|
|
55
|
|
|
/** @var \Geocoder\Model\Address $result */ |
56
|
|
|
$result = $results->first(); |
57
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
58
|
|
|
$this->assertEqualsWithDelta(51.521124, $result->getCoordinates()->getLatitude(), 0.01); |
59
|
|
|
$this->assertEqualsWithDelta(-0.20360200000000001, $result->getCoordinates()->getLongitude(), 0.01); |
60
|
|
|
$this->assertEquals('Acklam Road', $result->getStreetName()); |
61
|
|
|
$this->assertEquals('London', $result->getLocality()); |
62
|
|
|
$this->assertCount(4, $result->getAdminLevels()); |
63
|
|
|
$this->assertEquals('London', $result->getAdminLevels()->get(3)->getName()); |
64
|
|
|
$this->assertEquals('United Kingdom', $result->getCountry()->getName()); |
65
|
|
|
$this->assertEquals('GBR', $result->getCountry()->getCode()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testReverseWithRealCoordinates() |
69
|
|
|
{ |
70
|
|
|
if (!isset($_SERVER['OPEN_ROUTE_SERVICE_API_KEY'])) { |
71
|
|
|
$this->markTestSkipped('You need to configure the OPEN_ROUTE_SERVICE_API_KEY value in phpunit.xml'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$provider = new OpenRouteService($this->getHttpClient($_SERVER['OPEN_ROUTE_SERVICE_API_KEY']), $_SERVER['OPEN_ROUTE_SERVICE_API_KEY']); |
75
|
|
|
$results = $provider->reverseQuery(ReverseQuery::fromCoordinates(54.0484068, -2.7990345)); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
78
|
|
|
$this->assertCount(5, $results); |
79
|
|
|
|
80
|
|
|
/** @var \Geocoder\Model\Address $result */ |
81
|
|
|
$result = $results->first(); |
82
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
83
|
|
|
$this->assertEqualsWithDelta(54.048411999999999, $result->getCoordinates()->getLatitude(), 0.001); |
84
|
|
|
$this->assertEqualsWithDelta(-2.7989549999999999, $result->getCoordinates()->getLongitude(), 0.001); |
85
|
|
|
$this->assertEquals(1, $result->getStreetNumber()); |
86
|
|
|
$this->assertEquals('Gage Street', $result->getStreetName()); |
87
|
|
|
$this->assertEquals('LA1 1UH', $result->getPostalCode()); |
88
|
|
|
$this->assertEquals('Lancaster', $result->getLocality()); |
89
|
|
|
$this->assertCount(5, $result->getAdminLevels()); |
90
|
|
|
$this->assertEquals('Lancashire', $result->getAdminLevels()->get(1)->getName()); |
91
|
|
|
$this->assertEquals('England', $result->getAdminLevels()->get(4)->getName()); |
92
|
|
|
$this->assertEquals('United Kingdom', $result->getCountry()->getName()); |
93
|
|
|
$this->assertEquals('GBR', $result->getCountry()->getCode()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testReverseWithVillage() |
97
|
|
|
{ |
98
|
|
|
if (!isset($_SERVER['OPEN_ROUTE_SERVICE_API_KEY'])) { |
99
|
|
|
$this->markTestSkipped('You need to configure the OPEN_ROUTE_SERVICE_API_KEY value in phpunit.xml'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$provider = new OpenRouteService($this->getHttpClient($_SERVER['OPEN_ROUTE_SERVICE_API_KEY']), $_SERVER['OPEN_ROUTE_SERVICE_API_KEY']); |
103
|
|
|
$results = $provider->reverseQuery(ReverseQuery::fromCoordinates(49.1390924, 1.6572462)); |
104
|
|
|
|
105
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
106
|
|
|
$this->assertCount(5, $results); |
107
|
|
|
|
108
|
|
|
/** @var \Geocoder\Model\Address $result */ |
109
|
|
|
$result = $results->first(); |
110
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
111
|
|
|
$this->assertEquals('Bray-et-Lû', $result->getLocality()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testGeocodeWithCity() |
115
|
|
|
{ |
116
|
|
|
if (!isset($_SERVER['OPEN_ROUTE_SERVICE_API_KEY'])) { |
117
|
|
|
$this->markTestSkipped('You need to configure the OPEN_ROUTE_SERVICE_API_KEY value in phpunit.xml'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$provider = new OpenRouteService($this->getHttpClient($_SERVER['OPEN_ROUTE_SERVICE_API_KEY']), $_SERVER['OPEN_ROUTE_SERVICE_API_KEY']); |
121
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('Hanover')); |
122
|
|
|
|
123
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
124
|
|
|
$this->assertCount(5, $results); |
125
|
|
|
|
126
|
|
|
/** @var \Geocoder\Model\Address $result */ |
127
|
|
|
$result = $results->first(); |
128
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
129
|
|
|
$this->assertEqualsWithDelta(52.379952, $result->getCoordinates()->getLatitude(), 0.01); |
130
|
|
|
$this->assertEqualsWithDelta(9.787455, $result->getCoordinates()->getLongitude(), 0.01); |
131
|
|
|
$this->assertEquals('Hanover', $result->getLocality()); |
132
|
|
|
$this->assertCount(4, $result->getAdminLevels()); |
133
|
|
|
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName()); |
134
|
|
|
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName()); |
135
|
|
|
$this->assertEquals('Germany', $result->getCountry()->getName()); |
136
|
|
|
|
137
|
|
|
/** @var \Geocoder\Model\Address $result */ |
138
|
|
|
$result = $results->get(1); |
139
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
140
|
|
|
$this->assertEqualsWithDelta(52.37362, $result->getCoordinates()->getLatitude(), 0.01); |
141
|
|
|
$this->assertEqualsWithDelta(9.73711, $result->getCoordinates()->getLongitude(), 0.01); |
142
|
|
|
$this->assertCount(3, $result->getAdminLevels()); |
143
|
|
|
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName()); |
144
|
|
|
$this->assertEquals('Germany', $result->getCountry()->getName()); |
145
|
|
|
|
146
|
|
|
/** @var \Geocoder\Model\Address $result */ |
147
|
|
|
$result = $results->get(2); |
148
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
149
|
|
|
$this->assertEqualsWithDelta(18.393428, $result->getCoordinates()->getLatitude(), 0.01); |
150
|
|
|
$this->assertEqualsWithDelta(-78.107687, $result->getCoordinates()->getLongitude(), 0.01); |
151
|
|
|
$this->assertNull($result->getLocality()); |
152
|
|
|
$this->assertCount(2, $result->getAdminLevels()); |
153
|
|
|
$this->assertEquals('Hanover', $result->getAdminLevels()->get(1)->getName()); |
154
|
|
|
$this->assertEquals('Jamaica', $result->getCountry()->getName()); |
155
|
|
|
|
156
|
|
|
/** @var \Geocoder\Model\Address $result */ |
157
|
|
|
$result = $results->get(3); |
158
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
159
|
|
|
$this->assertEqualsWithDelta(39.192889999999998, $result->getCoordinates()->getLatitude(), 0.01); |
160
|
|
|
$this->assertEqualsWithDelta(-76.724140000000006, $result->getCoordinates()->getLongitude(), 0.01); |
161
|
|
|
$this->assertEquals('Hanover', $result->getLocality()); |
162
|
|
|
$this->assertCount(4, $result->getAdminLevels()); |
163
|
|
|
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName()); |
164
|
|
|
$this->assertEquals('United States', $result->getCountry()->getName()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testGeocodeWithCityDistrict() |
168
|
|
|
{ |
169
|
|
|
if (!isset($_SERVER['OPEN_ROUTE_SERVICE_API_KEY'])) { |
170
|
|
|
$this->markTestSkipped('You need to configure the OPEN_ROUTE_SERVICE_API_KEY value in phpunit.xml'); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$provider = new OpenRouteService($this->getHttpClient($_SERVER['OPEN_ROUTE_SERVICE_API_KEY']), $_SERVER['OPEN_ROUTE_SERVICE_API_KEY']); |
174
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany')); |
175
|
|
|
|
176
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
177
|
|
|
$this->assertCount(2, $results); |
178
|
|
|
|
179
|
|
|
/** @var \Geocoder\Model\Address $result */ |
180
|
|
|
$result = $results->first(); |
181
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
182
|
|
|
$this->assertEqualsWithDelta(50.189017, $result->getCoordinates()->getLatitude(), 0.01); |
183
|
|
|
$this->assertEqualsWithDelta(8.6367809999999992, $result->getCoordinates()->getLongitude(), 0.01); |
184
|
|
|
$this->assertEquals('10a', $result->getStreetNumber()); |
185
|
|
|
$this->assertEquals('Kalbacher Hauptstraße', $result->getStreetName()); |
186
|
|
|
$this->assertEquals(60437, $result->getPostalCode()); |
187
|
|
|
$this->assertEquals('Frankfurt', $result->getLocality()); |
188
|
|
|
$this->assertCount(4, $result->getAdminLevels()); |
189
|
|
|
$this->assertEquals('Frankfurt', $result->getAdminLevels()->get(2)->getName()); |
190
|
|
|
$this->assertEquals('Hessen', $result->getAdminLevels()->get(1)->getName()); |
191
|
|
|
$this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
192
|
|
|
$this->assertEquals('Germany', $result->getCountry()->getName()); |
193
|
|
|
$this->assertEquals('DEU', $result->getCountry()->getCode()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function testGeocodeQuotaExceeded() |
197
|
|
|
{ |
198
|
|
|
$this->expectException(\Geocoder\Exception\QuotaExceeded::class); |
199
|
|
|
$this->expectExceptionMessage('Valid request but quota exceeded.'); |
200
|
|
|
|
201
|
|
|
$provider = new OpenRouteService( |
202
|
|
|
$this->getMockedHttpClient( |
203
|
|
|
'{ |
204
|
|
|
"meta": { |
205
|
|
|
"version": 1, |
206
|
|
|
"status_code": 429 |
207
|
|
|
}, |
208
|
|
|
"results": { |
209
|
|
|
"error": { |
210
|
|
|
"type": "QpsExceededError", |
211
|
|
|
"message": "Queries per second exceeded: Queries exceeded (6 allowed)." |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
}' |
215
|
|
|
), |
216
|
|
|
'api_key' |
217
|
|
|
); |
218
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('New York')); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function testGeocodeInvalidApiKey() |
222
|
|
|
{ |
223
|
|
|
$this->expectException(\Geocoder\Exception\InvalidCredentials::class); |
224
|
|
|
$this->expectExceptionMessage('Invalid or missing api key.'); |
225
|
|
|
|
226
|
|
|
$provider = new OpenRouteService( |
227
|
|
|
$this->getMockedHttpClient( |
228
|
|
|
'{ |
229
|
|
|
"meta": { |
230
|
|
|
"version": 1, |
231
|
|
|
"status_code": 403 |
232
|
|
|
}, |
233
|
|
|
"results": { |
234
|
|
|
"error": { |
235
|
|
|
"type": "KeyError", |
236
|
|
|
"message": "No api_key specified." |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
}' |
240
|
|
|
), |
241
|
|
|
'api_key' |
242
|
|
|
); |
243
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('New York')); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testGeocodeWithLocalhostIPv4() |
247
|
|
|
{ |
248
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
249
|
|
|
$this->expectExceptionMessage('The openrouteservice provider does not support IP addresses, only street addresses.'); |
250
|
|
|
|
251
|
|
|
$provider = new OpenRouteService($this->getMockedHttpClient(), 'api_key'); |
252
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function testGeocodeWithLocalhostIPv6() |
256
|
|
|
{ |
257
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
258
|
|
|
$this->expectExceptionMessage('The openrouteservice provider does not support IP addresses, only street addresses.'); |
259
|
|
|
|
260
|
|
|
$provider = new OpenRouteService($this->getMockedHttpClient(), 'api_key'); |
261
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('::1')); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testGeocodeWithRealIPv4() |
265
|
|
|
{ |
266
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
267
|
|
|
$this->expectExceptionMessage('The openrouteservice provider does not support IP addresses, only street addresses.'); |
268
|
|
|
|
269
|
|
|
$provider = new OpenRouteService($this->getMockedHttpClient(), 'api_key'); |
270
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function testGeocodeWithRealIPv6() |
274
|
|
|
{ |
275
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
276
|
|
|
$this->expectExceptionMessage('The openrouteservice provider does not support IP addresses, only street addresses.'); |
277
|
|
|
|
278
|
|
|
$provider = new OpenRouteService($this->getMockedHttpClient(), 'api_key'); |
279
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59')); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|