Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ Rename the `phpunit.xml.dist` file to `phpunit.xml`, then uncomment the followin
<!-- <server name="BAIDU_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="TOMTOM_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
<!-- <server name="TOMTOM_MAP_KEY" value="YOUR_MAP_KEY" /> -->
<!-- <server name="GOOGLE_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
</php>
```

Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<!-- <server name="BAIDU_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="TOMTOM_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
<!-- <server name="TOMTOM_MAP_KEY" value="YOUR_MAP_KEY" /> -->
<!-- <server name="GOOGLE_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
</php>
<testsuites>
<testsuite name="Geocoder Test Suite">
Expand Down
5 changes: 5 additions & 0 deletions src/Geocoder/Provider/GoogleMapsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Geocoder/Tests/Provider/GoogleMapsBusinessProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
38 changes: 38 additions & 0 deletions tests/Geocoder/Tests/Provider/GoogleMapsProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

class GoogleMapsProviderTest extends TestCase
{
/**
* @var string
*/
private $testAPIKey = 'fake_key';

public function testGetName()
{
$provider = new GoogleMapsProvider($this->getMockAdapter($this->never()));
Expand Down Expand Up @@ -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');
}
}