Completed
Push — master ( a59d97...cf2b73 )
by Tobias
05:59
created

MapzenTest   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 268
Duplicated Lines 5.97 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 7
dl 16
loc 268
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Mapzen\Tests;
14
15
use Geocoder\Collection;
16
use Geocoder\IntegrationTest\BaseTestCase;
17
use Geocoder\Query\GeocodeQuery;
18
use Geocoder\Query\ReverseQuery;
19
use Geocoder\Provider\Mapzen\Mapzen;
20
21
/**
22
 * @author Gary Gale <[email protected]>
23
 */
24
class MapzenTest extends BaseTestCase
25
{
26
    protected function getCacheDir()
27
    {
28
        return __DIR__.'/.cached_responses';
29
    }
30
31
    public function testGetName()
32
    {
33
        $provider = new Mapzen($this->getMockedHttpClient(), 'api_key');
34
        $this->assertEquals('mapzen', $provider->getName());
35
    }
36
37
    public function testGeocode()
38
    {
39
        $provider = new Mapzen($this->getMockedHttpClient('{}'), 'api_key');
40
        $result = $provider->geocodeQuery(GeocodeQuery::create('foobar'));
41
42
        $this->assertInstanceOf(Collection::class, $result);
43
        $this->assertEquals(0, $result->count());
44
    }
45
46
    public function testSslSchema()
47
    {
48
        $provider = new Mapzen($this->getMockedHttpClient('{}'), 'api_key', true);
49
        $result = $provider->geocodeQuery(GeocodeQuery::create('foobar'));
50
51
        $this->assertInstanceOf(Collection::class, $result);
52
        $this->assertEquals(0, $result->count());
53
    }
54
55
    public function testGeocodeWithRealAddress()
56
    {
57
        if (!isset($_SERVER['MAPZEN_API_KEY'])) {
58
            $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml');
59
        }
60
61
        $provider = new Mapzen($this->getHttpClient($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']);
62
        $results = $provider->geocodeQuery(GeocodeQuery::create('242 Acklam Road, London, United Kingdom'));
63
64
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
65
        $this->assertCount(1, $results);
66
67
        /** @var \Geocoder\Model\Address $result */
68
        $result = $results->first();
69
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
70
        $this->assertEquals(51.521124, $result->getCoordinates()->getLatitude(), '', 0.01);
71
        $this->assertEquals(-0.20360200000000001, $result->getCoordinates()->getLongitude(), '', 0.01);
72
        $this->assertEquals('Acklam Road', $result->getStreetName());
73
        $this->assertEquals('London', $result->getLocality());
74
        $this->assertCount(4, $result->getAdminLevels());
75
        $this->assertEquals('London', $result->getAdminLevels()->get(2)->getName());
76
        $this->assertEquals('Kensington and Chelsea', $result->getAdminLevels()->get(1)->getName());
77
        $this->assertEquals('United Kingdom', $result->getCountry()->getName());
78
        $this->assertEquals('GBR', $result->getCountry()->getCode());
79
    }
80
81
    public function testReverseWithRealCoordinates()
82
    {
83
        if (!isset($_SERVER['MAPZEN_API_KEY'])) {
84
            $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml');
85
        }
86
87
        $provider = new Mapzen($this->getHttpClient($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']);
88
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(54.0484068, -2.7990345));
89
90
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
91
        $this->assertCount(5, $results);
92
93
        /** @var \Geocoder\Model\Address $result */
94
        $result = $results->first();
95
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
96
        $this->assertEquals(54.048411999999999, $result->getCoordinates()->getLatitude(), '', 0.001);
97
        $this->assertEquals(-2.7989549999999999, $result->getCoordinates()->getLongitude(), '', 0.001);
98
        $this->assertNull($result->getStreetNumber());
99
        $this->assertEquals('Gage Street', $result->getStreetName());
100
        $this->assertEquals('LA1 1UH', $result->getPostalCode());
101
        $this->assertEquals('Lancaster', $result->getLocality());
102
        $this->assertCount(4, $result->getAdminLevels());
103
        $this->assertEquals('Lancashire', $result->getAdminLevels()->get(1)->getName());
104
        $this->assertEquals('England', $result->getAdminLevels()->get(3)->getName());
105
        $this->assertEquals('United Kingdom', $result->getCountry()->getName());
106
        $this->assertEquals('GBR', $result->getCountry()->getCode());
107
    }
108
109
    public function testReverseWithVillage()
110
    {
111
        if (!isset($_SERVER['MAPZEN_API_KEY'])) {
112
            $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml');
113
        }
114
115
        $provider = new Mapzen($this->getHttpClient($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']);
116
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(49.1390924, 1.6572462));
117
118
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
119
        $this->assertCount(5, $results);
120
121
        /** @var \Geocoder\Model\Address $result */
122
        $result = $results->first();
123
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
124
        $this->assertEquals('Bus-Saint-Rémy', $result->getLocality());
125
    }
126
127
    public function testGeocodeWithCity()
128
    {
129
        if (!isset($_SERVER['MAPZEN_API_KEY'])) {
130
            $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml');
131
        }
132
133
        $provider = new Mapzen($this->getHttpClient($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']);
134
        $results = $provider->geocodeQuery(GeocodeQuery::create('Hanover'));
135
136
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
137
        $this->assertCount(5, $results);
138
139
        /** @var \Geocoder\Model\Address $result */
140
        $result = $results->first();
141
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
142
        $this->assertEquals(42.027323000000003, $result->getCoordinates()->getLatitude(), '', 0.01);
143
        $this->assertEquals(-88.204203000000007, $result->getCoordinates()->getLongitude(), '', 0.01);
144
        $this->assertNull($result->getLocality());
145
        $this->assertCount(2, $result->getAdminLevels());
146
        $this->assertEquals('United States', $result->getAdminLevels()->get(4)->getName());
147
        $this->assertEquals('Illinois', $result->getAdminLevels()->get(1)->getName());
148
        $this->assertEquals('United States', $result->getCountry()->getName());
149
150
        /** @var \Geocoder\Model\Address $result */
151
        $result = $results->get(1);
152
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
153
        $this->assertEquals(18.393428, $result->getCoordinates()->getLatitude(), '', 0.01);
154
        $this->assertEquals(-78.122906, $result->getCoordinates()->getLongitude(), '', 0.01);
155
        $this->assertNull($result->getLocality());
156
        $this->assertCount(2, $result->getAdminLevels());
157
        $this->assertEquals('Hanover', $result->getAdminLevels()->get(1)->getName());
158
        $this->assertEquals('Jamaica', $result->getCountry()->getName());
159
160
        /** @var \Geocoder\Model\Address $result */
161
        $result = $results->get(2);
162
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
163
        $this->assertEquals(39.192889999999998, $result->getCoordinates()->getLatitude(), '', 0.01);
164
        $this->assertEquals(-76.724140000000006, $result->getCoordinates()->getLongitude(), '', 0.01);
165
        $this->assertEquals('Hanover', $result->getLocality());
166
        $this->assertTrue($result->getAdminLevels()->has(4));
167
        $this->assertEquals('Hanover', $result->getAdminLevels()->get(2)->getName());
168
        $this->assertEquals('United States', $result->getCountry()->getName());
169
    }
170
171
    public function testGeocodeWithCityDistrict()
172
    {
173
        if (!isset($_SERVER['MAPZEN_API_KEY'])) {
174
            $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml');
175
        }
176
177
        $provider = new Mapzen($this->getHttpClient($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']);
178
        $results = $provider->geocodeQuery(GeocodeQuery::create('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'));
179
180
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
181
        $this->assertCount(2, $results);
182
183
        /** @var \Geocoder\Model\Address $result */
184
        $result = $results->first();
185
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
186
        $this->assertEquals(50.189017, $result->getCoordinates()->getLatitude(), '', 0.01);
187
        $this->assertEquals(8.6367809999999992, $result->getCoordinates()->getLongitude(), '', 0.01);
188
        $this->assertEquals('10a', $result->getStreetNumber());
189
        $this->assertEquals('Kalbacher Hauptstraße', $result->getStreetName());
190
        $this->assertEquals(60437, $result->getPostalCode());
191
        $this->assertEquals('Frankfurt am Main', $result->getLocality());
192
        $this->assertCount(3, $result->getAdminLevels());
193
        $this->assertEquals('Frankfurt am Main', $result->getAdminLevels()->get(2)->getName());
194
        $this->assertEquals('Hessen', $result->getAdminLevels()->get(1)->getName());
195
        $this->assertNull($result->getAdminLevels()->get(1)->getCode());
196
        $this->assertEquals('Germany', $result->getCountry()->getName());
197
        $this->assertEquals('DEU', $result->getCountry()->getCode());
198
    }
199
200
    /**
201
     * @expectedException \Geocoder\Exception\QuotaExceeded
202
     * @expectedExceptionMessage Valid request but quota exceeded.
203
     */
204
    public function testGeocodeQuotaExceeded()
205
    {
206
        $provider = new Mapzen(
207
            $this->getMockedHttpClient(
208
                '{
209
                    "meta": {
210
                        "version": 1,
211
                        "status_code": 429
212
                    },
213
                    "results": {
214
                        "error": {
215
                            "type": "QpsExceededError",
216
                            "message": "Queries per second exceeded: Queries exceeded (6 allowed)."
217
                        }
218
                    }
219
                }'
220
            ),
221
            'api_key'
222
        );
223
        $provider->geocodeQuery(GeocodeQuery::create('New York'));
224
    }
225
226
    /**
227
     * @expectedException \Geocoder\Exception\InvalidCredentials
228
     * @expectedExceptionMessage Invalid or missing api key.
229
     */
230
    public function testGeocodeInvalidApiKey()
231
    {
232
        $provider = new Mapzen(
233
            $this->getMockedHttpClient(
234
                '{
235
                    "meta": {
236
                        "version": 1,
237
                        "status_code": 403
238
                    },
239
                    "results": {
240
                        "error": {
241
                            "type": "KeyError",
242
                            "message": "No api_key specified."
243
                        }
244
                    }
245
                }'
246
            ),
247
            'api_key'
248
        );
249
        $provider->geocodeQuery(GeocodeQuery::create('New York'));
250
    }
251
252
    /**
253
     * @expectedException \Geocoder\Exception\UnsupportedOperation
254
     * @expectedExceptionMessage The Mapzen provider does not support IP addresses, only street addresses.
255
     */
256
    public function testGeocodeWithLocalhostIPv4()
257
    {
258
        $provider = new Mapzen($this->getMockedHttpClient(), 'api_key');
259
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
260
    }
261
262
    /**
263
     * @expectedException \Geocoder\Exception\UnsupportedOperation
264
     * @expectedExceptionMessage The Mapzen provider does not support IP addresses, only street addresses.
265
     */
266
    public function testGeocodeWithLocalhostIPv6()
267
    {
268
        $provider = new Mapzen($this->getMockedHttpClient(), 'api_key');
269
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
270
    }
271
272
    /**
273
     * @expectedException \Geocoder\Exception\UnsupportedOperation
274
     * @expectedExceptionMessage The Mapzen provider does not support IP addresses, only street addresses.
275
     */
276
    public function testGeocodeWithRealIPv4()
277
    {
278
        $provider = new Mapzen($this->getMockedHttpClient(), 'api_key');
279
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
280
    }
281
282
    /**
283
     * @expectedException \Geocoder\Exception\UnsupportedOperation
284
     * @expectedExceptionMessage The Mapzen provider does not support IP addresses, only street addresses.
285
     */
286
    public function testGeocodeWithRealIPv6()
287
    {
288
        $provider = new Mapzen($this->getMockedHttpClient(), 'api_key');
289
        $provider->geocodeQuery(GeocodeQuery::create('::ffff:74.200.247.59'));
290
    }
291
}
292