Completed
Push — master ( 5106b1...04c212 )
by Tobias
06:59
created

TomTomTest::testReverseWithRealCoordinates()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 20

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
dl 27
loc 27
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 20
nc 2
nop 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\TomTom\Tests;
14
15
use Geocoder\Collection;
16
use Geocoder\IntegrationTest\BaseTestCase;
17
use Geocoder\Location;
18
use Geocoder\Query\GeocodeQuery;
19
use Geocoder\Query\ReverseQuery;
20
use Geocoder\Provider\TomTom\TomTom;
21
22
class TomTomTest extends BaseTestCase
23
{
24
    protected function getCacheDir()
25
    {
26
        return __DIR__.'/.cached_responses';
27
    }
28
29
    public function testGetName()
30
    {
31
        $provider = new TomTom($this->getMockedHttpClient(), 'api_key');
32
        $this->assertEquals('tomtom', $provider->getName());
33
    }
34
35
    /**
36
     * @expectedException \Geocoder\Exception\InvalidServerResponse
37
     */
38
    public function testGeocodeWithAddress()
39
    {
40
        $provider = new TomTom($this->getMockedHttpClient(), 'api_key');
41
        $provider->geocodeQuery(GeocodeQuery::create('Tagensvej 47, 2200 København N'));
42
    }
43
44
    public function testGeocodeWithRealAddress()
45
    {
46
        $provider = new TomTom($this->getHttpClient($_SERVER['TOMTOM_MAP_KEY']), $_SERVER['TOMTOM_MAP_KEY']);
47
        $results = $provider->geocodeQuery(GeocodeQuery::create('Tagensvej 47, 2200 København N')->withLocale('en-GB'));
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->assertEquals(55.70, $result->getCoordinates()->getLatitude(), '', 0.001);
56
        $this->assertEquals(12.5529, $result->getCoordinates()->getLongitude(), '', 0.001);
57
        $this->assertNull($result->getBounds());
58
        $this->assertEquals(47, $result->getStreetNumber());
59
        $this->assertEquals('Tagensvej', $result->getStreetName());
60
        $this->assertEquals(2200, $result->getPostalCode());
61
        $this->assertEquals('Copenhagen', $result->getLocality());
62
        $this->assertCount(0, $result->getAdminLevels());
63
        $this->assertEquals('Denmark', $result->getCountry()->getName());
64
        $this->assertEquals('DK', $result->getCountry()->getCode());
65
        $this->assertNull($result->getTimezone());
66
    }
67
68
    public function testGeocodeWithRealAddressWithFrenchLocale()
69
    {
70
        $provider = new TomTom($this->getHttpClient($_SERVER['TOMTOM_MAP_KEY']), $_SERVER['TOMTOM_MAP_KEY']);
71
        $results = $provider->geocodeQuery(GeocodeQuery::create('Tagensvej 47, 2200 København N')->withLocale('fr-FR'));
72
73
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
74
        $this->assertCount(1, $results);
75
76
        /** @var Location $result */
77
        $result = $results->first();
78
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
79
    }
80
81
    /**
82
     * @expectedException \Geocoder\Exception\UnsupportedOperation
83
     * @expectedExceptionMessage The TomTom provider does not support IP addresses, only street addresses.
84
     */
85
    public function testGeocodeWithLocalhostIPv4()
86
    {
87
        $provider = new TomTom($this->getMockedHttpClient(), 'api_key');
88
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
89
    }
90
91
    /**
92
     * @expectedException \Geocoder\Exception\UnsupportedOperation
93
     * @expectedExceptionMessage The TomTom provider does not support IP addresses, only street addresses.
94
     */
95
    public function testGeocodeWithLocalhostIPv6()
96
    {
97
        $provider = new TomTom($this->getMockedHttpClient(), 'api_key');
98
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
99
    }
100
101
    /**
102
     * @expectedException \Geocoder\Exception\UnsupportedOperation
103
     * @expectedExceptionMessage The TomTom provider does not support IP addresses, only street addresses.
104
     */
105
    public function testGeocodeWithIPv4()
106
    {
107
        $provider = new TomTom($this->getMockedHttpClient(), 'api_key');
108
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
109
    }
110
111
    /**
112
     * @expectedException \Geocoder\Exception\UnsupportedOperation
113
     * @expectedExceptionMessage The TomTom provider does not support IP addresses, only street addresses.
114
     */
115
    public function testGeocodeWithIPv6()
116
    {
117
        $provider = new TomTom($this->getMockedHttpClient(), 'api_key');
118
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59'));
119
    }
120
121
    /**
122
     * @expectedException \Geocoder\Exception\InvalidCredentials
123
     * @expectedExceptionMessage No API key provided
124
     */
125
    public function testWithoutApiKey()
126
    {
127
        $provider = new TomTom($this->getMockedHttpClient(), '');
128
    }
129
130
    /**
131
     * @expectedException \Geocoder\Exception\InvalidServerResponse
132
     */
133
    public function testReverse()
134
    {
135
        $provider = new TomTom($this->getMockedHttpClient(), 'api_key');
136
        $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
137
    }
138
139
    public function testReverseError400()
140
    {
141
        $error400 = <<<'XML'
142
<errorResponse version="" description="" errorCode="400"/>
143
XML;
144
145
        $provider = new TomTom($this->getMockedHttpClient($error400), 'api_key');
146
        $result = $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
147
148
        $this->assertInstanceOf(Collection::class, $result);
149
        $this->assertEquals(0, $result->count());
150
    }
151
152
    public function testReverseWithRealCoordinates()
153
    {
154
        if (!isset($_SERVER['TOMTOM_MAP_KEY'])) {
155
            $this->markTestSkipped('You need to configure the TOMTOM_MAP_KEY value in phpunit.xml');
156
        }
157
158
        $provider = new TomTom($this->getHttpClient($_SERVER['TOMTOM_MAP_KEY']), $_SERVER['TOMTOM_MAP_KEY']);
159
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.86321648955345, 2.3887719959020615));
160
161
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
162
        $this->assertCount(1, $results);
