Skip to content

Commit cf7ca08

Browse files
committed
Merge pull request #276
2 parents 1858ca8 + 403e250 commit cf7ca08

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ The `ChainProvider` named `chain` is a special provider that takes a list of pro
190190
### MapQuestProvider ###
191191

192192
The `MapQuestProvider` named `map_quest` is able to geocode and reverse geocode **street addresses**.
193+
A valid api key is required.
193194

194195

195196
### OIORestProvider ###

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<!-- <server name="IGN_WEB_API_KEY" value="YOUR_API_KEY" /> -->
2525
<!-- <server name="GEOIPS_API_KEY" value="YOUR_API_KEY" /> -->
2626
<!-- <server name="MAXMIND_API_KEY" value="YOUR_API_KEY" /> -->
27+
<!-- <server name="MAPQUEST_API_KEY" value="YOUR_API_KEY" /> -->
2728
<!-- <server name="GEONAMES_USERNAME" value="YOUR_USERNAME" /> -->
2829
<!-- <server name="BAIDU_API_KEY" value="YOUR_API_KEY" /> -->
2930
<!-- <server name="TOMTOM_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" /> -->

src/Geocoder/Provider/MapQuestProvider.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Geocoder\Provider;
1212

13+
use Geocoder\Exception\InvalidCredentialsException;
1314
use Geocoder\HttpAdapter\HttpAdapterInterface;
1415
use Geocoder\Exception\NoResultException;
1516
use Geocoder\Exception\UnsupportedException;
@@ -22,17 +23,17 @@ class MapQuestProvider extends AbstractProvider implements ProviderInterface
2223
/**
2324
* @var string
2425
*/
25-
const GEOCODE_ENDPOINT_NOKEY_URL = 'http://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&thumbMaps=false';
26+
const GEOCODE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s&thumbMaps=false';
2627

2728
/**
2829
* @var string
2930
*/
30-
const GEOCODE_ENDPOINT_URL = 'http://www.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s';
31+
const REVERSE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/reverse?key=%s&lat=%F&lng=%F';
3132

3233
/**
3334
* @var string
3435
*/
35-
const REVERSE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/reverse?lat=%F&lng=%F';
36+
private $apiKey = null;
3637

3738
public function __construct(HttpAdapterInterface $adapter, $locale = null, $apiKey = null)
3839
{
@@ -52,11 +53,11 @@ public function getGeocodedData($address)
5253
}
5354

5455
if (null === $this->apiKey) {
55-
$query = sprintf(self::GEOCODE_ENDPOINT_NOKEY_URL, urlencode($address), $this->getMaxResults());
56-
} else {
57-
$query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey);
56+
throw new InvalidCredentialsException('No API Key provided.');
5857
}
5958

59+
$query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey);
60+
6061
return $this->executeQuery($query);
6162
}
6263

@@ -65,7 +66,11 @@ public function getGeocodedData($address)
6566
*/
6667
public function getReversedData(array $coordinates)
6768
{
68-
$query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[0], $coordinates[1]);
69+
if (null === $this->apiKey) {
70+
throw new InvalidCredentialsException('No API Key provided.');
71+
}
72+
73+
$query = sprintf(self::REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]);
6974

7075
return $this->executeQuery($query);
7176
}

tests/Geocoder/Tests/Provider/MapQuestProviderTest.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,11 @@ public function testGetName()
2222
*/
2323
public function testGetGeocodedData()
2424
{
25-
$provider = new MapQuestProvider($this->getMockAdapter());
26-
$provider->getGeocodedData('foobar');
27-
}
25+
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
26+
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
27+
}
2828

29-
/**
30-
* @expectedException Geocoder\Exception\NoResultException
31-
* @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
32-
*/
33-
public function testGetGeocodedDataWithApiKey()
34-
{
35-
$provider = new MapQuestProvider($this->getMockAdapter(), null, $apiKey = 'my-api-key');
29+
$provider = new MapQuestProvider($this->getMockAdapter(), null, $_SERVER['MAPQUEST_API_KEY']);
3630
$provider->getGeocodedData('foobar');
3731
}
3832

@@ -42,13 +36,21 @@ public function testGetGeocodedDataWithApiKey()
4236
*/
4337
public function testGetGeocodedDataWithAddressGetsNullContent()
4438
{
45-
$provider = new MapQuestProvider($this->getMockAdapterReturns(null));
39+
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
40+
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
41+
}
42+
43+
$provider = new MapQuestProvider($this->getMockAdapterReturns(null), null, $_SERVER['MAPQUEST_API_KEY']);
4644
$provider->getGeocodedData('10 avenue Gambetta, Paris, France');
4745
}
4846

