|
| 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() |
| 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() |
| 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 | + /** |
| 52 | + * @expectedException \Geocoder\Exception\InvalidArgument |
| 53 | + * @expectedExceptionMessage Given IP2Location BIN file "NOT_EXIST.BIN" does not exist. |
| 54 | + */ |
| 55 | + public function testThrowIfNotExistBinaryFileGiven() |
| 56 | + { |
| 57 | + new IP2LocationBinary('NOT_EXIST.BIN'); |
| 58 | + } |
| 59 | + |
| 60 | + public function testLocationResultContainsExpectedFieldsForAnAmericanIp() |
| 61 | + { |
| 62 | + $provider = new IP2LocationBinary($this->binaryFile); |
| 63 | + $results = $provider->geocodeQuery(GeocodeQuery::create('8.8.8.8')); |
| 64 | + |
| 65 | + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
| 66 | + $this->assertCount(1, $results); |
| 67 | + |
| 68 | + /** @var Location $result */ |
| 69 | + $result = $results->first(); |
| 70 | + $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
| 71 | + |
| 72 | + $this->assertEquals('37.405990600586', $result->getCoordinates()->getLatitude(), '', 0.001); |
| 73 | + $this->assertEquals('-122.07851409912', $result->getCoordinates()->getLongitude(), '', 0.001); |
| 74 | + $this->assertNull($result->getBounds()); |
| 75 | + $this->assertNull($result->getStreetNumber()); |
| 76 | + $this->assertNull($result->getStreetName()); |
| 77 | + $this->assertEquals('94043', $result->getPostalCode()); |
| 78 | + $this->assertEquals('Mountain View', $result->getLocality()); |
| 79 | + $this->assertNull($result->getSubLocality()); |
| 80 | + $this->assertCount(1, $result->getAdminLevels()); |
| 81 | + $this->assertEquals('California', $result->getAdminLevels()->get(1)->getName()); |
| 82 | + $this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
| 83 | + $this->assertEquals('United States', $result->getCountry()->getName()); |
| 84 | + $this->assertEquals('US', $result->getCountry()->getCode()); |
| 85 | + $this->assertNull($result->getTimezone()); |
| 86 | + } |
| 87 | + |
| 88 | + public function testLocationResultContainsExpectedFieldsForAChinaIp() |
| 89 | + { |
| 90 | + $provider = new IP2LocationBinary($this->binaryFile); |
| 91 | + $results = $provider->geocodeQuery(GeocodeQuery::create('123.123.123.123')); |
| 92 | + |
| 93 | + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
| 94 | + $this->assertCount(1, $results); |
| 95 | + |
| 96 | + /** @var Location $result */ |
| 97 | + $result = $results->first(); |
| 98 | + $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
| 99 | + |
| 100 | + $this->assertEquals('39.907501220703', $result->getCoordinates()->getLatitude(), '', 0.001); |
| 101 | + $this->assertEquals('116.39723205566', $result->getCoordinates()->getLongitude(), '', 0.001); |
| 102 | + $this->assertNull($result->getBounds()); |
| 103 | + $this->assertNull($result->getStreetNumber()); |
| 104 | + $this->assertNull($result->getStreetName()); |
| 105 | + $this->assertEquals('100006', $result->getPostalCode()); |
| 106 | + $this->assertEquals('Beijing', $result->getLocality()); |
| 107 | + $this->assertNull($result->getSubLocality()); |
| 108 | + $this->assertCount(1, $result->getAdminLevels()); |
| 109 | + $this->assertEquals('Beijing', $result->getAdminLevels()->get(1)->getName()); |
| 110 | + $this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
| 111 | + $this->assertEquals('China', $result->getCountry()->getName()); |
| 112 | + $this->assertEquals('CN', $result->getCountry()->getCode()); |
| 113 | + $this->assertNull($result->getTimezone()); |
| 114 | + } |
| 115 | + |
| 116 | + public function testGeocodeWithRealIPv6() |
| 117 | + { |
| 118 | + $provider = new IP2LocationBinary($this->binaryFile); |
| 119 | + $results = $provider->geocodeQuery(GeocodeQuery::create('2001:4860:4860::8888')); |
| 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 | + |
| 128 | + $this->assertEquals('37.386051', $result->getCoordinates()->getLatitude(), '', 0.001); |
| 129 | + $this->assertEquals('-122.083847', $result->getCoordinates()->getLongitude(), '', 0.001); |
| 130 | + $this->assertNull($result->getBounds()); |
| 131 | + $this->assertNull($result->getStreetNumber()); |
| 132 | + $this->assertNull($result->getStreetName()); |
| 133 | + $this->assertEquals('94041', $result->getPostalCode()); |
| 134 | + $this->assertEquals('Mountain View', $result->getLocality()); |
| 135 | + $this->assertNull($result->getSubLocality()); |
| 136 | + $this->assertCount(1, $result->getAdminLevels()); |
| 137 | + $this->assertEquals('California', $result->getAdminLevels()->get(1)->getName()); |
| 138 | + $this->assertNull($result->getAdminLevels()->get(1)->getCode()); |
| 139 | + $this->assertEquals('United States', $result->getCountry()->getName()); |
| 140 | + $this->assertEquals('US', $result->getCountry()->getCode()); |
| 141 | + $this->assertNull($result->getTimezone()); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @dataProvider provideIps |
| 146 | + */ |
| 147 | + public function testFindLocationByIp($ip, $expectedCity, $expectedCountry) |
| 148 | + { |
| 149 | + $provider = new IP2LocationBinary($this->binaryFile); |
| 150 | + $results = $provider->geocodeQuery(GeocodeQuery::create($ip)); |
| 151 | + |
| 152 | + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
| 153 | + $this->assertCount(1, $results); |
| 154 | + |
| 155 | + /** @var Location $result */ |
| 156 | + $result = $results->first(); |
| 157 | + $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
| 158 | + $this->assertEquals($expectedCity, $result->getLocality()); |
| 159 | + $this->assertEquals($expectedCountry, $result->getCountry()->getName()); |
| 160 | + } |
| 161 | + |
| 162 | + public function testGetName() |
| 163 | + { |
| 164 | + $provider = new IP2LocationBinary($this->binaryFile); |
| 165 | + |
| 166 | + $this->assertEquals('ip2location_binary', $provider->getName()); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @expectedException \Geocoder\Exception\UnsupportedOperation |
| 171 | + * @expectedExceptionMessage The IP2LocationBinary provider does not support street addresses. |
| 172 | + */ |
| 173 | + public function testThrowIfInvalidIpAddressGiven() |
| 174 | + { |
| 175 | + $provider = new IP2LocationBinary($this->binaryFile); |
| 176 | + |
| 177 | + $provider->geocodeQuery(GeocodeQuery::create('invalidIp')); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @expectedException \Geocoder\Exception\UnsupportedOperation |
| 182 | + * @expectedExceptionMessage The IP2LocationBinary is not able to do reverse geocoding. |
| 183 | + */ |
| 184 | + public function testThrowOnReverseMethodUsage() |
| 185 | + { |
| 186 | + $provider = new IP2LocationBinary($this->binaryFile); |
| 187 | + |
| 188 | + $provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0)); |
| 189 | + } |
| 190 | +} |
0 commit comments