forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeocodeEarthTest.php
304 lines (257 loc) · 14 KB
/
GeocodeEarthTest.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?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\GeocodeEarth\Tests;
use Geocoder\Collection;
use Geocoder\IntegrationTest\BaseTestCase;
use Geocoder\Provider\GeocodeEarth\GeocodeEarth;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
class GeocodeEarthTest extends BaseTestCase
{
protected function getCacheDir(): string
{
return __DIR__.'/.cached_responses';
}
public function testGetName(): void
{
$provider = new GeocodeEarth($this->getMockedHttpClient(), 'api_key');
$this->assertEquals('geocode_earth', $provider->getName());
}
public function testGeocode(): void
{
$provider = new GeocodeEarth($this->getMockedHttpClient('{}'), 'api_key');
$result = $provider->geocodeQuery(GeocodeQuery::create('foobar'));
$this->assertInstanceOf(Collection::class, $result);
$this->assertEquals(0, $result->count());
}
public function testGeocodeWithRealAddress(): void
{
if (!isset($_SERVER['GEOCODE_EARTH_API_KEY'])) {
$this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
}
$provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_API_KEY']);
$results = $provider->geocodeQuery(GeocodeQuery::create('242 Acklam Road, London, United Kingdom'));
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(1, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $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->assertCount(3, $result->getAdminLevels());
$this->assertEquals('London', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('United Kingdom', $result->getCountry()->getName());
$this->assertEquals('GBR', $result->getCountry()->getCode());
}
public function testReverseWithRealCoordinates(): void
{
if (!isset($_SERVER['GEOCODE_EARTH_API_KEY'])) {
$this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
}
$provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_API_KEY']);
$results = $provider->reverseQuery(ReverseQuery::fromCoordinates(54.0484068, -2.7990345));
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(5, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $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->assertCount(4, $result->getAdminLevels());
$this->assertEquals('England', $result->getAdminLevels()->get(2)->getName());
$this->assertEquals('Lancashire', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('United Kingdom', $result->getCountry()->getName());
$this->assertEquals('GBR', $result->getCountry()->getCode());
}
public function testReverseWithVillage(): void
{
if (!isset($_SERVER['GEOCODE_EARTH_API_KEY'])) {
$this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
}
$provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_API_KEY']);
$results = $provider->reverseQuery(ReverseQuery::fromCoordinates(49.1390924, 1.6572462));
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(5, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEquals('Bray-et-Lû', $result->getLocality());
}
public function testGeocodeWithCity(): void
{
if (!isset($_SERVER['GEOCODE_EARTH_API_KEY'])) {
$this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
}
$provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_API_KEY']);
$results = $provider->geocodeQuery(GeocodeQuery::create('Hanover'));
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(5, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(52.379952, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(9.787455, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Hanover', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('Germany', $result->getCountry()->getName());
/** @var \Geocoder\Model\Address $result */
$result = $results->get(1);
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(52.37362, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(9.73711, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertCount(3, $result->getAdminLevels());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Germany', $result->getCountry()->getName());
/** @var \Geocoder\Model\Address $result */
$result = $results->get(2);
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(18.393428, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-78.107687, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertNull($result->getLocality());
$this->assertCount(2, $result->getAdminLevels());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Jamaica', $result->getCountry()->getName());
/** @var \Geocoder\Model\Address $result */
$result = $results->get(3);
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(39.192889999999998, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(-76.724140000000006, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Hanover', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('United States', $result->getCountry()->getName());
}
public function testGeocodeWithCityDistrict(): void
{
if (!isset($_SERVER['GEOCODE_EARTH_API_KEY'])) {
$this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
}
$provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_API_KEY']);
$results = $provider->geocodeQuery(GeocodeQuery::create('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'));
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(2, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(50.189017, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(8.6367809999999992, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('10a', $result->getStreetNumber());
$this->assertEquals('Kalbacher Hauptstraße', $result->getStreetName());
$this->assertEquals(60437, $result->getPostalCode());
$this->assertEquals('Frankfurt', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Frankfurt', $result->getAdminLevels()->get(4)->getName());
$this->assertEquals('Hessen', $result->getAdminLevels()->get(3)->getName());
$this->assertNull($result->getAdminLevels()->get(1)->getCode());
$this->assertEquals('Germany', $result->getCountry()->getName());
$this->assertEquals('DEU', $result->getCountry()->getCode());
}
public function testGeocodeNoBounds(): void
{
if (!isset($_SERVER['GEOCODE_EARTH_API_KEY'])) {
$this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
}
$provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_API_KEY']);
$results = $provider->geocodeQuery(GeocodeQuery::create('dworzec centralny'));
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(5, $results);
/** @var \Geocoder\Model\Address $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEqualsWithDelta(52.230428, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(21.004552, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Warsaw', $result->getLocality());
$this->assertEquals('Poland', $result->getCountry()->getName());
$this->assertEquals('POL', $result->getCountry()->getCode());
$this->assertNull($result->getBounds());
}
public function testGeocodeQuotaExceeded(): void
{
$this->expectException(\Geocoder\Exception\QuotaExceeded::class);
$this->expectExceptionMessage('Valid request but quota exceeded.');
$provider = new GeocodeEarth(
$this->getMockedHttpClient(
'{
"meta": {
"version": 1,
"status_code": 429
},
"results": {
"error": {
"type": "QpsExceededError",
"message": "Queries per second exceeded: Queries exceeded (6 allowed)."
}
}
}'
),
'api_key'
);
$provider->geocodeQuery(GeocodeQuery::create('New York'));
}
public function testGeocodeInvalidApiKey(): void
{
$this->expectException(\Geocoder\Exception\InvalidCredentials::class);
$this->expectExceptionMessage('Invalid or missing api key.');
$provider = new GeocodeEarth(
$this->getMockedHttpClient(
'{
"meta": {
"version": 1,
"status_code": 403
},
"results": {
"error": {
"type": "KeyError",
"message": "No api_key specified."
}
}
}'
),
'api_key'
);
$provider->geocodeQuery(GeocodeQuery::create('New York'));
}
public function testGeocodeWithLocalhostIPv4(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The geocode_earth provider does not support IP addresses, only street addresses.');
$provider = new GeocodeEarth($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 geocode_earth provider does not support IP addresses, only street addresses.');
$provider = new GeocodeEarth($this->getMockedHttpClient(), 'api_key');
$provider->geocodeQuery(GeocodeQuery::create('::1'));
}
public function testGeocodeWithRealIPv4(): void
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The geocode_earth provider does not support IP addresses, only street addresses.');
$provider = new GeocodeEarth($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 geocode_earth provider does not support IP addresses, only street addresses.');
$provider = new GeocodeEarth($this->getMockedHttpClient(), 'api_key');
$provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59'));
}
}