|
| 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\IpInfo\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\IpInfo\IpInfo; |
| 20 | + |
| 21 | +class IpInfoTest extends BaseTestCase |
| 22 | +{ |
| 23 | + protected function getCacheDir() |
| 24 | + { |
| 25 | + return __DIR__.'/.cached_responses'; |
| 26 | + } |
| 27 | + |
| 28 | + public function testGetName() |
| 29 | + { |
| 30 | + $provider = new IpInfo($this->getMockedHttpClient()); |
| 31 | + $this->assertEquals('ip_info', $provider->getName()); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @expectedException \Geocoder\Exception\UnsupportedOperation |
| 36 | + * @expectedExceptionMessage The IpInfo provider does not support street addresses, only IP addresses. |
| 37 | + */ |
| 38 | + public function testGeocodeWithRandomString() |
| 39 | + { |
| 40 | + $provider = new IpInfo($this->getMockedHttpClient()); |
| 41 | + $provider->geocodeQuery(GeocodeQuery::create('foobar')); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @expectedException \Geocoder\Exception\UnsupportedOperation |
| 46 | + * @expectedExceptionMessage The IpInfo provider does not support street addresses, only IP addresses. |
| 47 | + */ |
| 48 | + public function testGeocodeWithAddress() |
| 49 | + { |
| 50 | + $provider = new IpInfo($this->getMockedHttpClient()); |
| 51 | + $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')); |
| 52 | + } |
| 53 | + |
| 54 | + /** @dataProvider provideLocalhostIps */ |
| 55 | + public function testGeocodeWithLocalhost($localhostIp) |
| 56 | + { |
| 57 | + $provider = new IpInfo($this->getMockedHttpClient()); |
| 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->assertNull($result->getCoordinates()); |
| 67 | + |
| 68 | + $this->assertNull($result->getPostalCode()); |
| 69 | + $this->assertNull($result->getTimezone()); |
| 70 | + $this->assertEmpty($result->getAdminLevels()); |
| 71 | + |
| 72 | + $this->assertEquals('localhost', $result->getLocality()); |
| 73 | + $this->assertEquals('localhost', $result->getCountry()->getName()); |
| 74 | + } |
| 75 | + |
| 76 | + public function provideLocalhostIps() |
| 77 | + { |
| 78 | + yield ['127.0.0.1']; |
| 79 | + yield ['::1']; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @expectedException \Geocoder\Exception\InvalidServerResponse |
| 84 | + */ |
| 85 | + public function testGeocodeWithRealIPv4GetsNullContent() |
| 86 | + { |
| 87 | + $provider = new IpInfo($this->getMockedHttpClient()); |
| 88 | + $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); |
| 89 | + } |
| 90 | + |
| 91 | + public function testGeocodeWithRealIPv4() |
| 92 | + { |
| 93 | + $provider = new IpInfo($this->getHttpClient()); |
| 94 | + $results = $provider->geocodeQuery(GeocodeQuery::create('74.125.45.100')); |
| 95 | + |
| 96 | + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
| 97 | + $this->assertCount(1, $results); |
| 98 | + |
| 99 | + /** @var Location $result */ |
| 100 | + $result = $results->first(); |
| 101 | + $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
| 102 | + $this->assertEquals(36.154, $result->getCoordinates()->getLatitude(), '', 0.001); |
| 103 | + $this->assertEquals(-95.9928, $result->getCoordinates()->getLongitude(), '', 0.001); |
| 104 | + $this->assertEquals(74102, $result->getPostalCode()); |
| 105 | + $this->assertEquals('Tulsa', $result->getLocality()); |
| 106 | + $this->assertCount(1, $result->getAdminLevels()); |
| 107 | + $this->assertEquals('Oklahoma', $result->getAdminLevels()->get(1)->getName()); |
| 108 | + $this->assertNull($result->getCountry()->getName()); |
| 109 | + $this->assertEquals('US', $result->getCountry()->getCode()); |
| 110 | + $this->assertNull($result->getTimezone()); |
| 111 | + } |
| 112 | + |
| 113 | + public function testGeocodeWithRealIPv6() |
| 114 | + { |
| 115 | + $provider = new IpInfo($this->getHttpClient()); |
| 116 | + $results = $provider->geocodeQuery(GeocodeQuery::create('2601:9:7680:363:75df:f491:6f85:352f')); |
| 117 | + |
| 118 | + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); |
| 119 | + $this->assertCount(1, $results); |
| 120 | + |
| 121 | + /** @var Location $result */ |
| 122 | + $result = $results->first(); |
| 123 | + $this->assertInstanceOf('\Geocoder\Model\Address', $result); |
| 124 | + $this->assertEquals(37.751, $result->getCoordinates()->getLatitude(), '', 0.001); |
| 125 | + $this->assertEquals(-97.822, $result->getCoordinates()->getLongitude(), '', 0.001); |
| 126 | + $this->assertNull($result->getPostalCode()); |
| 127 | + $this->assertEmpty($result->getLocality()); |
| 128 | + $this->assertNull($result->getCountry()->getName()); |
| 129 | + $this->assertEquals('US', $result->getCountry()->getCode()); |
| 130 | + $this->assertNull($result->getTimezone()); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @expectedException \Geocoder\Exception\UnsupportedOperation |
| 135 | + * @expectedExceptionMessage The IpInfo provider is not able to do reverse geocoding. |
| 136 | + */ |
| 137 | + public function testReverse() |
| 138 | + { |
| 139 | + $provider = new IpInfo($this->getMockedHttpClient()); |
| 140 | + $provider->reverseQuery(ReverseQuery::fromCoordinates(0, 0)); |
| 141 | + } |
| 142 | +} |
0 commit comments