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 ( 89afed...81a6bd )
by Tobias
15:14 queued 06:27
created

testGeocodeWithNaturalFeatureComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 12
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\Model\Address;
19
use Geocoder\Model\AddressCollection;
20
use Geocoder\Provider\GoogleMaps\Model\GoogleAddress;
21
use Geocoder\Query\GeocodeQuery;
22
use Geocoder\Query\ReverseQuery;
23
use Geocoder\Provider\GoogleMaps\GoogleMaps;
24
use Psr\Http\Message\RequestInterface;
25
26
class GoogleMapsTest extends BaseTestCase
27
{
28
    /**
29
     * @var string
30
     */
31
    private $testAPIKey = 'fake_key';
32
33
    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...
34
    {
35
        if (isset($_SERVER['USE_CACHED_RESPONSES']) && true === $_SERVER['USE_CACHED_RESPONSES']) {
36
            return __DIR__.'/.cached_responses';
37
        }
38
39
        return null;
40
    }
41
42
    public function testGetName()
43
    {
44
        $provider = new GoogleMaps($this->getMockedHttpClient());
45
        $this->assertEquals('google_maps', $provider->getName());
46
    }
47
48
    /**
49
     * @expectedException \Geocoder\Exception\UnsupportedOperation
50
     */
51
    public function testGeocodeWithLocalhostIPv4()
52
    {
53
        $provider = new GoogleMaps($this->getMockedHttpClient());
54
        $provider->geocodeQuery(GeocodeQuery::create('127.0.0.1'));
55
    }
56
57
    /**
58
     * @expectedException \Geocoder\Exception\UnsupportedOperation
59
     * @expectedExceptionMessage The GoogleMaps provider does not support IP addresses, only street addresses.
60
     */
61
    public function testGeocodeWithLocalhostIPv6()
62
    {
63
        $provider = new GoogleMaps($this->getMockedHttpClient());
64
        $provider->geocodeQuery(GeocodeQuery::create('::1'));
65
    }
66
67
    /**
68
     * @expectedException \Geocoder\Exception\UnsupportedOperation
69
     * @expectedExceptionMessage The GoogleMaps provider does not support IP addresses, only street addresses.
70
     */
71
    public function testGeocodeWithRealIp()
72
    {
73
        $provider = new GoogleMaps($this->getHttpClient());
74
        $provider->geocodeQuery(GeocodeQuery::create('74.200.247.59'));
75
    }
76
77
    /**
78
     * @expectedException \Geocoder\Exception\QuotaExceeded
79
     * @expectedExceptionMessage Daily quota exceeded https://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France
80
     */
81
    public function testGeocodeWithQuotaExceeded()
82
    {
83
        $provider = new GoogleMaps($this->getMockedHttpClient('{"status":"OVER_QUERY_LIMIT"}'));
84
        $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
85
    }
86
87
    public function testGeocodeWithRealAddress()
88
    {
89
        $provider = new GoogleMaps($this->getHttpClient(), 'Île-de-France');
90
        $results = $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France')->withLocale('fr-FR'));
91
92
        $this->assertInstanceOf(AddressCollection::class, $results);
93
        $this->assertCount(1, $results);
94
95
        /** @var Location $result */
96
        $result = $results->first();
97
        $this->assertInstanceOf(Address::class, $result);
98
        $this->assertEquals(48.8630462, $result->getCoordinates()->getLatitude(), '', 0.001);
99
        $this->assertEquals(2.3882487, $result->getCoordinates()->getLongitude(), '', 0.001);
100
        $this->assertNotNull($result->getBounds());
101
        $this->assertEquals(48.8617, $result->getBounds()->getSouth(), '', 0.001);
102
        $this->assertEquals(2.3882487, $result->getBounds()->getWest(), '', 0.001);
103
        $this->assertEquals(48.8644, $result->getBounds()->getNorth(), '', 0.001);
104
        $this->assertEquals(2.3901, $result->getBounds()->getEast(), '', 0.001);
105
        $this->assertEquals(10, $result->getStreetNumber());
106
        $this->assertEquals('Avenue Gambetta', $result->getStreetName());
107
        $this->assertEquals(75020, $result->getPostalCode());
108
        $this->assertEquals('Paris', $result->getLocality());
109
        $this->assertEquals('Paris', $result->getAdminLevels()->get(2)->getName());
110
        $this->assertEquals('Île-de-France', $result->getAdminLevels()->get(1)->getName());
111
        $this->assertEquals('France', $result->getCountry()->getName());
112
        $this->assertEquals('FR', $result->getCountry()->getCode());
113
        $this->assertEquals('ChIJ4b303vJt5kcRF9AQdh4ZjWc', $result->getId());
114
115
        // not provided
116
        $this->assertNull($result->getTimezone());
117
    }
118
119
    public function testGeocodeBoundsWithRealAddressForNonRooftopLocation()
120
    {
121
        $provider = new GoogleMaps($this->getHttpClient());
122
        $results = $provider->geocodeQuery(GeocodeQuery::create('Paris, France'));
123
124
        $this->assertInstanceOf(AddressCollection::class, $results);
125
        $this->assertCount(1, $results);
126
127
        /** @var Location $result */
128
        $result = $results->first();
129
        $this->assertInstanceOf(Address::class, $result);
130
        $this->assertNotNull($result->getBounds());
131
        $this->assertEquals(48.815573, $result->getBounds()->getSouth(), '', 0.0001);
132
        $this->assertEquals(2.224199, $result->getBounds()->getWest(), '', 0.0001);
133
        $this->assertEquals(48.902145, $result->getBounds()->getNorth(), '', 0.0001);
134
        $this->assertEquals(2.4699209, $result->getBounds()->getEast(), '', 0.0001);
135
        $this->assertEquals('ChIJD7fiBh9u5kcRYJSMaMOCCwQ', $result->getId());
136
    }
137
138
    /**
139
     * @expectedException \Geocoder\Exception\InvalidServerResponse
140
     */
141
    public function testReverse()
142
    {
143
        $provider = new GoogleMaps($this->getMockedHttpClient());
144
        $provider->reverseQuery(ReverseQuery::fromCoordinates(1, 2));
145
    }
146
147
    public function testReverseWithRealCoordinates()
148
    {
149
        $provider = new GoogleMaps($this->getHttpClient());
150
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(48.8631507, 2.388911));
151
152
        $this->assertInstanceOf(AddressCollection::class, $results);
153
        $this->assertCount(5, $results);
154
155
        /** @var Location $result */
156
        $result = $results->first();
157
        $this->assertInstanceOf(Address::class, $result);
158
        $this->assertEquals(12, $result->getStreetNumber());
159
        $this->assertEquals('Avenue Gambetta', $result->getStreetName());
160
        $this->assertEquals(75020, $result->getPostalCode());
161
        $this->assertEquals('Paris', $result->getLocality());
162
        $this->assertCount(2, $result->getAdminLevels());
163
        $this->assertEquals('Paris', $result->getAdminLevels()->get(2)->getName());
164
        $this->assertEquals('Île-de-France', $result->getAdminLevels()->get(1)->getName());
165
        $this->assertEquals('France', $result->getCountry()->getName());
166
        $this->assertEquals('FR', $result->getCountry()->getCode());
167
        $this->assertEquals('ChIJ9aLL3vJt5kcR61GCze3v6fg', $result->getId());
168
    }
