GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 691675...503bab )
by Tobias
07:29 queued 04:40
created

testGeocodeWithColloquialAreaComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 8
nc 1
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\GoogleMaps\Tests;
14
15
use Geocoder\Exception\InvalidServerResponse;
16
use Geocoder\IntegrationTest\BaseTestCase;
17
use Geocoder\Location;
18
use Geocoder\Provider\GoogleMaps\Model\GoogleAddress;
19
use Geocoder\Query\GeocodeQuery;
20
use Geocoder\Query\ReverseQuery;
21
use Geocoder\Provider\GoogleMaps\GoogleMaps;
22
use Psr\Http\Message\RequestInterface;
23
24
class GoogleMapsTest extends BaseTestCase
25
{
26
    /**
27
     * @var string
28
     */
29
    private $testAPIKey = 'fake_key';
30
31
    protected function getCacheDir()
0 ignored issues
show
Coding Style introduced by
getCacheDir uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
32
    {
33
        if (isset($_SERVER['USE_CACHED_RESPONSES']) && true === $_SERVER['USE_CACHED_RESPONSES']) {
34
            return __DIR__.'/.cached_responses';
35
        }
36
37
        return null;
38
    }
39
40
    public function testGetName()
41
    {
42
        $provider = new GoogleMaps($this->getMockedHttpClient());
43
        $this->assertEquals('google_maps', $provider->getName());
44
    }
45
46
    /**
47
     * @expectedException \Geocoder\Exception\UnsupportedOperation
48
     */
49
    public function testGeocodeWithLocalhostIPv4()
50
    {
51
        $provider = new GoogleMaps($this->getMockedHttpClient());
52
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
53
    }
54
55
    /**
56
     * @expectedException \Geocoder\Exception\UnsupportedOperation
57
     * @expectedExceptionMessage The GoogleMaps provider does not support IP addresses, only street addresses.
58
     */
59
    public function testGeocodeWithLocalhostIPv6()
60
    {
61
        $provider = new GoogleMaps($this->getMockedHttpClient());
62
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
63
    }
64
65
    /**
66
     * @expectedException \Geocoder\Exception\UnsupportedOperation
67
     * @expectedExceptionMessage The GoogleMaps provider does not support IP addresses, only street addresses.
68
     */
69
    public function testGeocodeWithRealIp()
70
    {
71
        $provider = new GoogleMaps($this->getHttpClient());
72
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
73
    }
74
75
    /**
76
     * @expectedException \Geocoder\Exception\QuotaExceeded
77
     * @expectedExceptionMessage Daily quota exceeded https://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France
78
     */
79
    public function testGeocodeWithQuotaExceeded()
80
    {
81
        $provider = new GoogleMaps($this->getMockedHttpClient('{"status":"OVER_QUERY_LIMIT"}'));
82
        $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
83
    }
84
85
    public function testGeocodeWithRealAddress()
86
    {
87
        $provider = new GoogleMaps($this->getHttpClient(), 'Île-de-France');
88
        $results = $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')->withLocale('fr-FR'));
89
90
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
91
        $this->assertCount(1, $results);
92
93
        /** @var Location $result */
94
        $result = $results->first();
95
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
96
        $this->assertEquals(48.8630462, $result->getCoordinates()->getLatitude(), '', 0.001);
97
        $this->assertEquals(2.3882487, $result->getCoordinates()->getLongitude(), '', 0.001);
98
        $this->assertNotNull($result->getBounds());
99
        $this->assertEquals(48.8617, $result->getBounds()->getSouth(), '', 0.001);
100
        $this->assertEquals(2.3882487, $result->getBounds()->getWest(), '', 0.001);
101
        $this->assertEquals(48.8644, $result->getBounds()->getNorth(), '', 0.001);
102
        $this->assertEquals(2.3901, $result->getBounds()->getEast(), '', 0.001);
103
        $this->assertEquals(10, $result->getStreetNumber());
104
        $this->assertEquals('Avenue Gambetta', $result->getStreetName());
105
        $this->assertEquals(75020, $result->getPostalCode());
106
        $this->assertEquals('Paris', $result->getLocality());
107
        $this->assertEquals('Paris', $result->getAdminLevels()->get(2)->getName());
108
        $this->assertEquals('Île-de-France', $result->getAdminLevels()->get(1)->getName());
109
        $this->assertEquals('France', $result->getCountry()->getName());
110
        $this->assertEquals('FR', $result->getCountry()->getCode());
111
        $this->assertEquals('ChIJ4b303vJt5kcRF9AQdh4ZjWc', $result->getId());
112
113
        // not provided
114
        $this->assertNull($result->getTimezone());
115
    }
116
117
    public function testGeocodeBoundsWithRealAddressForNonRooftopLocation()
118
    {
119
        $provider = new GoogleMaps($this->getHttpClient());
120
        $results = $provider->geocodeQuery(GeocodeQuery::create('Paris, France'));
121
122
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
123
        $this->assertCount(1, $results);
124
125
        /** @var Location $result */
126
        $result = $results->first();
127
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
128
        $this->assertNotNull($result->getBounds());
129
        $this->assertEquals(48.815573, $result->getBounds()->getSouth(), '', 0.0001);
130
        $this->assertEquals(2.224199, $result->getBounds()->getWest(), '', 0.0001);
131
        $this->assertEquals(48.902145, $result->getBounds()->getNorth(), '', 0.0001);
132
        $this->assertEquals(2.4699209, $result->getBounds()->getEast(), '', 0.0001);
133
        $this->assertEquals('ChIJD7fiBh9u5kcRYJSMaMOCCwQ', $result->getId());
134
    }
135
136
    /**
137
     * @expectedException \Geocoder\Exception\InvalidServerResponse
138
     */
139
    public function testReverse()
140
    {
141
        $provider = new GoogleMaps($this->getMockedHttpClient());
142
        $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
143
    }
144
145
    public function testReverseWithRealCoordinates()
146
    {
147
        $provider = new GoogleMaps($this->getHttpClient());
148
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.8631507, 2.388911));
149
150
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
151
        $this->assertCount(5, $results);