163
164
        /** @var Location $result */
165
        $result = $results->first();
166
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
167
        $this->assertEquals(48.86323, $result->getCoordinates()->getLatitude(), '', 0.001);
168
        $this->assertEquals(2.38877, $result->getCoordinates()->getLongitude(), '', 0.001);
169
        $this->assertNull($result->getBounds());
170
        $this->assertEquals('Avenue Gambetta', $result->getStreetName());
171
        $this->assertNull($result->getPostalCode());
172
        $this->assertEquals('Paris', $result->getLocality());
173
        $this->assertEquals('20e Arrondissement Paris', $result->getSubLocality());
174
        $this->assertCount(0, $result->getAdminLevels());
175
        $this->assertEquals('France', $result->getCountry()->getName());
176
        $this->assertEquals('FR', $result->getCountry()->getCode());
177
        $this->assertNull($result->getTimezone());
178
    }
179
180
    public function testGeocodeWithRealCoordinates()
181
    {
182
        if (!isset($_SERVER['TOMTOM_MAP_KEY'])) {
183
            $this->markTestSkipped('You need to configure the TOMTOM_MAP_KEY value in phpunit.xml');
184
        }
185
186
        $provider = new TomTom($this->getHttpClient($_SERVER['TOMTOM_MAP_KEY']), $_SERVER['TOMTOM_MAP_KEY']);
187
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(56.5231, 10.0659));
188
189
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
190
        $this->assertCount(1, $results);
191
192
        /** @var Location $result */
193
        $result = $results->first();
194
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
195
        $this->assertEquals(56.52435, $result->getCoordinates()->getLatitude(), '', 0.001);
196
        $this->assertEquals(10.06744, $result->getCoordinates()->getLongitude(), '', 0.001);
197
        $this->assertNull($result->getBounds());
198
        $this->assertEquals(16, $result->getStreetNumber());
199
        $this->assertEquals('Stabelsvej', $result->getStreetName());
200
        $this->assertNull($result->getPostalCode());
201
        $this->assertEquals('Spentrup', $result->getLocality());
202
        $this->assertEquals('Spentrup', $result->getSubLocality());
203
        $this->assertCount(0, $result->getAdminLevels());
204
        $this->assertEquals('Denmark', $result->getCountry()->getName());
205
        $this->assertEquals('DK', $result->getCountry()->getCode());
206
        $this->assertNull($result->getTimezone());
207
    }
208
}
209