4947
public function testGetGeocodedDataWithRealAddress()
5048
{
51-
$provider = new MapQuestProvider($this->getAdapter());
49+
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
50+
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
51+
}
52+
53+
$provider = new MapQuestProvider($this->getAdapter(), null, $_SERVER['MAPQUEST_API_KEY']);
5254
$results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France');
5355

5456
$this->assertInternalType('array', $results);
@@ -77,13 +79,21 @@ public function testGetGeocodedDataWithRealAddress()
7779
*/
7880
public function testGetReversedData()
7981
{
80-
$provider = new MapQuestProvider($this->getMockAdapter());
82+
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
83+
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
84+
}
85+
86+
$provider = new MapQuestProvider($this->getMockAdapter(), null, $_SERVER['MAPQUEST_API_KEY']);
8187
$provider->getReversedData(array(1, 2));
8288
}
8389

8490
public function testGetReversedDataWithRealCoordinates()
8591
{
86-
$provider = new MapQuestProvider($this->getAdapter());
92+
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
93+
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
94+
}
95+
96+
$provider = new MapQuestProvider($this->getAdapter(), null, $_SERVER['MAPQUEST_API_KEY']);
8797
$result = $provider->getReversedData(array(54.0484068, -2.7990345));
8898

8999
$this->assertInternalType('array', $result);
@@ -108,7 +118,11 @@ public function testGetReversedDataWithRealCoordinates()
108118

109119
public function testGetGeocodedDataWithCity()
110120
{
111-
$provider = new MapQuestProvider($this->getAdapter());
121+
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
122+
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
123+
}
124+
125+
$provider = new MapQuestProvider($this->getAdapter(), null, $_SERVER['MAPQUEST_API_KEY']);
112126
$results = $provider->getGeocodedData('Hanover');
113127

114128
$this->assertInternalType('array', $results);
@@ -148,7 +162,11 @@ public function testGetGeocodedDataWithCity()
148162

149163
public function testGetGeocodedDataWithCityDistrict()
150164
{
151-
$provider = new MapQuestProvider($this->getAdapter());
165+
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
166+
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
167+
}
168+
169+
$provider = new MapQuestProvider($this->getAdapter(), null, $_SERVER['MAPQUEST_API_KEY']);
152170
$result = $provider->getGeocodedData('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany');
153171

154172
$this->assertInternalType('array', $result);
@@ -178,7 +196,7 @@ public function testGetGeocodedDataWithCityDistrict()
178196
*/
179197
public function testGetGeocodedDataWithLocalhostIPv4()
180198
{
181-
$provider = new MapQuestProvider($this->getMockAdapter($this->never()));
199+
$provider = new MapQuestProvider($this->getMockAdapter($this->never()), null, $apiKey = 'my-api-key');
182200
$provider->getGeocodedData('127.0.0.1');
183201
}
184202

@@ -188,7 +206,7 @@ public function testGetGeocodedDataWithLocalhostIPv4()
188206
*/
189207
public function testGetGeocodedDataWithLocalhostIPv6()
190208
{
191-
$provider = new MapQuestProvider($this->getMockAdapter($this->never()));
209+
$provider = new MapQuestProvider($this->getMockAdapter($this->never()), null, $apiKey = 'my-api-key');
192210
$provider->getGeocodedData('::1');
193211
}
194212

@@ -198,7 +216,7 @@ public function testGetGeocodedDataWithLocalhostIPv6()
198216
*/
199217
public function testGetGeocodedDataWithRealIPv4()
200218
{
201-
$provider = new MapQuestProvider($this->getAdapter());
219+
$provider = new MapQuestProvider($this->getAdapter(), null, $apiKey = 'my-api-key');
202220
$provider->getGeocodedData('74.200.247.59');
203221
}
204222

@@ -208,7 +226,7 @@ public function testGetGeocodedDataWithRealIPv4()
208226
*/
209227
public function testGetGeocodedDataWithRealIPv6()
210228
{
211-
$provider = new MapQuestProvider($this->getAdapter());
229+
$provider = new MapQuestProvider($this->getAdapter(), null, $apiKey = 'my-api-key');
212230
$provider->getGeocodedData('::ffff:74.200.247.59');
213231
}
214232
}

0 commit comments

Comments
 (0)