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\OpenCage\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\Tests\TestCase; |
21
|
|
|
use Geocoder\Provider\OpenCage\OpenCage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author mtm <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class OpenCageTest extends BaseTestCase |
27
|
|
|
{ |
28
|
|
|
protected function getCacheDir() |
29
|
|
|
{ |
30
|
|
|
return __DIR__.'/.cached_responses'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function testGetName() |
35
|
|
|
{ |
36
|
|
|
$provider = new OpenCage($this->getMockedHttpClient(), 'api_key'); |
37
|
|
|
$this->assertEquals('opencage', $provider->getName()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testSslSchema() |
41
|
|
|
{ |
42
|
|
|
$provider = new OpenCage($this->getMockedHttpClient('{}'), 'api_key'); |
43
|
|
|
$result = $provider->geocodeQuery(GeocodeQuery::create('foobar')); |
44
|
|
|
|
45
|
|
|
$this->assertInstanceOf(Collection::class, $result); |
46
|
|
|
$this->assertEquals(0, $result->count()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function testGeocodeWithRealAddress() |
51
|
|
|
{ |
52
|
|
|
if (!isset($_SERVER['OPENCAGE_API_KEY'])) { |
53
|
|
|
$this->markTestSkipped('You need to configure the OPENCAGE_API_KEY value in phpunit.xml'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$provider = new OpenCage($this->getHttpClient($_SERVER['OPENCAGE_API_KEY']), $_SERVER['OPENCAGE_API_KEY']); |
57
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
58
|
|
|
|
59
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
60
|
|
|
$this->assertCount(2, $results); |
61
|
|
|
|
62
|
|
|
/** @var Location $result */ |
63
|
|
|
$result = $results->first(); |
64
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
65
|
|
|
$this->assertEquals(48.866205, $result->getCoordinates()->getLatitude(), '', 0.01); |
66
|
|
|
$this->assertEquals(2.389089, $result->getCoordinates()->getLongitude(), '', 0.01); |
67
|
|
|
$this->assertNotNull($result->getBounds()); |
68
|
|
|
$this->assertEquals(48.863142699999997, $result->getBounds()->getSouth()); |
69
|
|
|
$this->assertEquals(2.3890394000000001, $result->getBounds()->getWest()); |
70
|
|
|
$this->assertEquals(48.863242700000001, $result->getBounds()->getNorth()); |
71
|
|
|
$this->assertEquals(2.3891393999999999, $result->getBounds()->getEast()); |
72
|
|
|
$this->assertEquals(10, $result->getStreetNumber()); |
73
|
|
|
$this->assertEquals('Avenue Gambetta', $result->getStreetName()); |
74
|
|
|
$this->assertEquals(75020, $result->getPostalCode()); |
75
|
|
|
$this->assertEquals('Paris', $result->getLocality()); |
76
|
|
|
$this->assertCount(2, $result->getAdminLevels()); |
77
|
|
|
$this->assertEquals('Paris', $result->getAdminLevels()->get(2)->getName()); |
78
|
|
|
$this->assertEquals('Ile-de-France', $result->getAdminLevels()->get(1)->getName()); |
79
|
|
|
$this->assertEquals('France', $result->getCountry()->getName()); |
80
|
|
|
$this->assertEquals('FR', $result->getCountry()->getCode()); |
81
|
|
|
$this->assertEquals('Europe/Paris', $result->getTimezone()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testReverseWithRealCoordinates() |
85
|
|
|
{ |
86
|
|
|
if (!isset($_SERVER['OPENCAGE_API_KEY'])) { |
87
|
|
|
$this->markTestSkipped('You need to configure the OPENCAGE_API_KEY value in phpunit.xml'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$provider = new OpenCage($this->getHttpClient($_SERVER['OPENCAGE_API_KEY']), $_SERVER['OPENCAGE_API_KEY']); |
91
|
|
|
$results = $provider->reverseQuery(ReverseQuery::fromCoordinates(54.0484068, -2.7990345)); |
92
|
|
|
|
93
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
94
|
|
|
$this->assertCount(1, $results); |
95
|
|
|
|
96
|
|
|
/** @var Location $result */ |
97
|
|
|
$result = $results->first(); |
98
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
99
|
|
|
$this->assertEquals(54.0484068, $result->getCoordinates()->getLatitude(), '', 0.001); |
100
|
|
|
$this->assertEquals(-2.7990345, $result->getCoordinates()->getLongitude(), '', 0.001); |
101
|
|
|
$this->assertNotNull($result->getBounds()); |
102
|
|
|
$this->assertEquals(54.0484068, $result->getBounds()->getSouth(), '', 0.001); |
103
|
|
|
$this->assertEquals(-2.7998815, $result->getBounds()->getWest(), '', 0.001); |
104
|
|
|
$this->assertEquals(54.049472, $result->getBounds()->getNorth(), '', 0.001); |
105
|
|
|
$this->assertEquals(-2.7980925, $result->getBounds()->getEast(), '', 0.001); |
106
|
|
|
$this->assertNull($result->getStreetNumber()); |
107
|
|
|
$this->assertNull($result->getStreetName()); |
108
|
|
|
$this->assertEquals('LA1 1LZ', $result->getPostalCode()); |
109
|
|
|
$this->assertEquals('Lancaster', $result->getLocality()); |
110
|
|
|
$this->assertCount(2, $result->getAdminLevels()); |
111
|
|
|
$this->assertEquals('Lancashire', $result->getAdminLevels()->get(2)->getName()); |
112
|
|
|
$this->assertEquals('England', $result->getAdminLevels()->get(1)->getName()); |
113
|
|
|
$this->assertEquals('United Kingdom', $result->getCountry()->getName()); |
114
|
|
|
$this->assertEquals('GB', $result->getCountry()->getCode()); |
115
|
|
|
$this->assertEquals('Europe/London', $result->getTimezone()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testReverseWithVillage() |
119
|
|
|
{ |
120
|
|
|
if (!isset($_SERVER['OPENCAGE_API_KEY'])) { |
121
|
|
|
$this->markTestSkipped('You need to configure the OPENCAGE_API_KEY value in phpunit.xml'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$provider = new OpenCage($this->getHttpClient($_SERVER['OPENCAGE_API_KEY']), $_SERVER['OPENCAGE_API_KEY']); |
125
|
|
|
$results = $provider->reverseQuery(ReverseQuery::fromCoordinates(49.1390924, 1.6572462)); |
126
|
|
|
|
127
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
128
|
|
|
$this->assertCount(1, $results); |
129
|
|
|
|
130
|
|
|
/** @var Location $result */ |
131
|
|
|
$result = $results->first(); |
132
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
133
|
|
|
$this->assertEquals('Bray-et-Lû', $result->getLocality()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testGeocodeWithCity() |
137
|
|
|
{ |
138
|
|
|
if (!isset($_SERVER['OPENCAGE_API_KEY'])) { |
139
|
|
|
$this->markTestSkipped('You need to configure the OPENCAGE_API_KEY value in phpunit.xml'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$provider = new OpenCage($this->getHttpClient($_SERVER['OPENCAGE_API_KEY']), $_SERVER['OPENCAGE_API_KEY']); |
143
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('Hanover')); |
144
|
|
|
|
145
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
146
|
|
|
$this->assertCount(5, $results); |
147
|
|
|
|
148
|
|
|
/** @var Location $result */ |
149
|
|
|
$result = $results->first(); |
150
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
151
|
|
|
$this->assertEquals(52.374478, $result->getCoordinates()->getLatitude(), '', 0.01); |
152
|
|
|
$this->assertEquals(9.738553, $result->getCoordinates()->getLongitude(), '', 0.01); |
153
|
|
|
$this->assertEquals('Hanover', $result->getLocality()); |
154
|
|
|
$this->assertCount(2, $result->getAdminLevels()); |
155
|
|
|
$this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); |
156
|
|
|
$this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName()); |
157
|
|
|
$this->assertEquals('Germany', $result->getCountry()->getName()); |
158
|
|
|
|
159
|
|
|
/** @var Location $result */ |
160
|
|
|
$result = $results->get(1); |
161
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
162
|
|
|
$this->assertEquals(18.3840489, $result->getCoordinates()->getLatitude(), '', 0.01); |
163
|
|
|
$this->assertEquals(-78.131485, $result->getCoordinates()->getLongitude(), '', 0.01); |
164
|
|
|
$this->assertNull($result->getLocality()); |
165
|
|
|
$this->assertTrue($result->getAdminLevels()->has(2)); |
166
|
|
|
$this->assertEquals('Hanover', $result->getAdminLevels()->get(2)->getName()); |
167
|
|
|
$this->assertEquals('Jamaica', $result->getCountry()->getName()); |
168
|
|
|
|
169
|
|
|
/** @var Location $result */ |
170
|
|
|
$result = $results->get(2); |
171
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
172
|
|
|
$this->assertEquals(43.7033073, $result->getCoordinates()->getLatitude(), '', 0.01); |
173
|
|
|
$this->assertEquals(-72.2885663, $result->getCoordinates()->getLongitude(), '', 0.01); |
174
|
|
|
$this->assertEquals('Hanover', $result->getLocality()); |
175
|
|
|
$this->assertCount(2, $result->getAdminLevels()); |
176
|
|
|
$this->assertEquals('Grafton County', $result->getAdminLevels()->get(2)->getName()); |
177
|
|
|
$this->assertEquals('New Hampshire', $result->getAdminLevels()->get(1)->getName()); |
178
|
|
|
$this->assertEquals('United States of America', $result->getCountry()->getName()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testGeocodeWithCityDistrict() |
182
|
|
|
{ |
183
|
|
|
if (!isset($_SERVER['OPENCAGE_API_KEY'])) { |
184
|
|
|
$this->markTestSkipped('You need to configure the OPENCAGE_API_KEY value in phpunit.xml'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
$provider = new OpenCage($this->getHttpClient($_SERVER['OPENCAGE_API_KEY']), $_SERVER['OPENCAGE_API_KEY']); |
188
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany')); |
189
|
|
|
|
190
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
191
|
|
|
$this->assertCount(2, $results); |
192
|
|
|
|
193
|
|
|
/** @var Location $result */ |
194
|
|
|
$result = $results->first(); |
195
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
196
|
|
|
$this->assertEquals(50.189062, $result->getCoordinates()->getLatitude(), '', 0.01); |
197
|
|
|
$this->assertEquals(8.636567, $result->getCoordinates()->getLongitude(), '', 0.01); |
198
|
|
|
$this->assertEquals(10, $result->getStreetNumber()); |
199
|
|
|
$this->assertEquals('Kalbacher Hauptstraße', $result->getStreetName()); |
200
|
|
|
$this->assertEquals(60437, $result->getPostalCode()); |
201
|
|
|
$this->assertEquals('Frankfurt', $result->getLocality()); |
202
|
|
|
$this->assertCount(1, $result->getAdminLevels()); |
203
|
|
|
$this->assertEquals('Hesse', $result->getAdminLevels()->get(1)->getName()); |
204
|
|
|
$this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
205
|
|
|
$this->assertEquals('Germany', $result->getCountry()->getName()); |
206
|
|
|
$this->assertEquals('DE', $result->getCountry()->getCode()); |
207
|
|
|
$this->assertEquals('Europe/Berlin', $result->getTimezone()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testGeocodeWithLocale() |
211
|
|
|
{ |
212
|
|
|
if (!isset($_SERVER['OPENCAGE_API_KEY'])) { |
213
|
|
|
$this->markTestSkipped('You need to configure the OPENCAGE_API_KEY value in phpunit.xml'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$provider = new OpenCage($this->getHttpClient($_SERVER['OPENCAGE_API_KEY']), $_SERVER['OPENCAGE_API_KEY']); |
217
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('London')->withLocale('es')); |
218
|
|
|
|
219
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
220
|
|
|
$this->assertCount(5, $results); |
221
|
|
|
|
222
|
|
|
/** @var Location $result */ |
223
|
|
|
$result = $results->first(); |
224
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
225
|
|
|
$this->assertEquals('Londres', $result->getLocality()); |
226
|
|
|
$this->assertCount(2, $result->getAdminLevels()); |
227
|
|
|
$this->assertEquals('Londres', $result->getAdminLevels()->get(2)->getName()); |
228
|
|
|
$this->assertEquals('Inglaterra', $result->getAdminLevels()->get(1)->getName()); |
229
|
|
|
$this->assertEquals('Reino Unido', $result->getCountry()->getName()); |
230
|
|
|
$this->assertEquals('GB', $result->getCountry()->getCode()); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @expectedException \Geocoder\Exception\QuotaExceeded |
235
|
|
|
* @expectedExceptionMessage Valid request but quota exceeded. |
236
|
|
|
*/ |
237
|
|
|
public function testGeocodeQuotaExceeded() |
238
|
|
|
{ |
239
|
|
|
$provider = new OpenCage( |
240
|
|
|
$this->getMockedHttpClient( |
241
|
|
|
'{ |
242
|
|
|
"status": { |
243
|
|
|
"code": 402, |
244
|
|
|
"message": "quota exceeded" |
245
|
|
|
} |
246
|
|
|
}' |
247
|
|
|
), |
248
|
|
|
'api_key' |
249
|
|
|
); |
250
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('New York')); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @expectedException \Geocoder\Exception\InvalidCredentials |
255
|
|
|
* @expectedExceptionMessage Invalid or missing api key. |
256
|
|
|
*/ |
257
|
|
|
public function testGeocodeInvalidApiKey() |
258
|
|
|
{ |
259
|
|
|
$provider = new OpenCage( |
260
|
|
|
$this->getMockedHttpClient( |
261
|
|
|
'{ |
262
|
|
|
"status": { |
263
|
|
|
"code": 403, |
264
|
|
|
"message": "invalid API key" |
265
|
|
|
} |
266
|
|
|
}' |
267
|
|
|
), |
268
|
|
|
'api_key' |
269
|
|
|
); |
270
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('New York')); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
275
|
|
|
* @expectedExceptionMessage The OpenCage provider does not support IP addresses, only street addresses. |
276
|
|
|
*/ |
277
|
|
|
public function testGeocodeWithLocalhostIPv4() |
278
|
|
|
{ |
279
|
|
|
$provider = new OpenCage($this->getMockedHttpClient(), 'api_key'); |
280
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
285
|
|
|
* @expectedExceptionMessage The OpenCage provider does not support IP addresses, only street addresses. |
286
|
|
|
*/ |
287
|
|
|
public function testGeocodeWithLocalhostIPv6() |
288
|
|
|
{ |
289
|
|
|
$provider = new OpenCage($this->getMockedHttpClient(), 'api_key'); |
290
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('::1')); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
295
|
|
|
* @expectedExceptionMessage The OpenCage provider does not support IP addresses, only street addresses. |
296
|
|
|
*/ |
297
|
|
|
public function testGeocodeWithRealIPv4() |
298
|
|
|
{ |
299
|
|
|
$provider = new OpenCage($this->getHttpClient(), 'api_key'); |
300
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
305
|
|
|
* @expectedExceptionMessage The OpenCage provider does not support IP addresses, only street addresses. |
306
|
|
|
*/ |
307
|
|
|
public function testGeocodeWithRealIPv6() |
308
|
|
|
{ |
309
|
|
|
$provider = new OpenCage($this->getHttpClient(), 'api_key'); |
310
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59')); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|