Completed
Push — master ( 0531a7...52b0f1 )
by Tobias
05:03
created

GeonamesTest::testReverseWithRealCoordinates()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 18

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 18
nc 2
nop 0
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\Geonames\Tests;
14
15
use Geocoder\Collection;
16
use Geocoder\IntegrationTest\BaseTestCase;
17
use Geocoder\Location;
18
use Geocoder\Provider\Geonames\Model\GeonamesAddress;
19
use Geocoder\Query\GeocodeQuery;
20
use Geocoder\Query\ReverseQuery;
21
use Geocoder\Provider\Geonames\Geonames;
22
23
class GeonamesTest extends BaseTestCase
24
{
25
    protected function getCacheDir()
26
    {
27
        return __DIR__.'/.cached_responses';
28
    }
29
30
    public function testGetName()
31
    {
32
        $provider = new Geonames($this->getMockedHttpClient(), 'username');
33
        $this->assertEquals('geonames', $provider->getName());
34
    }
35
36
    /**
37
     * @expectedException \Geocoder\Exception\UnsupportedOperation
38
     * @expectedExceptionMessage The Geonames provider does not support IP addresses.
39
     */
40
    public function testGeocodeWithLocalhostIPv4()
41
    {
42
        $provider = new Geonames($this->getMockedHttpClient(), 'username');
43
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
44
    }
45
46
    /**
47
     * @expectedException \Geocoder\Exception\UnsupportedOperation
48
     * @expectedExceptionMessage The Geonames provider does not support IP addresses.
49
     */
50
    public function testGeocodeWithLocalhostIPv6()
51
    {
52
        $provider = new Geonames($this->getMockedHttpClient(), 'username');
53
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
54
    }
55
56
    public function testGeocodeWithUnknownCity()
57
    {
58
        $noPlacesFoundResponse = <<<'JSON'
59
{
60
    "totalResultsCount": 0,
61
    "geonames": [ ]
62
}
63
JSON;
64
        $provider = new Geonames($this->getMockedHttpClient($noPlacesFoundResponse), 'username');
65
        $result = $provider->geocodeQuery(GeocodeQuery::create('BlaBlaBla'));
66
67
        $this->assertInstanceOf(Collection::class, $result);
68
        $this->assertEquals(0, $result->count());
69
    }
70
71
    public function testGeocodeWithRealPlace()
72
    {
73
        if (!isset($_SERVER['GEONAMES_USERNAME'])) {
74
            $this->markTestSkipped('You need to configure the GEONAMES_USERNAME value in phpunit.xml');
75
        }
76
77
        $provider = new Geonames($this->getHttpClient($_SERVER['GEONAMES_USERNAME']), $_SERVER['GEONAMES_USERNAME']);
78
        $results = $provider->geocodeQuery(GeocodeQuery::create('Harrods, London'));
79
80
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
81
82
        /** @var GeonamesAddress $result */
83
        $result = $results->first();
84
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
85
        $this->assertEquals(51.49957, $result->getCoordinates()->getLatitude(), '', 0.01);
86
        $this->assertEquals(-0.16359, $result->getCoordinates()->getLongitude(), '', 0.01);
87
        $this->assertEquals('United Kingdom', $result->getCountry()->getName());
88
        $this->assertEquals('GB', $result->getCountry()->getCode());
89
        $this->assertEquals('Europe/London', $result->getTimezone());
90
91
        $this->assertEquals('MALL', $result->getFcode());
92
        $this->assertEquals('spot, building, farm', $result->getFclName());
93
        $this->assertEquals('Harrods', $result->getName());
94
        $this->assertEquals('Harrods', $result->getAsciiName());
95
        $this->assertEquals(0, $result->getPopulation());
96
        $this->assertEquals(6944333, $result->getGeonameId());
97
        $this->assertCount(10, $result->getAlternateNames());
98
    }
99
100
    public function testGeocodeWithMultipleRealPlaces()
101
    {
102
        if (!isset($_SERVER['GEONAMES_USERNAME'])) {
103
            $this->markTestSkipped('You need to configure the GEONAMES_USERNAME value in phpunit.xml');
104
        }
105
106
        $provider = new Geonames($this->getHttpClient($_SERVER['GEONAMES_USERNAME']), $_SERVER['GEONAMES_USERNAME']);
107
        $results = $provider->geocodeQuery(GeocodeQuery::create('London'));
108
109
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
110
        $this->assertCount(5, $results);
111
112
        /** @var Location $result */
113
        $result = $results->first();
114
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
115
        $this->assertEquals(51.508528775863, $result->getCoordinates()->getLatitude(), '', 0.01);
116
        $this->assertEquals(-0.12574195861816, $result->getCoordinates()->getLongitude(), '', 0.01);
117
        $this->assertNotNull($result->getBounds());
118
        $this->assertEquals(51.151689398345, $result->getBounds()->getSouth(), '', 0.01);
119
        $this->assertEquals(-0.70360885396019, $result->getBounds()->getWest(), '', 0.01);
120
        $this->assertEquals(51.865368153381, $result->getBounds()->getNorth(), '', 0.01);
121
        $this->assertEquals(0.45212493672386, $result->getBounds()->getEast(), '', 0.01);
122
        $this->assertEquals('London', $result->getLocality());
123
        $this->assertCount(2, $result->getAdminLevels());
124
        $this->assertEquals('Greater London', $result->getAdminLevels()->get(2)->getName());
125
        $this->assertEquals('England', $result->getAdminLevels()->get(1)->getName());
126
        $this->assertEquals('United Kingdom', $result->getCountry()->getName());
127
        $this->assertEquals('GB', $result->getCountry()->getCode());
128
        $this->assertEquals('Europe/London', $result->getTimezone());
129
130
        /** @var Location $result */
131
        $result = $results->get(1);
132
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
133
        $this->assertEquals(-33.015285093464, $result->getCoordinates()->getLatitude(), '', 0.01);
134
        $this->assertEquals(27.911624908447, $result->getCoordinates()->getLongitude(), '', 0.01);
135
        $this->assertNotNull($result->getBounds());
136
        $this->assertEquals(-33.104996458003, $result->getBounds()->getSouth(), '', 0.01);
137
        $this->assertEquals(27.804746435655, $result->getBounds()->getWest(), '', 0.01);
138
        $this->assertEquals(-32.925573728925, $result->getBounds()->getNorth(), '', 0.01);
139
        $this->assertEquals(28.018503381239, $result->getBounds()->getEast(), '', 0.01);
140
        $this->assertEquals('East London', $result->getLocality());
141
        $this->assertCount(3, $result->getAdminLevels());
142
        $this->assertEquals('Buffalo City', $result->getAdminLevels()->get(3)->getName());
143
        $this->assertEquals('Buffalo City Metropolitan Municipality', $result->getAdminLevels()->get(2)->getName());
144
        $this->assertEquals('Eastern Cape', $result->getAdminLevels()->get(1)->getName());
145
        $this->assertEquals('South Africa', $result->getCountry()->getName());
146
        $this->assertEquals('ZA', $result->getCountry()->getCode());
147
        $this->assertEquals('Africa/Johannesburg', $result->getTimezone());
148
149
        /** @var Location $result */
150
        $result = $results->get(2);
151
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
152
        $this->assertEquals(51.512788890295, $result->getCoordinates()->getLatitude(), '', 0.01);
153
        $this->assertEquals(-0.091838836669922, $result->getCoordinates()->getLongitude(), '', 0.01);
154
        $this->assertNotNull($result->getBounds());
155
        $this->assertEquals(51.155949512764, $result->getBounds()->getSouth(), '', 0.01);
156
        $this->assertEquals(-0.66976046752962, $result->getBounds()->getWest(), '', 0.01);
157
        $this->assertEquals(51.869628267826, $result->getBounds()->getNorth(), '', 0.01);
158
        $this->assertEquals(0.48608279418978, $result->getBounds()->getEast(), '', 0.01);
159
        $this->assertEquals('City of London', $result->getLocality());
160
        $this->assertCount(3, $result->getAdminLevels());
161
        $this->assertEquals('City of London', $result->getAdminLevels()->get(3)->getName());
162
        $this->assertEquals('Greater London', $result->getAdminLevels()->get(2)->getName());
163
        $this->assertEquals('England', $result->getAdminLevels()->get(1)->getName());
164
        $this->assertEquals('United Kingdom', $result->getCountry()->getName());
165
        $this->assertEquals('GB', $result->getCountry()->getCode());
166
        $this->assertEquals('Europe/London', $result->getTimezone());
167
168
        /** @var Location $result */
169
        $result = $results->get(3);
170
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
171
        $this->assertEquals(42.983389283, $result->getCoordinates()->getLatitude(), '', 0.01);
172
        $this->assertEquals(-81.233042387, $result->getCoordinates()->getLongitude(), '', 0.01);
173
        $this->assertNotNull($result->getBounds());
174
        $this->assertEquals(42.907075642763, $result->getBounds()->getSouth(), '', 0.01);
175
        $this->assertEquals(-81.337489676463, $result->getBounds()->getWest(), '', 0.01);
176
        $this->assertEquals(43.059702923237, $result->getBounds()->getNorth(), '', 0.01);
177
        $this->assertEquals(-81.128595097537, $result->getBounds()->getEast(), '', 0.01);
178
        $this->assertEquals('London', $result->getLocality());
179
        $this->assertCount(1, $result->getAdminLevels());
180
        $this->assertEquals('Ontario', $result->getAdminLevels()->get(1)->getName());
181
        $this->assertEquals('Canada', $result->getCountry()->getName());
182
        $this->assertEquals('CA', $result->getCountry()->getCode());
183
        $this->assertEquals('America/Toronto', $result->getTimezone());
184
185
        /** @var Location $result */
186
        $result = $results->get(4);
187
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
188
        $this->assertEquals(41.3556539, $result->getCoordinates()->getLatitude(), '', 0.01);
189
        $this->assertEquals(-72.0995209, $result->getCoordinates()->getLongitude(), '', 0.01);
190
        $this->assertNotNull($result->getBounds());
191
        $this->assertEquals(41.334087887904, $result->getBounds()->getSouth(), '', 0.01);
192
        $this->assertEquals(-72.128261254846, $result->getBounds()->getWest(), '', 0.01);
193
        $this->assertEquals(41.377219912096, $result->getBounds()->getNorth(), '', 0.01);
194
        $this->assertEquals(-72.070780545154, $result->getBounds()->getEast(), '', 0.01);
195
        $this->assertEquals('New London', $result->getLocality());
196
        $this->assertCount(2, $result->getAdminLevels());
197
        $this->assertEquals('New London County', $result->getAdminLevels()->get(2)->getName());
198
        $this->assertEquals('Connecticut', $result->getAdminLevels()->get(1)->getName());
199
        $this->assertEquals('United States', $result->getCountry()->getName());
200
        $this->assertEquals('US', $result->getCountry()->getCode());
201
        $this->assertEquals('America/New_York', $result->getTimezone());
202
    }
203
204
    public function testGeocodeWithMultipleRealPlacesWithLocale()
205
    {
206
        if (!isset($_SERVER['GEONAMES_USERNAME'])) {
207
            $this->markTestSkipped('You need to configure the GEONAMES_USERNAME value in phpunit.xml');
208
        }
209
210
        $provider = new Geonames($this->getHttpClient($_SERVER['GEONAMES_USERNAME']), $_SERVER['GEONAMES_USERNAME']);
211
        $results = $provider->geocodeQuery(GeocodeQuery::create('London')->withLocale('it_IT'));
212
213
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
214
        $this->assertCount(5, $results);
215
216
        /** @var Location $result */
217
        $result = $results->first();
218
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
219
        $this->assertEquals(51.50853, $result->getCoordinates()->getLatitude(), '', 0.01);
220
        $this->assertEquals(-0.12574, $result->getCoordinates()->getLongitude(), '', 0.01);
221
        $this->assertNotNull($result->getBounds());
222
        $this->assertEquals(51.15169, $result->getBounds()->getSouth(), '', 0.01);
223
        $this->assertEquals(-0.70361, $result->getBounds()->getWest(), '', 0.01);
224
        $this->assertEquals(51.86537, $result->getBounds()->getNorth(), '', 0.01);
225
        $this->assertEquals(0.45212, $result->getBounds()->getEast(), '', 0.01);
226
        $this->assertEquals('Londra', $result->getLocality());
227
        $this->assertCount(2, $result->getAdminLevels());
228
        $this->assertEquals('Greater London', $result->getAdminLevels()->get(2)->getName());
229
        $this->assertEquals('Inghilterra', $result->getAdminLevels()->get(1)->getName());
230
        $this->assertEquals('Regno Unito', $result->getCountry()->getName());
231
        $this->assertEquals('GB', $result->getCountry()->getCode());
232
        $this->assertEquals('Europe/London', $result->getTimezone());
233
234
        /** @var Location $result */
235
        $result = $results->get(1);
236
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
237
        $this->assertEquals(-33.015285093464, $result->getCoordinates()->getLatitude(), '', 0.01);
238
        $this->assertEquals(27.911624908447, $result->getCoordinates()->getLongitude(), '', 0.01);
239
        $this->assertNotNull($result->getBounds());
240
        $this->assertEquals(-33.104996458003, $result->getBounds()->getSouth(), '', 0.01);
241
        $this->assertEquals(27.804746435655, $result->getBounds()->getWest(), '', 0.01);
242
        $this->assertEquals(-32.925573728925, $result->getBounds()->getNorth(), '', 0.01);
243
        $this->assertEquals(28.018503381239, $result->getBounds()->getEast(), '', 0.01);
244
        $this->assertEquals('East London', $result->getLocality());
245
        $this->assertCount(3, $result->getAdminLevels());
246
        $this->assertEquals('Buffalo City', $result->getAdminLevels()->get(3)->getName());
247
        $this->assertEquals('Buffalo City Metropolitan Municipality', $result->getAdminLevels()->get(2)->getName());
248
        $this->assertEquals('Eastern Cape', $result->getAdminLevels()->get(1)->getName());
249
        $this->assertEquals('Sudafrica', $result->getCountry()->getName());
250
        $this->assertEquals('ZA', $result->getCountry()->getCode());
251
        $this->assertEquals('Africa/Johannesburg', $result->getTimezone());
252
253
        /** @var Location $result */
254
        $result = $results->get(2);
255
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
256
        $this->assertEquals(51.512788890295, $result->getCoordinates()->getLatitude(), '', 0.01);
257
        $this->assertEquals(-0.091838836669922, $result->getCoordinates()->getLongitude(), '', 0.01);
258
        $this->assertNotNull($result->getBounds());
259
        $this->assertEquals(51.155949512764, $result->getBounds()->getSouth(), '', 0.01);
260
        $this->assertEquals(-0.66976046752962, $result->getBounds()->getWest(), '', 0.01);
261
        $this->assertEquals(51.869628267826, $result->getBounds()->getNorth(), '', 0.01);
262
        $this->assertEquals(0.48608279418978, $result->getBounds()->getEast(), '', 0.01);
263
        $this->assertEquals('Città di Londra', $result->getLocality());
264
        $this->assertCount(3, $result->getAdminLevels());
265
        $this->assertEquals('City of London', $result->getAdminLevels()->get(3)->getName());
266
        $this->assertEquals('Greater London', $result->getAdminLevels()->get(2)->getName());
267
        $this->assertEquals('Inghilterra', $result->getAdminLevels()->get(1)->getName());
268
        $this->assertEquals('Regno Unito', $result->getCountry()->getName());
269
        $this->assertEquals('GB', $result->getCountry()->getCode());
270
        $this->assertEquals('Europe/London', $result->getTimezone());
271
272
        /** @var Location $result */
273
        $result = $results->get(3);
274
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
275
        $this->assertEquals(42.983389283, $result->getCoordinates()->getLatitude(), '', 0.01);
276
        $this->assertEquals(-81.233042387, $result->getCoordinates()->getLongitude(), '', 0.01);
277
        $this->assertNotNull($result->getBounds());
278
        $this->assertEquals(42.907075642763, $result->getBounds()->getSouth(), '', 0.01);
279
        $this->assertEquals(-81.337489676463, $result->getBounds()->getWest(), '', 0.01);
280
        $this->assertEquals(43.059702923237, $result->getBounds()->getNorth(), '', 0.01);
281
        $this->assertEquals(-81.128595097537, $result->getBounds()->getEast(), '', 0.01);
282
        $this->assertEquals('London', $result->getLocality());
283
        $this->assertCount(1, $result->getAdminLevels());
284
        $this->assertEquals('Ontario', $result->getAdminLevels()->get(1)->getName());
285
        $this->assertEquals('Canada', $result->getCountry()->getName());
286
        $this->assertEquals('CA', $result->getCountry()->getCode());
287
        $this->assertEquals('America/Toronto', $result->getTimezone());
288
289
        /** @var Location $result */
290
        $result = $results->get(4);
291
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
292
        $this->assertEquals(41.3556539, $result->getCoordinates()->getLatitude(), '', 0.01);
293
        $this->assertEquals(-72.0995209, $result->getCoordinates()->getLongitude(), '', 0.01);
294
        $this->assertNotNull($result->getBounds());
295
        $this->assertEquals(41.334087887904, $result->getBounds()->getSouth(), '', 0.01);
296
        $this->assertEquals(-72.128261254846, $result->getBounds()->getWest(), '', 0.01);
297
        $this->assertEquals(41.377219912096, $result->getBounds()->getNorth(), '', 0.01);
298
        $this->assertEquals(-72.070780545154, $result->getBounds()->getEast(), '', 0.01);
299
        $this->assertEquals('New London', $result->getLocality());
300
        $this->assertCount(2, $result->getAdminLevels());
301
        $this->assertEquals('Contea di New London', $result->getAdminLevels()->get(2)->getName());
302
        $this->assertEquals('Connecticut', $result->getAdminLevels()->get(1)->getName());
303
        $this->assertEquals('Stati Uniti', $result->getCountry()->getName());
304
        $this->assertEquals('US', $result->getCountry()->getCode());
305
        $this->assertEquals('America/New_York', $result->getTimezone());
306
    }
307
308
    public function testReverseWithRealCoordinates()
309
    {
310
        if (!isset($_SERVER['GEONAMES_USERNAME'])) {
311
            $this->markTestSkipped('You need to configure the GEONAMES_USERNAME value in phpunit.xml');
312
        }
313
314
        $provider = new Geonames($this->getHttpClient($_SERVER['GEONAMES_USERNAME']), $_SERVER['GEONAMES_USERNAME']);
315
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(51.50853, -0.12574));
316
317
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
318
        $this->assertCount(1, $results);
319
320
        /** @var Location $result */
321
        $result = $results->first();
322
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
323
        $this->assertEquals(51.50853, $result->getCoordinates()->getLatitude(), '', 0.01);
324
        $this->assertEquals(-0.12574, $result->getCoordinates()->getLongitude(), '', 0.01);
325
        $this->assertEquals('London', $result->getLocality());
326
        $this->assertCount(2, $result->getAdminLevels());
327
        $this->assertEquals('Greater London', $result->getAdminLevels()->get(2)->getName());
328
        $this->assertEquals('England', $result->getAdminLevels()->get(1)->getName());
329
        $this->assertEquals('United Kingdom', $result->getCountry()->getName());
330
        $this->assertEquals('GB', $result->getCountry()->getCode());
331
        $this->assertEquals('Europe/London', $result->getTimezone());
332
    }