169
170
    public function testGeocodeWithCityDistrict()
171
    {
172
        $provider = new GoogleMaps($this->getHttpClient());
173
        $results = $provider->geocodeQuery(GeocodeQuery::create('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'));
174
175
        $this->assertInstanceOf(AddressCollection::class, $results);
176
        $this->assertCount(1, $results);
177
178
        /** @var Location $result */
179
        $result = $results->first();
180
        $this->assertInstanceOf(Address::class, $result);
181
        $this->assertEquals('Kalbach-Riedberg', $result->getSubLocality());
182
    }
183
184
    /**
185
     * @expectedException \Geocoder\Exception\InvalidCredentials
186
     * @expectedExceptionMessage API key is invalid https://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France
187
     */
188
    public function testGeocodeWithInavlidApiKey()
189
    {
190
        $provider = new GoogleMaps($this->getMockedHttpClient('{"error_message":"The provided API key is invalid.", "status":"REQUEST_DENIED"}'));
191
        $provider->geocodeQuery(GeocodeQuery::create('10 avenue Gambetta, Paris, France'));
192
    }
193
194
    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...
195
    {
196
        if (!isset($_SERVER['GOOGLE_GEOCODING_KEY'])) {
197
            $this->markTestSkipped('You need to configure the GOOGLE_GEOCODING_KEY value in phpunit.xml');
198
        }
199
200
        $provider = new GoogleMaps($this->getHttpClient($_SERVER['GOOGLE_GEOCODING_KEY']), null, $_SERVER['GOOGLE_GEOCODING_KEY']);
201
202
        $results = $provider->geocodeQuery(GeocodeQuery::create('Columbia University'));
203
204
        $this->assertInstanceOf(AddressCollection::class, $results);
205
        $this->assertCount(1, $results);
206
207
        /** @var Location $result */
208
        $result = $results->first();
209
        $this->assertInstanceOf(Address::class, $result);
210
        $this->assertNotNull($result->getCoordinates()->getLatitude());
211
        $this->assertNotNull($result->getCoordinates()->getLongitude());
212
        $this->assertEquals('New York', $result->getLocality());
213
        $this->assertEquals('Manhattan', $result->getSubLocality());
214
        $this->assertCount(2, $result->getAdminLevels());
215
        $this->assertEquals('New York', $result->getAdminLevels()->get(1)->getName());
216
    }
