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\IpInfoDb\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\IpInfoDb\IpInfoDb; |
20
|
|
|
|
21
|
|
|
class IpInfoDbTest extends BaseTestCase |
22
|
|
|
{ |
23
|
|
|
protected function getCacheDir() |
24
|
|
|
{ |
25
|
|
|
return __DIR__.'/.cached_responses'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @expectedException \Geocoder\Exception\InvalidArgument |
30
|
|
|
* @expectedExceptionMessage Invalid precision value "foo" (allowed values: "city", "country"). |
31
|
|
|
*/ |
32
|
|
|
public function testConstructWithInvalidPrecision() |
33
|
|
|
{ |
34
|
|
|
new IpInfoDb($this->getMockedHttpClient(), 'api_key', 'foo'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testGetName() |
38
|
|
|
{ |
39
|
|
|
$provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); |
40
|
|
|
$this->assertEquals('ip_info_db', $provider->getName()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
45
|
|
|
* @expectedExceptionMessage The IpInfoDb provider does not support street addresses, only IPv4 addresses. |
46
|
|
|
*/ |
47
|
|
|
public function testGeocodeWithRandomString() |
48
|
|
|
{ |
49
|
|
|
$provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); |
50
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('foobar')); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
55
|
|
|
* @expectedExceptionMessage The IpInfoDb provider does not support street addresses, only IPv4 addresses. |
56
|
|
|
*/ |
57
|
|
|
public function testGeocodeWithAddress() |
58
|
|
|
{ |
59
|
|
|
$provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); |
60
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testGeocodeWithLocalhostIPv4() |
64
|
|
|
{ |
65
|
|
|
$provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); |
66
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
67
|
|
|
|
68
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
69
|
|
|
$this->assertCount(1, $results); |
70
|
|
|
|
71
|
|
|
/** @var Location $result */ |
72
|
|
|
$result = $results->first(); |
73
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
74
|
|
|
$this->assertNull($result->getCoordinates()); |
75
|
|
|
|
76
|
|
|
$this->assertNull($result->getPostalCode()); |
77
|
|
|
$this->assertNull($result->getTimezone()); |
78
|
|
|
$this->assertEmpty($result->getAdminLevels()); |
79
|
|
|
|
80
|
|
|
$this->assertEquals('localhost', $result->getLocality()); |
81
|
|
|
$this->assertEquals('localhost', $result->getCountry()->getName()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
86
|
|
|
* @expectedExceptionMessage The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses. |
87
|
|
|
*/ |
88
|
|
|
public function testGeocodeWithLocalhostIPv6() |
89
|
|
|
{ |
90
|
|
|
$provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); |
91
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('::1')); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @expectedException \Geocoder\Exception\InvalidServerResponse |
96
|
|
|
*/ |
97
|
|
|
public function testGeocodeWithRealIPv4GetsNullContent() |
98
|
|
|
{ |
99
|
|
|
$provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); |
100
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @expectedException \Geocoder\Exception\InvalidServerResponse |
105
|
|
|
*/ |
106
|
|
|
public function testGeocodeWithRealIPv4GetsEmptyContent() |
107
|
|
|
{ |
108
|
|
|
$provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); |
109
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGeocodeWithRealIPv4() |
113
|
|
|
{ |
114
|
|
|
if (!isset($_SERVER['IPINFODB_API_KEY'])) { |
115
|
|
|
$this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$provider = new IpInfoDb($this->getHttpClient($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY']); |
119
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); |
120
|
|
|
|
121
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
122
|
|
|
$this->assertCount(1, $results); |
123
|
|
|
|
124
|
|
|
/** @var Location $result */ |
125
|
|
|
$result = $results->first(); |
126
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
127
|
|
|
$this->assertEquals(36.154, $result->getCoordinates()->getLatitude(), '', 0.001); |
128
|
|
|
$this->assertEquals(-95.9928, $result->getCoordinates()->getLongitude(), '', 0.001); |
129
|
|
|
$this->assertEquals(74101, $result->getPostalCode()); |
130
|
|
|
$this->assertEquals('Tulsa', $result->getLocality()); |
131
|
|
|
$this->assertCount(1, $result->getAdminLevels()); |
132
|
|
|
$this->assertEquals('Oklahoma', $result->getAdminLevels()->get(1)->getName()); |
133
|
|
|
$this->assertEquals('United States', $result->getCountry()->getName()); |
134
|
|
|
$this->assertEquals('United States', $result->getCountry()->getCode()); |
135
|
|
|
$this->assertEquals('America/New_York', $result->getTimezone()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
140
|
|
|
* @expectedExceptionMessage The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses. |
141
|
|
|
*/ |
142
|
|
|
public function testGeocodeWithRealIPv6() |
143
|
|
|
{ |
144
|
|
|
if (!isset($_SERVER['IPINFODB_API_KEY'])) { |
145
|
|
|
$this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml'); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$provider = new IpInfoDb($this->getHttpClient($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY']); |
149
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('::ffff:74.125.45.100')); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @group temp |
154
|
|
|
*/ |
155
|
|
|
public function testGetGeocodedDataWithCountryPrecision() |
156
|
|
|
{ |
157
|
|
|
if (!isset($_SERVER['IPINFODB_API_KEY'])) { |
158
|
|
|
$this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml'); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$provider = new IpInfoDb($this->getHttpClient($_SERVER['IPINFODB_API_KEY']), $_SERVER['IPINFODB_API_KEY'], 'country'); |
162
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); |
163
|
|
|
|
164
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
165
|
|
|
$this->assertCount(1, $results); |
166
|
|
|
|
167
|
|
|
/** @var Location $result */ |
168
|
|
|
$result = $results->first(); |
169
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
170
|
|
|
$this->assertNull($result->getCoordinates()); |
171
|
|
|
|
172
|
|
|
$this->assertNull($result->getPostalCode()); |
173
|
|
|
$this->assertNull($result->getLocality()); |
174
|
|
|
$this->assertEmpty($result->getAdminLevels()); |
175
|
|
|
$this->assertEquals('United States', $result->getCountry()->getName()); |
176
|
|
|
$this->assertEquals('United States', $result->getCountry()->getCode()); |
177
|
|
|
$this->assertNull($result->getTimezone()); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
182
|
|
|
* @expectedExceptionMessage The IpInfoDb provider is not able to do reverse geocoding. |
183
|
|
|
*/ |
184
|
|
|
public function testReverse() |
185
|
|
|
{ |
186
|
|
|
$provider = new IpInfoDb($this->getMockedHttpClient(), 'api_key'); |
187
|
|
|
$provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0)); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|