Skip to content

Commit 763b93b

Browse files
comxdjbelien
andauthored
[GoogleMapsPlaces] Fix error message not displayed when using a bad API key (#1138)
* Fix error message not displayed when using a bad API key Fixes #1137 * Add test * Add cached response --------- Co-authored-by: Jonathan Beliën <jbelien@users.noreply.github.com> Co-authored-by: Jonathan Beliën <dev@jbelien.be>
1 parent 5556556 commit 763b93b

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/Provider/GoogleMapsPlaces/GoogleMapsPlaces.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ private function validateResponse(string $url, $content): StdClass
433433
throw new InvalidArgument(sprintf('Invalid Request %s', $url));
434434
}
435435

436-
if ('REQUEST_DENIED' === $json->status && 'The provided API key is invalid.' === $json->error_messages) {
436+
if ('REQUEST_DENIED' === $json->status && 'The provided API key is invalid.' === $json->error_message) {
437437
throw new InvalidCredentials(sprintf('API key is invalid %s', $url));
438438
}
439439

440440
if ('REQUEST_DENIED' === $json->status) {
441-
throw new InvalidServerResponse(sprintf('API access denied. Request: %s - Message: %s', $url, $json->error_messages));
441+
throw new InvalidServerResponse(sprintf('API access denied. Request: %s - Message: %s', $url, $json->error_message));
442442
}
443443

444444
if ('OVER_QUERY_LIMIT' === $json->status) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
s:114:"{
2+
"candidates" : [],
3+
"error_message" : "The provided API key is invalid.",
4+
"status" : "REQUEST_DENIED"
5+
}
6+
";

src/Provider/GoogleMapsPlaces/Tests/GoogleMapsPlacesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Geocoder\Provider\GoogleMapsPlaces\Tests;
1414

1515
use Geocoder\Exception\InvalidArgument;
16+
use Geocoder\Exception\InvalidCredentials;
1617
use Geocoder\Exception\QuotaExceeded;
1718
use Geocoder\Exception\UnsupportedOperation;
1819
use Geocoder\IntegrationTest\BaseTestCase;
@@ -308,6 +309,15 @@ public function testReverseGeocodePlaceSearchWithEmptyOpeningHours()
308309
// sometimes giving: Failed asserting that Object ['openNow' => null, 'periods' => [], 'weekdayText' => []] is null
309310
}
310311

312+
public function testInvalidAPIKey()
313+
{
314+
$this->expectException(InvalidCredentials::class);
315+
$this->expectExceptionMessageMatches('/^API key is invalid/');
316+
317+
$provider = new GoogleMapsPlaces($this->getHttpClient('invalidKey'), 'invalidKey');
318+
$provider->geocodeQuery(GeocodeQuery::create('White house'));
319+
}
320+
311321
private function getGoogleMapsProvider(): GoogleMapsPlaces
312322
{
313323
if (!isset($_SERVER['GOOGLE_GEOCODING_KEY'])) {

0 commit comments

Comments
 (0)