Completed
Push — master ( 648ed6...8bdcbd )
by Tobias
04:25
created

GeoIPsTest::testGeocodeWithRealIPv4()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
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\GeoIPs\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\Provider\GeoIPs\GeoIPs;
21
22
class GeoIPsTest extends BaseTestCase
23
{
24
    public function testGetName()
25
    {
26
        $provider = new GeoIPs($this->getMockedHttpClient(), 'api_key');
27
        $this->assertEquals('geo_ips', $provider->getName());
28
    }
29
30
    protected function getCacheDir()
31
    {
32
        return __DIR__.'/.cached_responses';
33
    }
34
35
    /**
36
     * @expectedException \Geocoder\Exception\UnsupportedOperation
37
     * @expectedExceptionMessage The GeoIPs provider does not support street addresses, only IPv4 addresses.
38
     */
39
    public function testGeocodeWithAddress()
40
    {
41
        $provider = new GeoIPs($this->getMockedHttpClient(), 'api_key');
42
        $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
43
    }
44
45
    public function testGeocodeWithLocalhostIPv4()
46
    {
47
        $provider = new GeoIPs($this->getMockedHttpClient(), 'api_key');
48
        $results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
49
50
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
51
        $this->assertCount(1, $results);
52
53
        /** @var Location $result */
54
        $result = $results->first();
55
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
56
        $this->assertEquals('localhost', $result->getLocality());
57
        $this->assertEquals('localhost', $result->getCountry()->getName());
58
    }
59
60
    /**
61
     * @expectedException \Geocoder\Exception\UnsupportedOperation
62
     * @expectedExceptionMessage The GeoIPs provider does not support IPv6 addresses, only IPv4 addresses.
63
     */
64
    public function testGeocodeWithLocalhostIPv6()
65
    {
66
        $provider = new GeoIPs($this->getMockedHttpClient(), 'api_key');
67
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
68
    }
69
70
    /**
71
     * @expectedException \Geocoder\Exception\InvalidServerResponse
72
     */
73
    public function testGeocodeWithRealIPv4GetsNullContent()
74
    {
75
        $provider = new GeoIPs($this->getMockedHttpClient(null), 'api_key');
76
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
77
    }
78
79
    /**
80
     * @expectedException \Geocoder\Exception\InvalidServerResponse
81
     */
82
    public function testGeocodeWithRealIPv4GetsEmptyContent()
83
    {
84
        $provider = new GeoIPs($this->getMockedHttpClient(''), 'api_key');
85
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
86
    }
87
88
    public function testGeocodeWithRealIPv4GetsFakeContentFormattedEmpty()
89
    {
90
        $json = '{"response":{
91
            "status": "Propper Request",
92
            "message": "Success",
93
            "notes": "The following results has been returned",
94
            "code": "200_1",
95
            "location": {
96
                "ip" : "66.147.244.214",
97
                "owner" : "",
98
                "continent_name" : "",
99
                "continent_code" : "",
100
                "country_name" : "",
101
                "country_code" : "",
102
                "region_name" : "",
103
                "region_code" : "",
104
                "county_name" : "",
105
                "city_name" : "",
106
                "latitude" : "",
107
                "longitude" : "",
108
                "timezone" : ""
109
            },
110
            "unit_test": {
111
                "elapsed_time": "0.0676",
112
                "memory_usage": "2.2MB"
113
            }
114
        }}';
115
116
        $provider = new GeoIPs($this->getMockedHttpClient($json), 'api_key');
117
        $results = $provider->geocodeQuery(GeocodeQuery::create('66.147.244.214'));
118
119
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
120
        $this->assertCount(1, $results);
121
122
        /** @var Location $result */
123
        $result = $results->first();
124
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
125
        $this->assertNull($result->getCoordinates());
126
127
        $this->assertNull($result->getPostalCode());
128
        $this->assertNull($result->getLocality());
129
        $this->assertEmpty($result->getAdminLevels());
130
        $this->assertNull($result->getCountry()->getName());
131
        $this->assertNull($result->getCountry()->getCode());
132
        $this->assertNull($result->getTimezone());
133
    }
134
135
    public function testGeocodeWithRealIPv4GetsFakeContent()
