GeocodeEarthTest::testGeocodeWithLocalhostIPv6()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 7
rs 10
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\GeocodeEarth\Tests;
14
15
use Geocoder\Collection;
16
use Geocoder\IntegrationTest\BaseTestCase;
17
use Geocoder\Provider\GeocodeEarth\GeocodeEarth;
18
use Geocoder\Query\GeocodeQuery;
19
use Geocoder\Query\ReverseQuery;
20
21
class GeocodeEarthTest extends BaseTestCase
22
{
23
    protected function getCacheDir()
24
    {
25
        return __DIR__.'/.cached_responses';
26
    }
27
28
    public function testGetName()
29
    {
30
        $provider = new GeocodeEarth($this->getMockedHttpClient(), 'api_key');
31
        $this->assertEquals('geocode_earth', $provider->getName());
32
    }
33
34
    public function testGeocode()
35
    {
36
        $provider = new GeocodeEarth($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['GEOCODE_EARTH_API_KEY'])) {
46
            $this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
47
        }
48
49
        $provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_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(3, $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['GEOCODE_EARTH_API_KEY'])) {
71
            $this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
72
        }
73
74
        $provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_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(4, $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['GEOCODE_EARTH_API_KEY'])) {
99
            $this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
100
        }
101
102
        $provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_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['GEOCODE_EARTH_API_KEY'])) {
117
            $this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
118
        }
119
120
        $provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_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['GEOCODE_EARTH_API_KEY'])) {
170
            $this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
171
        }
172
173
        $provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_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 testGeocodeNoBounds()
197
    {
198
        if (!isset($_SERVER['GEOCODE_EARTH_API_KEY'])) {
199
            $this->markTestSkipped('You need to configure the GEOCODE_EARTH_API_KEY value in phpunit.xml');
200
        }
201
202
        $provider = new GeocodeEarth($this->getHttpClient($_SERVER['GEOCODE_EARTH_API_KEY']), $_SERVER['GEOCODE_EARTH_API_KEY']);
203
        $results = $provider->geocodeQuery(GeocodeQuery::create('dworzec centralny'));
204
205
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
206
        $this->assertCount(5, $results);
207
208
        /** @var \Geocoder\Model\Address $result */
209
        $result = $results->first();
210
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
211
        $this->assertEqualsWithDelta(52.230428, $result->getCoordinates()->getLatitude(), 0.01);
212
        $this->assertEqualsWithDelta(21.004552, $result->getCoordinates()->getLongitude(), 0.01);
213
        $this->assertEquals('Warsaw', $result->getLocality());
214
        $this->assertEquals('Poland', $result->getCountry()->getName());
215
        $this->assertEquals('POL', $result->getCountry()->getCode());
216
        $this->assertNull($result->getBounds());
217
    }
218
219
    public function testGeocodeQuotaExceeded()
220
    {
221
        $this->expectException(\Geocoder\Exception\QuotaExceeded::class);
222
        $this->expectExceptionMessage('Valid request but quota exceeded.');
223
224
        $provider = new GeocodeEarth(
225
            $this->getMockedHttpClient(
226
                '{
227
                    "meta": {
228
                        "version": 1,
229
                        "status_code": 429
230
                    },
231
                    "results": {
232
                        "error": {
233
                            "type": "QpsExceededError",
234
                            "message": "Queries per second exceeded: Queries exceeded (6 allowed)."
235
                        }
236
                    }
237
                }'
238
            ),
239
            'api_key'
240
        );
241
        $provider->geocodeQuery(GeocodeQuery::create('New York'));
242
    }
243
244
    public function testGeocodeInvalidApiKey()
245
    {
246
        $this->expectException(\Geocoder\Exception\InvalidCredentials::class);
247
        $this->expectExceptionMessage('Invalid or missing api key.');
248
249
        $provider = new GeocodeEarth(
250
            $this->getMockedHttpClient(
251
                '{
252
                    "meta": {
253
                        "version": 1,
254
                        "status_code": 403
255
                    },
256
                    "results": {
257
                        "error": {
258
                            "type": "KeyError",
259
                            "message": "No api_key specified."
260
                        }
261
                    }
262
                }'
263
            ),
264
            'api_key'
265
        );
266
        $provider->geocodeQuery(GeocodeQuery::create('New York'));
267
    }
268
269
    public function testGeocodeWithLocalhostIPv4()
270
    {
271
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
272
        $this->expectExceptionMessage('The geocode_earth provider does not support IP addresses, only street addresses.');
273
274
        $provider = new GeocodeEarth($this->getMockedHttpClient(), 'api_key');
275
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
276
    }
277
278
    public function testGeocodeWithLocalhostIPv6()
279
    {
280
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
281
        $this->expectExceptionMessage('The geocode_earth provider does not support IP addresses, only street addresses.');
282
283
        $provider = new GeocodeEarth($this->getMockedHttpClient(), 'api_key');
284
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
285
    }
286
287
    public function testGeocodeWithRealIPv4()
288
    {
289
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
290
        $this->expectExceptionMessage('The geocode_earth provider does not support IP addresses, only street addresses.');
291
292
        $provider = new GeocodeEarth($this->getMockedHttpClient(), 'api_key');
293
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
294
    }
295
296
    public function testGeocodeWithRealIPv6()
297
    {
298
        $this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
299
        $this->expectExceptionMessage('The geocode_earth provider does not support IP addresses, only street addresses.');
300
301
        $provider = new GeocodeEarth($this->getMockedHttpClient(), 'api_key');
302
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59'));
303
    }
304
}
305