|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Geocoder\Tests\Provider; |
| 4 | + |
| 5 | +use Geocoder\Tests\TestCase; |
| 6 | +use Geocoder\Provider\IpInfoProvider; |
| 7 | + |
| 8 | +class IpInfoProviderTest extends TestCase |
| 9 | +{ |
| 10 | + public function testGetName() |
| 11 | + { |
| 12 | + $provider = new IpInfoProvider($this->getMockAdapter($this->never())); |
| 13 | + $this->assertEquals('ip_info', $provider->getName()); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * @expectedException \Geocoder\Exception\UnsupportedException |
| 18 | + * @expectedExceptionMessage The IpInfoProvider does not support Street addresses. |
| 19 | + */ |
| 20 | + public function testGetGeocodedDataWithNull() |
| 21 | + { |
| 22 | + $provider = new IpInfoProvider($this->getMockAdapter($this->never())); |
| 23 | + $provider->getGeocodedData(null); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @expectedException \Geocoder\Exception\UnsupportedException |
| 28 | + * @expectedExceptionMessage The IpInfoProvider does not support Street addresses. |
| 29 | + */ |
| 30 | + public function testGetGeocodedDataWithEmpty() |
| 31 | + { |
| 32 | + $provider = new IpInfoProvider($this->getMockAdapter($this->never())); |
| 33 | + $provider->getGeocodedData(''); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @expectedException \Geocoder\Exception\UnsupportedException |
| 38 | + * @expectedExceptionMessage The IpInfoProvider does not support Street addresses. |
| 39 | + */ |
| 40 | + public function testGetGeocodedDataWithAddress() |
| 41 | + { |
| 42 | + $provider = new IpInfoProvider($this->getMockAdapter($this->never())); |
| 43 | + $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); |
| 44 | + } |
| 45 | + |
| 46 | + public function testGetGeocodedDataWithLocalhostIPv4() |
| 47 | + { |
| 48 | + $provider = new IpInfoProvider($this->getMockAdapter($this->never())); |
| 49 | + $result = $provider->getGeocodedData('127.0.0.1'); |
| 50 | + |
| 51 | + $this->assertInternalType('array', $result); |
| 52 | + $this->assertCount(1, $result); |
| 53 | + |
| 54 | + $result = $result[0]; |
| 55 | + $this->assertInternalType('array', $result); |
| 56 | + $this->assertArrayNotHasKey('latitude', $result); |
| 57 | + $this->assertArrayNotHasKey('longitude', $result); |
| 58 | + $this->assertArrayNotHasKey('zipcode', $result); |
| 59 | + $this->assertArrayNotHasKey('timezone', $result); |
| 60 | + |
| 61 | + $this->assertEquals('localhost', $result['city']); |
| 62 | + $this->assertEquals('localhost', $result['region']); |
| 63 | + $this->assertEquals('localhost', $result['county']); |
| 64 | + $this->assertEquals('localhost', $result['country']); |
| 65 | + } |
| 66 | + |
| 67 | + public function testGetGeocodedDataWithLocalhostIPv6() |
| 68 | + { |
| 69 | + $provider = new IpInfoProvider($this->getMockAdapter($this->never())); |
| 70 | + $result = $provider->getGeocodedData('::1'); |
| 71 | + |
| 72 | + $this->assertInternalType('array', $result); |
| 73 | + $this->assertCount(1, $result); |
| 74 | + |
| 75 | + $result = $result[0]; |
| 76 | + $this->assertInternalType('array', $result); |
| 77 | + $this->assertArrayNotHasKey('latitude', $result); |
| 78 | + $this->assertArrayNotHasKey('longitude', $result); |
| 79 | + $this->assertArrayNotHasKey('zipcode', $result); |
| 80 | + $this->assertArrayNotHasKey('timezone', $result); |
| 81 | + |
| 82 | + $this->assertEquals('localhost', $result['city']); |
| 83 | + $this->assertEquals('localhost', $result['region']); |
| 84 | + $this->assertEquals('localhost', $result['county']); |
| 85 | + $this->assertEquals('localhost', $result['country']); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @expectedException \Geocoder\Exception\NoResultException |
| 90 | + * @expectedExceptionMessage Could not execute query http://ipinfo.io/74.200.247.59/json |
| 91 | + */ |
| 92 | + public function testGetGeocodedDataWithRealIPv4GetsNullContent() |
| 93 | + { |
| 94 | + $provider = new IpInfoProvider($this->getMockAdapterReturns(null)); |
| 95 | + $provider->getGeocodedData('74.200.247.59'); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @expectedException \Geocoder\Exception\NoResultException |
| 100 | + * @expectedExceptionMessage Could not execute query http://ipinfo.io/74.200.247.59/json |
| 101 | + */ |
| 102 | + public function testGetGeocodedDataWithRealIPv4GetsEmptyContent() |
| 103 | + { |
| 104 | + $provider = new IpInfoProvider($this->getMockAdapterReturns('')); |
| 105 | + $provider->getGeocodedData('74.200.247.59'); |
| 106 | + } |
| 107 | + |
| 108 | + public function testGetGeocodedDataWithRealIPv4() |
| 109 | + { |
| 110 | + $json = <<<JSON |
| 111 | +{ |
| 112 | + "ip": "74.200.247.59", |
| 113 | + "hostname": "wordpress.com", |
| 114 | + "city": "Plano", |
| 115 | + "region": "Texas", |
| 116 | + "country": "US", |
| 117 | + "loc": "33.0347,-96.8134", |
| 118 | + "org": "AS22576 Layered Technologies, Inc.", |
| 119 | + "postal": "75093" |
| 120 | +} |
| 121 | +JSON; |
| 122 | + |
| 123 | + $provider = new IpInfoProvider($this->getMockAdapterReturns($json)); |
| 124 | + $result = $provider->getGeocodedData('74.200.247.59'); |
| 125 | + |
| 126 | + $this->assertInternalType('array', $result); |
| 127 | + $this->assertCount(1, $result); |
| 128 | + |
| 129 | + $result = $result[0]; |
| 130 | + $this->assertInternalType('array', $result); |
| 131 | + $this->assertEquals(33.0347, $result['latitude'], '', 0.01); |
| 132 | + $this->assertEquals(-96.8134, $result['longitude'], '', 0.01); |
| 133 | + $this->assertEquals(75093, $result['zipcode']); |
| 134 | + $this->assertEquals('Plano', $result['city']); |
| 135 | + $this->assertEquals('Texas', $result['region']); |
| 136 | + $this->assertEquals('US', $result['countryCode']); |
| 137 | + } |
| 138 | + |
| 139 | + public function testGetGeocodedDataWithRealIPv6() |
| 140 | + { |
| 141 | + $json = <<<JSON |
| 142 | +{ |
| 143 | + "ip": "74.200.247.59", |
| 144 | + "hostname": "No Hostname", |
| 145 | + "city": "Plano", |
| 146 | + "region": "Texas", |
| 147 | + "country": "US", |
| 148 | + "loc": "33.0347,-96.8134", |
| 149 | + "org": "AS22576 Layered Technologies, Inc.", |
| 150 | + "postal": "75093" |
| 151 | +} |
| 152 | +JSON; |
| 153 | + |
| 154 | + $provider = new IpInfoProvider($this->getMockAdapterReturns($json)); |
| 155 | + $result = $provider->getGeocodedData('::ffff:74.200.247.59'); |
| 156 | + |
| 157 | + $this->assertInternalType('array', $result); |
| 158 | + $this->assertCount(1, $result); |
| 159 | + |
| 160 | + $result = $result[0]; |
| 161 | + $this->assertInternalType('array', $result); |
| 162 | + $this->assertEquals(33.0347, $result['latitude'], '', 0.01); |
| 163 | + $this->assertEquals(-96.8134, $result['longitude'], '', 0.01); |
| 164 | + $this->assertEquals(75093, $result['zipcode']); |
| 165 | + $this->assertEquals('Plano', $result['city']); |
| 166 | + $this->assertEquals('Texas', $result['region']); |
| 167 | + $this->assertEquals('US', $result['countryCode']); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @expectedException \Geocoder\Exception\NoResultException |
| 172 | + * @expectedExceptionMessage Could not execute query http://ipinfo.io/255.255.255.255/json |
| 173 | + */ |
| 174 | + public function testGetGeocodedDataWithoutLocation() |
| 175 | + { |
| 176 | + $json = <<<JSON |
| 177 | +{ |
| 178 | + "ip": "255.255.255.255", |
| 179 | + "hostname": "No Hostname", |
| 180 | + "loc": "", |
| 181 | + "bogon": true |
| 182 | +} |
| 183 | +JSON; |
| 184 | + |
| 185 | + $provider = new IpInfoProvider($this->getMockAdapterReturns($json)); |
| 186 | + $provider->getGeocodedData('255.255.255.255'); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @expectedException \Geocoder\Exception\NoResultException |
| 191 | + * @expectedExceptionMessage Could not execute query http://ipinfo.io/::ffff:74.200.247.59/json |
| 192 | + */ |
| 193 | + public function testGetGeocodedDataWithRealIPv6GetsNullContent() |
| 194 | + { |
| 195 | + $provider = new IpInfoProvider($this->getMockAdapterReturns(null)); |
| 196 | + $provider->getGeocodedData('::ffff:74.200.247.59'); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * @expectedException \Geocoder\Exception\UnsupportedException |
| 201 | + * @expectedExceptionMessage The IpInfoProvider is not able to do reverse geocoding. |
| 202 | + */ |
| 203 | + public function testGetReverseData() |
| 204 | + { |
| 205 | + $provider = new IpInfoProvider($this->getMockAdapter($this->never())); |
| 206 | + $provider->getReversedData(array(1, 2)); |
| 207 | + } |
| 208 | +} |
0 commit comments