217
218
    /**
219
     * @expectedException \Geocoder\Exception\InvalidCredentials
220
     * @expectedExceptionMessage API key is invalid https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&key=fake_key
221
     */
222
    public function testGeocodeWithRealInvalidApiKey()
223
    {
224
        $provider = new GoogleMaps($this->getHttpClient(), null, $this->testAPIKey);
225
226
        $provider->geocodeQuery(GeocodeQuery::create('Columbia University'));
227
    }
228
229
    public function testGeocodePostalTown()
230
    {
231
        $provider = new GoogleMaps($this->getHttpClient());
232
        $results = $provider->geocodeQuery(GeocodeQuery::create('CF37, United Kingdom'));
233
234
        $this->assertInstanceOf(AddressCollection::class, $results);
235
        $this->assertCount(1, $results);
236
237
        /** @var Location $result */
238
        $result = $results->first();
239
        $this->assertInstanceOf(Address::class, $result);
240
        $this->assertEquals('Pontypridd', $result->getLocality());
241
    }
242
243
    public function testBusinessQueryWithoutPrivateKey()
244
    {
245
        $uri = '';
246
247
        $provider = GoogleMaps::business(
248
            $this->getMockedHttpClientCallback(
249
                function (RequestInterface $request) use (&$uri) {
250
                    $uri = $request->getUri();
251
                }
252
            ),
253
            'foo'
254
        );
255
256
        try {
257
            $provider->geocodeQuery(GeocodeQuery::create('blah'));
258
        } catch (InvalidServerResponse $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
259
        }
260
        $this->assertEquals('https://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo', $uri);
261
    }
262
263
    public function testBusinessQueryWithPrivateKey()
264
    {
265
        $uri = '';
266
267
        $provider = GoogleMaps::business(
268
            $this->getMockedHttpClientCallback(
269
                function (RequestInterface $request) use (&$uri) {
270
                    $uri = (string) $request->getUri();
271
                }
272
            ),
273
            'foo',
274
            'bogus'
275
        );
276
277
        try {
278
            $provider->geocodeQuery(GeocodeQuery::create('blah'));
279
        } catch (InvalidServerResponse $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
280
        }
281
        $this->assertEquals(
282
            'https://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo&signature=9G2weMhhd4E2ciR681gp9YabvUg=',
283
            $uri
284
        );
285
    }
