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