152
153
        /** @var Location $result */
154
        $result = $results->first();
155
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
156
        $this->assertEquals(12, $result->getStreetNumber());
157
        $this->assertEquals('Avenue Gambetta', $result->getStreetName());
158
        $this->assertEquals(75020, $result->getPostalCode());
159
        $this->assertEquals('Paris', $result->getLocality());
160
        $this->assertCount(2, $result->getAdminLevels());
161
        $this->assertEquals('Paris', $result->getAdminLevels()->get(2)->getName());
162
        $this->assertEquals('Île-de-France', $result->getAdminLevels()->get(1)->getName());
163
        $this->assertEquals('France', $result->getCountry()->getName());
164
        $this->assertEquals('FR', $result->getCountry()->getCode());
165
        $this->assertEquals('ChIJ9aLL3vJt5kcR61GCze3v6fg', $result->getId());
166
    }
167
168
    public function testGeocodeWithCityDistrict()
169
    {
170
        $provider = new GoogleMaps($this->getHttpClient());
171
        $results = $provider->geocodeQuery(GeocodeQuery::create('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'));
172
173
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
174
        $this->assertCount(1, $results);
175
176
        /** @var Location $result */
177
        $result = $results->first();
178
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
179
        $this->assertEquals('Kalbach-Riedberg', $result->getSubLocality());
180
    }
181
182
    /**
183
     * @expectedException \Geocoder\Exception\InvalidCredentials
184
     * @expectedExceptionMessage API key is invalid https://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France
185
     */
186
    public function testGeocodeWithInavlidApiKey()
187
    {
188
        $provider = new GoogleMaps($this->getMockedHttpClient('{"error_message":"The provided API key is invalid.", "status":"REQUEST_DENIED"}'));
189
        $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
190
    }
191
192
    public function testGeocodeWithRealValidApiKey()
0 ignored issues
show
Coding Style introduced by
testGeocodeWithRealValidApiKey uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
193
    {
194
        if (!isset($_SERVER['GOOGLE_GEOCODING_KEY'])) {
195
            $this->markTestSkipped('You need to configure the GOOGLE_GEOCODING_KEY value in phpunit.xml');
196
        }
197
198
        $provider = new GoogleMaps($this->getHttpClient($_SERVER['GOOGLE_GEOCODING_KEY']), null, $_SERVER['GOOGLE_GEOCODING_KEY']);
199
200
        $results = $provider->geocodeQuery(GeocodeQuery::create('Columbia University'));
201
202
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
203
        $this->assertCount(1, $results);
204
205
        /** @var Location $result */
206
        $result = $results->first();
207
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
208
        $this->assertNotNull($result->getCoordinates()->getLatitude());
209
        $this->assertNotNull($result->getCoordinates()->getLongitude());
210
        $this->assertEquals('New York', $result->getLocality());
211
        $this->assertEquals('Manhattan', $result->getSubLocality());
212
        $this->assertCount(2, $result->getAdminLevels());
213
        $this->assertEquals('New York', $result->getAdminLevels()->get(1)->getName());
214
    }