136
    {
137
        $json = '{"response":{
138
            "status": "Propper Request",
139
            "message": "Success",
140
            "notes": "The following results has been returned",
141
            "code": "200_1",
142
            "location": {
143
                "ip" : "66.147.244.214",
144
                "owner" : "BLUEHOST INC.",
145
                "continent_name" : "NORTH AMERICA",
146
                "continent_code" : "NA",
147
                "country_name" : "UNITED STATES",
148
                "country_code" : "US",
149
                "region_name" : "UTAH",
150
                "region_code" : "UT",
151
                "county_name" : "UTAH",
152
                "city_name" : "PROVO",
153
                "latitude" : "40.3402",
154
                "longitude" : "-111.6073",
155
                "timezone" : "MST"
156
            }
157
        }}';
158
159
        $provider = new GeoIPs($this->getMockedHttpClient($json), 'api_key');
160
        $results = $provider->geocodeQuery(GeocodeQuery::create('66.147.244.214'));
161
162
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
163
        $this->assertCount(1, $results);
164
165
        /** @var Location $result */
166
        $result = $results->first();
167
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
168
        $this->assertEquals(40.3402, $result->getCoordinates()->getLatitude(), '', 0.0001);
169
        $this->assertEquals(-111.6073, $result->getCoordinates()->getLongitude(), '', 0.0001);
170
        $this->assertNull($result->getStreetName());
171
        $this->assertNull($result->getPostalCode());
172
        $this->assertEquals('PROVO', $result->getLocality());
173
        $this->assertCount(2, $result->getAdminLevels());
174
        $this->assertEquals('UTAH', $result->getAdminLevels()->get(2)->getName());
175
        $this->assertEquals('UTAH', $result->getAdminLevels()->get(1)->getName());
176
        $this->assertEquals('UT', $result->getAdminLevels()->get(1)->getCode());
177
        $this->assertEquals('UNITED STATES', $result->getCountry()->getName());
178
        $this->assertEquals('US', $result->getCountry()->getCode());
179
        $this->assertEquals('MST', $result->getTimezone());
180
    }
181
182
    /**
183
     * @expectedException \Geocoder\Exception\InvalidCredentials
184
     * @expectedExceptionMessage The API key associated with your request was not recognized.
185
     */
186
    public function testGeocodeWithRealIPv4AndInvalidApiKeyGetsFakeContent()
187
    {
188
        $provider = new GeoIPs(
189
            $this->getMockedHttpClient(
190
                '{
191
                    "error": {
192
                        "status": "Forbidden",
193
                        "message": "Not Authorized",
194
                        "notes": "The API key associated with your request was not recognized",
195
                        "code": "403_1",
196
                        "unit_test": {
197
                            "elapsed_time": "0.0474",
198
                            "memory_usage": "2.2MB"
199
                        }
200
                    }
201
                }'
202
            ),
203
            'api_key'
204
        );
205
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
206
    }
207
208
    /**
209
     * @expectedException \Geocoder\Exception\InvalidCredentials
210
     * @expectedExceptionMessage The API key has not been approved or has been disabled.
211
     */
212
    public function testGeocodeWithRealIPv4AndInvalidApiKeyGetsFakeContent2()
213
    {
214
        $provider = new GeoIPs(
215
            $this->getMockedHttpClient(
216
                '{
217
                    "error": {
218
                        "status": "Forbidden",
219
                        "message": "Account Inactive",
220
                        "notes": "The API key has not been approved or has been disabled.",
221
                        "code": "403_2",
222
                        "unit_test": {
223
                            "elapsed_time": "0.0474",
224
                            "memory_usage": "2.2MB"
225
                        }
226
                    }
227
                }'
228
            ),
229
            'api_key'
230
        );
231
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
232
    }
233
234
    /**
235
     * @expectedException \Geocoder\Exception\QuotaExceeded
236
     * @expectedExceptionMessage The service you have requested is over capacity.
237
     */
238
    public function testGeocodeWithRealIPv4AndQuotaExceeded()
239
    {
240
        $provider = new GeoIPs(
241
            $this->getMockedHttpClient(
242
                '{
243
                    "error": {
244
                        "status": "Forbidden",
245
                        "message": "Limit Exceeded",
246
                        "notes": "The service you have requested is over capacity.",
247
                        "code": "403_3",
248
                        "unit_test": {
249
                            "elapsed_time": "0.0474",
250
                            "memory_usage": "2.2MB"
251
                        }
252
                    }
253
                }'
254
            ),
255
            'api_key'
256
        );
