diff --git a/README.md b/README.md index 174d270fb..b44e99a26 100644 --- a/README.md +++ b/README.md @@ -631,6 +631,7 @@ Rename the `phpunit.xml.dist` file to `phpunit.xml`, then uncomment the followin + ``` diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2fdd998bb..f0bb67127 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -29,6 +29,7 @@ + diff --git a/src/Geocoder/Provider/GoogleMapsProvider.php b/src/Geocoder/Provider/GoogleMapsProvider.php index 74a919175..fa6964073 100644 --- a/src/Geocoder/Provider/GoogleMapsProvider.php +++ b/src/Geocoder/Provider/GoogleMapsProvider.php @@ -130,6 +130,11 @@ protected function executeQuery($query) $content = $this->getAdapter()->getContent($query); + // Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider + if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) { + throw new InvalidCredentialsException(sprintf('Invalid client ID / API Key %s', $query)); + } + if (null === $content) { throw new NoResultException(sprintf('Could not execute query %s', $query)); } diff --git a/tests/Geocoder/Tests/Provider/GoogleMapsBusinessProviderTest.php b/tests/Geocoder/Tests/Provider/GoogleMapsBusinessProviderTest.php index 0b99b0b78..54f6577ab 100644 --- a/tests/Geocoder/Tests/Provider/GoogleMapsBusinessProviderTest.php +++ b/tests/Geocoder/Tests/Provider/GoogleMapsBusinessProviderTest.php @@ -68,4 +68,26 @@ public function testSignQuery() $this->assertEquals($query.'&signature=yd07DufNspPyDE-Vj6nTeI5Fk-o=', $method->invoke($provider, $query)); } + + /** + * @expectedException Geocoder\Exception\InvalidCredentialsException + * @expectedExceptionMessage Invalid client ID / API Key https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&sensor=false&client=foo&signature=xhvnqoQoLos6iqijCu3b1Odmqr0= + */ + public function testGetGeocodedDataWithInvalidClientIdAndKey() + { + $provider = new GoogleMapsBusinessProvider($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, true); + + $provider->getGeocodedData('Columbia University'); + } + + /** + * @expectedException Geocoder\Exception\InvalidCredentialsException + * @expectedExceptionMessage Invalid client ID / API Key http://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&sensor=false&client=foo&signature=xhvnqoQoLos6iqijCu3b1Odmqr0= + */ + public function testGetGeocodedDataWithINvalidClientIdAndKeyNoSsl() + { + $provider = new GoogleMapsBusinessProvider($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, false); + + $provider->getGeocodedData('Columbia University', true); + } } diff --git a/tests/Geocoder/Tests/Provider/GoogleMapsProviderTest.php b/tests/Geocoder/Tests/Provider/GoogleMapsProviderTest.php index 982697069..5897400d6 100644 --- a/tests/Geocoder/Tests/Provider/GoogleMapsProviderTest.php +++ b/tests/Geocoder/Tests/Provider/GoogleMapsProviderTest.php @@ -7,6 +7,11 @@ class GoogleMapsProviderTest extends TestCase { + /** + * @var string + */ + private $testAPIKey = 'fake_key'; + public function testGetName() { $provider = new GoogleMapsProvider($this->getMockAdapter($this->never())); @@ -296,4 +301,37 @@ public function testGetGeocodedDataWithInavlidApiKey() $provider = new GoogleMapsProvider($this->getMockAdapterReturns('{"error_message":"The provided API key is invalid.", "status":"REQUEST_DENIED"}')); $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); } + + public function testGetGeocodedDataWithRealValidApiKey() + { + if (!isset($_SERVER['GOOGLE_GEOCODING_KEY'])) { + $this->markTestSkipped('You need to configure the GOOGLE_GEOCODING_KEY value in phpunit.xml'); + } + + $provider = new GoogleMapsProvider($this->getAdapter(), null, null, true, $_SERVER['GOOGLE_GEOCODING_KEY']); + + $data = $provider->getGeocodedData('Columbia University'); + $data = $data[0]; + + $this->assertNotNull($data['latitude']); + $this->assertNotNull($data['longitude']); + $this->assertNotNull($data['bounds']['south']); + $this->assertNotNull($data['bounds']['west']); + $this->assertNotNull($data['bounds']['north']); + $this->assertNotNull($data['bounds']['east']); + $this->assertEquals('New York', $data['city']); + $this->assertEquals('Manhattan', $data['cityDistrict']); + $this->assertEquals('New York', $data['region']); + } + + /** + * @expectedException \Geocoder\Exception\InvalidCredentialsException + * @expectedExceptionMessage API key is invalid https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&sensor=false&key=fake_key + */ + public function testGetGeocodedDataWithRealInvalidApiKey() + { + $provider = new GoogleMapsProvider($this->getAdapter(), null, null, true, $this->testAPIKey); + + $provider->getGeocodedData('Columbia University'); + } }