286
287
    public function testBusinessQueryWithPrivateKeyAndChannel()
288
    {
289
        $uri = '';
290
291
        $provider = GoogleMaps::business(
292
            $this->getMockedHttpClientCallback(
293
                function (RequestInterface $request) use (&$uri) {
294
                    $uri = (string) $request->getUri();
295
                }
296
            ),
297
            'foo',
298
            'bogus',
299
            null,
300
            null,
301
            'bar'
302
        );
303
304
        try {
305
            $provider->geocodeQuery(GeocodeQuery::create('blah'));
306
        } catch (InvalidServerResponse $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
307
        }
308
        $this->assertEquals(
309
            'https://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo&channel=bar&signature=IdRm_EBPMWFgQNQ9eIDBxSWVlb8=',
310
            $uri
311
        );
312
    }
313
314
    /**
315
     * @expectedException \Geocoder\Exception\InvalidCredentials
316
     */
317
    public function testGeocodeWithInvalidClientIdAndKey()
318
    {
319
        $provider = GoogleMaps::business($this->getHttpClient(), 'foo', 'bogus');
320
        $provider->geocodeQuery(GeocodeQuery::create('Columbia University'));
321
    }
322
323
    /**
324
     * @expectedException \Geocoder\Exception\InvalidCredentials
325
     */
326
    public function testGeocodeWithInvalidClientIdAndKeyNoSsl()
327
    {
328
        $provider = GoogleMaps::business($this->getHttpClient(), 'foo', 'bogus');
329
        $provider->geocodeQuery(GeocodeQuery::create('Columbia University'));
330
    }
331
332
    public function testGeocodeWithSupremise()
333
    {
334
        $provider = new GoogleMaps($this->getHttpClient());
335
        $results = $provider->geocodeQuery(GeocodeQuery::create('2123 W Mineral Ave Apt 61,Littleton,CO8 0120'));
336
337
        $this->assertInstanceOf(AddressCollection::class, $results);
338
        $this->assertCount(1, $results);
339
340
        /** @var GoogleAddress $result */
341
        $result = $results->first();
342
        $this->assertInstanceOf(Address::class, $result);
343
        $this->assertEquals('61', $result->getSubpremise());
344
    }
345
346
    public function testGeocodeWithNaturalFeatureComponent()
347
    {
348
        $provider = new GoogleMaps($this->getHttpClient());
349
        $results = $provider->geocodeQuery(GeocodeQuery::create('Durmitor Nacionalni Park'));
350
351
        $this->assertInstanceOf(AddressCollection::class, $results);
352
        $this->assertCount(1, $results);
353
354
        /** @var GoogleAddress $result */
355
        $result = $results->first();
356
        $this->assertInstanceOf(Address::class, $result);
357
        $this->assertEquals('Durmitor Nacionalni Park', $result->getNaturalFeature());
358
        $this->assertEquals('Durmitor Nacionalni Park', $result->getPark());
359
        $this->assertEquals('Durmitor Nacionalni Park', $result->getPointOfInterest());
360
        $this->assertEquals('Montenegro', $result->getPolitical());
361
        $this->assertEquals('Montenegro', $result->getCountry());
362
    }
363
364
    public function testGeocodeWithAirportComponent()
365
    {
366
        $provider = new GoogleMaps($this->getHttpClient());
367
        $results = $provider->geocodeQuery(GeocodeQuery::create('Brisbane Airport'));
368
369
        $this->assertInstanceOf(AddressCollection::class, $results);
370
        $this->assertCount(1, $results);
371
372
        /** @var GoogleAddress $result */
373
        $result = $results->first();
374
        $this->assertInstanceOf(Address::class, $result);
375
        $this->assertEquals('Brisbane Airport', $result->getAirport());
376
        $this->assertEquals('Brisbane Airport', $result->getEstablishment());
377
        $this->assertEquals('Brisbane Airport', $result->getPointOfInterest());
378
    }
