diff --git a/src/Geocoder/Provider/ArcGISOnline.php b/src/Geocoder/Provider/ArcGISOnline.php index 1a1378abc..0549e3219 100644 --- a/src/Geocoder/Provider/ArcGISOnline.php +++ b/src/Geocoder/Provider/ArcGISOnline.php @@ -17,7 +17,7 @@ /** * @author ALKOUM Dorian */ -class ArcGISOnline extends AbstractHttpProvider implements Provider +final class ArcGISOnline extends AbstractHttpProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/BingMaps.php b/src/Geocoder/Provider/BingMaps.php index 38896617a..2723c18f4 100644 --- a/src/Geocoder/Provider/BingMaps.php +++ b/src/Geocoder/Provider/BingMaps.php @@ -18,7 +18,7 @@ /** * @author David Guyon */ -class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider +final class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider { use LocaleTrait; diff --git a/src/Geocoder/Provider/Chain.php b/src/Geocoder/Provider/Chain.php index 5abf7bfcb..8c3f7da47 100644 --- a/src/Geocoder/Provider/Chain.php +++ b/src/Geocoder/Provider/Chain.php @@ -16,7 +16,7 @@ /** * @author Markus Bachmann */ -class Chain implements LocaleAwareProvider +final class Chain implements LocaleAwareProvider { use LocaleTrait; diff --git a/src/Geocoder/Provider/FreeGeoIp.php b/src/Geocoder/Provider/FreeGeoIp.php index 6ccb0e99c..0cdfabe3f 100644 --- a/src/Geocoder/Provider/FreeGeoIp.php +++ b/src/Geocoder/Provider/FreeGeoIp.php @@ -17,7 +17,7 @@ /** * @author William Durand */ -class FreeGeoIp extends AbstractHttpProvider implements Provider +final class FreeGeoIp extends AbstractHttpProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/GeoIP2.php b/src/Geocoder/Provider/GeoIP2.php index f6f7c6d70..e0006d3de 100644 --- a/src/Geocoder/Provider/GeoIP2.php +++ b/src/Geocoder/Provider/GeoIP2.php @@ -18,7 +18,7 @@ /** * @author Jens Wiese */ -class GeoIP2 extends AbstractProvider implements LocaleAwareProvider +final class GeoIP2 extends AbstractProvider implements LocaleAwareProvider { use LocaleTrait; diff --git a/src/Geocoder/Provider/GeoIPs.php b/src/Geocoder/Provider/GeoIPs.php index 7eac8963c..176fb8e24 100644 --- a/src/Geocoder/Provider/GeoIPs.php +++ b/src/Geocoder/Provider/GeoIPs.php @@ -23,7 +23,7 @@ * * @link http://www.geoips.com/en/developer/api-guide */ -class GeoIPs extends AbstractHttpProvider implements Provider +final class GeoIPs extends AbstractHttpProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/GeoPlugin.php b/src/Geocoder/Provider/GeoPlugin.php index 2330c1e25..9efa88bcd 100644 --- a/src/Geocoder/Provider/GeoPlugin.php +++ b/src/Geocoder/Provider/GeoPlugin.php @@ -16,7 +16,7 @@ /** * @author Andrea Cristaudo */ -class GeoPlugin extends AbstractHttpProvider implements Provider +final class GeoPlugin extends AbstractHttpProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/Geoip.php b/src/Geocoder/Provider/Geoip.php index 0789bce61..e594c82b9 100644 --- a/src/Geocoder/Provider/Geoip.php +++ b/src/Geocoder/Provider/Geoip.php @@ -19,7 +19,7 @@ * * @link http://php.net/manual/ref.geoip.php */ -class Geoip extends AbstractProvider implements Provider +final class Geoip extends AbstractProvider implements Provider { public function __construct() { diff --git a/src/Geocoder/Provider/Geonames.php b/src/Geocoder/Provider/Geonames.php index 45b537fe9..2649c1458 100644 --- a/src/Geocoder/Provider/Geonames.php +++ b/src/Geocoder/Provider/Geonames.php @@ -19,7 +19,7 @@ /** * @author Giovanni Pirrotta */ -class Geonames extends AbstractHttpProvider implements LocaleAwareProvider +final class Geonames extends AbstractHttpProvider implements LocaleAwareProvider { /** * @var string diff --git a/src/Geocoder/Provider/GoogleMaps.php b/src/Geocoder/Provider/GoogleMaps.php index 977dea858..e0d179507 100644 --- a/src/Geocoder/Provider/GoogleMaps.php +++ b/src/Geocoder/Provider/GoogleMaps.php @@ -20,7 +20,7 @@ /** * @author William Durand */ -class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider +final class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider { /** * @var string @@ -49,6 +49,38 @@ class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider */ private $apiKey; + /** + * @var string + */ + private $clientId; + + /** + * @var string + */ + private $privateKey; + + /** + * Google Maps for Business + * https://developers.google.com/maps/documentation/business/ + * + * @param HttpClient $client An HTTP adapter + * @param string $clientId Your Client ID + * @param string $privateKey Your Private Key (optional) + * @param string $locale A locale (optional) + * @param string $region Region biasing (optional) + * @param bool $useSsl Whether to use an SSL connection (optional) + * @param string $apiKey Google Geocoding API key (optional) + * @return GoogleMaps + */ + public static function business(HttpClient $client, $clientId, $privateKey = null, $locale = null, $region = null, $useSsl = false, $apiKey = null) + { + $provider = new self($client, $locale, $region, $useSsl, $apiKey); + $provider->clientId = $clientId; + $provider->privateKey = $privateKey; + + return $provider; + } + /** * @param HttpClient $client An HTTP adapter * @param string $locale A locale (optional) @@ -113,7 +145,7 @@ public function setRegion($region) * * @return string query with extra params */ - protected function buildQuery($query) + private function buildQuery($query) { if (null !== $this->getLocale()) { $query = sprintf('%s&language=%s', $query, $this->getLocale()); @@ -127,6 +159,14 @@ protected function buildQuery($query) $query = sprintf('%s&key=%s', $query, $this->apiKey); } + if (null !== $this->clientId) { + $query = sprintf('%s&client=%s', $query, $this->clientId); + + if (null !== $this->privateKey) { + $query = $this->signQuery($query); + } + } + return $query; } @@ -269,4 +309,31 @@ private function updateAddressComponent(&$resultSet, $type, $values) return $resultSet; } + + /** + * Sign a URL with a given crypto key + * Note that this URL must be properly URL-encoded + * src: http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source + * + * @param string $query Query to be signed + * + * @return string $query Query with signature appended. + */ + private function signQuery($query) + { + $url = parse_url($query); + + $urlPartToSign = $url['path'] . '?' . $url['query']; + + // Decode the private key into its binary format + $decodedKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $this->privateKey)); + + // Create a signature using the private key and the URL-encoded + // string using HMAC SHA1. This signature will be binary. + $signature = hash_hmac('sha1', $urlPartToSign, $decodedKey, true); + + $encodedSignature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature)); + + return sprintf('%s&signature=%s', $query, $encodedSignature); + } } diff --git a/src/Geocoder/Provider/GoogleMapsBusiness.php b/src/Geocoder/Provider/GoogleMapsBusiness.php deleted file mode 100644 index 3cb97891b..000000000 --- a/src/Geocoder/Provider/GoogleMapsBusiness.php +++ /dev/null @@ -1,98 +0,0 @@ - - */ -class GoogleMapsBusiness extends GoogleMaps implements Provider -{ - /** - * @var string - */ - private $clientId; - - /** - * @var string - */ - private $privateKey; - - /** - * @param HttpClient $client An HTTP adapter. - * @param string $clientId Your Client ID. - * @param string $privateKey Your Private Key (optional). - * @param string $locale A locale (optional). - * @param string $region Region biasing (optional). - * @param bool $useSsl Whether to use an SSL connection (optional) - */ - public function __construct(HttpClient $client, $clientId, $privateKey = null, $locale = null, $region = null, $useSsl = false) - { - parent::__construct($client, $locale, $region, $useSsl); - - $this->clientId = $clientId; - $this->privateKey = $privateKey; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'google_maps_business'; - } - - /** - * {@inheritDoc} - */ - protected function buildQuery($query) - { - $query = parent::buildQuery($query); - $query = sprintf('%s&client=%s', $query, $this->clientId); - - if (null !== $this->privateKey) { - $query = $this->signQuery($query); - } - - return $query; - } - - /** - * Sign a URL with a given crypto key - * Note that this URL must be properly URL-encoded - * src: http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source - * - * @param string $query Query to be signed - * - * @return string $query Query with signature appended. - */ - private function signQuery($query) - { - $url = parse_url($query); - - $urlPartToSign = $url['path'] . '?' . $url['query']; - - // Decode the private key into its binary format - $decodedKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $this->privateKey)); - - // Create a signature using the private key and the URL-encoded - // string using HMAC SHA1. This signature will be binary. - $signature = hash_hmac('sha1', $urlPartToSign, $decodedKey, true); - - $encodedSignature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature)); - - return sprintf('%s&signature=%s', $query, $encodedSignature); - } -} diff --git a/src/Geocoder/Provider/HostIp.php b/src/Geocoder/Provider/HostIp.php index be9c31726..5a5eb6dee 100644 --- a/src/Geocoder/Provider/HostIp.php +++ b/src/Geocoder/Provider/HostIp.php @@ -16,7 +16,7 @@ /** * @author William Durand */ -class HostIp extends AbstractHttpProvider implements Provider +final class HostIp extends AbstractHttpProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/IpInfoDb.php b/src/Geocoder/Provider/IpInfoDb.php index a81c2d49e..5a042ef13 100644 --- a/src/Geocoder/Provider/IpInfoDb.php +++ b/src/Geocoder/Provider/IpInfoDb.php @@ -19,7 +19,7 @@ /** * @author William Durand */ -class IpInfoDb extends AbstractHttpProvider implements Provider +final class IpInfoDb extends AbstractHttpProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/MapQuest.php b/src/Geocoder/Provider/MapQuest.php index a37819ac9..55e1878ad 100644 --- a/src/Geocoder/Provider/MapQuest.php +++ b/src/Geocoder/Provider/MapQuest.php @@ -18,7 +18,7 @@ /** * @author William Durand */ -class MapQuest extends AbstractHttpProvider implements Provider +final class MapQuest extends AbstractHttpProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/Mapzen.php b/src/Geocoder/Provider/Mapzen.php index 2f1fe8e8e..0641168db 100644 --- a/src/Geocoder/Provider/Mapzen.php +++ b/src/Geocoder/Provider/Mapzen.php @@ -18,7 +18,7 @@ /** * @author Gary Gale */ -class Mapzen extends AbstractHttpProvider +final class Mapzen extends AbstractHttpProvider { /** * @var string @@ -69,7 +69,7 @@ public function geocode($address) $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->scheme, urlencode($address), $this->apiKey, $this->getLimit()); - return $this->executeQuery($query); + return $this->executeQuery($query); } /** diff --git a/src/Geocoder/Provider/MaxMind.php b/src/Geocoder/Provider/MaxMind.php index 0bd7d0217..5eb6e0212 100644 --- a/src/Geocoder/Provider/MaxMind.php +++ b/src/Geocoder/Provider/MaxMind.php @@ -18,7 +18,7 @@ /** * @author Andrea Cristaudo */ -class MaxMind extends AbstractHttpProvider implements Provider +final class MaxMind extends AbstractHttpProvider implements Provider { /** * @var string Country, City, ISP and Organization diff --git a/src/Geocoder/Provider/MaxMindBinary.php b/src/Geocoder/Provider/MaxMindBinary.php index c7a7d19dd..7f59f147c 100644 --- a/src/Geocoder/Provider/MaxMindBinary.php +++ b/src/Geocoder/Provider/MaxMindBinary.php @@ -15,7 +15,7 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -class MaxMindBinary extends AbstractProvider implements Provider +final class MaxMindBinary extends AbstractProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/Nominatim.php b/src/Geocoder/Provider/Nominatim.php index f9ed8cac5..d903c2ff9 100644 --- a/src/Geocoder/Provider/Nominatim.php +++ b/src/Geocoder/Provider/Nominatim.php @@ -17,7 +17,7 @@ /** * @author Niklas Närhinen */ -class Nominatim extends AbstractHttpProvider implements LocaleAwareProvider +final class Nominatim extends AbstractHttpProvider implements LocaleAwareProvider { use LocaleTrait; @@ -26,6 +26,16 @@ class Nominatim extends AbstractHttpProvider implements LocaleAwareProvider */ private $rootUrl; + /** + * @param HttpClient $client + * @param string|null $locale + * @return Nominatim + */ + public static function withOpenStreetMapServer(HttpClient $client, $locale = null) + { + return new self($client, 'http://nominatim.openstreetmap.org', $locale); + } + /** * @param HttpClient $client An HTTP adapter. * @param string $rootUrl Root URL of the nominatim server diff --git a/src/Geocoder/Provider/OpenCage.php b/src/Geocoder/Provider/OpenCage.php index e98786b11..a75448f58 100644 --- a/src/Geocoder/Provider/OpenCage.php +++ b/src/Geocoder/Provider/OpenCage.php @@ -18,7 +18,7 @@ /** * @author mtm */ -class OpenCage extends AbstractHttpProvider implements LocaleAwareProvider +final class OpenCage extends AbstractHttpProvider implements LocaleAwareProvider { /** * @var string diff --git a/src/Geocoder/Provider/OpenStreetMap.php b/src/Geocoder/Provider/OpenStreetMap.php deleted file mode 100644 index 081b56b8e..000000000 --- a/src/Geocoder/Provider/OpenStreetMap.php +++ /dev/null @@ -1,41 +0,0 @@ - - */ -class OpenStreetMap extends Nominatim -{ - /** - * @var string - */ - const ROOT_URL = 'http://nominatim.openstreetmap.org'; - - /** - * @param HttpClient $client An HTTP adapter. - * @param string $locale A locale (optional). - */ - public function __construct(HttpClient $client, $locale = null) - { - parent::__construct($client, static::ROOT_URL, $locale); - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'openstreetmap'; - } -} diff --git a/src/Geocoder/Provider/TomTom.php b/src/Geocoder/Provider/TomTom.php index e78750b70..731d1c5f2 100644 --- a/src/Geocoder/Provider/TomTom.php +++ b/src/Geocoder/Provider/TomTom.php @@ -18,7 +18,7 @@ /** * @author Antoine Corcy */ -class TomTom extends AbstractHttpProvider implements LocaleAwareProvider +final class TomTom extends AbstractHttpProvider implements LocaleAwareProvider { use LocaleTrait; diff --git a/src/Geocoder/Provider/Yandex.php b/src/Geocoder/Provider/Yandex.php index 6deab1e46..e41e3d12b 100644 --- a/src/Geocoder/Provider/Yandex.php +++ b/src/Geocoder/Provider/Yandex.php @@ -17,7 +17,7 @@ /** * @author Antoine Corcy */ -class Yandex extends AbstractHttpProvider implements LocaleAwareProvider +final class Yandex extends AbstractHttpProvider implements LocaleAwareProvider { use LocaleTrait; diff --git a/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php b/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php deleted file mode 100644 index fc5323721..000000000 --- a/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php +++ /dev/null @@ -1,93 +0,0 @@ -getMockAdapter($this->never()), $this->testClientId); - $this->assertEquals('google_maps_business', $provider->getName()); - } - - public function testBuildQueryWithNoPrivateKey() - { - $method = new \ReflectionMethod( - 'Geocoder\Provider\GoogleMapsBusiness', 'buildQuery' - ); - - $method->setAccessible(true); - - $provider = new GoogleMapsBusiness($this->getMockAdapter($this->never()), $this->testClientId); - $query = 'http://maps.googleapis.com/maps/api/geocode/json?address=blah'; - - $this->assertEquals($query.'&client=foo', $method->invoke($provider, $query)); - } - - public function testBuildQueryWithPrivateKey() - { - $method = new \ReflectionMethod( - 'Geocoder\Provider\GoogleMapsBusiness', 'buildQuery' - ); - - $method->setAccessible(true); - - $provider = new GoogleMapsBusiness( - $this->getMockAdapter($this->never()), - $this->testClientId, - $this->testPrivateKey - ); - - $query = 'http://maps.googleapis.com/maps/api/geocode/json?address=blah'; - - $this->assertEquals($query.'&client=foo&signature=9G2weMhhd4E2ciR681gp9YabvUg=', $method->invoke($provider, $query)); - } - - public function testSignQuery() - { - $method = new \ReflectionMethod( - 'Geocoder\Provider\GoogleMapsBusiness', 'signQuery' - ); - - $method->setAccessible(true); - - $provider = new GoogleMapsBusiness( - $this->getMockAdapter($this->never()), - $this->testClientId, - $this->testPrivateKey - ); - - $query = 'http://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France'; - - $this->assertEquals($query.'&signature=7oRS85BVVpPUsyrd4MWFGMJNWok=', $method->invoke($provider, $query)); - } - - /** - * @expectedException \Geocoder\Exception\InvalidCredentials - * @expectedExceptionMessage Invalid client ID / API Key https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&client=foo&signature=9dJq1hPF7_iwafUpnqXUqEkP0gY= - */ - public function testGeocodeWithInvalidClientIdAndKey() - { - $provider = new GoogleMapsBusiness($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, true); - - $provider->geocode('Columbia University'); - } - - /** - * @expectedException \Geocoder\Exception\InvalidCredentials - * @expectedExceptionMessage Invalid client ID / API Key http://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&client=foo&signature=9dJq1hPF7_iwafUpnqXUqEkP0gY= - */ - public function testGeocodeWithInvalidClientIdAndKeyNoSsl() - { - $provider = new GoogleMapsBusiness($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, false); - - $provider->geocode('Columbia University', true); - } -} diff --git a/tests/Geocoder/Tests/Provider/GoogleMapsTest.php b/tests/Geocoder/Tests/Provider/GoogleMapsTest.php index 5e89c2ca6..258ed5bfc 100644 --- a/tests/Geocoder/Tests/Provider/GoogleMapsTest.php +++ b/tests/Geocoder/Tests/Provider/GoogleMapsTest.php @@ -2,8 +2,11 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Exception\NoResult; use Geocoder\Tests\TestCase; use Geocoder\Provider\GoogleMaps; +use Http\Client\HttpClient; +use Psr\Http\Message\RequestInterface; class GoogleMapsTest extends TestCase { @@ -358,4 +361,70 @@ public function testGeocodePostalTown() $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('Pontypridd', $result->getLocality()); } + + public function testBusinessQueryWithoutPrivateKey() + { + $uri = ''; + + $provider = GoogleMaps::business( + $this->getMockAdapterWithRequestCallback( + function (RequestInterface $request) use (&$uri) { + $uri = $request->getUri(); + } + ), + 'foo' + ); + + try { + $provider->geocode('blah'); + } catch (NoResult $e) { + } + + $this->assertEquals('http://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo', $uri); + } + + public function testBusinessQueryWithPrivateKey() + { + $uri = ''; + + $provider = GoogleMaps::business( + $this->getMockAdapterWithRequestCallback( + function (RequestInterface $request) use (&$uri) { + $uri = (string)$request->getUri(); + } + ), + 'foo', + 'bogus' + ); + + try { + $provider->geocode('blah'); + } catch (NoResult $e) { + } + + $this->assertEquals( + 'http://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo&signature=9G2weMhhd4E2ciR681gp9YabvUg=', + $uri + ); + } + + /** + * @expectedException \Geocoder\Exception\InvalidCredentials + * @expectedExceptionMessage Invalid client ID / API Key https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&client=foo&signature=9dJq1hPF7_iwafUpnqXUqEkP0gY= + */ + public function testGeocodeWithInvalidClientIdAndKey() + { + $provider = GoogleMaps::business($this->getAdapter(), 'foo', 'bogus', null, null, true); + $provider->geocode('Columbia University'); + } + + /** + * @expectedException \Geocoder\Exception\InvalidCredentials + * @expectedExceptionMessage Invalid client ID / API Key http://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&client=foo&signature=9dJq1hPF7_iwafUpnqXUqEkP0gY= + */ + public function testGeocodeWithInvalidClientIdAndKeyNoSsl() + { + $provider = GoogleMaps::business($this->getAdapter(), 'foo', 'bogus', null, null, false); + $provider->geocode('Columbia University'); + } } diff --git a/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php b/tests/Geocoder/Tests/Provider/NominatimTest.php similarity index 93% rename from tests/Geocoder/Tests/Provider/OpenStreetMapTest.php rename to tests/Geocoder/Tests/Provider/NominatimTest.php index 1c28b5389..7fb931548 100644 --- a/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php +++ b/tests/Geocoder/Tests/Provider/NominatimTest.php @@ -2,20 +2,14 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Provider\Nominatim; use Geocoder\Tests\TestCase; -use Geocoder\Provider\OpenStreetMap; -class OpenStreetMapTest extends TestCase +class NominatimTest extends TestCase { - public function testGetName() - { - $provider = new OpenStreetMap($this->getMockAdapter($this->never())); - $this->assertEquals('openstreetmap', $provider->getName()); - } - public function testGeocodeWithRealAddress() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $results = $provider->geocode('Paris'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); @@ -134,7 +128,7 @@ public function testGeocodeWithRealAddress() public function testGeocodeWithRealAddressWithLocale() { - $provider = new OpenStreetMap($this->getAdapter(), 'fr_FR'); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter(), 'fr_FR'); $results = $provider->geocode('10 allée Evariste Galois, Clermont ferrand'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); @@ -187,7 +181,7 @@ public function testGeocodeWithRealAddressWithLocale() public function testReverseWithRealCoordinates() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $results = $provider->reverse(60.4539471728726, 22.2567841926781); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); @@ -218,13 +212,13 @@ public function testReverseWithRealCoordinates() */ public function testGeocodeWithUnknownCity() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $provider->geocode('Hammm'); } public function testReverseWithRealCoordinatesWithLocale() { - $provider = new OpenStreetMap($this->getAdapter(), 'de_DE'); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter(), 'de_DE'); $results = $provider->geocode('Kalbacher Hauptstraße, 60437 Frankfurt, Germany'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); @@ -317,7 +311,7 @@ public function testReverseWithRealCoordinatesWithLocale() public function testGeocodeWithLocalhostIPv4() { - $provider = new OpenStreetMap($this->getMockAdapter($this->never())); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapter($this->never())); $results = $provider->geocode('127.0.0.1'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); @@ -333,17 +327,17 @@ public function testGeocodeWithLocalhostIPv4() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The Geocoder\Provider\OpenStreetMap provider does not support IPv6 addresses. + * @expectedExceptionMessage The Geocoder\Provider\Nominatim provider does not support IPv6 addresses. */ public function testGeocodeWithLocalhostIPv6() { - $provider = new OpenStreetMap($this->getMockAdapter($this->never())); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapter($this->never())); $provider->geocode('::1'); } public function testGeocodeWithRealIPv4() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $results = $provider->geocode('88.188.221.14'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); @@ -374,7 +368,7 @@ public function testGeocodeWithRealIPv4() public function testGeocodeWithRealIPv4WithLocale() { - $provider = new OpenStreetMap($this->getAdapter(), 'da_DK'); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter(), 'da_DK'); $results = $provider->geocode('88.188.221.14'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); @@ -405,11 +399,11 @@ public function testGeocodeWithRealIPv4WithLocale() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The Geocoder\Provider\OpenStreetMap provider does not support IPv6 addresses. + * @expectedExceptionMessage The Geocoder\Provider\Nominatim provider does not support IPv6 addresses. */ public function testGeocodeWithRealIPv6() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $provider->geocode('::ffff:88.188.221.14'); } @@ -419,7 +413,7 @@ public function testGeocodeWithRealIPv6() */ public function testGeocodeWithAddressGetsNullContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns(null)); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns(null)); $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } @@ -429,7 +423,7 @@ public function testGeocodeWithAddressGetsNullContent() */ public function testGeocodeWithAddressGetsEmptyContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns('')); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns('')); $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } @@ -442,7 +436,7 @@ public function testGeocodeWithAddressGetsEmptyXML() $emptyXML = << XML; - $provider = new OpenStreetMap($this->getMockAdapterReturns($emptyXML)); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns($emptyXML)); $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } @@ -452,7 +446,7 @@ public function testGeocodeWithAddressGetsEmptyXML() */ public function testReverseWithCoordinatesGetsNullContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns(null)); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns(null)); $provider->reverse(60.4539471728726, 22.2567841926781); } @@ -462,7 +456,7 @@ public function testReverseWithCoordinatesGetsNullContent() */ public function testReverseWithCoordinatesGetsEmptyContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns('')); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns('')); $provider->reverse(60.4539471728726, 22.2567841926781); } @@ -478,13 +472,13 @@ public function testReverseWithCoordinatesGetsError() Unable to geocode XML; - $provider = new OpenStreetMap($this->getMockAdapterReturns($errorXml)); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns($errorXml)); $provider->reverse(-80.000000, -170.000000); } public function testGetNodeStreetName() { - $provider = new OpenStreetMap($this->getAdapter(), 'fr_FR'); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter(), 'fr_FR'); $results = $provider->reverse(48.86, 2.35); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); diff --git a/tests/Geocoder/Tests/Provider/OpenCageTest.php b/tests/Geocoder/Tests/Provider/OpenCageTest.php index db0ef0bed..9a7ebb1ed 100644 --- a/tests/Geocoder/Tests/Provider/OpenCageTest.php +++ b/tests/Geocoder/Tests/Provider/OpenCageTest.php @@ -334,15 +334,3 @@ public function testGeocodeWithRealIPv6() $provider->geocode('::ffff:74.200.247.59'); } } - -class OpenCageMock extends OpenCage -{ - /** - * Short circuits so assertions can inspect the - * executed query URL - */ - protected function executeQuery($query) - { - return $query; - } -} diff --git a/tests/Geocoder/Tests/TestCase.php b/tests/Geocoder/Tests/TestCase.php index 34be318e1..6af5baba8 100644 --- a/tests/Geocoder/Tests/TestCase.php +++ b/tests/Geocoder/Tests/TestCase.php @@ -7,6 +7,8 @@ use Http\Client\HttpClient; use Http\Mock\Client as MockClient; use Http\Adapter\Guzzle6\Client as GuzzleClient; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; /** * @author William Durand @@ -47,6 +49,30 @@ protected function getMockAdapterReturns($returnValue) return $client; } + /** + * @param callable $requestCallback + * @return HttpClient + */ + protected function getMockAdapterWithRequestCallback(callable $requestCallback) + { + $client = $this->getMockForAbstractClass(HttpClient::class); + + $client + ->expects($this->once()) + ->method('sendRequest') + ->willReturnCallback(function (RequestInterface $request) use ($requestCallback) { + $response = $requestCallback($request); + + if (!$response instanceof ResponseInterface) { + $response = new Response(200, [], (string) $response); + } + + return $response; + }); + + return $client; + } + /** * Because I was bored to fix the test suite because of * a change in a third-party API...