215
216
    /**
217
     * @expectedException \Geocoder\Exception\InvalidCredentials
218
     * @expectedExceptionMessage API key is invalid https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&key=fake_key
219
     */
220
    public function testGeocodeWithRealInvalidApiKey()
221
    {
222
        $provider = new GoogleMaps($this->getHttpClient(), null, $this->testAPIKey);
223
224
        $provider->geocodeQuery(GeocodeQuery::create('Columbia University'));
225
    }
226
227
    public function testGeocodePostalTown()
228
    {
229
        $provider = new GoogleMaps($this->getHttpClient());
230
        $results = $provider->geocodeQuery(GeocodeQuery::create('CF37, United Kingdom'));
231
232
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
233
        $this->assertCount(1, $results);
234
235
        /** @var Location $result */
236
        $result = $results->first();
237
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
238
        $this->assertEquals('Pontypridd', $result->getLocality());
239
    }
240
241
    public function testBusinessQueryWithoutPrivateKey()
242
    {
243
        $uri = '';
244
245
        $provider = GoogleMaps::business(
246
            $this->getMockedHttpClientCallback(
247
                function (RequestInterface $request) use (&$uri) {
248
                    $uri = $request->getUri();
249
                }
250
            ),
251
            'foo'
252
        );
253
254
        try {
255
            $provider->geocodeQuery(GeocodeQuery::create('blah'));
256
        } catch (InvalidServerResponse $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
257
        }
258
        $this->assertEquals('https://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo', $uri);
259
    }
260
261
    public function testBusinessQueryWithPrivateKey()
262
    {
263
        $uri = '';
264
265
        $provider = GoogleMaps::business(
266
            $this->getMockedHttpClientCallback(
267
                function (RequestInterface $request) use (&$uri) {
268
                    $uri = (string) $request->getUri();
269
                }
270
            ),
271
            'foo',
272
            'bogus'
273
        );
274
275
        try {
276
            $provider->geocodeQuery(GeocodeQuery::create('blah'));
277
        } catch (InvalidServerResponse $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
278
        }
279
        $this->assertEquals(
280
            'https://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo&signature=9G2weMhhd4E2ciR681gp9YabvUg=',
281
            $uri
282
        );
283
    }
284
285
    /**
286
     * @expectedException \Geocoder\Exception\InvalidCredentials
287
     */
288
    public function testGeocodeWithInvalidClientIdAndKey()
289
    {
290
        $provider = GoogleMaps::business($this->getHttpClient(), 'foo', 'bogus');
291
        $provider->geocodeQuery(GeocodeQuery::create('Columbia University'));
292
    }
293
294
    /**
295
     * @expectedException \Geocoder\Exception\InvalidCredentials
296
     */
297
    public function testGeocodeWithInvalidClientIdAndKeyNoSsl()
298
    {
299
        $provider = GoogleMaps::business($this->getHttpClient(), 'foo', 'bogus');
300
        $provider->geocodeQuery(GeocodeQuery::create('Columbia University'));
301
    }
302
303
    public function testGeocodeWithSupremise()
304
    {
305
        $provider = new GoogleMaps($this->getHttpClient());
306
        $results = $provider->geocodeQuery(GeocodeQuery::create('2123 W Mineral Ave Apt 61,Littleton,CO8 0120'));
307
308
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
309
        $this->assertCount(1, $results);
310
311
        /** @var GoogleAddress $result */
312
        $result = $results->first();
313
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
314
        $this->assertEquals('61', $result->getSubpremise());
315
    }
316
317
    public function testGeocodeWithNaturalFeatureComponent()
318
    {
319
        $provider = new GoogleMaps($this->getHttpClient());
320
        $results = $provider->geocodeQuery(GeocodeQuery::create('Durmitor Nacionalni Park'));
321
322
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
323
        $this->assertCount(1, $results);
324
325
        /** @var GoogleAddress $result */
326
        $result = $results->first();
327
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
328
        $this->assertEquals('Durmitor Nacionalni Park', $result->getNaturalFeature());
329
        $this->assertEquals('Durmitor Nacionalni Park', $result->getPark());
330
        $this->assertEquals('Durmitor Nacionalni Park', $result->getPointOfInterest());
331
        $this->assertEquals('Montenegro', $result->getPolitical());
332
        $this->assertEquals('Montenegro', $result->getCountry());
333
    }
