/
HostIpXmlTest.php
124 lines (99 loc) · 4.51 KB
/
HostIpXmlTest.php
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
<?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\HostIp\Tests;
use Geocoder\IntegrationTest\BaseTestCase;
use Geocoder\Location;
use Geocoder\Provider\HostIp\HostIpXml;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
class HostIpXmlTest extends BaseTestCase
{
protected function getCacheDir()
{
return __DIR__.'/.cached_responses';
}
public function testGetName()
{
$provider = new HostIpXml($this->getMockedHttpClient());
$this->assertEquals('host_ip_xml', $provider->getName());
}
public function testGeocodeWithAddress()
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The Geocoder\Provider\HostIp\HostIpXml provider does not support Street addresses.');
$provider = new HostIpXml($this->getMockedHttpClient());
$provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
}
public function testGeocodeWithLocalhostIPv4()
{
$provider = new HostIpXml($this->getMockedHttpClient());
$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->assertNull($result->getCoordinates());
$this->assertNull($result->getPostalCode());
$this->assertNull($result->getTimezone());
$this->assertEmpty($result->getAdminLevels());
$this->assertEquals('localhost', $result->getLocality());
$this->assertEquals('localhost', $result->getCountry()->getName());
}
public function testGeocodeWithLocalhostIPv6()
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The HostIp provider does not support IPv6 addresses.');
$provider = new HostIpXml($this->getMockedHttpClient());
$provider->geocodeQuery(GeocodeQuery::create('::1'));
}
public function testGeocodeWithRealIPv4()
{
$provider = new HostIpXml($this->getHttpClient());
$results = $provider->geocodeQuery(GeocodeQuery::create('77.38.216.139'));
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(1, $results);
/** @var Location $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertEquals(56.8833, $result->getCoordinates()->getLatitude(), '', 0.0001);
$this->assertEquals(24.0833, $result->getCoordinates()->getLongitude(), '', 0.0001);
$this->assertNull($result->getPostalCode());
$this->assertEquals('Riga', $result->getLocality());
$this->assertEmpty($result->getAdminLevels());
$this->assertEquals('LATVIA', $result->getCountry()->getName());
$this->assertEquals('LV', $result->getCountry()->getCode());
}
public function testGeocodeWithRealIPv6()
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The HostIp provider does not support IPv6 addresses.');
$provider = new HostIpXml($this->getHttpClient());
$provider->geocodeQuery(GeocodeQuery::create('::ffff:88.188.221.14'));
}
public function testReverse()
{
$this->expectException(\Geocoder\Exception\UnsupportedOperation::class);
$this->expectExceptionMessage('The HostIp provider is not able to do reverse geocoding.');
$provider = new HostIpXml($this->getMockedHttpClient());
$provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
}
public function testGeocodeWithAnotherIp()
{
$provider = new HostIpXml($this->getHttpClient());
$results = $provider->geocodeQuery(GeocodeQuery::create('33.33.33.22'));
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
$this->assertCount(1, $results);
/** @var Location $result */
$result = $results->first();
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
$this->assertNull($result->getCoordinates());
}
}