diff --git a/README.md b/README.md index e23f64c24..6589d39d4 100644 --- a/README.md +++ b/README.md @@ -190,6 +190,7 @@ The `ChainProvider` named `chain` is a special provider that takes a list of pro ### MapQuestProvider ### The `MapQuestProvider` named `map_quest` is able to geocode and reverse geocode **street addresses**. +A valid api key is required. ### OIORestProvider ### diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ffc02bc30..2fdd998bb 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -24,6 +24,7 @@ + diff --git a/src/Geocoder/Provider/MapQuestProvider.php b/src/Geocoder/Provider/MapQuestProvider.php index b6e48393d..f0ba15ac5 100644 --- a/src/Geocoder/Provider/MapQuestProvider.php +++ b/src/Geocoder/Provider/MapQuestProvider.php @@ -10,6 +10,7 @@ namespace Geocoder\Provider; +use Geocoder\Exception\InvalidCredentialsException; use Geocoder\HttpAdapter\HttpAdapterInterface; use Geocoder\Exception\NoResultException; use Geocoder\Exception\UnsupportedException; @@ -22,17 +23,17 @@ class MapQuestProvider extends AbstractProvider implements ProviderInterface /** * @var string */ - const GEOCODE_ENDPOINT_NOKEY_URL = 'http://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&thumbMaps=false'; + const GEOCODE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s&thumbMaps=false'; /** * @var string */ - const GEOCODE_ENDPOINT_URL = 'http://www.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s'; + const REVERSE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/reverse?key=%s&lat=%F&lng=%F'; /** * @var string */ - const REVERSE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/reverse?lat=%F&lng=%F'; + private $apiKey = null; public function __construct(HttpAdapterInterface $adapter, $locale = null, $apiKey = null) { @@ -52,11 +53,11 @@ public function getGeocodedData($address) } if (null === $this->apiKey) { - $query = sprintf(self::GEOCODE_ENDPOINT_NOKEY_URL, urlencode($address), $this->getMaxResults()); - } else { - $query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey); + throw new InvalidCredentialsException('No API Key provided.'); } + $query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey); + return $this->executeQuery($query); } @@ -65,7 +66,11 @@ public function getGeocodedData($address) */ public function getReversedData(array $coordinates) { - $query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[0], $coordinates[1]); + if (null === $this->apiKey) { + throw new InvalidCredentialsException('No API Key provided.'); + } + + $query = sprintf(self::REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]); return $this->executeQuery($query); } diff --git a/tests/Geocoder/Tests/Provider/MapQuestProviderTest.php b/tests/Geocoder/Tests/Provider/MapQuestProviderTest.php index b1c7c1e2f..13abac361 100644 --- a/tests/Geocoder/Tests/Provider/MapQuestProviderTest.php +++ b/tests/Geocoder/Tests/Provider/MapQuestProviderTest.php @@ -22,17 +22,11 @@ public function testGetName() */ public function testGetGeocodedData() { - $provider = new MapQuestProvider($this->getMockAdapter()); - $provider->getGeocodedData('foobar'); - } + if (!isset($_SERVER['MAPQUEST_API_KEY'])) { + $this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml'); + } - /** - * @expectedException Geocoder\Exception\NoResultException - * @expectedExceptionMessage Could not find results for given query: http://www.mapquestapi.com/geocoding/v1/address?location=foobar&outFormat=json&maxResults=5&key=my-api-key - */ - public function testGetGeocodedDataWithApiKey() - { - $provider = new MapQuestProvider($this->getMockAdapter(), null, $apiKey = 'my-api-key'); + $provider = new MapQuestProvider($this->getMockAdapter(), null, $_SERVER['MAPQUEST_API_KEY']); $provider->getGeocodedData('foobar'); } @@ -42,13 +36,21 @@ public function testGetGeocodedDataWithApiKey() */ public function testGetGeocodedDataWithAddressGetsNullContent() { - $provider = new MapQuestProvider($this->getMockAdapterReturns(null)); + if (!isset($_SERVER['MAPQUEST_API_KEY'])) { + $this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml'); + } + + $provider = new MapQuestProvider($this->getMockAdapterReturns(null), null, $_SERVER['MAPQUEST_API_KEY']); $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithRealAddress() { - $provider = new MapQuestProvider($this->getAdapter()); + if (!isset($_SERVER['MAPQUEST_API_KEY'])) { + $this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml'); + } + + $provider = new MapQuestProvider($this->getAdapter(), null, $_SERVER['MAPQUEST_API_KEY']); $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); $this->assertInternalType('array', $results); @@ -77,13 +79,21 @@ public function testGetGeocodedDataWithRealAddress() */ public function testGetReversedData() { - $provider = new MapQuestProvider($this->getMockAdapter()); + if (!isset($_SERVER['MAPQUEST_API_KEY'])) { + $this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml'); + } + + $provider = new MapQuestProvider($this->getMockAdapter(), null, $_SERVER['MAPQUEST_API_KEY']); $provider->getReversedData(array(1, 2)); } public function testGetReversedDataWithRealCoordinates() { - $provider = new MapQuestProvider($this->getAdapter()); + if (!isset($_SERVER['MAPQUEST_API_KEY'])) { + $this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml'); + } + + $provider = new MapQuestProvider($this->getAdapter(), null, $_SERVER['MAPQUEST_API_KEY']); $result = $provider->getReversedData(array(54.0484068, -2.7990345)); $this->assertInternalType('array', $result); @@ -108,7 +118,11 @@ public function testGetReversedDataWithRealCoordinates() public function testGetGeocodedDataWithCity() { - $provider = new MapQuestProvider($this->getAdapter()); + if (!isset($_SERVER['MAPQUEST_API_KEY'])) { + $this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml'); + } + + $provider = new MapQuestProvider($this->getAdapter(), null, $_SERVER['MAPQUEST_API_KEY']); $results = $provider->getGeocodedData('Hanover'); $this->assertInternalType('array', $results); @@ -148,7 +162,11 @@ public function testGetGeocodedDataWithCity() public function testGetGeocodedDataWithCityDistrict() { - $provider = new MapQuestProvider($this->getAdapter()); + if (!isset($_SERVER['MAPQUEST_API_KEY'])) { + $this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml'); + } + + $provider = new MapQuestProvider($this->getAdapter(), null, $_SERVER['MAPQUEST_API_KEY']); $result = $provider->getGeocodedData('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'); $this->assertInternalType('array', $result); @@ -178,7 +196,7 @@ public function testGetGeocodedDataWithCityDistrict() */ public function testGetGeocodedDataWithLocalhostIPv4() { - $provider = new MapQuestProvider($this->getMockAdapter($this->never())); + $provider = new MapQuestProvider($this->getMockAdapter($this->never()), null, $apiKey = 'my-api-key'); $provider->getGeocodedData('127.0.0.1'); } @@ -188,7 +206,7 @@ public function testGetGeocodedDataWithLocalhostIPv4() */ public function testGetGeocodedDataWithLocalhostIPv6() { - $provider = new MapQuestProvider($this->getMockAdapter($this->never())); + $provider = new MapQuestProvider($this->getMockAdapter($this->never()), null, $apiKey = 'my-api-key'); $provider->getGeocodedData('::1'); } @@ -198,7 +216,7 @@ public function testGetGeocodedDataWithLocalhostIPv6() */ public function testGetGeocodedDataWithRealIPv4() { - $provider = new MapQuestProvider($this->getAdapter()); + $provider = new MapQuestProvider($this->getAdapter(), null, $apiKey = 'my-api-key'); $provider->getGeocodedData('74.200.247.59'); } @@ -208,7 +226,7 @@ public function testGetGeocodedDataWithRealIPv4() */ public function testGetGeocodedDataWithRealIPv6() { - $provider = new MapQuestProvider($this->getAdapter()); + $provider = new MapQuestProvider($this->getAdapter(), null, $apiKey = 'my-api-key'); $provider->getGeocodedData('::ffff:74.200.247.59'); } }