333
334
    public function testReverseWithRealCoordinatesWithLocale()
335
    {
336
        if (!isset($_SERVER['GEONAMES_USERNAME'])) {
337
            $this->markTestSkipped('You need to configure the GEONAMES_USERNAME value in phpunit.xml');
338
        }
339
340
        $provider = new Geonames($this->getHttpClient($_SERVER['GEONAMES_USERNAME']), $_SERVER['GEONAMES_USERNAME']);
341
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(51.50853, -0.12574)->withLocale('it_IT'));
342
343
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
344
        $this->assertCount(1, $results);
345
346
        /** @var Location $result */
347
        $result = $results->first();
348
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
349
        $this->assertEquals(51.50853, $result->getCoordinates()->getLatitude(), '', 0.01);
350
        $this->assertEquals(-0.12574, $result->getCoordinates()->getLongitude(), '', 0.01);
351
        $this->assertEquals('Londra', $result->getLocality());
352
        $this->assertCount(2, $result->getAdminLevels());
353
        $this->assertEquals('Greater London', $result->getAdminLevels()->get(2)->getName());
354
        $this->assertEquals('Inghilterra', $result->getAdminLevels()->get(1)->getName());
355
        $this->assertEquals('Regno Unito', $result->getCountry()->getName());
356
        $this->assertEquals('GB', $result->getCountry()->getCode());
357
        $this->assertEquals('Europe/London', $result->getTimezone());
358
    }
359
360
    public function testReverseWithBadCoordinates()
361
    {
362
        $badCoordinateResponse = <<<'JSON'
363
{
364
    "geonames": [ ]
365
}
366
JSON;
367
        $provider = new Geonames($this->getMockedHttpClient($badCoordinateResponse), 'username');
368
        $result = $provider->reverseQuery(ReverseQuery::fromCoordinates(-80.000000, -170.000000));
369
370
        $this->assertInstanceOf(Collection::class, $result);
371
        $this->assertEquals(0, $result->count());
372
    }
373
}
374