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\IP2Location\Tests; |
14
|
|
|
|
15
|
|
|
use Geocoder\IntegrationTest\BaseTestCase; |
16
|
|
|
use Geocoder\Location; |
17
|
|
|
use Geocoder\Query\GeocodeQuery; |
18
|
|
|
use Geocoder\Query\ReverseQuery; |
19
|
|
|
use Geocoder\Provider\IP2Location\IP2Location; |
20
|
|
|
|
21
|
|
|
class IP2LocationTest extends BaseTestCase |
22
|
|
|
{ |
23
|
|
|
protected function getCacheDir() |
24
|
|
|
{ |
25
|
|
|
return __DIR__.'/.cached_responses'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testGetName() |
29
|
|
|
{ |
30
|
|
|
$provider = new IP2Location($this->getMockedHttpClient(), 'api_key'); |
31
|
|
|
$this->assertEquals('ip2location', $provider->getName()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testGeocodeWithRandomString() |
35
|
|
|
{ |
36
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
37
|
|
|
$this->expectExceptionMessage('The IP2Location provider does not support street addresses, only IP addresses.'); |
38
|
|
|
|
39
|
|
|
$provider = new IP2Location($this->getMockedHttpClient(), 'api_key'); |
40
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('foobar')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testGeocodeWithAddress() |
44
|
|
|
{ |
45
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
46
|
|
|
$this->expectExceptionMessage('The IP2Location provider does not support street addresses, only IP addresses.'); |
47
|
|
|
|
48
|
|
|
$provider = new IP2Location($this->getMockedHttpClient(), 'api_key'); |
49
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testGeocodeWithInvalidKey() |
53
|
|
|
{ |
54
|
|
|
$this->expectException(\Geocoder\Exception\InvalidCredentials::class); |
55
|
|
|
$this->expectExceptionMessage('API Key provided is not valid.'); |
56
|
|
|
|
57
|
|
|
$provider = new IP2Location($this->getHttpClient('invalid_key'), 'api_key'); |
58
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGeocodeWithInvalidIPAddress() |
62
|
|
|
{ |
63
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
64
|
|
|
$this->expectExceptionMessage('The IP2Location provider does not support street addresses, only IP addresses.'); |
65
|
|
|
|
66
|
|
|
$provider = new IP2Location($this->getMockedHttpClient(), 'api_key'); |
67
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('300.23.255.5')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGeocodeWithRealIPv4() |
71
|
|
|
{ |
72
|
|
|
if (!isset($_SERVER['IP2LOCATION_API_KEY'])) { |
73
|
|
|
$this->markTestSkipped('You need to configure the IP2LOCATION_API_KEY value in phpunit.xml'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$provider = new IP2Location($this->getHttpClient($_SERVER['IP2LOCATION_API_KEY']), $_SERVER['IP2LOCATION_API_KEY']); |
77
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); |
78
|
|
|
|
79
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
80
|
|
|
$this->assertCount(1, $results); |
81
|
|
|
|
82
|
|
|
/** @var Location $result */ |
83
|
|
|
$result = $results->first(); |
84
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
85
|
|
|
$this->assertEqualsWithDelta(36.154, $result->getCoordinates()->getLatitude(), 0.001); |
86
|
|
|
$this->assertEqualsWithDelta(-95.9928, $result->getCoordinates()->getLongitude(), 0.001); |
87
|
|
|
$this->assertEquals(74101, $result->getPostalCode()); |
88
|
|
|
$this->assertEquals('Tulsa', $result->getLocality()); |
89
|
|
|
$this->assertCount(1, $result->getAdminLevels()); |
90
|
|
|
$this->assertEquals('Oklahoma', $result->getAdminLevels()->get(1)->getName()); |
91
|
|
|
$this->assertEquals('United States', $result->getCountry()->getName()); |
92
|
|
|
$this->assertEquals('US', $result->getCountry()->getCode()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testGeocodeWithRealIPv6() |
96
|
|
|
{ |
97
|
|
|
if (!isset($_SERVER['IP2LOCATION_API_KEY'])) { |
98
|
|
|
$this->markTestSkipped('You need to configure the IP2LOCATION_API_KEY value in phpunit.xml'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$provider = new IP2Location($this->getHttpClient($_SERVER['IP2LOCATION_API_KEY']), $_SERVER['IP2LOCATION_API_KEY']); |
102
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.125.45.100')); |
103
|
|
|
|
104
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
105
|
|
|
$this->assertCount(1, $results); |
106
|
|
|
|
107
|
|
|
/** @var Location $result */ |
108
|
|
|
$result = $results->first(); |
109
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
110
|
|
|
$this->assertEqualsWithDelta(36.154, $result->getCoordinates()->getLatitude(), 0.001); |
111
|
|
|
$this->assertEqualsWithDelta(-95.9928, $result->getCoordinates()->getLongitude(), 0.001); |
112
|
|
|
$this->assertEquals(74101, $result->getPostalCode()); |
113
|
|
|
$this->assertEquals('Tulsa', $result->getLocality()); |
114
|
|
|
$this->assertCount(1, $result->getAdminLevels()); |
115
|
|
|
$this->assertEquals('Oklahoma', $result->getAdminLevels()->get(1)->getName()); |
116
|
|
|
$this->assertEquals('United States', $result->getCountry()->getName()); |
117
|
|
|
$this->assertEquals('US', $result->getCountry()->getCode()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testReverse() |
121
|
|
|
{ |
122
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
123
|
|
|
$this->expectExceptionMessage('The IP2Location provider is not able to do reverse geocoding.'); |
124
|
|
|
|
125
|
|
|
$provider = new IP2Location($this->getMockedHttpClient(), 'api_key'); |
126
|
|
|
$provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|