Skip to content

Commit

Permalink
Merge pull request #276
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Dec 12, 2013
2 parents 1858ca8 + 403e250 commit cf7ca08
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<!-- <server name="IGN_WEB_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="GEOIPS_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="MAXMIND_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="MAPQUEST_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="GEONAMES_USERNAME" value="YOUR_USERNAME" /> -->
<!-- <server name="BAIDU_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="TOMTOM_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->
Expand Down
19 changes: 12 additions & 7 deletions src/Geocoder/Provider/MapQuestProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Geocoder\Provider;

use Geocoder\Exception\InvalidCredentialsException;
use Geocoder\HttpAdapter\HttpAdapterInterface;
use Geocoder\Exception\NoResultException;
use Geocoder\Exception\UnsupportedException;
Expand All @@ -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)
{
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand Down
58 changes: 38 additions & 20 deletions tests/Geocoder/Tests/Provider/MapQuestProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}

Expand All @@ -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');
}

Expand All @@ -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');
}

Expand All @@ -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');
}
}

0 comments on commit cf7ca08

Please sign in to comment.