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\Ipstack\Tests; |
14
|
|
|
|
15
|
|
|
use Geocoder\IntegrationTest\BaseTestCase; |
16
|
|
|
use Geocoder\Provider\Ipstack\Ipstack; |
17
|
|
|
use Geocoder\Query\GeocodeQuery; |
18
|
|
|
use Geocoder\Query\ReverseQuery; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Jonas Gielen <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class IpstackTest extends BaseTestCase |
24
|
|
|
{ |
25
|
|
|
protected function getCacheDir() |
26
|
|
|
{ |
27
|
|
|
return __DIR__.'/.cached_responses'; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function testGetName() |
31
|
|
|
{ |
32
|
|
|
$provider = new Ipstack($this->getMockedHttpClient(), 'api_key'); |
33
|
|
|
$this->assertEquals('ipstack', $provider->getName()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @expectedException \Geocoder\Exception\InvalidCredentials |
38
|
|
|
* @expectedExceptionMessage No API key provided. |
39
|
|
|
*/ |
40
|
|
|
public function testGeocodeWithNoKey() |
41
|
|
|
{ |
42
|
|
|
$provider = new Ipstack($this->getMockedHttpClient(), ''); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
47
|
|
|
* @expectedExceptionMessage The Ipstack provider does not support street addresses. |
48
|
|
|
*/ |
49
|
|
|
public function testGeocodeWithAddress() |
50
|
|
|
{ |
51
|
|
|
$provider = new Ipstack($this->getMockedHttpClient(), 'api_key'); |
52
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testGeocodeWithLocalhostIPv4() |
56
|
|
|
{ |
57
|
|
|
$provider = new Ipstack($this->getMockedHttpClient(), 'api_key'); |
58
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1')); |
59
|
|
|
|
60
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
61
|
|
|
$this->assertCount(1, $results); |
62
|
|
|
|
63
|
|
|
/** @var Location $result */ |
64
|
|
|
$result = $results->first(); |
65
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
66
|
|
|
$this->assertEquals('localhost', $result->getLocality()); |
67
|
|
|
$this->assertEquals('localhost', $result->getCountry()->getName()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGeocodeWithLocalhostIPv6() |
71
|
|
|
{ |
72
|
|
|
$provider = new Ipstack($this->getMockedHttpClient(), 'api_key'); |
73
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('::1')); |
74
|
|
|
|
75
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
76
|
|
|
$this->assertCount(1, $results); |
77
|
|
|
|
78
|
|
|
/** @var Location $result */ |
79
|
|
|
$result = $results->first(); |
80
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
81
|
|
|
$this->assertEquals('localhost', $result->getLocality()); |
82
|
|
|
$this->assertEquals('localhost', $result->getCountry()->getName()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testGeocodeWithRealIPv4() |
86
|
|
|
{ |
87
|
|
|
$provider = new Ipstack($this->getHttpClient($_SERVER['IPSTACK_API_KEY']), $_SERVER['IPSTACK_API_KEY']); |
88
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
89
|
|
|
|
90
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
91
|
|
|
$this->assertCount(1, $results); |
92
|
|
|
|
93
|
|
|
/** @var Location $result */ |
94
|
|
|
$result = $results->first(); |
95
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
96
|
|
|
$this->assertEquals(37.751, $result->getCoordinates()->getLatitude(), '', 0.01); |
97
|
|
|
$this->assertEquals(-97.822, $result->getCoordinates()->getLongitude(), '', 0.01); |
98
|
|
|
$this->assertEquals('United States', $result->getCountry()->getName()); |
99
|
|
|
$this->assertEquals('US', $result->getCountry()->getCode()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testGeocodeWithRealIPv4InFrench() |
103
|
|
|
{ |
104
|
|
|
$provider = new Ipstack($this->getHttpClient($_SERVER['IPSTACK_API_KEY']), $_SERVER['IPSTACK_API_KEY']); |
105
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')->withLocale('fr')); |
106
|
|
|
|
107
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
108
|
|
|
$this->assertCount(1, $results); |
109
|
|
|
|
110
|
|
|
/** @var Location $result */ |
111
|
|
|
$result = $results->first(); |
112
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
113
|
|
|
$this->assertEquals(37.751, $result->getCoordinates()->getLatitude(), '', 0.01); |
114
|
|
|
$this->assertEquals(-97.822, $result->getCoordinates()->getLongitude(), '', 0.01); |
115
|
|
|
$this->assertEquals('États-Unis', $result->getCountry()->getName()); |
116
|
|
|
$this->assertEquals('US', $result->getCountry()->getCode()); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @expectedException \Geocoder\Exception\UnsupportedOperation |
121
|
|
|
* @expectedExceptionMessage The Ipstack provider is not able to do reverse geocoding. |
122
|
|
|
*/ |
123
|
|
|
public function testReverse() |
124
|
|
|
{ |
125
|
|
|
$provider = new Ipstack($this->getMockedHttpClient(), 'api_key'); |
126
|
|
|
$provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @expectedException \Geocoder\Exception\InvalidArgument |
131
|
|
|
* @expectedExceptionMessage Invalid request (a required parameter is missing). |
132
|
|
|
*/ |
133
|
|
|
public function testGeocodeWith301Code() |
134
|
|
|
{ |
135
|
|
|
$json = <<<'JSON' |
136
|
|
|
{"success":false,"error":{"code":301}} |
137
|
|
|
JSON; |
138
|
|
|
$provider = new Ipstack($this->getMockedHttpClient($json), 'api_key'); |
139
|
|
|
$result = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @expectedException \Geocoder\Exception\InvalidArgument |
144
|
|
|
* @expectedExceptionMessage Bulk requests are not supported on your plan. Please upgrade your subscription. |
145
|
|
|
*/ |
146
|
|
|
public function testGeocodeWith303Code() |
147
|
|
|
{ |
148
|
|
|
$json = <<<'JSON' |
149
|
|
|
{"success":false,"error":{"code":303,"type":"batch_not_supported_on_plan","info":"Bulk requests are not supported on your plan. Please upgrade your subscription."}} |
150
|
|
|
JSON; |
151
|
|
|
$provider = new Ipstack($this->getMockedHttpClient($json), 'api_key'); |
152
|
|
|
$result = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @expectedException \Geocoder\Exception\QuotaExceeded |
157
|
|
|
* @expectedExceptionMessage The maximum allowed amount of monthly API requests has been reached. |
158
|
|
|
*/ |
159
|
|
|
public function testGeocodeWith104Code() |
160
|
|
|
{ |
161
|
|
|
$json = <<<'JSON' |
162
|
|
|
{"success":false,"error":{"code":104}} |
163
|
|
|
JSON; |
164
|
|
|
$provider = new Ipstack($this->getMockedHttpClient($json), 'api_key'); |
165
|
|
|
$result = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @expectedException \Geocoder\Exception\InvalidCredentials |
170
|
|
|
* @expectedExceptionMessage No API Key was specified or an invalid API Key was specified. |
171
|
|
|
*/ |
172
|
|
|
public function testGeocodeWith101Code() |
173
|
|
|
{ |
174
|
|
|
$json = <<<'JSON' |
175
|
|
|
{"success":false,"error":{"code":101,"type":"invalid_access_key","info":"You have not supplied a valid API Access Key. [Technical Support: [email protected]]"}} |
176
|
|
|
JSON; |
177
|
|
|
$provider = new Ipstack($this->getMockedHttpClient($json), 'api_key'); |
178
|
|
|
$result = $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59')); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|