Completed
Push — master ( 791438...8ed65d )
by Tobias
05:08
created

HostIpTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 108
rs 10
c 0
b 0
f 0
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\HostIp\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\HostIp\HostIp;
20
21
class HostIpTest extends BaseTestCase
22
{
23
    protected function getCacheDir()
24
    {
25
        return __DIR__.'/.cached_responses';
26
    }
27
28
    public function testGetName()
29
    {
30
        $provider = new HostIp($this->getMockedHttpClient());
31
        $this->assertEquals('host_ip', $provider->getName());
32
    }
33
34
    /**
35
     * @expectedException \Geocoder\Exception\UnsupportedOperation
36
     * @expectedExceptionMessage The HostIp provider does not support Street addresses.
37
     */
38
    public function testGeocodeWithAddress()
39
    {
40
        $provider = new HostIp($this->getMockedHttpClient());
41
        $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
42
    }
43
44
    public function testGeocodeWithLocalhostIPv4()
45
    {
46
        $provider = new HostIp($this->getMockedHttpClient());
47
        $results = $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
48
49
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
50
        $this->assertCount(1, $results);
51
52
        /** @var Location $result */
53
        $result = $results->first();
54
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
55
        $this->assertNull($result->getCoordinates());
56
57
        $this->assertNull($result->getPostalCode());
58
        $this->assertNull($result->getTimezone());
59
        $this->assertEmpty($result->getAdminLevels());
60
61
        $this->assertEquals('localhost', $result->getLocality());
62
        $this->assertEquals('localhost', $result->getCountry()->getName());
63
    }
64
65
    /**
66
     * @expectedException \Geocoder\Exception\UnsupportedOperation
67
     * @expectedExceptionMessage The HostIp provider does not support IPv6 addresses.
68
     */
69
    public function testGeocodeWithLocalhostIPv6()
70
    {
71
        $provider = new HostIp($this->getMockedHttpClient());
72
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
73
    }
74
75
    public function testGeocodeWithRealIPv4()
76
    {
77
        $provider = new HostIp($this->getHttpClient());
78
        $results = $provider->geocodeQuery(GeocodeQuery::create('88.188.221.14'));
79
80
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
81
        $this->assertCount(1, $results);
82
83
        /** @var Location $result */
84
        $result = $results->first();
85
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
86
        $this->assertEquals(45.5333, $result->getCoordinates()->getLatitude(), '', 0.0001);
87
        $this->assertEquals(2.6167, $result->getCoordinates()->getLongitude(), '', 0.0001);
88
        $this->assertNull($result->getPostalCode());
89
        $this->assertEquals('Aulnat', $result->getLocality());
90
        $this->assertEmpty($result->getAdminLevels());
91
        $this->assertEquals('FRANCE', $result->getCountry()->getName());
92
        $this->assertEquals('FR', $result->getCountry()->getCode());
93
    }
94
95
    /**
96
     * @expectedException \Geocoder\Exception\UnsupportedOperation
97
     * @expectedExceptionMessage The HostIp provider does not support IPv6 addresses.
98
     */
99
    public function testGeocodeWithRealIPv6()
100
    {
101
        $provider = new HostIp($this->getHttpClient());
102
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:88.188.221.14'));
103
    }
104
105
    /**
106
     * @expectedException \Geocoder\Exception\UnsupportedOperation
107
     * @expectedExceptionMessage The HostIp provider is not able to do reverse geocoding.
108
     */
109
    public function testReverse()
110
    {
111
        $provider = new HostIp($this->getMockedHttpClient());
112
        $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
113
    }
114
115
    public function testGeocodeWithAnotherIp()
116
    {
117
        $provider = new HostIp($this->getHttpClient());
118
        $results = $provider->geocodeQuery(GeocodeQuery::create('33.33.33.22'));
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
        $this->assertNull($result->getCoordinates());
127
    }
128
}
129