|
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\IP2LocationBinary\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\IP2LocationBinary\IP2LocationBinary; |
|
20
|
|
|
|
|
21
|
|
|
class IP2LocationBinaryTest extends BaseTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
private $binaryFile; |
|
24
|
|
|
|
|
25
|
|
|
public function setUp(): void |
|
26
|
|
|
{ |
|
27
|
|
|
// Download this BIN database from https://lite.ip2location.com/database/ip-country-region-city-latitude-longitude-zipcode |
|
28
|
|
|
$this->binaryFile = __DIR__.'/fixtures/IP2LOCATION-LITE-DB9.IPV6.BIN'; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function getCacheDir() |
|
32
|
|
|
{ |
|
33
|
|
|
return __DIR__.'/.cached_responses'; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function setUpBeforeClass(): void |
|
37
|
|
|
{ |
|
38
|
|
|
if (false == class_exists('\\IP2Location\\Database')) { |
|
|
|
|
|
|
39
|
|
|
self::markTestSkipped('The IP2Location\'s official library required to run these tests.'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function provideIps() |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
|
|
'8.8.8.8' => ['8.8.8.8', 'Mountain View', 'United States'], |
|
47
|
|
|
'123.123.123.123' => ['123.123.123.123', 'Beijing', 'China'], |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function testThrowIfNotExistBinaryFileGiven() |
|
52
|
|
|
{ |
|
53
|
|
|
$this->expectException(\Geocoder\Exception\InvalidArgument::class); |
|
54
|
|
|
$this->expectExceptionMessage('Given IP2Location BIN file "NOT_EXIST.BIN" does not exist.'); |
|
55
|
|
|
|
|
56
|
|
|
new IP2LocationBinary('NOT_EXIST.BIN'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function testLocationResultContainsExpectedFieldsForAnAmericanIp() |
|
60
|
|
|
{ |
|
61
|
|
|
$provider = new IP2LocationBinary($this->binaryFile); |
|
62
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('8.8.8.8')); |
|
63
|
|
|
|
|
64
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
|
65
|
|
|
$this->assertCount(1, $results); |
|
66
|
|
|
|
|
67
|
|
|
/** @var Location $result */ |
|
68
|
|
|
$result = $results->first(); |
|
69
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertEquals('37.405990600586', $result->getCoordinates()->getLatitude(), '', 0.001); |
|
|
|
|
|
|
72
|
|
|
$this->assertEquals('-122.07851409912', $result->getCoordinates()->getLongitude(), '', 0.001); |
|
73
|
|
|
$this->assertNull($result->getBounds()); |
|
74
|
|
|
$this->assertNull($result->getStreetNumber()); |
|
75
|
|
|
$this->assertNull($result->getStreetName()); |
|
76
|
|
|
$this->assertEquals('94043', $result->getPostalCode()); |
|
77
|
|
|
$this->assertEquals('Mountain View', $result->getLocality()); |
|
78
|
|
|
$this->assertNull($result->getSubLocality()); |
|
79
|
|
|
$this->assertCount(1, $result->getAdminLevels()); |
|
80
|
|
|
$this->assertEquals('California', $result->getAdminLevels()->get(1)->getName()); |
|
81
|
|
|
$this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
|
82
|
|
|
$this->assertEquals('United States', $result->getCountry()->getName()); |
|
83
|
|
|
$this->assertEquals('US', $result->getCountry()->getCode()); |
|
84
|
|
|
$this->assertNull($result->getTimezone()); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testLocationResultContainsExpectedFieldsForAChinaIp() |
|
88
|
|
|
{ |
|
89
|
|
|
$provider = new IP2LocationBinary($this->binaryFile); |
|
90
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('123.123.123.123')); |
|
91
|
|
|
|
|
92
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
|
93
|
|
|
$this->assertCount(1, $results); |
|
94
|
|
|
|
|
95
|
|
|
/** @var Location $result */ |
|
96
|
|
|
$result = $results->first(); |
|
97
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals('39.907501220703', $result->getCoordinates()->getLatitude(), '', 0.001); |
|
|
|
|
|
|
100
|
|
|
$this->assertEquals('116.39723205566', $result->getCoordinates()->getLongitude(), '', 0.001); |
|
101
|
|
|
$this->assertNull($result->getBounds()); |
|
102
|
|
|
$this->assertNull($result->getStreetNumber()); |
|
103
|
|
|
$this->assertNull($result->getStreetName()); |
|
104
|
|
|
$this->assertEquals('100006', $result->getPostalCode()); |
|
105
|
|
|
$this->assertEquals('Beijing', $result->getLocality()); |
|
106
|
|
|
$this->assertNull($result->getSubLocality()); |
|
107
|
|
|
$this->assertCount(1, $result->getAdminLevels()); |
|
108
|
|
|
$this->assertEquals('Beijing', $result->getAdminLevels()->get(1)->getName()); |
|
109
|
|
|
$this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
|
110
|
|
|
$this->assertEquals('China', $result->getCountry()->getName()); |
|
111
|
|
|
$this->assertEquals('CN', $result->getCountry()->getCode()); |
|
112
|
|
|
$this->assertNull($result->getTimezone()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function testGeocodeWithRealIPv6() |
|
116
|
|
|
{ |
|
117
|
|
|
$provider = new IP2LocationBinary($this->binaryFile); |
|
118
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create('2001:4860:4860::8888')); |
|
119
|
|
|
|
|
120
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
|
121
|
|
|
$this->assertCount(1, $results); |
|
122
|
|
|
|
|
123
|
|
|
/** @var Location $result */ |
|
124
|
|
|
$result = $results->first(); |
|
125
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertEquals('37.386051', $result->getCoordinates()->getLatitude(), '', 0.001); |
|
|
|
|
|
|
128
|
|
|
$this->assertEquals('-122.083847', $result->getCoordinates()->getLongitude(), '', 0.001); |
|
129
|
|
|
$this->assertNull($result->getBounds()); |
|
130
|
|
|
$this->assertNull($result->getStreetNumber()); |
|
131
|
|
|
$this->assertNull($result->getStreetName()); |
|
132
|
|
|
$this->assertEquals('94041', $result->getPostalCode()); |
|
133
|
|
|
$this->assertEquals('Mountain View', $result->getLocality()); |
|
134
|
|
|
$this->assertNull($result->getSubLocality()); |
|
135
|
|
|
$this->assertCount(1, $result->getAdminLevels()); |
|
136
|
|
|
$this->assertEquals('California', $result->getAdminLevels()->get(1)->getName()); |
|
137
|
|
|
$this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
|
138
|
|
|
$this->assertEquals('United States', $result->getCountry()->getName()); |
|
139
|
|
|
$this->assertEquals('US', $result->getCountry()->getCode()); |
|
140
|
|
|
$this->assertNull($result->getTimezone()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @dataProvider provideIps |
|
145
|
|
|
*/ |
|
146
|
|
|
public function testFindLocationByIp($ip, $expectedCity, $expectedCountry) |
|
147
|
|
|
{ |
|
148
|
|
|
$provider = new IP2LocationBinary($this->binaryFile); |
|
149
|
|
|
$results = $provider->geocodeQuery(GeocodeQuery::create($ip)); |
|
150
|
|
|
|
|
151
|
|
|
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
|
152
|
|
|
$this->assertCount(1, $results); |
|
153
|
|
|
|
|
154
|
|
|
/** @var Location $result */ |
|
155
|
|
|
$result = $results->first(); |
|
156
|
|
|
$this->assertInstanceOf('\Geocoder\Model\Address', $result); |
|
157
|
|
|
$this->assertEquals($expectedCity, $result->getLocality()); |
|
158
|
|
|
$this->assertEquals($expectedCountry, $result->getCountry()->getName()); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testGetName() |
|
162
|
|
|
{ |
|
163
|
|
|
$provider = new IP2LocationBinary($this->binaryFile); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertEquals('ip2location_binary', $provider->getName()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function testThrowIfInvalidIpAddressGiven() |
|
169
|
|
|
{ |
|
170
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
|
171
|
|
|
$this->expectExceptionMessage('The IP2LocationBinary provider does not support street addresses.'); |
|
172
|
|
|
|
|
173
|
|
|
$provider = new IP2LocationBinary($this->binaryFile); |
|
174
|
|
|
|
|
175
|
|
|
$provider->geocodeQuery(GeocodeQuery::create('invalidIp')); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testThrowOnReverseMethodUsage() |
|
179
|
|
|
{ |
|
180
|
|
|
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class); |
|
181
|
|
|
$this->expectExceptionMessage('The IP2LocationBinary is not able to do reverse geocoding.'); |
|
182
|
|
|
|
|
183
|
|
|
$provider = new IP2LocationBinary($this->binaryFile); |
|
184
|
|
|
|
|
185
|
|
|
$provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0)); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.