334
335
    public function testGeocodeWithAirportComponent()
336
    {
337
        $provider = new GoogleMaps($this->getHttpClient());
338
        $results = $provider->geocodeQuery(GeocodeQuery::create('Brisbane Airport'));
339
340
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
341
        $this->assertCount(1, $results);
342
343
        /** @var GoogleAddress $result */
344
        $result = $results->first();
345
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
346
        $this->assertEquals('Brisbane Airport', $result->getAirport());
347
        $this->assertEquals('Brisbane Airport', $result->getEstablishment());
348
        $this->assertEquals('Brisbane Airport', $result->getPointOfInterest());
349
    }
350
351
    public function testGeocodeWithPremiseComponent()
352
    {
353
        $provider = new GoogleMaps($this->getHttpClient());
354
        $results = $provider->geocodeQuery(GeocodeQuery::create('1125 17th St, Denver, CO 80202'));
355
356
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
357
        $this->assertCount(1, $results);
358
359
        /** @var GoogleAddress $result */
360
        $result = $results->first();
361
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
362
        $this->assertEquals('1125 17th Street', $result->getPremise());
363
        $this->assertEquals('Denver', $result->getLocality());
364
        $this->assertEquals('United States', $result->getCountry());
365
        $this->assertEquals('Central', $result->getNeighborhood());
366
    }
367
368
    public function testGeocodeWithColloquialAreaComponent()
369
    {
370
        $provider = new GoogleMaps($this->getHttpClient());
371
        $results = $provider->geocodeQuery(GeocodeQuery::create('darwin'));
372
373
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
374
        $this->assertCount(3, $results);
375
376
        /** @var GoogleAddress $result */
377
        $result = $results->first();
378
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
379
        $this->assertEquals('Darwin', $result->getColloquialArea());
380
    }
381
382
    public function testGeocodeWithWardComponent()
383
    {
384
        $provider = new GoogleMaps($this->getHttpClient());
385
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(35.03937, 135.729243));
386
387
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
388
        $this->assertCount(5, $results);
389
390
        /** @var GoogleAddress $result */
391
        $result = $results->first();
392
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
393
        $this->assertEquals('Kita-ku', $result->getWard());
394
    }
395
396
    public function testReverseWithSubLocalityLevels()
397
    {
398
        $provider = new GoogleMaps($this->getHttpClient());
399
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(36.2745084, 136.9003169));
400
401
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
402
        $this->assertCount(5, $results);
403
404
        /** @var GoogleAddress $result */
405
        $result = $results->first();
406
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
407
        $this->assertInstanceOf('\Geocoder\Model\AdminLevelCollection', $result->getSubLocalityLevels());
408
        $this->assertEquals('Iijima', $result->getSubLocalityLevels()->get(1)->getName());
409
        $this->assertEquals('58', $result->getSubLocalityLevels()->get(4)->getName());
410
        $this->assertEquals(1, $result->getSubLocalityLevels()->get(1)->getLevel());
411
        $this->assertEquals(4, $result->getSubLocalityLevels()->get(4)->getLevel());
412
    }
413
414
    public function testGeocodeBoundsWithRealAddressWithViewportOnly()
415
    {
416
        $provider = new GoogleMaps($this->getHttpClient());
417
        $results = $provider->geocodeQuery(GeocodeQuery::create('Sibbe, Netherlands'));
418
419
        $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
420
        $this->assertCount(1, $results);
421
422
        /** @var \Geocoder\Model\Address $result */
423
        $result = $results->first();
424
        $this->assertInstanceOf('\Geocoder\Model\Address', $result);
425
        $this->assertNotNull($result->getBounds());
426
        $this->assertEquals(50.8433, $result->getBounds()->getSouth(), '', 0.001);
427
        $this->assertEquals(5.8259, $result->getBounds()->getWest(), '', 0.001);
428
        $this->assertEquals(50.8460, $result->getBounds()->getNorth(), '', 0.001);
429
        $this->assertEquals(5.8286, $result->getBounds()->getEast(), '', 0.001);
430
    }
431
}
432