379
380
    public function testGeocodeWithPremiseComponent()
381
    {
382
        $provider = new GoogleMaps($this->getHttpClient());
383
        $results = $provider->geocodeQuery(GeocodeQuery::create('1125 17th St, Denver, CO 80202'));
384
385
        $this->assertInstanceOf(AddressCollection::class, $results);
386
        $this->assertCount(1, $results);
387
388
        /** @var GoogleAddress $result */
389
        $result = $results->first();
390
        $this->assertInstanceOf(Address::class, $result);
391
        $this->assertEquals('1125 17th Street', $result->getPremise());
392
        $this->assertEquals('Denver', $result->getLocality());
393
        $this->assertEquals('United States', $result->getCountry());
394
        $this->assertEquals('Central', $result->getNeighborhood());
395
    }
396
397
    public function testGeocodeWithColloquialAreaComponent()
398
    {
399
        $provider = new GoogleMaps($this->getHttpClient());
400
        $results = $provider->geocodeQuery(GeocodeQuery::create('darwin'));
401
402
        $this->assertInstanceOf(AddressCollection::class, $results);
403
        $this->assertCount(3, $results);
404
405
        /** @var GoogleAddress $result */
406
        $result = $results->first();
407
        $this->assertInstanceOf(Address::class, $result);
408
        $this->assertEquals('Darwin', $result->getColloquialArea());
409
    }
410
411
    public function testGeocodeWithWardComponent()
412
    {
413
        $provider = new GoogleMaps($this->getHttpClient());
414
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(35.03937, 135.729243));
415
416
        $this->assertInstanceOf(AddressCollection::class, $results);
417
        $this->assertCount(5, $results);
418
419
        /** @var GoogleAddress $result */
420
        $result = $results->first();
421
        $this->assertInstanceOf(Address::class, $result);
422
        $this->assertEquals('Kita-ku', $result->getWard());
423
    }
424
425
    public function testReverseWithSubLocalityLevels()
426
    {
427
        $provider = new GoogleMaps($this->getHttpClient());
428
        $results = $provider->reverseQuery(ReverseQuery::fromCoordinates(36.2745084, 136.9003169));
429
430
        $this->assertInstanceOf(AddressCollection::class, $results);
431
        $this->assertCount(5, $results);
432
433
        /** @var GoogleAddress $result */
434
        $result = $results->first();
435
        $this->assertInstanceOf(Address::class, $result);
436
        $this->assertInstanceOf('\Geocoder\Model\AdminLevelCollection', $result->getSubLocalityLevels());
437
        $this->assertEquals('Iijima', $result->getSubLocalityLevels()->get(1)->getName());
438
        $this->assertEquals('58', $result->getSubLocalityLevels()->get(4)->getName());
439
        $this->assertEquals(1, $result->getSubLocalityLevels()->get(1)->getLevel());
440
        $this->assertEquals(4, $result->getSubLocalityLevels()->get(4)->getLevel());
441
    }
442
443
    public function testGeocodeBoundsWithRealAddressWithViewportOnly()
444
    {
445
        $provider = new GoogleMaps($this->getHttpClient());
446
        $results = $provider->geocodeQuery(GeocodeQuery::create('Sibbe, Netherlands'));
447
448
        $this->assertInstanceOf(AddressCollection::class, $results);
449
        $this->assertCount(1, $results);
450
451
        /** @var \Geocoder\Model\Address $result */
452
        $result = $results->first();
453
        $this->assertInstanceOf(Address::class, $result);
454
        $this->assertNotNull($result->getBounds());
455
        $this->assertEquals(50.8433, $result->getBounds()->getSouth(), '', 0.001);
456
        $this->assertEquals(5.8259, $result->getBounds()->getWest(), '', 0.001);
457
        $this->assertEquals(50.8460, $result->getBounds()->getNorth(), '', 0.001);
458
        $this->assertEquals(5.8286, $result->getBounds()->getEast(), '', 0.001);
459
    }
460
}
461