257
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
258
    }
259
260
    /**
261
     * @expectedException \Geocoder\Exception\InvalidArgument
262
     * @expectedExceptionMessage The API call should include a valid IP address.
263
     */
264
    public function testGeocodeGetsFakeContentWithIpNotFound()
265
    {
266
        $provider = new GeoIPs(
267
            $this->getMockedHttpClient(
268
                '{
269
                    "error": {
270
                        "status": "Bad Request",
271
                        "message": "Error in the URI",
272
                        "notes": "The API call should include a valid IP address.",
273
                        "code": "400_2",
274
                        "unit_test": {
275
                            "elapsed_time": "0.0474",
276
                            "memory_usage": "2.2MB"
277
                        }
278
                    }
279
                }'
280
            ),
281
            'api_key'
282
        );
283
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
284
    }
285
286
    /**
287
     * @expectedException \Geocoder\Exception\InvalidCredentials
288
     * @expectedExceptionMessage The API call should include a API key parameter.
289
     */
290
    public function testGeocodeGetsFakeContentWithKeyNotFound()
291
    {
292
        $provider = new GeoIPs(
293
            $this->getMockedHttpClient(
294
                '{
295
                    "error": {
296
                        "status": "Bad Request",
297
                        "message": "Error in the URI",
298
                        "notes": "The API call should include a API key parameter.",
299
                        "code": "400_1",
300
                        "unit_test": {
301
                            "elapsed_time": "0.0474",
302
                            "memory_usage": "2.2MB"
303
                        }
304
                    }
305
                }'
306
            ),
307
            'api_key'
308
        );
309
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
310
    }
311
312
    public function testGeocodeWithRealIPv4()
313
    {
314
        if (!isset($_SERVER['GEOIPS_API_KEY'])) {
315
            $this->markTestSkipped('You need to configure the GEOIPS_API_KEY value in phpunit.xml');
316
        }
317
318
        $provider = new GeoIPs($this->getHttpClient($_SERVER['GEOIPS_API_KEY']), $_SERVER['GEOIPS_API_KEY']);
319
        $results = $provider->geocodeQuery(GeocodeQuery::create('66.147.244.214'));
320
321
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
322
        $this->assertCount(1, $results);
323
324
        /** @var Location $result */
325
        $result = $results->first();
326
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
327
        $this->assertEquals(40.3402, $result->getCoordinates()->getLatitude(), '', 0.0001);
328
        $this->assertEquals(-111.6073, $result->getCoordinates()->getLongitude(), '', 0.0001);
329
        $this->assertNull($result->getStreetName());
330
        $this->assertNull($result->getPostalCode());
331
        $this->assertEquals('PROVO', $result->getLocality());
332
        $this->assertCount(2, $result->getAdminLevels());
333
        $this->assertEquals('UTAH', $result->getAdminLevels()->get(2)->getName());
334
        $this->assertEquals('UTAH', $result->getAdminLevels()->get(1)->getName());
335
        $this->assertEquals('UT', $result->getAdminLevels()->get(1)->getCode());
336
        $this->assertEquals('UNITED STATES', $result->getCountry()->getName());
337
        $this->assertEquals('US', $result->getCountry()->getCode());
338
        $this->assertEquals('MST', $result->getTimezone());
339
    }
340
341
    public function testGeocodeWithRealIPv4ZeroResults()
342
    {
343
        if (!isset($_SERVER['GEOIPS_API_KEY'])) {
344
            $this->markTestSkipped('You need to configure the GEOIPS_API_KEY value in phpunit.xml');
345
        }
346
347
        $provider = new GeoIPs($this->getHttpClient($_SERVER['GEOIPS_API_KEY']), $_SERVER['GEOIPS_API_KEY']);
348
        $result = $provider->geocodeQuery(GeocodeQuery::create('255.255.150.96'));
349
350
        $this->assertInstanceOf(Collection::class, $result);
351
        $this->assertEquals(0, $result->count());
352
    }
353
354
    /**
355
     * @expectedException \Geocoder\Exception\UnsupportedOperation
356
     * @expectedExceptionMessage The GeoIPs provider is not able to do reverse geocoding.
357
     */
358
    public function testReverse()
359
    {
360
        $provider = new GeoIPs($this->getMockedHttpClient(), 'api_key');
361
        $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
362
    }
363
}
364