Skip to content

Commit 1b385bb

Browse files
committed
Merge pull request #315 from Decave/314-throw_invalid_credentials_exception_with_bad_google_maps_key
Throw InvalidCredentialsException with GoogleMBP
2 parents 3ade97d + 607e47b commit 1b385bb

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ Rename the `phpunit.xml.dist` file to `phpunit.xml`, then uncomment the followin
631631
<!-- <server name="BAIDU_API_KEY" value="YOUR_API_KEY" /> -->
632632
<!-- <server name="TOMTOM_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
633633
<!-- <server name="TOMTOM_MAP_KEY" value="YOUR_MAP_KEY" /> -->
634+
<!-- <server name="GOOGLE_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
634635
</php>
635636
```
636637

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<!-- <server name="BAIDU_API_KEY" value="YOUR_API_KEY" /> -->
3030
<!-- <server name="TOMTOM_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
3131
<!-- <server name="TOMTOM_MAP_KEY" value="YOUR_MAP_KEY" /> -->
32+
<!-- <server name="GOOGLE_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
3233
</php>
3334
<testsuites>
3435
<testsuite name="Geocoder Test Suite">

src/Geocoder/Provider/GoogleMapsProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ protected function executeQuery($query)
130130

131131
$content = $this->getAdapter()->getContent($query);
132132

133+
// Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider
134+
if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) {
135+
throw new InvalidCredentialsException(sprintf('Invalid client ID / API Key %s', $query));
136+
}
137+
133138
if (null === $content) {
134139
throw new NoResultException(sprintf('Could not execute query %s', $query));
135140
}

tests/Geocoder/Tests/Provider/GoogleMapsBusinessProviderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,26 @@ public function testSignQuery()
6868

6969
$this->assertEquals($query.'&signature=yd07DufNspPyDE-Vj6nTeI5Fk-o=', $method->invoke($provider, $query));
7070
}
71+
72+
/**
73+
* @expectedException Geocoder\Exception\InvalidCredentialsException
74+
* @expectedExceptionMessage Invalid client ID / API Key https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&sensor=false&client=foo&signature=xhvnqoQoLos6iqijCu3b1Odmqr0=
75+
*/
76+
public function testGetGeocodedDataWithInvalidClientIdAndKey()
77+
{
78+
$provider = new GoogleMapsBusinessProvider($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, true);
79+
80+
$provider->getGeocodedData('Columbia University');
81+
}
82+
83+
/**
84+
* @expectedException Geocoder\Exception\InvalidCredentialsException
85+
* @expectedExceptionMessage Invalid client ID / API Key http://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&sensor=false&client=foo&signature=xhvnqoQoLos6iqijCu3b1Odmqr0=
86+
*/
87+
public function testGetGeocodedDataWithINvalidClientIdAndKeyNoSsl()
88+
{
89+
$provider = new GoogleMapsBusinessProvider($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, false);
90+
91+
$provider->getGeocodedData('Columbia University', true);
92+
}
7193
}

tests/Geocoder/Tests/Provider/GoogleMapsProviderTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class GoogleMapsProviderTest extends TestCase
99
{
10+
/**
11+
* @var string
12+
*/
13+
private $testAPIKey = 'fake_key';
14+
1015
public function testGetName()
1116
{
1217
$provider = new GoogleMapsProvider($this->getMockAdapter($this->never()));
@@ -296,4 +301,37 @@ public function testGetGeocodedDataWithInavlidApiKey()
296301
$provider = new GoogleMapsProvider($this->getMockAdapterReturns('{"error_message":"The provided API key is invalid.", "status":"REQUEST_DENIED"}'));
297302
$provider->getGeocodedData('10 avenue Gambetta, Paris, France');
298303
}
304+
305+
public function testGetGeocodedDataWithRealValidApiKey()
306+
{
307+
if (!isset($_SERVER['GOOGLE_GEOCODING_KEY'])) {
308+
$this->markTestSkipped('You need to configure the GOOGLE_GEOCODING_KEY value in phpunit.xml');
309+
}
310+
311+
$provider = new GoogleMapsProvider($this->getAdapter(), null, null, true, $_SERVER['GOOGLE_GEOCODING_KEY']);
312+
313+
$data = $provider->getGeocodedData('Columbia University');
314+
$data = $data[0];
315+
316+
$this->assertNotNull($data['latitude']);
317+
$this->assertNotNull($data['longitude']);
318+
$this->assertNotNull($data['bounds']['south']);
319+
$this->assertNotNull($data['bounds']['west']);
320+
$this->assertNotNull($data['bounds']['north']);
321+
$this->assertNotNull($data['bounds']['east']);
322+
$this->assertEquals('New York', $data['city']);
323+
$this->assertEquals('Manhattan', $data['cityDistrict']);
324+
$this->assertEquals('New York', $data['region']);
325+
}
326+
327+
/**
328+
* @expectedException \Geocoder\Exception\InvalidCredentialsException
329+
* @expectedExceptionMessage API key is invalid https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&sensor=false&key=fake_key
330+
*/
331+
public function testGetGeocodedDataWithRealInvalidApiKey()
332+
{
333+
$provider = new GoogleMapsProvider($this->getAdapter(), null, null, true, $this->testAPIKey);
334+
335+
$provider->getGeocodedData('Columbia University');
336+
}
299337
}

0 commit comments

Comments
 (0)