diff --git a/src/Geocoder/Dumper/Gpx.php b/src/Geocoder/Dumper/Gpx.php index c2bf15900..b4f258b4f 100644 --- a/src/Geocoder/Dumper/Gpx.php +++ b/src/Geocoder/Dumper/Gpx.php @@ -10,7 +10,7 @@ namespace Geocoder\Dumper; -use Geocoder\ProviderBasedGeocoder; +use Geocoder\Geocoder; use Geocoder\Model\Address; /** @@ -35,7 +35,7 @@ public function dump(Address $address) xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd"> GPX - , ProviderBasedGeocoder::VERSION); + , Geocoder::VERSION); if ($address->getBounds()->isDefined()) { $bounds = $address->getBounds(); diff --git a/src/Geocoder/Geocoder.php b/src/Geocoder/Geocoder.php index 5b1c0988e..8c9c164ee 100644 --- a/src/Geocoder/Geocoder.php +++ b/src/Geocoder/Geocoder.php @@ -25,7 +25,7 @@ interface Geocoder /** * Geocodes a given value. * - * @param string $value A value to geocode. + * @param string $value * * @return Address[] */ @@ -34,10 +34,28 @@ public function geocode($value); /** * Reverses geocode given latitude and longitude values. * - * @param double $latitude Latitude. - * @param double $longitude Longitude. + * @param double $latitude. + * @param double $longitude * * @return Address[] */ public function reverse($latitude, $longitude); + + /** + * Returns the maximum number of Address objects that can be + * returned by `geocode()` or `reverse()` methods. + * + * @return integer + */ + public function getLimit(); + + /** + * Sets the maximum number of `Address` objects that can be + * returned by `geocode()` or `reverse()` methods. + * + * @param integer $limit + * + * @return Geocoder + */ + public function limit($limit); } diff --git a/src/Geocoder/Provider/AbstractProvider.php b/src/Geocoder/Provider/AbstractProvider.php index 45a2df95f..e7fbb5e25 100644 --- a/src/Geocoder/Provider/AbstractProvider.php +++ b/src/Geocoder/Provider/AbstractProvider.php @@ -10,7 +10,8 @@ namespace Geocoder\Provider; -use Geocoder\ProviderBasedGeocoder; +use Geocoder\Geocoder; +use Geocoder\Model\AddressFactory; use Ivory\HttpAdapter\HttpAdapterInterface; /** @@ -21,68 +22,33 @@ abstract class AbstractProvider /** * @var HttpAdapterInterface */ - protected $adapter; + private $adapter; /** - * @var string + * @var AddressFactory */ - protected $locale; + private $factory; /** * @var integer */ - protected $maxResults = ProviderBasedGeocoder::MAX_RESULTS; + private $limit = Provider::MAX_RESULTS; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $locale A locale (optional). + * @param HttpAdapterInterface $adapter An HTTP adapter */ - public function __construct(HttpAdapterInterface $adapter, $locale = null) - { - $this->setAdapter($adapter); - $this->setLocale($locale); - } - - /** - * Returns the HTTP adapter. - * - * @return HttpAdapterInterface - */ - public function getAdapter() - { - return $this->adapter; - } - - /** - * Sets the HTTP adapter to be used for further requests. - * - * @param HttpAdapterInterface $adapter - * - * @return AbstractProvider - */ - public function setAdapter($adapter) + public function __construct(HttpAdapterInterface $adapter) { $this->adapter = $adapter; - - return $this; - } - - /** - * Returns the configured locale or null. - * - * @return string - */ - public function getLocale() - { - return $this->locale; + $this->factory = new AddressFactory(); } /** * {@inheritDoc} */ - public function setLocale($locale = null) + public function limit($limit) { - $this->locale = $locale; + $this->limit = $limit; return $this; } @@ -90,21 +56,19 @@ public function setLocale($locale = null) /** * {@inheritDoc} */ - public function setMaxResults($maxResults) + public function getLimit() { - $this->maxResults = $maxResults; - - return $this; + return $this->limit; } /** - * Returns the maximum of wished results. + * Returns the HTTP adapter. * - * @return integer + * @return HttpAdapterInterface */ - public function getMaxResults() + public function getAdapter() { - return $this->maxResults; + return $this->adapter; } /** @@ -114,10 +78,15 @@ public function getMaxResults() */ protected function getDefaults() { - return array( + return [ 'latitude' => null, 'longitude' => null, - 'bounds' => null, + 'bounds' => [ + 'south' => null, + 'west' => null, + 'north' => null, + 'east' => null, + ], 'streetNumber' => null, 'streetName' => null, 'locality' => null, @@ -130,7 +99,7 @@ protected function getDefaults() 'country' => null, 'countryCode' => null, 'timezone' => null, - ); + ]; } /** @@ -140,23 +109,25 @@ protected function getDefaults() */ protected function getLocalhostDefaults() { - return array( + return [ 'locality' => 'localhost', 'region' => 'localhost', 'county' => 'localhost', 'country' => 'localhost', - ); + ]; } /** - * @param array $results + * @param array $data An array of data. * - * @return array + * @return \Geocoder\Model\Address[] */ - protected function fixEncoding(array $results) + protected function returnResults(array $data = []) { - return array_map(function ($value) { - return is_string($value) ? utf8_encode($value) : $value; - }, $results); + if (0 < $this->getLimit()) { + $data = array_slice($data, 0, $this->getLimit()); + } + + return $this->factory->createFromArray($data); } } diff --git a/src/Geocoder/Provider/ArcGISOnline.php b/src/Geocoder/Provider/ArcGISOnline.php index 8f9ca870e..3e091ff06 100644 --- a/src/Geocoder/Provider/ArcGISOnline.php +++ b/src/Geocoder/Provider/ArcGISOnline.php @@ -40,25 +40,25 @@ class ArcGISOnline extends AbstractProvider implements Provider private $protocol; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $sourceCountry Country biasing (optional). - * @param bool $useSsl Whether to use an SSL connection (optional). + * @param HttpAdapterInterface $adapter An HTTP adapter + * @param string $sourceCountry Country biasing (optional) + * @param bool $useSsl Whether to use an SSL connection (optional) */ public function __construct(HttpAdapterInterface $adapter, $sourceCountry = null, $useSsl = false) { parent::__construct($adapter); $this->sourceCountry = $sourceCountry; - $this->protocol = $useSsl ? 'https' : 'http'; + $this->protocol = $useSsl ? 'https' : 'http'; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The ArcGISOnline does not support IP addresses.'); + throw new UnsupportedOperation('The ArcGISOnline provider does not support IP addresses, only street addresses.'); } // Save a request if no valid address entered @@ -67,16 +67,14 @@ public function getGeocodedData($address) } $query = sprintf(self::ENDPOINT_URL, $this->protocol, urlencode($address)); - - $json = $this->executeQuery($query); + $json = $this->executeQuery($query); // no result if (empty($json->locations)) { - throw new NoResult(sprintf('No results found for query %s', $query)); + throw new NoResult(sprintf('No results found for query "%s".', $query)); } - $results = array(); - + $results = []; foreach ($json->locations as $location) { $data = $location->feature->attributes; @@ -89,7 +87,7 @@ public function getGeocodedData($address) $county = !empty($data->Subregion) ? $data->Subregion : null; $countryCode = !empty($data->Country) ? $data->Country : null; - $results[] = array_merge($this->getDefaults(), array( + $results[] = array_merge($this->getDefaults(), [ 'latitude' => $coordinates['y'], 'longitude' => $coordinates['x'], 'streetNumber' => $streetNumber, @@ -99,23 +97,22 @@ public function getGeocodedData($address) 'region' => $region, 'countryCode' => $countryCode, 'county' => $county, - )); + ]); } - return $results; + return $this->returnResults($results); } /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { $query = sprintf(self::REVERSE_ENDPOINT_URL, $this->protocol, $coordinates[1], $coordinates[0]); - - $json = $this->executeQuery($query); + $json = $this->executeQuery($query); if (property_exists($json, 'error')) { - throw new NoResult(sprintf('No results found for query %s', $query)); + throw new NoResult(sprintf('No results found for query "%s".', $query)); } $data = $json->address; @@ -127,16 +124,18 @@ public function getReversedData(array $coordinates) $county = !empty($data->Subregion) ? $data->Subregion : null; $countryCode = !empty($data->CountryCode) ? $data->CountryCode : null; - return array(array_merge($this->getDefaults(), array( - 'latitude' => $coordinates[0], - 'longitude' => $coordinates[1], - 'streetName' => $streetName, - 'locality' => $city, - 'postalCode' => $zipcode, - 'region' => $region, - 'countryCode' => $countryCode, - 'county' => $county, - ))); + return $this->returnResults([ + array_merge($this->getDefaults(), [ + 'latitude' => $coordinates[0], + 'longitude' => $coordinates[1], + 'streetName' => $streetName, + 'locality' => $city, + 'postalCode' => $zipcode, + 'region' => $region, + 'countryCode' => $countryCode, + 'county' => $county, + ]) + ]); } /** @@ -147,43 +146,29 @@ public function getName() return 'arcgis_online'; } - /** - * @param string $query - * - * @return string Query with extra params - */ private function buildQuery($query) { if (null !== $this->sourceCountry) { $query = sprintf('%s&sourceCountry=%s', $query, $this->sourceCountry); } - return sprintf('%s&maxLocations=%d&f=%s&outFields=*', $query, $this->getMaxResults(), 'json'); + return sprintf('%s&maxLocations=%d&f=%s&outFields=*', $query, $this->getLimit(), 'json'); } - /** - * Executes a query - * - * @param string $query - * - * @throws NoResult - * - * @return \stdClass json object representing the query result - */ private function executeQuery($query) { - $query = $this->buildQuery($query); + $query = $this->buildQuery($query); $content = (string) $this->getAdapter()->get($query)->getBody(); if (empty($content)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $json = json_decode($content); // API error if (!isset($json)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } return $json; diff --git a/src/Geocoder/Provider/BingMaps.php b/src/Geocoder/Provider/BingMaps.php index 081e53e65..24f8d5c4c 100644 --- a/src/Geocoder/Provider/BingMaps.php +++ b/src/Geocoder/Provider/BingMaps.php @@ -36,32 +36,38 @@ class BingMaps extends AbstractProvider implements LocaleAwareProvider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param string $locale A locale (optional). + * @var string + */ + private $locale; + + /** + * @param HttpAdapterInterface $adapter An HTTP adapter + * @param string $apiKey An API key + * @param string $locale A locale (optional) */ public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null) { - parent::__construct($adapter, $locale); + parent::__construct($adapter); $this->apiKey = $apiKey; + $this->locale = $locale; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (null === $this->apiKey) { - throw new InvalidCredentials('No API Key provided'); + throw new InvalidCredentials('No API key provided.'); } // This API doesn't handle IPs if (filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The BingMaps does not support IP addresses.'); + throw new UnsupportedOperation('The BingMaps provider does not support IP addresses, only street providers.'); } - $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->getMaxResults(), urlencode($address), $this->apiKey); + $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->getLimit(), urlencode($address), $this->apiKey); return $this->executeQuery($query); } @@ -69,10 +75,10 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { if (null === $this->apiKey) { - throw new InvalidCredentials('No API Key provided'); + throw new InvalidCredentials('No API key provided.'); } $query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[0], $coordinates[1], $this->apiKey); @@ -89,10 +95,23 @@ public function getName() } /** - * @param string $query - * - * @return array + * {@inheritDoc} + */ + public function getLocale() + { + return $this->locale; + } + + /** + * {@inheritDoc} */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + private function executeQuery($query) { if (null !== $this->getLocale()) { @@ -102,30 +121,29 @@ private function executeQuery($query) $content = (string) $this->getAdapter()->get($query)->getBody(); if (empty($content)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $json = json_decode($content); if (!isset($json->resourceSets[0]) || !isset($json->resourceSets[0]->resources)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $data = (array) $json->resourceSets[0]->resources; - $results = array(); - + $results = []; foreach ($data as $item) { $coordinates = (array) $item->geocodePoints[0]->coordinates; $bounds = null; if (isset($item->bbox) && is_array($item->bbox) && count($item->bbox) > 0) { - $bounds = array( + $bounds = [ 'south' => $item->bbox[0], 'west' => $item->bbox[1], 'north' => $item->bbox[2], 'east' => $item->bbox[3] - ); + ]; } $streetNumber = null; @@ -136,7 +154,7 @@ private function executeQuery($query) $region = property_exists($item->address, 'adminDistrict') ? (string) $item->address->adminDistrict: ''; $country = property_exists($item->address, 'countryRegion') ? (string) $item->address->countryRegion: ''; - $results[] = array_merge($this->getDefaults(), array( + $results[] = array_merge($this->getDefaults(), [ 'latitude' => $coordinates[0], 'longitude' => $coordinates[1], 'bounds' => $bounds, @@ -147,9 +165,9 @@ private function executeQuery($query) 'county' => empty($county) ? null : $county, 'region' => empty($region) ? null : $region, 'country' => empty($country) ? null : $country, - )); + ]); } - return $results; + return $this->returnResults($results); } } diff --git a/src/Geocoder/Provider/Chain.php b/src/Geocoder/Provider/Chain.php index 6d50d7773..79550e867 100644 --- a/src/Geocoder/Provider/Chain.php +++ b/src/Geocoder/Provider/Chain.php @@ -24,8 +24,6 @@ class Chain implements Provider private $providers = []; /** - * Constructor - * * @param Provider[] $providers */ public function __construct(array $providers = []) @@ -33,26 +31,15 @@ public function __construct(array $providers = []) $this->providers = $providers; } - /** - * Add a provider - * - * @param Provider $provider - */ - public function add(Provider $provider) - { - $this->providers[] = $provider; - } - /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { $exceptions = []; - foreach ($this->providers as $provider) { try { - return $provider->getGeocodedData($address); + return $provider->geocode($address); } catch (InvalidCredentials $e) { throw $e; } catch (\Exception $e) { @@ -60,18 +47,18 @@ public function getGeocodedData($address) } } - throw new ChainNoResult(sprintf('No provider could provide the address "%s"', $address), $exceptions); + throw new ChainNoResult(sprintf('No provider could geocode address: "%s".', $address), $exceptions); } /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { $exceptions = []; foreach ($this->providers as $provider) { try { - return $provider->getReversedData($coordinates); + return $provider->reverse($latitude, $longitude); } catch (InvalidCredentials $e) { throw $e; } catch (\Exception $e) { @@ -79,21 +66,29 @@ public function getReversedData(array $coordinates) } } - throw new ChainNoResult(sprintf('No provider could provide the coordinated %s', json_encode($coordinates)), $exceptions); + throw new ChainNoResult(sprintf('No provider could reverse coordinates: %f, %f.', $latitude, $longitude), $exceptions); } /** * {@inheritDoc} */ - public function setMaxResults($limit) + public function limit($limit) { foreach ($this->providers as $provider) { - $provider->setMaxResults($limit); + $provider->limit($limit); } return $this; } + /** + * {@inheritDoc} + */ + public function getLimit() + { + throw new \LogicException("The `Chain` provider is not able to return the limit value."); + } + /** * {@inheritDoc} */ @@ -101,4 +96,18 @@ public function getName() { return 'chain'; } + + /** + * Adds a provider. + * + * @param Provider $provider + * + * @return Chain + */ + public function add(Provider $provider) + { + $this->providers[] = $provider; + + return $this; + } } diff --git a/src/Geocoder/Provider/FreeGeoIp.php b/src/Geocoder/Provider/FreeGeoIp.php index d2be04036..020a65ac8 100644 --- a/src/Geocoder/Provider/FreeGeoIp.php +++ b/src/Geocoder/Provider/FreeGeoIp.php @@ -26,10 +26,10 @@ class FreeGeoIp extends AbstractProvider implements Provider /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (!filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The FreeGeoIp does not support Street addresses.'); + throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.'); } if (in_array($address, array('127.0.0.1', '::1'))) { @@ -44,9 +44,9 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - throw new UnsupportedOperation('The FreeGeoIp is not able to do reverse geocoding.'); + throw new UnsupportedOperation('The FreeGeoIp provider is not able to do reverse geocoding.'); } /** @@ -57,11 +57,6 @@ public function getName() return 'free_geo_ip'; } - /** - * @param string $query - * - * @return array - */ private function executeQuery($query) { $content = (string) $this->getAdapter()->get($query)->getBody(); @@ -76,26 +71,28 @@ private function executeQuery($query) throw new NoResult(sprintf('Could not execute query %s', $query)); } - //it appears that for US states the region code is not returning the FIPS standard + // it appears that for US states the region code is not returning the FIPS standard if ('US' === $data['country_code'] && isset($data['region_code']) && !is_numeric($data['region_code'])) { - $newRegionCode = $this->stateToRegionCode($data['region_code']); + $newRegionCode = $this->stateToRegionCode($data['region_code']); $data['region_code'] = is_numeric($newRegionCode) ? $newRegionCode : null; } - return array(array_merge($this->getDefaults(), array( - 'latitude' => isset($data['latitude']) ? $data['latitude'] : null, - 'longitude' => isset($data['longitude']) ? $data['longitude'] : null, - 'locality' => isset($data['city']) ? $data['city'] : null, - 'postalCode' => isset($data['zipcode']) ? $data['zipcode'] : null, - 'region' => isset($data['region_name']) ? $data['region_name'] : null, - 'regionCode' => isset($data['region_code']) ? $data['region_code'] : null, - 'country' => isset($data['country_name']) ? $data['country_name'] : null, - 'countryCode' => isset($data['country_code']) ? $data['country_code'] : null, - ))); + return $this->returnResults([ + array_merge($this->getDefaults(), array( + 'latitude' => isset($data['latitude']) ? $data['latitude'] : null, + 'longitude' => isset($data['longitude']) ? $data['longitude'] : null, + 'locality' => isset($data['city']) ? $data['city'] : null, + 'postalCode' => isset($data['zipcode']) ? $data['zipcode'] : null, + 'region' => isset($data['region_name']) ? $data['region_name'] : null, + 'regionCode' => isset($data['region_code']) ? $data['region_code'] : null, + 'country' => isset($data['country_name']) ? $data['country_name'] : null, + 'countryCode' => isset($data['country_code']) ? $data['country_code'] : null, + )) + ]); } /** - * Converts the state code to FIPS standard + * Converts the state code to FIPS standard. * * @param string $state * diff --git a/src/Geocoder/Provider/GeoIP2.php b/src/Geocoder/Provider/GeoIP2.php index 957ed3c3d..5f0c94b74 100644 --- a/src/Geocoder/Provider/GeoIP2.php +++ b/src/Geocoder/Provider/GeoIP2.php @@ -19,24 +19,27 @@ /** * @author Jens Wiese */ -class GeoIP2 extends AbstractProvider implements Provider +class GeoIP2 extends AbstractProvider implements LocaleAwareProvider { /** - * {@inheritdoc} + * @var string */ + private $locale; + public function __construct(GeoIP2Adapter $adapter, $locale = 'en') { - $this->setAdapter($adapter); - $this->setLocale($locale); + parent::__construct($adapter); + + $this->locale = $locale; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { - if (false === filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation(sprintf('The %s does not support street addresses.', __CLASS__)); + if (!filter_var($address, FILTER_VALIDATE_IP)) { + throw new UnsupportedOperation('The GeoIP2 provider does not support street addresses, only IP addresses.'); } if ('127.0.0.1' === $address) { @@ -46,34 +49,36 @@ public function getGeocodedData($address) $result = json_decode($this->executeQuery($address)); //Try to extract the region name and code - $region = null; + $region = null; $regionCode = null; if (isset($result->subdivisions) && is_array($result->subdivisions) && !empty($result->subdivisions)) { $lastSubdivision = array_pop($result->subdivisions); - $region = (isset($lastSubdivision->names->{$this->locale}) ? $lastSubdivision->names->{$this->locale} : null); + $region = (isset($lastSubdivision->names->{$this->locale}) ? $lastSubdivision->names->{$this->locale} : null); $regionCode = (isset($lastSubdivision->iso_code) ? $lastSubdivision->iso_code : null); } - return array($this->fixEncoding(array_merge($this->getDefaults(), array( - 'countryCode' => (isset($result->country->iso_code) ? $result->country->iso_code : null), - 'country' => (isset($result->country->names->{$this->locale}) ? $result->country->names->{$this->locale} : null), - 'locality' => (isset($result->city->names->{$this->locale}) ? $result->city->names->{$this->locale} : null), - 'latitude' => (isset($result->location->latitude) ? $result->location->latitude : null), - 'longitude' => (isset($result->location->longitude) ? $result->location->longitude : null), - 'timezone' => (isset($result->location->timezone) ? $result->location->timezone : null), - 'postalCode' => (isset($result->location->postalcode) ? $result->location->postalcode : null), - 'region' => $region, - 'regionCode' => $regionCode - )))); + return $this->returnResults([ + array_merge($this->getDefaults(), array( + 'countryCode' => (isset($result->country->iso_code) ? $result->country->iso_code : null), + 'country' => (isset($result->country->names->{$this->locale}) ? $result->country->names->{$this->locale} : null), + 'locality' => (isset($result->city->names->{$this->locale}) ? $result->city->names->{$this->locale} : null), + 'latitude' => (isset($result->location->latitude) ? $result->location->latitude : null), + 'longitude' => (isset($result->location->longitude) ? $result->location->longitude : null), + 'timezone' => (isset($result->location->timezone) ? $result->location->timezone : null), + 'postalCode' => (isset($result->location->postalcode) ? $result->location->postalcode : null), + 'region' => $region, + 'regionCode' => $regionCode + )) + ]); } /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - throw new UnsupportedOperation(sprintf('The %s is not able to do reverse geocoding.', __CLASS__)); + throw new UnsupportedOperation('The GeoIP2 provider is not able to do reverse geocoding.'); } /** @@ -81,25 +86,39 @@ public function getReversedData(array $coordinates) */ public function getName() { - return 'maxmind_geoip2'; + return 'geoip2'; + } + + /** + * {@inheritDoc} + */ + public function getLocale() + { + return $this->locale; } /** - * @param string $address - * @throws \Geocoder\Exception\NoResult - * @return City + * {@inheritDoc} */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + private function executeQuery($address) { $uri = sprintf('file://geoip?%s', $address); try { - $result = $this->getAdapter()->setLocale($this->locale)->getContent($uri); + $result = $this->adapter + ->setLocale($this->locale) + ->getContent($uri); } catch (AddressNotFoundException $e) { - throw new NoResult(sprintf('No results found for IP address %s', $address)); + throw new NoResult(sprintf('No results found for IP address "%s".', $address)); } return $result; } - } diff --git a/src/Geocoder/Provider/GeoIPs.php b/src/Geocoder/Provider/GeoIPs.php index fc400acf9..30272148d 100644 --- a/src/Geocoder/Provider/GeoIPs.php +++ b/src/Geocoder/Provider/GeoIPs.php @@ -28,28 +28,34 @@ class GeoIPs extends AbstractProvider implements Provider /** * @var string */ - const GEOCODE_ENDPOINT_URL = 'http://api.geoips.com/ip/%s/key/%s/output/json/timezone/true/'; + const GEOCODE_ENDPOINT_URL = 'http://api.geoips.com/ip/%s/key/%s/output/json/timezone/true/'; const CODE_SUCCESS = '200_1'; // The following results has been returned. + const CODE_NOT_FOUND = '200_2'; // No result set has been returned. + const CODE_BAD_KEY = '400_1'; // Error in the URI - The API call should include a API key parameter. + const CODE_BAD_IP = '400_2'; // Error in the URI - The API call should include a valid IP address. + const CODE_NOT_AUTHORIZED = '403_1'; // The API key associated with your request was not recognized. + const CODE_ACCOUNT_INACTIVE = '403_2'; // The API key has not been approved or has been disabled. + const CODE_LIMIT_EXCEEDED = '403_3'; // The service you have requested is over capacity. /** * @var string */ - private $apiKey = null; + private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. + * @param HttpAdapterInterface $adapter An HTTP adapter + * @param string $apiKey An API key */ public function __construct(HttpAdapterInterface $adapter, $apiKey) { - parent::__construct($adapter, null); + parent::__construct($adapter); $this->apiKey = $apiKey; } @@ -57,19 +63,18 @@ public function __construct(HttpAdapterInterface $adapter, $apiKey) /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (null === $this->apiKey) { - throw new InvalidCredentials('No API Key provided.'); + throw new InvalidCredentials('No API key provided.'); } if (!filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The GeoIPs does not support street addresses.'); + throw new UnsupportedOperation('The GeoIPs provider does not support street addresses, only IPv4 addresses.'); } - // This API does not support IPv6 if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - throw new UnsupportedOperation('The GeoIPs does not support IPv6 addresses.'); + throw new UnsupportedOperation('The GeoIPs provider does not support IPv6 addresses, only IPv4 addresses.'); } if ('127.0.0.1' === $address) { @@ -84,9 +89,9 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - throw new UnsupportedOperation('The GeoIPs is not able to do reverse geocoding.'); + throw new UnsupportedOperation('The GeoIPs provider is not able to do reverse geocoding.'); } /** @@ -97,16 +102,12 @@ public function getName() return 'geo_ips'; } - /** - * @param string $query - * @return array - */ private function executeQuery($query) { $content = (string) $this->getAdapter()->get($query)->getBody(); if (empty($content)) { - throw new NoResult(sprintf('Invalid response from GeoIPs server for query %s', $query)); + throw new NoResult(sprintf('Invalid response from GeoIPs API for query "%s".', $query)); } $json = json_decode($content, true); @@ -136,7 +137,7 @@ private function executeQuery($query) } if (!is_array($json) || empty($json) || empty($json['response']) || empty($json['response']['code'])) { - throw new NoResult(sprintf('Invalid response from GeoIPs server for query %s', $query)); + throw new NoResult(sprintf('Invalid response from GeoIPs API for query "%s".', $query)); } $response = $json['response']; @@ -150,7 +151,7 @@ private function executeQuery($query) break; default: throw new NoResult(sprintf( - 'GeoIPs returned unknown result code %s for query: %s', + 'The GeoIPs API returned unknown result code "%s" for query: "%s".', $response['code'], $query )); @@ -158,11 +159,11 @@ private function executeQuery($query) // Make sure that we do have proper result array if (empty($response['location']) || !is_array($response['location'])) { - throw new NoResult(sprintf('Invalid response from GeoIPs server for query %s', $query)); + throw new NoResult(sprintf('Invalid response from GeoIPs API for query "%s".', $query)); } - $locations = []; - $location = $response['location']; + $locations = []; + $location = $response['location']; $locations[] = array_merge($this->getDefaults(), array( 'country' => '' === $location['country_name'] ? null : $location['country_name'], 'countryCode' => '' === $location['country_code'] ? null : $location['country_code'], @@ -175,6 +176,6 @@ private function executeQuery($query) 'timezone' => '' === $location['timezone'] ? null : $location['timezone'], )); - return $locations; + return $this->returnResults($locations); } } diff --git a/src/Geocoder/Provider/GeoPlugin.php b/src/Geocoder/Provider/GeoPlugin.php index 2100296d1..5368181d9 100644 --- a/src/Geocoder/Provider/GeoPlugin.php +++ b/src/Geocoder/Provider/GeoPlugin.php @@ -26,14 +26,14 @@ class GeoPlugin extends AbstractProvider implements Provider /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (!filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The GeoPlugin does not support street addresses.'); + throw new UnsupportedOperation('The GeoPlugin provider does not support street addresses, only IP addresses.'); } if (in_array($address, array('127.0.0.1', '::1'))) { - return array($this->getLocalhostDefaults()); + return $this->returnResults([ $this->getLocalhostDefaults() ]); } $query = sprintf(self::GEOCODE_ENDPOINT_URL, $address); @@ -44,9 +44,9 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - throw new UnsupportedOperation('The GeoPlugin is not able to do reverse geocoding.'); + throw new UnsupportedOperation('The GeoPlugin provider is not able to do reverse geocoding.'); } /** @@ -57,39 +57,37 @@ public function getName() return 'geo_plugin'; } - /** - * @param string $query - * - * @return array - */ private function executeQuery($query) { $content = (string) $this->getAdapter()->get($query)->getBody(); if (empty($content)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $json = json_decode($content, true); if (!is_array($json) || !count($json)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } if (!array_key_exists('geoplugin_status', $json) || (200 !== $json['geoplugin_status'] && 206 !== $json['geoplugin_status'])) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $data = array_filter($json); - return array(array_merge($this->getDefaults(), array( - 'locality' => isset($data['geoplugin_city']) ? $data['geoplugin_city'] : null, - 'country' => isset($data['geoplugin_countryName']) ? $data['geoplugin_countryName'] : null, - 'countryCode' => isset($data['geoplugin_countryCode']) ? $data['geoplugin_countryCode'] : null, - 'region' => isset($data['geoplugin_regionName']) ? $data['geoplugin_regionName'] : null, - 'regionCode' => isset($data['geoplugin_regionCode']) ? $data['geoplugin_regionCode'] : null, - 'latitude' => isset($data['geoplugin_latitude']) ? $data['geoplugin_latitude'] : null, - 'longitude' => isset($data['geoplugin_longitude']) ? $data['geoplugin_longitude'] : null, - ))); + $results = []; + $results[] = array_merge($this->getDefaults(), [ + 'locality' => isset($data['geoplugin_city']) ? $data['geoplugin_city'] : null, + 'country' => isset($data['geoplugin_countryName']) ? $data['geoplugin_countryName'] : null, + 'countryCode' => isset($data['geoplugin_countryCode']) ? $data['geoplugin_countryCode'] : null, + 'region' => isset($data['geoplugin_regionName']) ? $data['geoplugin_regionName'] : null, + 'regionCode' => isset($data['geoplugin_regionCode']) ? $data['geoplugin_regionCode'] : null, + 'latitude' => isset($data['geoplugin_latitude']) ? $data['geoplugin_latitude'] : null, + 'longitude' => isset($data['geoplugin_longitude']) ? $data['geoplugin_longitude'] : null, + ]); + + return $this->returnResults($results); } } diff --git a/src/Geocoder/Provider/Geoip.php b/src/Geocoder/Provider/Geoip.php index fe31ed396..dbf6bc8a1 100644 --- a/src/Geocoder/Provider/Geoip.php +++ b/src/Geocoder/Provider/Geoip.php @@ -11,7 +11,7 @@ namespace Geocoder\Provider; use Geocoder\Exception\NoResult; -use Geocoder\Exception\ExceptionNotLoaded; +use Geocoder\Exception\ExtensionNotLoaded; use Geocoder\Exception\UnsupportedOperation; /** @@ -21,62 +21,61 @@ */ class Geoip extends AbstractProvider implements Provider { - /** - * No need to pass a HTTP adapter. - */ public function __construct() { if (!function_exists('geoip_record_by_name')) { - throw new ExceptionNotLoaded('You have to install GeoIP'); + throw new ExtensionNotLoaded('You must install the GeoIP extension, see: https://php.net/manual/book.geoip.php.'); } } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (!filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The Geoip does not support Street addresses.'); + throw new UnsupportedOperation('The Geoip provider does not support street addresses, only IPv4 addresses.'); } // This API does not support IPv6 if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - throw new UnsupportedOperation('The Geoip does not support IPv6 addresses.'); + throw new UnsupportedOperation('The Geoip provider does not support IPv6 addresses, only IPv4 addresses.'); } if ('127.0.0.1' === $address) { - return array($this->getLocalhostDefaults()); + return $this->returnResults([ $this->getLocalhostDefaults() ]); } $results = @geoip_record_by_name($address); if (!is_array($results)) { - throw new NoResult(sprintf('Could not find %s ip address in database.', $address)); + throw new NoResult(sprintf('Could not find "%s" IP address in database.', $address)); } $timezone = @geoip_time_zone_by_country_and_region($results['country_code'], $results['region']) ?: null; $region = @geoip_region_name_by_code($results['country_code'], $results['region']) ?: $results['region']; - return array($this->fixEncoding(array_merge($this->getDefaults(), array( - 'latitude' => $results['latitude'], - 'longitude' => $results['longitude'], - 'locality' => $results['city'], - 'postalCode' => $results['postal_code'], - 'region' => $region, - 'regionCode' => $results['region'], - 'country' => $results['country_name'], - 'countryCode' => $results['country_code'], - 'timezone' => $timezone, - )))); + return $this->returnResults([ + array_merge($this->getDefaults(), [ + 'latitude' => $results['latitude'], + 'longitude' => $results['longitude'], + 'locality' => $results['city'], + 'postalCode' => $results['postal_code'], + 'region' => $region, + 'regionCode' => $results['region'], + 'country' => $results['country_name'], + 'countryCode' => $results['country_code'], + 'timezone' => $timezone, + ]) + ]); } /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - throw new UnsupportedOperation('The Geoip is not able to do reverse geocoding.'); + throw new UnsupportedOperation('The Geoip provider is not able to do reverse geocoding.'); } /** diff --git a/src/Geocoder/Provider/Geonames.php b/src/Geocoder/Provider/Geonames.php index 09b0731d5..46751f51c 100644 --- a/src/Geocoder/Provider/Geonames.php +++ b/src/Geocoder/Provider/Geonames.php @@ -36,32 +36,38 @@ class Geonames extends AbstractProvider implements LocaleAwareProvider private $username; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. + * @var string + */ + private $locale; + + /** + * @param HttpAdapterInterface $adapter An HTTP adapter * @param string $username Username login (Free registration at http://www.geonames.org/login) - * @param string $locale A locale (optional). + * @param string $locale A locale (optional) */ public function __construct(HttpAdapterInterface $adapter, $username, $locale = null) { - parent::__construct($adapter, $locale); + parent::__construct($adapter); $this->username = $username; + $this->locale = $locale; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (null === $this->username) { - throw new InvalidCredentials('No Username provided'); + throw new InvalidCredentials('No username provided.'); } // This API doesn't handle IPs if (filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The Geonames does not support IP addresses.'); + throw new UnsupportedOperation('The Geonames provider does not support IP addresses.'); } - $query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->username); + $query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->getLimit(), $this->username); return $this->executeQuery($query); } @@ -69,13 +75,13 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { if (null === $this->username) { - throw new InvalidCredentials('No Username provided'); + throw new InvalidCredentials('No username provided.'); } - $query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[0], $coordinates[1], $this->getMaxResults(), $this->username); + $query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[0], $coordinates[1], $this->getLimit(), $this->username); return $this->executeQuery($query); } @@ -89,10 +95,23 @@ public function getName() } /** - * @param string $query - * - * @return array + * {@inheritDoc} */ + public function getLocale() + { + return $this->locale; + } + + /** + * {@inheritDoc} + */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + private function executeQuery($query) { if (null !== $this->getLocale()) { @@ -103,27 +122,27 @@ private function executeQuery($query) $content = (string) $this->getAdapter()->get($query)->getBody(); if (empty($content)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } if (null === $json = json_decode($content)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } if (isset($json->totalResultsCount) && empty($json->totalResultsCount)) { - throw new NoResult(sprintf('No places found for query %s', $query)); + throw new NoResult(sprintf('No places found for query "%s".', $query)); } $data = $json->geonames; if (empty($data)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $results = []; - foreach ($data as $item) { $bounds = null; + if (isset($item->bbox)) { $bounds = array( 'south' => $item->bbox->south, @@ -133,7 +152,7 @@ private function executeQuery($query) ); } - $results[] = array_merge($this->getDefaults(), array( + $results[] = array_merge($this->getDefaults(), [ 'latitude' => isset($item->lat) ? $item->lat : null, 'longitude' => isset($item->lng) ? $item->lng : null, 'bounds' => $bounds, @@ -143,9 +162,9 @@ private function executeQuery($query) 'country' => isset($item->countryName) ? $item->countryName : null, 'countryCode' => isset($item->countryCode) ? $item->countryCode : null, 'timezone' => isset($item->timezone->timeZoneId) ? $item->timezone->timeZoneId : null, - )); + ]); } - return $results; + return $this->returnResults($results); } } diff --git a/src/Geocoder/Provider/GoogleMaps.php b/src/Geocoder/Provider/GoogleMaps.php index 406df361e..0f9f2647d 100644 --- a/src/Geocoder/Provider/GoogleMaps.php +++ b/src/Geocoder/Provider/GoogleMaps.php @@ -10,10 +10,10 @@ namespace Geocoder\Provider; +use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\QuotaExceeded; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\Exception\InvalidCredentials; use Ivory\HttpAdapter\HttpAdapterInterface; /** @@ -31,6 +31,11 @@ class GoogleMaps extends AbstractProvider implements LocaleAwareProvider */ const ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s'; + /** + * @var string + */ + private $locale; + /** * @var string */ @@ -47,16 +52,17 @@ class GoogleMaps extends AbstractProvider implements LocaleAwareProvider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $locale A locale (optional). - * @param string $region Region biasing (optional). + * @param HttpAdapterInterface $adapter An HTTP adapter + * @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) */ public function __construct(HttpAdapterInterface $adapter, $locale = null, $region = null, $useSsl = false, $apiKey = null) { - parent::__construct($adapter, $locale); + parent::__construct($adapter); + $this->locale = $locale; $this->region = $region; $this->useSsl = $useSsl; $this->apiKey = $apiKey; @@ -65,12 +71,12 @@ public function __construct(HttpAdapterInterface $adapter, $locale = null, $regi /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { // Google API returns invalid data if IP address given // This API doesn't handle IPs if (filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The GoogleMaps does not support IP addresses.'); + throw new UnsupportedOperation('The GoogleMaps provider does not support IP addresses, only street addresses.'); } $query = sprintf( @@ -84,9 +90,9 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - return $this->getGeocodedData(sprintf('%F,%F', $coordinates[0], $coordinates[1])); + return $this->geocode(sprintf('%F,%F', $latitude, $longitude)); } /** @@ -97,10 +103,35 @@ public function getName() return 'google_maps'; } + /** + * {@inheritDoc} + */ + public function getLocale() + { + return $this->locale; + } + + /** + * {@inheritDoc} + */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + + public function setRegion($region) + { + $this->region = $region; + + return $this; + } + /** * @param string $query * - * @return string Query with extra params + * @return string query with extra params */ protected function buildQuery($query) { @@ -119,14 +150,9 @@ protected function buildQuery($query) return $query; } - /** - * @param string $query - * - * @return array - */ private function executeQuery($query) { - $query = $this->buildQuery($query); + $query = $this->buildQuery($query); $content = (string) $this->getAdapter()->get($query)->getBody(); // Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider @@ -135,14 +161,14 @@ private function executeQuery($query) } if (empty($content)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $json = json_decode($content); // API error if (!isset($json)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } if ('REQUEST_DENIED' === $json->status && 'The provided API key is invalid.' === $json->error_message) { @@ -156,29 +182,28 @@ private function executeQuery($query) // no result if (!isset($json->results) || !count($json->results) || 'OK' !== $json->status) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } - $results = array(); - + $results = []; foreach ($json->results as $result) { - $resultset = $this->getDefaults(); + $resultSet = $this->getDefaults(); // update address components foreach ($result->address_components as $component) { foreach ($component->types as $type) { - $this->updateAddressComponent($resultset, $type, $component); + $this->updateAddressComponent($resultSet, $type, $component); } } // update coordinates $coordinates = $result->geometry->location; - $resultset['latitude'] = $coordinates->lat; - $resultset['longitude'] = $coordinates->lng; + $resultSet['latitude'] = $coordinates->lat; + $resultSet['longitude'] = $coordinates->lng; - $resultset['bounds'] = null; + $resultSet['bounds'] = null; if (isset($result->geometry->bounds)) { - $resultset['bounds'] = array( + $resultSet['bounds'] = array( 'south' => $result->geometry->bounds->southwest->lat, 'west' => $result->geometry->bounds->southwest->lng, 'north' => $result->geometry->bounds->northeast->lat, @@ -186,7 +211,7 @@ private function executeQuery($query) ); } elseif ('ROOFTOP' === $result->geometry->location_type) { // Fake bounds - $resultset['bounds'] = array( + $resultSet['bounds'] = array( 'south' => $coordinates->lat, 'west' => $coordinates->lng, 'north' => $coordinates->lat, @@ -194,62 +219,62 @@ private function executeQuery($query) ); } - $results[] = array_merge($this->getDefaults(), $resultset); + $results[] = array_merge($this->getDefaults(), $resultSet); } - return $results; + return $this->returnResults($results); } /** - * Update current resultset with given key/value. + * Update current resultSet with given key/value. * - * @param array $resultset Resultset to update. - * @param string $type Component type. - * @param object $values The component values; + * @param array $resultSet resultSet to update + * @param string $type Component type + * @param object $values The component values * * @return array */ - private function updateAddressComponent(&$resultset, $type, $values) + private function updateAddressComponent(&$resultSet, $type, $values) { switch ($type) { case 'postal_code': - $resultset['postalCode'] = $values->long_name; + $resultSet['postalCode'] = $values->long_name; break; case 'locality': - $resultset['locality'] = $values->long_name; + $resultSet['locality'] = $values->long_name; break; case 'administrative_area_level_2': - $resultset['county'] = $values->long_name; - $resultset['countyCode'] = $values->short_name; + $resultSet['county'] = $values->long_name; + $resultSet['countyCode'] = $values->short_name; break; case 'administrative_area_level_1': - $resultset['region'] = $values->long_name; - $resultset['regionCode'] = $values->short_name; + $resultSet['region'] = $values->long_name; + $resultSet['regionCode'] = $values->short_name; break; case 'country': - $resultset['country'] = $values->long_name; - $resultset['countryCode'] = $values->short_name; + $resultSet['country'] = $values->long_name; + $resultSet['countryCode'] = $values->short_name; break; case 'street_number': - $resultset['streetNumber'] = $values->long_name; + $resultSet['streetNumber'] = $values->long_name; break; case 'route': - $resultset['streetName'] = $values->long_name; + $resultSet['streetName'] = $values->long_name; break; case 'sublocality': - $resultset['subLocality'] = $values->long_name; + $resultSet['subLocality'] = $values->long_name; break; default: } - return $resultset; + return $resultSet; } } diff --git a/src/Geocoder/Provider/HostIp.php b/src/Geocoder/Provider/HostIp.php index 37c40522a..b6910df5f 100644 --- a/src/Geocoder/Provider/HostIp.php +++ b/src/Geocoder/Provider/HostIp.php @@ -26,15 +26,15 @@ class HostIp extends AbstractProvider implements Provider /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (!filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The HostIp does not support Street addresses.'); + throw new UnsupportedOperation('The HostIp provider does not support Street addresses.'); } // This API does not support IPv6 if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - throw new UnsupportedOperation('The HostIp does not support IPv6 addresses.'); + throw new UnsupportedOperation('The HostIp provider does not support IPv6 addresses.'); } if ('127.0.0.1' === $address) { @@ -49,9 +49,9 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - throw new UnsupportedOperation('The HostIp is not able to do reverse geocoding.'); + throw new UnsupportedOperation('The HostIp provider is not able to do reverse geocoding.'); } /** @@ -74,7 +74,7 @@ private function executeQuery($query) $data = json_decode($content, true); if (!$data) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } return array(array_merge($this->getDefaults(), array( diff --git a/src/Geocoder/Provider/IpInfoDb.php b/src/Geocoder/Provider/IpInfoDb.php index 4b0507c9a..616b0dd27 100644 --- a/src/Geocoder/Provider/IpInfoDb.php +++ b/src/Geocoder/Provider/IpInfoDb.php @@ -31,8 +31,8 @@ class IpInfoDb extends AbstractProvider implements Provider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. + * @param HttpAdapterInterface $adapter An HTTP adapter + * @param string $apiKey An API key */ public function __construct(HttpAdapterInterface $adapter, $apiKey) { @@ -44,19 +44,19 @@ public function __construct(HttpAdapterInterface $adapter, $apiKey) /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (null === $this->apiKey) { throw new InvalidCredentials('No API Key provided'); } if (!filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The IpInfoDb does not support Street addresses.'); + throw new UnsupportedOperation('The IpInfoDb provider does not support street addresses, only IPv4 addresses.'); } // This API does not support IPv6 if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - throw new UnsupportedOperation('The IpInfoDb does not support IPv6 addresses.'); + throw new UnsupportedOperation('The IpInfoDb provider does not support IPv6 addresses.'); } if ('127.0.0.1' === $address) { @@ -71,9 +71,9 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - throw new UnsupportedOperation('The IpInfoDb is not able to do reverse geocoding.'); + throw new UnsupportedOperation('The IpInfoDb provider is not able to do reverse geocoding.'); } /** @@ -84,23 +84,18 @@ public function getName() return 'ip_info_db'; } - /** - * @param string $query - * - * @return array - */ private function executeQuery($query) { $content = (string) $this->getAdapter()->get($query)->getBody(); if (empty($content)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $data = (array) json_decode($content); if (empty($data) || 'OK' !== $data['statusCode']) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $timezone = null; @@ -108,15 +103,17 @@ private function executeQuery($query) $timezone = timezone_name_from_abbr("", (int) substr($data['timeZone'], 0, strpos($data['timeZone'], ':')) * 3600, 0); } - return array(array_merge($this->getDefaults(), array( - 'latitude' => isset($data['latitude']) ? $data['latitude'] : null, - 'longitude' => isset($data['longitude']) ? $data['longitude'] : null, - 'locality' => isset($data['cityName']) ? $data['cityName'] : null, - 'postalCode' => isset($data['zipCode']) ? $data['zipCode'] : null, - 'region' => isset($data['regionName']) ? $data['regionName'] : null, - 'country' => isset($data['countryName']) ? $data['countryName'] : null, - 'countryCode' => isset($data['countryName']) ? $data['countryCode'] : null, - 'timezone' => $timezone, - ))); + return $this->returnResults([ + array_merge($this->getDefaults(), array( + 'latitude' => isset($data['latitude']) ? $data['latitude'] : null, + 'longitude' => isset($data['longitude']) ? $data['longitude'] : null, + 'locality' => isset($data['cityName']) ? $data['cityName'] : null, + 'postalCode' => isset($data['zipCode']) ? $data['zipCode'] : null, + 'region' => isset($data['regionName']) ? $data['regionName'] : null, + 'country' => isset($data['countryName']) ? $data['countryName'] : null, + 'countryCode' => isset($data['countryName']) ? $data['countryCode'] : null, + 'timezone' => $timezone, + )) + ]); } } diff --git a/src/Geocoder/Provider/LocaleAwareProvider.php b/src/Geocoder/Provider/LocaleAwareProvider.php index d645db50b..36b3951cd 100644 --- a/src/Geocoder/Provider/LocaleAwareProvider.php +++ b/src/Geocoder/Provider/LocaleAwareProvider.php @@ -24,9 +24,9 @@ public function getLocale(); /** * Sets the locale to be used. * - * @param string|null $locale If no locale is set, the provider or service will fallback. + * @param string|null * - * @return LocaleAwareProvider Self object + * @return LocaleAwareProvider */ - public function setLocale($locale = null); + public function setLocale($locale); } diff --git a/src/Geocoder/Provider/MapQuest.php b/src/Geocoder/Provider/MapQuest.php index 8c4f0b776..1f353f7c2 100644 --- a/src/Geocoder/Provider/MapQuest.php +++ b/src/Geocoder/Provider/MapQuest.php @@ -46,12 +46,12 @@ class MapQuest extends AbstractProvider implements Provider * * @var bool */ - private $licensed = false; + private $licensed; /** * @var string */ - private $apiKey = null; + private $apiKey; /** * @param HttpAdapterInterface $adapter An HTTP adapter. @@ -63,28 +63,28 @@ public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = nu { parent::__construct($adapter, $locale); - $this->apiKey = $apiKey; + $this->apiKey = $apiKey; $this->licensed = $licensed; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { - // This API doesn't handle IPs - if (filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The MapQuest does not support IP addresses.'); - } - if (null === $this->apiKey) { throw new InvalidCredentials('No API Key provided.'); } + // This API doesn't handle IPs + if (filter_var($address, FILTER_VALIDATE_IP)) { + throw new UnsupportedOperation('The MapQuest provider does not support IP addresses, only street addresses.'); + } + if ($this->licensed) { - $query = sprintf(self::LICENSED_GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey); + $query = sprintf(self::LICENSED_GEOCODE_ENDPOINT_URL, urlencode($address), $this->getLimit(), $this->apiKey); } else { - $query = sprintf(self::OPEN_GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey); + $query = sprintf(self::OPEN_GEOCODE_ENDPOINT_URL, urlencode($address), $this->getLimit(), $this->apiKey); } return $this->executeQuery($query); @@ -93,16 +93,16 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { if (null === $this->apiKey) { throw new InvalidCredentials('No API Key provided.'); } if ($this->licensed) { - $query = sprintf(self::LICENSED_REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]); + $query = sprintf(self::LICENSED_REVERSE_ENDPOINT_URL, $this->apiKey, $latitude, $longitude); } else { - $query = sprintf(self::OPEN_REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]); + $query = sprintf(self::OPEN_REVERSE_ENDPOINT_URL, $this->apiKey, $latitude, $longitude); } return $this->executeQuery($query); @@ -116,33 +116,27 @@ public function getName() return 'map_quest'; } - /** - * @param string $query - * - * @return array - */ - protected function executeQuery($query) + private function executeQuery($query) { $content = (string) $this->getAdapter()->get($query)->getBody(); if (empty($content)) { - throw new NoResult(sprintf('Could not execute query: %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $json = json_decode($content, true); if (!isset($json['results']) || empty($json['results'])) { - throw new NoResult(sprintf('Could not find results for given query: %s', $query)); + throw new NoResult(sprintf('Could not find results for query "%s".', $query)); } $locations = $json['results'][0]['locations']; if (empty($locations)) { - throw new NoResult(sprintf('Could not find results for given query: %s', $query)); + throw new NoResult(sprintf('Could not find results for query "%s".', $query)); } $results = []; - foreach ($locations as $location) { if ($location['street'] || $location['postalCode'] || $location['adminArea5'] || $location['adminArea4'] || $location['adminArea3'] || $location['adminArea1']) { $results[] = array_merge($this->getDefaults(), array( @@ -159,9 +153,9 @@ protected function executeQuery($query) } if (empty($results)) { - throw new NoResult(sprintf('Could not find results for given query: %s', $query)); + throw new NoResult(sprintf('Could not find results for query "%s".', $query)); } - return $results; + return $this->returnResults($results); } } diff --git a/src/Geocoder/Provider/MaxMind.php b/src/Geocoder/Provider/MaxMind.php index a91d7c9f9..35bcc0207 100644 --- a/src/Geocoder/Provider/MaxMind.php +++ b/src/Geocoder/Provider/MaxMind.php @@ -61,10 +61,9 @@ class MaxMind extends AbstractProvider implements Provider * @param string $service The specific Maxmind service to use (optional). * @param bool $useSsl Whether to use an SSL connection (optional). */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $service = self::CITY_EXTENDED_SERVICE, - $useSsl = false) + public function __construct(HttpAdapterInterface $adapter, $apiKey, $service = self::CITY_EXTENDED_SERVICE, $useSsl = false) { - parent::__construct($adapter, null); + parent::__construct($adapter); $this->apiKey = $apiKey; $this->service = $service; @@ -74,18 +73,18 @@ public function __construct(HttpAdapterInterface $adapter, $apiKey, $service = s /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (null === $this->apiKey) { throw new InvalidCredentials('No API Key provided.'); } if (!filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The MaxMind does not support street addresses.'); + throw new UnsupportedOperation('The MaxMind provider does not support street addresses, only IP addresses.'); } if (in_array($address, array('127.0.0.1', '::1'))) { - return array($this->getLocalhostDefaults()); + return $this->returnResults([ $this->getLocalhostDefaults() ]); } $query = sprintf( @@ -99,23 +98,26 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - throw new UnsupportedOperation('The MaxMind is not able to do reverse geocoding.'); + throw new UnsupportedOperation('The MaxMind provider is not able to do reverse geocoding.'); } /** - * @param string $query - * - * @return array + * {@inheritDoc} */ + public function getName() + { + return 'maxmind'; + } + private function executeQuery($query) { $content = (string) $this->getAdapter()->get($query)->getBody(); $fields = $this->fieldsForService($this->service); if (null === $content || '' === $content) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $data = str_getcsv($content); @@ -125,11 +127,11 @@ private function executeQuery($query) } if ('IP_NOT_FOUND' === end($data)) { - throw new NoResult('Could not retrieve informations for the ip address provided.'); + throw new NoResult('Could not retrieve information for the supplied IP address.'); } if (count($fields) !== count($data)) { - throw new NoResult('Invalid result returned by provider.'); + throw new NoResult('Invalid result returned by the API.'); } $data = array_combine($fields, $data); @@ -141,14 +143,11 @@ private function executeQuery($query) $data['country'] = $this->countryCodeToCountryName($data['countryCode']); } - return array(array_merge($this->getDefaults(), $data)); + return $this->returnResults([ + array_merge($this->getDefaults(), $data) + ]); } - /** - * @param string $code - * - * @return string - */ private function countryCodeToCountryName($code) { $countryNames = $this->getCountryNames(); @@ -156,14 +155,6 @@ private function countryCodeToCountryName($code) return $countryNames[$code]; } - /** - * {@inheritDoc} - */ - public function getName() - { - return 'maxmind'; - } - /** * We do not support Country and City services because they do not return much fields. * @see http://dev.maxmind.com/geoip/web-services diff --git a/src/Geocoder/Provider/MaxMindBinary.php b/src/Geocoder/Provider/MaxMindBinary.php index d414642a6..e850bb84d 100644 --- a/src/Geocoder/Provider/MaxMindBinary.php +++ b/src/Geocoder/Provider/MaxMindBinary.php @@ -65,7 +65,7 @@ public function __construct($datFile, $openFlag = null) /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (false === filter_var($address, FILTER_VALIDATE_IP)) { throw new UnsupportedOperation('The MaxMindBinary does not support street addresses.'); @@ -93,7 +93,7 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { throw new UnsupportedOperation('The MaxMindBinary is not able to do reverse geocoding.'); } diff --git a/src/Geocoder/Provider/Nominatim.php b/src/Geocoder/Provider/Nominatim.php index 944f5c888..09ba64f8c 100644 --- a/src/Geocoder/Provider/Nominatim.php +++ b/src/Geocoder/Provider/Nominatim.php @@ -21,6 +21,8 @@ class Nominatim extends AbstractProvider implements LocaleAwareProvider { private $rootUrl; + private $locale; + /** * @param HttpAdapterInterface $adapter An HTTP adapter. * @param string $rootUrl Root URL of the nominatim server @@ -31,43 +33,43 @@ public function __construct(HttpAdapterInterface $adapter, $rootUrl, $locale = n parent::__construct($adapter, $locale); $this->rootUrl = rtrim($rootUrl, '/'); + $this->locale = $locale; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { // This API does not support IPv6 if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - throw new UnsupportedOperation('The Nominatim does not support IPv6 addresses.'); + throw new UnsupportedOperation('The ' . end(explode('\\', get_called_class())) . ' provider does not support IPv6 addresses.'); } if ('127.0.0.1' === $address) { - return array($this->getLocalhostDefaults()); + return $this->returnResults([ $this->getLocalhostDefaults() ]); } - $query = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $this->getMaxResults()); + $query = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $this->getLimit()); $content = $this->executeQuery($query); if (empty($content)) { - throw new NoResult(sprintf('Could not resolve address "%s"', $address)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $doc = new \DOMDocument(); if (!@$doc->loadXML($content) || null === $doc->getElementsByTagName('searchresults')->item(0)) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $searchResult = $doc->getElementsByTagName('searchresults')->item(0); $places = $searchResult->getElementsByTagName('place'); if (null === $places || 0 === $places->length) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } - $results = array(); - + $results = []; foreach ($places as $place) { $boundsAttr = $place->getAttribute('boundingbox'); $bounds = []; @@ -90,43 +92,45 @@ public function getGeocodedData($address) )); } - return $results; + return $this->returnResults($results); } /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - $query = sprintf($this->getReverseEndpointUrl(), $coordinates[0], $coordinates[1]); + $query = sprintf($this->getReverseEndpointUrl(), $latitude, $longitude); $content = $this->executeQuery($query); if (empty($content)) { - throw new NoResult(sprintf('Unable to resolve the coordinates %s', implode(', ', $coordinates))); + throw new NoResult(sprintf('Unable to find results for coordinates [ %f, %f ].', $latitude, $longitude)); } $doc = new \DOMDocument(); if (!@$doc->loadXML($content) || $doc->getElementsByTagName('error')->length > 0) { - throw new NoResult(sprintf('Could not resolve coordinates %s', implode(', ', $coordinates))); + throw new NoResult(sprintf('Unable to find results for coordinates [ %f, %f ].', $latitude, $longitude)); } $searchResult = $doc->getElementsByTagName('reversegeocode')->item(0); $addressParts = $searchResult->getElementsByTagName('addressparts')->item(0); $result = $searchResult->getElementsByTagName('result')->item(0); - return array(array_merge($this->getDefaults(), array( - 'latitude' => $result->getAttribute('lat'), - 'longitude' => $result->getAttribute('lon'), - 'postalCode' => $this->getNodeValue($addressParts->getElementsByTagName('postcode')), - 'county' => $this->getNodeValue($addressParts->getElementsByTagName('county')), - 'region' => $this->getNodeValue($addressParts->getElementsByTagName('state')), - 'streetNumber' => $this->getNodeValue($addressParts->getElementsByTagName('house_number')), - 'streetName' => $this->getNodeValue($addressParts->getElementsByTagName('road')) ?: $this->getNodeValue($addressParts->getElementsByTagName('pedestrian')), - 'locality' => $this->getNodeValue($addressParts->getElementsByTagName('city')), - 'subLocality' => $this->getNodeValue($addressParts->getElementsByTagName('suburb')), - 'country' => $this->getNodeValue($addressParts->getElementsByTagName('country')), - 'countryCode' => strtoupper($this->getNodeValue($addressParts->getElementsByTagName('country_code'))), - ))); + return $this->returnResults([ + array_merge($this->getDefaults(), [ + 'latitude' => $result->getAttribute('lat'), + 'longitude' => $result->getAttribute('lon'), + 'postalCode' => $this->getNodeValue($addressParts->getElementsByTagName('postcode')), + 'county' => $this->getNodeValue($addressParts->getElementsByTagName('county')), + 'region' => $this->getNodeValue($addressParts->getElementsByTagName('state')), + 'streetNumber' => $this->getNodeValue($addressParts->getElementsByTagName('house_number')), + 'streetName' => $this->getNodeValue($addressParts->getElementsByTagName('road')) ?: $this->getNodeValue($addressParts->getElementsByTagName('pedestrian')), + 'locality' => $this->getNodeValue($addressParts->getElementsByTagName('city')), + 'subLocality' => $this->getNodeValue($addressParts->getElementsByTagName('suburb')), + 'country' => $this->getNodeValue($addressParts->getElementsByTagName('country')), + 'countryCode' => strtoupper($this->getNodeValue($addressParts->getElementsByTagName('country_code'))), + ]) + ]); } /** @@ -134,14 +138,27 @@ public function getReversedData(array $coordinates) */ public function getName() { - return 'openstreetmap'; + return 'nominatim'; + } + + /** + * {@inheritDoc} + */ + public function getLocale() + { + return $this->locale; } /** - * @param string $query - * - * @return string + * {@inheritDoc} */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + private function executeQuery($query) { if (null !== $this->getLocale()) { @@ -151,27 +168,16 @@ private function executeQuery($query) return (string) $this->getAdapter()->get($query)->getBody(); } - /** - * @return string - */ private function getGeocodeEndpointUrl() { - return $this->rootUrl.'/search?q=%s&format=xml&addressdetails=1&limit=%d'; + return $this->rootUrl . '/search?q=%s&format=xml&addressdetails=1&limit=%d'; } - /** - * @return string - */ private function getReverseEndpointUrl() { - return $this->rootUrl.'/reverse?format=xml&lat=%F&lon=%F&addressdetails=1&zoom=18'; + return $this->rootUrl . '/reverse?format=xml&lat=%F&lon=%F&addressdetails=1&zoom=18'; } - /** - * @param \DOMNodeList - * - * @return string - */ private function getNodeValue(\DOMNodeList $element) { return $element->length ? $element->item(0)->nodeValue : null; diff --git a/src/Geocoder/Provider/OpenCage.php b/src/Geocoder/Provider/OpenCage.php index 812d58db2..8e94dfdda 100644 --- a/src/Geocoder/Provider/OpenCage.php +++ b/src/Geocoder/Provider/OpenCage.php @@ -18,7 +18,7 @@ /** * @author mtm */ -class OpenCage extends AbstractProvider implements Provider +class OpenCage extends AbstractProvider implements LocaleAwareProvider { /** * @var string @@ -28,12 +28,12 @@ class OpenCage extends AbstractProvider implements Provider /** * @var string */ - private $scheme = 'http'; + private $scheme; /** * @var string */ - private $apiKey = null; + private $apiKey; /** * @param HttpAdapterInterface $adapter An HTTP adapter. @@ -46,24 +46,24 @@ public function __construct(HttpAdapterInterface $adapter, $apiKey, $useSsl = fa parent::__construct($adapter, $locale); $this->apiKey = $apiKey; - $this->scheme = $useSsl ? 'https' : $this->scheme; + $this->scheme = $useSsl ? 'https' : 'http'; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { - // This API doesn't handle IPs - if (filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The OpenCage does not support IP addresses.'); - } - if (null === $this->apiKey) { throw new InvalidCredentials('No API Key provided.'); } - $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->scheme, $this->apiKey, urlencode($address), $this->getMaxResults() ); + // This API doesn't handle IPs + if (filter_var($address, FILTER_VALIDATE_IP)) { + throw new UnsupportedOperation('The OpenCage provider does not support IP addresses, only street addresses.'); + } + + $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->scheme, $this->apiKey, urlencode($address), $this->getLimit() ); return $this->executeQuery($query); } @@ -71,27 +71,39 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - // latitude, longitude - $address = sprintf("%f, %f", $coordinates[0], $coordinates[1]); + $address = sprintf("%f, %f", $latitude, $longitude); - return $this->getGeocodedData($address); + return $this->geocode($address); } /** * {@inheritDoc} */ - public function getName() + public function getLocale() { - return 'opencage'; + return $this->locale; + } + + /** + * {@inheritDoc} + */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; } /** - * @param string $query - * - * @return array + * {@inheritDoc} */ + public function getName() + { + return 'opencage'; + } + private function executeQuery($query) { if (null !== $this->getLocale()) { @@ -101,39 +113,34 @@ private function executeQuery($query) $content = (string) $this->getAdapter()->get($query)->getBody(); if (empty($content)) { - throw new NoResult(sprintf('Could not execute query: %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $json = json_decode($content, true); if (!isset($json['total_results']) || $json['total_results'] == 0 ) { - throw new NoResult(sprintf('Could not find results for given query: %s', $query)); + throw new NoResult(sprintf('Could not find results for query "%s".', $query)); } $locations = $json['results']; if (empty($locations)) { - throw new NoResult(sprintf('Could not find results for given query: %s', $query)); + throw new NoResult(sprintf('Could not find results for query "%s".', $query)); } - $results = array(); - - - + $results = []; foreach ($locations as $location) { - - $bounds = null; + $bounds = []; if (isset($location['bounds'])) { - $bounds = array( + $bounds = [ 'south' => $location['bounds']['southwest']['lat'], 'west' => $location['bounds']['southwest']['lng'], 'north' => $location['bounds']['northeast']['lat'], 'east' => $location['bounds']['northeast']['lng'], - ); + ]; } - $comp = $location['components']; - + $comp = $location['components']; $results[] = array_merge($this->getDefaults(), array( 'latitude' => $location['geometry']['lat'], 'longitude' => $location['geometry']['lng'], @@ -151,6 +158,6 @@ private function executeQuery($query) )); } - return $results; + return $this->returnResults($results); } } diff --git a/src/Geocoder/Provider/OpenStreetMap.php b/src/Geocoder/Provider/OpenStreetMap.php index 152647905..8097d3474 100644 --- a/src/Geocoder/Provider/OpenStreetMap.php +++ b/src/Geocoder/Provider/OpenStreetMap.php @@ -30,4 +30,12 @@ public function __construct(HttpAdapterInterface $adapter, $locale = null) { parent::__construct($adapter, static::ROOT_URL, $locale); } + + /** + * {@inheritDoc} + */ + public function getName() + { + return 'openstreetmap'; + } } diff --git a/src/Geocoder/Provider/Provider.php b/src/Geocoder/Provider/Provider.php index 34f47267e..5026c76ad 100644 --- a/src/Geocoder/Provider/Provider.php +++ b/src/Geocoder/Provider/Provider.php @@ -10,40 +10,17 @@ namespace Geocoder\Provider; -use Geocoder\Exception\NoResult; -use Geocoder\Exception\InvalidCredentials; -use Geocoder\Exception\UnsupportedOperation; +use Geocoder\Geocoder; /** * @author William Durand */ -interface Provider +interface Provider extends Geocoder { /** - * Returns an associative array with data treated by the provider. - * - * @param string $address An address (IP or street). - * - * @throws NoResult If the address could not be resolved - * @throws InvalidCredentials If the credentials are invalid - * @throws UnsupportedOperation If IPv4, IPv6 or street is not supported - * - * @return array - */ - public function getGeocodedData($address); - - /** - * Returns an associative array with data treated by the provider. - * - * @param array $coordinates Coordinates (latitude, longitude). - * - * @throws NoResult If the coordinates could not be resolved - * @throws InvalidCredentials If the credentials are invalid - * @throws UnsupportedOperation If reverse geocoding is not supported - * - * @return array + * @var integer */ - public function getReversedData(array $coordinates); + const MAX_RESULTS = 5; /** * Returns the provider's name. @@ -51,13 +28,4 @@ public function getReversedData(array $coordinates); * @return string */ public function getName(); - - /** - * Sets the maximum number of returned results. - * - * @param integer $maxResults - * - * @return Provider - */ - public function setMaxResults($maxResults); } diff --git a/src/Geocoder/Provider/TomTom.php b/src/Geocoder/Provider/TomTom.php index 0da7505aa..b9dda70a4 100644 --- a/src/Geocoder/Provider/TomTom.php +++ b/src/Geocoder/Provider/TomTom.php @@ -33,7 +33,12 @@ class TomTom extends AbstractProvider implements LocaleAwareProvider /** * @var string */ - private $apiKey = null; + private $apiKey; + + /** + * @var string + */ + private $locale; /** * @param HttpAdapterInterface $adapter An HTTP adapter. @@ -42,26 +47,27 @@ class TomTom extends AbstractProvider implements LocaleAwareProvider */ public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null) { - parent::__construct($adapter, $locale); + parent::__construct($adapter); $this->apiKey = $apiKey; + $this->locale = $locale; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { if (null === $this->apiKey) { - throw new InvalidCredentials('No Geocoding API Key provided'); + throw new InvalidCredentials('No API Key provided.'); } // This API doesn't handle IPs if (filter_var($address, FILTER_VALIDATE_IP)) { - throw new UnsupportedOperation('The TomTom does not support IP addresses.'); + throw new UnsupportedOperation('The TomTom provider does not support IP addresses, only street addresses.'); } - $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->apiKey, rawurlencode($address), $this->getMaxResults()); + $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->apiKey, rawurlencode($address), $this->getLimit()); return $this->executeQuery($query); } @@ -69,10 +75,10 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { if (null === $this->apiKey) { - throw new InvalidCredentials('No Map API Key provided'); + throw new InvalidCredentials('No Map API Key provided.'); } $query = sprintf(self::REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]); @@ -89,10 +95,23 @@ public function getName() } /** - * @param string $query - * - * @return array + * {@inheritDoc} + */ + public function getLocale() + { + return $this->locale; + } + + /** + * {@inheritDoc} */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + private function executeQuery($query) { if (null !== $this->getLocale()) { @@ -106,13 +125,13 @@ private function executeQuery($query) try { $xml = new \SimpleXmlElement($content); } catch (\Exception $e) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $attributes = $xml->attributes(); if (isset($attributes['count']) && 0 === (int) $attributes['count']) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } if (isset($attributes['errorCode'])) { @@ -120,30 +139,24 @@ private function executeQuery($query) throw new InvalidCredentials('Map API Key provided is not valid.'); } - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $data = isset($xml->geoResult) ? $xml->geoResult : $xml->reverseGeoResult; if (0 === count($data)) { - return array($this->getResultArray($data)); + return $this->returnResults([ $this->getResultArray($data) ]); } - $results = array(); - + $results = []; foreach ($data as $item) { $results[] = $this->getResultArray($item); } - return $results; + return $this->returnResults($results); } - /** - * @param \SimpleXmlElement $data - * - * @return array - */ private function getResultArray(\SimpleXmlElement $data) { return array_merge($this->getDefaults(), array( diff --git a/src/Geocoder/Provider/Yandex.php b/src/Geocoder/Provider/Yandex.php index c03ec89fb..cd37764a0 100644 --- a/src/Geocoder/Provider/Yandex.php +++ b/src/Geocoder/Provider/Yandex.php @@ -32,7 +32,12 @@ class Yandex extends AbstractProvider implements LocaleAwareProvider /** * @var string */ - private $toponym = null; + private $toponym; + + /** + * @var string + */ + private $locale; /** * @param HttpAdapterInterface $adapter An HTTP adapter. @@ -41,15 +46,16 @@ class Yandex extends AbstractProvider implements LocaleAwareProvider */ public function __construct(HttpAdapterInterface $adapter, $locale = null, $toponym = null) { - parent::__construct($adapter, $locale); + parent::__construct($adapter); + $this->locale = $locale; $this->toponym = $toponym; } /** * {@inheritDoc} */ - public function getGeocodedData($address) + public function geocode($address) { // This API doesn't handle IPs if (filter_var($address, FILTER_VALIDATE_IP)) { @@ -64,9 +70,9 @@ public function getGeocodedData($address) /** * {@inheritDoc} */ - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - $query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[1], $coordinates[0]); + $query = sprintf(self::REVERSE_ENDPOINT_URL, $longitude, $latitude); if (null !== $this->toponym) { $query = sprintf('%s&kind=%s', $query, $this->toponym); @@ -84,29 +90,41 @@ public function getName() } /** - * @param string $query - * - * @return array + * {@inheritDoc} + */ + public function getLocale() + { + return $this->locale; + } + + /** + * {@inheritDoc} */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } + private function executeQuery($query) { if (null !== $this->getLocale()) { $query = sprintf('%s&lang=%s', $query, str_replace('_', '-', $this->getLocale())); } - $query = sprintf('%s&results=%d', $query, $this->getMaxResults()); + $query = sprintf('%s&results=%d', $query, $this->getLimit()); $content = (string) $this->getAdapter()->get($query)->getBody(); $json = (array) json_decode($content, true); if (empty($json) || '0' === $json['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['found']) { - throw new NoResult(sprintf('Could not execute query %s', $query)); + throw new NoResult(sprintf('Could not execute query "%s".', $query)); } $data = $json['response']['GeoObjectCollection']['featureMember']; - $results = array(); - + $results = []; foreach ($data as $item) { $bounds = null; $details = array('pos' => ' '); @@ -118,21 +136,21 @@ function ($value, $key) use (&$details) {$details[$key] = $value;} if (! empty($details['lowerCorner'])) { $coordinates = explode(' ', $details['lowerCorner']); - $bounds['south'] = $coordinates[1]; - $bounds['west'] = $coordinates[0]; + $bounds['south'] = $longitude; + $bounds['west'] = $latitude; } if (! empty($details['upperCorner'])) { $coordinates = explode(' ', $details['upperCorner']); - $bounds['north'] = $coordinates[1]; - $bounds['east'] = $coordinates[0]; + $bounds['north'] = $longitude; + $bounds['east'] = $latitude; } $coordinates = explode(' ', $details['pos']); $results[] = array_merge($this->getDefaults(), array( - 'latitude' => $coordinates[1], - 'longitude' => $coordinates[0], + 'latitude' => $longitude, + 'longitude' => $latitude, 'bounds' => $bounds, 'streetNumber' => isset($details['PremiseNumber']) ? $details['PremiseNumber'] : null, 'streetName' => isset($details['ThoroughfareName']) ? $details['ThoroughfareName'] : null, @@ -144,6 +162,6 @@ function ($value, $key) use (&$details) {$details[$key] = $value;} )); } - return $results; + return $this->returnResults($results); } } diff --git a/src/Geocoder/ProviderBasedGeocoder.php b/src/Geocoder/ProviderAggregator.php similarity index 54% rename from src/Geocoder/ProviderBasedGeocoder.php rename to src/Geocoder/ProviderAggregator.php index cde3fccf9..3fdf8ab86 100644 --- a/src/Geocoder/ProviderBasedGeocoder.php +++ b/src/Geocoder/ProviderAggregator.php @@ -11,20 +11,13 @@ namespace Geocoder; use Geocoder\Exception\ProviderNotRegistered; -use Geocoder\Model\AddressFactory; -use Geocoder\Provider\LocaleAwareProvider; use Geocoder\Provider\Provider; /** * @author William Durand */ -class ProviderBasedGeocoder implements Geocoder +class ProviderAggregator implements Geocoder { - /** - * @var integer - */ - const MAX_RESULTS = 5; - /** * @var Provider[] */ @@ -35,26 +28,17 @@ class ProviderBasedGeocoder implements Geocoder */ private $provider; - /** - * @var AddressFactory - */ - private $factory; - /** * @var integer */ - private $maxResults; + private $limit; /** - * @param Provider $provider - * @param integer $maxResults + * @param integer $limit */ - public function __construct(Provider $provider = null, $maxResults = self::MAX_RESULTS) + public function __construct($limit = Provider::MAX_RESULTS) { - $this->provider = $provider; - $this->factory = new AddressFactory(); - - $this->limit($maxResults); + $this->limit($limit); } /** @@ -62,15 +46,16 @@ public function __construct(Provider $provider = null, $maxResults = self::MAX_R */ public function geocode($value) { + $value = trim($value); + if (empty($value)) { // let's save a request return []; } - $provider = $this->getProvider()->setMaxResults($this->getMaxResults()); - $data = $provider->getGeocodedData(trim($value)); - - return $this->returnResult($data); + return $this->getProvider() + ->limit($this->getLimit()) + ->geocode($value); } /** @@ -83,14 +68,31 @@ public function reverse($latitude, $longitude) return []; } - $provider = $this->getProvider()->setMaxResults($this->getMaxResults()); - $data = $provider->getReversedData([ $latitude, $longitude ]); + return $this->getProvider() + ->limit($this->getLimit()) + ->reverse($latitude, $longitude); + } + + /** + * {@inheritDoc} + */ + public function limit($limit) + { + $this->limit = $limit; - return $this->returnResult($data); + return $this; } /** - * Registers a provider. + * {@inheritDoc} + */ + public function getLimit() + { + return $this->limit; + } + + /** + * Registers a new provider to the aggregator. * * @param Provider $provider * @@ -106,7 +108,7 @@ public function registerProvider(Provider $provider) } /** - * Convenient method to egister a set of providers. + * Registers a set of providers. * * @param Provider[] $providers * @@ -122,9 +124,9 @@ public function registerProviders(array $providers = []) } /** - * Set the provider to use. + * Sets the default provider to use. * - * @param string $name A provider's name + * @param string $name * * @return ProviderBasedGeocoder */ @@ -140,7 +142,7 @@ public function using($name) } /** - * Return registered providers indexed by name. + * Returns all registered providers indexed by their name. * * @return Provider[] */ @@ -150,41 +152,7 @@ public function getProviders() } /** - * @param integer $maxResults - * - * @return ProviderBasedGeocoder - */ - public function limit($maxResults) - { - $this->maxResults = $maxResults; - - return $this; - } - - /** - * @return integer $maxResults - */ - public function getMaxResults() - { - return $this->maxResults; - } - - /** - * Change the locale to be used in locale aware requests. - * - * @param string - */ - public function setLocale($locale) - { - foreach ($this->providers as $provider) { - if ($provider instanceof LocaleAwareProvider) { - $provider->setLocale($locale); - } - } - } - - /** - * Return the provider to use. + * Returns the current provider in use. * * @return Provider */ @@ -200,14 +168,4 @@ protected function getProvider() return $this->provider; } - - /** - * @param array $data An array of data. - * - * @return \Geocoder\Model\Address[] - */ - protected function returnResult(array $data = []) - { - return $this->factory->createFromArray($data); - } } diff --git a/tests/Geocoder/Tests/Provider/AbstractProviderTest.php b/tests/Geocoder/Tests/Provider/AbstractProviderTest.php index 44b4307e9..18909996d 100644 --- a/tests/Geocoder/Tests/Provider/AbstractProviderTest.php +++ b/tests/Geocoder/Tests/Provider/AbstractProviderTest.php @@ -13,30 +13,6 @@ */ class AbstractProviderTest extends TestCase { - public function testSetGetAdapter() - { - $adapter = new MockHttpAdapter(); - $adapter2 = new MockHttpAdapter(); - $provider = new MockProvider($adapter); - - $this->assertSame($adapter, $provider->getAdapter()); - $this->assertNull($provider->getLocale()); - - $provider->setAdapter($adapter2); - $this->assertSame($adapter2, $provider->getAdapter()); - } - - public function testSetGetLocale() - { - $adapter = new MockHttpAdapter(); - $provider = new MockProvider($adapter, 'fr_FR'); - - $this->assertEquals('fr_FR', $provider->getLocale()); - - $provider->setLocale('de_DE'); - $this->assertEquals('de_DE', $provider->getLocale()); - } - public function testGetLocalhostDefaults() { $adapter = new MockHttpAdapter(); @@ -53,16 +29,6 @@ public function testGetLocalhostDefaults() class MockProvider extends AbstractProvider { - public function getAdapter() - { - return parent::getAdapter(); - } - - public function getLocale() - { - return parent::getLocale(); - } - public function getLocalhostDefaults() { return parent::getLocalhostDefaults(); @@ -78,6 +44,5 @@ public function getName() protected function doSend(InternalRequestInterface $internalRequest) { - } } diff --git a/tests/Geocoder/Tests/Provider/ArcGISOnlineTest.php b/tests/Geocoder/Tests/Provider/ArcGISOnlineTest.php index 1ff15b794..f7596185d 100644 --- a/tests/Geocoder/Tests/Provider/ArcGISOnlineTest.php +++ b/tests/Geocoder/Tests/Provider/ArcGISOnlineTest.php @@ -19,7 +19,7 @@ public function testGetName() public function testGetGeocodedDataWithInvalidData() { $provider = new ArcGISOnline($this->getMockAdapter()); - $provider->getGeocodedData('loremipsum'); + $provider->geocode('loremipsum'); } /** @@ -29,7 +29,7 @@ public function testGetGeocodedDataWithInvalidData() public function testGetGeocodedDataWithNull() { $provider = new ArcGISOnline($this->getMockAdapter($this->never())); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** @@ -39,7 +39,7 @@ public function testGetGeocodedDataWithNull() public function testGetGeocodedDataWithEmpty() { $provider = new ArcGISOnline($this->getMockAdapter($this->never())); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** @@ -49,7 +49,7 @@ public function testGetGeocodedDataWithEmpty() public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new ArcGISOnline($this->getMockAdapter($this->never())); - $provider->getGeocodedData('127.0.0.1'); + $provider->geocode('127.0.0.1'); } /** @@ -59,7 +59,7 @@ public function testGetGeocodedDataWithLocalhostIPv4() public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new ArcGISOnline($this->getMockAdapter($this->never())); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** @@ -69,13 +69,13 @@ public function testGetGeocodedDataWithLocalhostIPv6() public function testGetGeocodedDataWithAddressGetsNullContent() { $provider = new ArcGISOnline($this->getMockAdapterReturns(null)); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithRealAddress() { $provider = new ArcGISOnline($this->getAdapter()); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); $this->assertInternalType('array', $results); $this->assertCount(5, $results); @@ -103,7 +103,7 @@ public function testGetGeocodedDataWithRealAddress() public function testGetGeocodedDataWithRealAddressAndHttps() { $provider = new ArcGISOnline($this->getAdapter(), null, true); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); $this->assertInternalType('array', $results); $this->assertCount(5, $results); @@ -135,7 +135,7 @@ public function testGetGeocodedDataWithRealAddressAndHttps() public function testGetGeocodedDataWithInvalidAddressForSourceCountry() { $provider = new ArcGISOnline($this->getAdapter(), 'USA'); - $result = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $result = $provider->geocode('10 avenue Gambetta, Paris, France'); } /** @@ -145,7 +145,7 @@ public function testGetGeocodedDataWithInvalidAddressForSourceCountry() public function testGetGeocodedDataWithInvalidAddressWithHttpsForSourceCountry() { $provider = new ArcGISOnline($this->getAdapter(), 'USA', true); - $result = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $result = $provider->geocode('10 avenue Gambetta, Paris, France'); } /** @@ -155,7 +155,7 @@ public function testGetGeocodedDataWithInvalidAddressWithHttpsForSourceCountry() public function testGetReversedDataWithInvalid() { $provider = new ArcGISOnline($this->getMockAdapter()); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } /** @@ -165,13 +165,13 @@ public function testGetReversedDataWithInvalid() public function testGetReversedDataWithCoordinatesContentReturnNull() { $provider = new ArcGISOnline($this->getMockAdapterReturns(null)); - $provider->getReversedData(array(48.863279997000461, 2.3890199980004354)); + $provider->reverse(48.863279997000461, 2.3890199980004354); } public function testGetReversedDataWithRealCoordinates() { $provider = new ArcGISOnline($this->getAdapter()); - $result = $provider->getReversedData(array(48.863279997000461, 2.3890199980004354)); + $result = $provider->reverse(48.863279997000461, 2.3890199980004354); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -198,7 +198,7 @@ public function testGetReversedDataWithRealCoordinates() public function testGetReversedDataWithRealCoordinatesWithHttps() { $provider = new ArcGISOnline($this->getAdapter(), null, true); - $result = $provider->getReversedData(array(48.863279997000461, 2.3890199980004354)); + $result = $provider->reverse(48.863279997000461, 2.3890199980004354); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -225,7 +225,7 @@ public function testGetReversedDataWithRealCoordinatesWithHttps() public function testGetGeocodedDataWithCity() { $provider = new ArcGISOnline($this->getAdapter()); - $results = $provider->getGeocodedData('Hannover'); + $results = $provider->geocode('Hannover'); $this->assertInternalType('array', $results); $this->assertCount(5, $results); @@ -288,7 +288,7 @@ public function testGetGeocodedDataWithCity() public function testGetGeocodedDataWithRealIPv4() { $provider = new ArcGISOnline($this->getMockAdapter($this->never())); - $provider->getGeocodedData('88.188.221.14'); + $provider->geocode('88.188.221.14'); } /** @@ -298,6 +298,6 @@ public function testGetGeocodedDataWithRealIPv4() public function testGetGeocodedDataWithRealIPv6() { $provider = new ArcGISOnline($this->getMockAdapter($this->never())); - $provider->getGeocodedData('::ffff:88.188.221.14'); + $provider->geocode('::ffff:88.188.221.14'); } } diff --git a/tests/Geocoder/Tests/Provider/BingMapsTest.php b/tests/Geocoder/Tests/Provider/BingMapsTest.php index f8faa704a..da442eded 100644 --- a/tests/Geocoder/Tests/Provider/BingMapsTest.php +++ b/tests/Geocoder/Tests/Provider/BingMapsTest.php @@ -16,80 +16,80 @@ public function testGetName() /** * @expectedException \RuntimeException */ - public function testGetGeocodedDataWithNullApiKey() + public function testGeocodeWithNullApiKey() { $provider = new BingMaps($this->getMockAdapter($this->never()), null); - $provider->getGeocodedData('foo'); + $provider->geocode('foo'); } /** * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://dev.virtualearth.net/REST/v1/Locations/?maxResults=5&q=foobar&key=api_key */ - public function testGetGeocodedDataWithInvalidData() + public function testGeocodeWithInvalidData() { $provider = new BingMaps($this->getMockAdapter(), 'api_key'); - $provider->getGeocodedData('foobar'); + $provider->geocode('foobar'); } /** * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://dev.virtualearth.net/REST/v1/Locations/?maxResults=5&q=&key=api_key */ - public function testGetGeocodedDataWithNull() + public function testGeocodeWithNull() { $provider = new BingMaps($this->getMockAdapter(), 'api_key'); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://dev.virtualearth.net/REST/v1/Locations/?maxResults=5&q=&key=api_key */ - public function testGetGeocodedDataWithEmpty() + public function testGeocodeWithEmpty() { $provider = new BingMaps($this->getMockAdapter(), 'api_key'); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The BingMaps does not support IP addresses. */ - public function testGetGeocodedDataWithLocalhostIPv4() + public function testGeocodeWithLocalhostIPv4() { $provider = new BingMaps($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('127.0.0.1'); + $provider->geocode('127.0.0.1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The BingMaps does not support IP addresses. */ - public function testGetGeocodedDataWithLocalhostIPv6() + public function testGeocodeWithLocalhostIPv6() { $provider = new BingMaps($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://dev.virtualearth.net/REST/v1/Locations/?maxResults=5&q=10+avenue+Gambetta%2C+Paris%2C+France&key=api_key */ - public function testGetGeocodedDataWithAddressGetsNullContent() + public function testGeocodeWithAddressGetsNullContent() { $provider = new BingMaps($this->getMockAdapterReturns(null), 'api_key'); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } - public function testGetGeocodedDataReturnsMultipleResults() + public function testGeocodeReturnsMultipleResults() { $json = <<getMockAdapterReturns($json), 'api_key', 'fr_FR'); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); $this->assertInternalType('array', $results); $this->assertCount(3, $results); @@ -155,14 +155,14 @@ public function testGetGeocodedDataReturnsMultipleResults() $this->assertEquals('France', $results[2]['country']); } - public function testGetReversedDataReturnsSingleResult() + public function testReverseReturnsSingleResult() { $json = <<getMockAdapterReturns($json), 'api_key'); - $results = $provider->getReversedData(array(48.86321648955345, 2.3887719959020615)); + $results = $provider->reverse(48.86321648955345, 2.3887719959020615); $this->assertInternalType('array', $results); $this->assertCount(1, $results); @@ -192,14 +192,14 @@ public function testGetReversedDataReturnsSingleResult() $this->assertNull($result['timezone']); } - public function testGetGeocodedDataWithRealAddressReturnsMultipleResults() + public function testGeocodeWithRealAddressReturnsMultipleResults() { if (!isset($_SERVER['BINGMAPS_API_KEY'])) { $this->markTestSkipped('You need to configure the BINGMAPS_API_KEY value in phpunit.xml'); } $provider = new BingMaps($this->getAdapter(), $_SERVER['BINGMAPS_API_KEY'], 'fr-FR'); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); $this->assertInternalType('array', $results); $this->assertCount(3, $results); @@ -269,30 +269,30 @@ public function testGetGeocodedDataWithRealAddressReturnsMultipleResults() * @expectedException Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://dev.virtualearth.net/REST/v1/Locations/1.000000,2.000000?key=api_key */ - public function testGetReversedData() + public function testReverse() { $provider = new BingMaps($this->getMockAdapter(), 'api_key'); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } /** * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://dev.virtualearth.net/REST/v1/Locations/48.863216,2.388772?key=api_key */ - public function testGetReversedDataWithCoordinatesContentReturnNull() + public function testReverseWithCoordinatesContentReturnNull() { $provider = new BingMaps($this->getMockAdapterReturns(null), 'api_key'); - $provider->getReversedData(array(48.86321648955345, 2.3887719959020615)); + $provider->reverse(48.86321648955345, 2.3887719959020615); } - public function testGetReversedDataWithRealCoordinatesReturnsSingleResult() + public function testReverseWithRealCoordinatesReturnsSingleResult() { if (!isset($_SERVER['BINGMAPS_API_KEY'])) { $this->markTestSkipped('You need to configure the BINGMAPS_API_KEY value in phpunit.xml'); } $provider = new BingMaps($this->getAdapter(), $_SERVER['BINGMAPS_API_KEY']); - $results = $provider->getReversedData(array(48.86321648955345, 2.3887719959020615)); + $results = $provider->reverse(48.86321648955345, 2.3887719959020615); $this->assertInternalType('array', $results); $this->assertCount(1, $results); @@ -326,27 +326,27 @@ public function testGetReversedDataWithRealCoordinatesReturnsSingleResult() * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The BingMaps does not support IP addresses. */ - public function testGetGeocodedDataWithRealIPv4() + public function testGeocodeWithRealIPv4() { if (!isset($_SERVER['BINGMAPS_API_KEY'])) { $this->markTestSkipped('You need to configure the BINGMAPS_API_KEY value in phpunit.xml'); } $provider = new BingMaps($this->getAdapter(), $_SERVER['BINGMAPS_API_KEY']); - $provider->getGeocodedData('88.188.221.14'); + $provider->geocode('88.188.221.14'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The BingMaps does not support IP addresses. */ - public function testGetGeocodedDataWithRealIPv6() + public function testGeocodeWithRealIPv6() { if (!isset($_SERVER['BINGMAPS_API_KEY'])) { $this->markTestSkipped('You need to configure the BINGMAPS_API_KEY value in phpunit.xml'); } $provider = new BingMaps($this->getAdapter(), $_SERVER['BINGMAPS_API_KEY']); - $provider->getGeocodedData('::ffff:88.188.221.14'); + $provider->geocode('::ffff:88.188.221.14'); } } diff --git a/tests/Geocoder/Tests/Provider/ChainTest.php b/tests/Geocoder/Tests/Provider/ChainTest.php index a80fadc2e..3e1c753c7 100644 --- a/tests/Geocoder/Tests/Provider/ChainTest.php +++ b/tests/Geocoder/Tests/Provider/ChainTest.php @@ -25,69 +25,69 @@ public function testGetName() $this->assertEquals('chain', $chain->getName()); } - public function testGetReversedData() + public function testReverse() { $mockOne = $this->getMock('Geocoder\\Provider\\Provider'); $mockOne->expects($this->once()) - ->method('getReversedData') + ->method('reverse') ->will($this->returnCallback(function () { throw new \Exception; })); $mockTwo = $this->getMock('Geocoder\\Provider\\Provider'); $mockTwo->expects($this->once()) - ->method('getReversedData') - ->with(array('11', '22')) + ->method('reverse') + ->with('11', '22') ->will($this->returnValue(array('foo' => 'bar'))); $chain = new Chain(array($mockOne, $mockTwo)); - $this->assertEquals(array('foo' => 'bar'), $chain->getReversedData(array('11', '22'))); + $this->assertEquals(array('foo' => 'bar'), $chain->reverse('11', '22')); } - public function testChainProviderReverseThrowsChainNoResult() + public function testReverseThrowsChainNoResult() { $mockOne = $this->getMock('Geocoder\\Provider\\Provider'); $mockOne->expects($this->exactly(2)) - ->method('getReversedData') + ->method('reverse') ->will($this->returnCallback(function () { throw new \Exception; })); $chain = new Chain(array($mockOne, $mockOne)); try { - $chain->getReversedData(array('11', '22')); + $chain->reverse('11', '22'); } catch (ChainNoResult $e) { $this->assertCount(2, $e->getExceptions()); } } - public function testGetGeocodedData() + public function testGeocode() { $mockOne = $this->getMock('Geocoder\\Provider\\Provider'); $mockOne->expects($this->once()) - ->method('getGeocodedData') + ->method('geocode') ->will($this->returnCallback(function () { throw new \Exception; })); $mockTwo = $this->getMock('Geocoder\\Provider\\Provider'); $mockTwo->expects($this->once()) - ->method('getGeocodedData') + ->method('geocode') ->with('Paris') ->will($this->returnValue(array('foo' => 'bar'))); $chain = new Chain(array($mockOne, $mockTwo)); - $this->assertEquals(array('foo' => 'bar'), $chain->getGeocodedData('Paris')); + $this->assertEquals(array('foo' => 'bar'), $chain->geocode('Paris')); } - public function testChainProviderGeocodeThrowsChainNoResult() + public function testGeocodeThrowsChainNoResult() { $mockOne = $this->getMock('Geocoder\\Provider\\Provider'); $mockOne->expects($this->exactly(2)) - ->method('getGeocodedData') + ->method('geocode') ->will($this->returnCallback(function () { throw new \Exception; })); $chain = new Chain(array($mockOne, $mockOne)); try { - $chain->getGeocodedData('Paris'); + $chain->geocode('Paris'); } catch (ChainNoResult $e) { $this->assertCount(2, $e->getExceptions()); } diff --git a/tests/Geocoder/Tests/Provider/FreeGeoIpTest.php b/tests/Geocoder/Tests/Provider/FreeGeoIpTest.php index 7032eda0f..33f3ba1fe 100644 --- a/tests/Geocoder/Tests/Provider/FreeGeoIpTest.php +++ b/tests/Geocoder/Tests/Provider/FreeGeoIpTest.php @@ -17,36 +17,36 @@ public function testGetName() * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The FreeGeoIp does not support Street addresses. */ - public function testGetGeocodedDataWithNull() + public function testGeocodeWithNull() { $provider = new FreeGeoIp($this->getMockAdapter($this->never())); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The FreeGeoIp does not support Street addresses. */ - public function testGetGeocodedDataWithEmpty() + public function testGeocodeWithEmpty() { $provider = new FreeGeoIp($this->getMockAdapter($this->never())); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The FreeGeoIp does not support Street addresses. */ - public function testGetGeocodedDataWithAddress() + public function testGeocodeWithAddress() { $provider = new FreeGeoIp($this->getMockAdapter($this->never())); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } - public function testGetGeocodedDataWithLocalhostIPv4() + public function testGeocodeWithLocalhostIPv4() { $provider = new FreeGeoIp($this->getMockAdapter($this->never())); - $result = $provider->getGeocodedData('127.0.0.1'); + $result = $provider->geocode('127.0.0.1'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -64,10 +64,10 @@ public function testGetGeocodedDataWithLocalhostIPv4() $this->assertEquals('localhost', $result['country']); } - public function testGetGeocodedDataWithLocalhostIPv6() + public function testGeocodeWithLocalhostIPv6() { $provider = new FreeGeoIp($this->getMockAdapter($this->never())); - $result = $provider->getGeocodedData('::1'); + $result = $provider->geocode('::1'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -89,26 +89,26 @@ public function testGetGeocodedDataWithLocalhostIPv6() * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://freegeoip.net/json/74.200.247.59 */ - public function testGetGeocodedDataWithRealIPv4GetsNullContent() + public function testGeocodeWithRealIPv4GetsNullContent() { $provider = new FreeGeoIp($this->getMockAdapterReturns(null)); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://freegeoip.net/json/74.200.247.59 */ - public function testGetGeocodedDataWithRealIPv4GetsEmptyContent() + public function testGeocodeWithRealIPv4GetsEmptyContent() { $provider = new FreeGeoIp($this->getMockAdapterReturns('')); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } - public function testGetGeocodedDataWithRealIPv4() + public function testGeocodeWithRealIPv4() { $provider = new FreeGeoIp($this->getAdapter()); - $result = $provider->getGeocodedData('74.200.247.59'); + $result = $provider->geocode('74.200.247.59'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -124,10 +124,10 @@ public function testGetGeocodedDataWithRealIPv4() $this->assertEquals('US', $result['countryCode']); } - public function testGetGeocodedDataWithRealIPv6() + public function testGeocodeWithRealIPv6() { $provider = new FreeGeoIp($this->getAdapter()); - $result = $provider->getGeocodedData('::ffff:74.200.247.59'); + $result = $provider->geocode('::ffff:74.200.247.59'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -147,16 +147,16 @@ public function testGetGeocodedDataWithRealIPv6() * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Could not execute query http://freegeoip.net/json/::ffff:74.200.247.59 */ - public function testGetGeocodedDataWithRealIPv6GetsNullContent() + public function testGeocodeWithRealIPv6GetsNullContent() { $provider = new FreeGeoIp($this->getMockAdapterReturns(null)); - $provider->getGeocodedData('::ffff:74.200.247.59'); + $provider->geocode('::ffff:74.200.247.59'); } - public function testGetGeocodedDataWithUSIPv4() + public function testGeocodeWithUSIPv4() { $provider = new FreeGeoIp($this->getAdapter()); - $result = $provider->getGeocodedData('74.200.247.59'); + $result = $provider->geocode('74.200.247.59'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -166,10 +166,10 @@ public function testGetGeocodedDataWithUSIPv4() $this->assertEquals('48', $result['regionCode']); } - public function testGetGeocodedDataWithUSIPv6() + public function testGeocodeWithUSIPv6() { $provider = new FreeGeoIp($this->getAdapter()); - $result = $provider->getGeocodedData('::ffff:74.200.247.59'); + $result = $provider->geocode('::ffff:74.200.247.59'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -179,10 +179,10 @@ public function testGetGeocodedDataWithUSIPv6() $this->assertEquals('48', $result['regionCode']); } - public function testGetGeocodedDataWithUKIPv4() + public function testGeocodeWithUKIPv4() { $provider = new FreeGeoIp($this->getAdapter()); - $result = $provider->getGeocodedData('132.185.255.60'); + $result = $provider->geocode('132.185.255.60'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -192,10 +192,10 @@ public function testGetGeocodedDataWithUKIPv4() $this->assertEquals('H9', $result['regionCode']); } - public function testGetGeocodedDataWithUKIPv6() + public function testGeocodeWithUKIPv6() { $provider = new FreeGeoIp($this->getAdapter()); - $result = $provider->getGeocodedData('::ffff:132.185.255.60'); + $result = $provider->geocode('::ffff:132.185.255.60'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -209,9 +209,9 @@ public function testGetGeocodedDataWithUKIPv6() * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The FreeGeoIp is not able to do reverse geocoding. */ - public function testGetReverseData() + public function testReverse() { $provider = new FreeGeoIp($this->getMockAdapter($this->never())); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } } diff --git a/tests/Geocoder/Tests/Provider/GeoIP2Test.php b/tests/Geocoder/Tests/Provider/GeoIP2Test.php index 476b4c280..9c8b5fe7a 100644 --- a/tests/Geocoder/Tests/Provider/GeoIP2Test.php +++ b/tests/Geocoder/Tests/Provider/GeoIP2Test.php @@ -39,9 +39,9 @@ public function testGetName() * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The Geocoder\Provider\GeoIP2 is not able to do reverse geocoding. */ - public function testQueryingReversedDataLeadToException() + public function testQueryingReverseLeadsToException() { - $this->provider->getReversedData(array(50, 9)); + $this->provider->reverse(50, 9); } public function testLocalhostDefaults() @@ -53,7 +53,7 @@ public function testLocalhostDefaults() 'country' => 'localhost', ); - $actualResult = $this->provider->getGeocodedData('127.0.0.1'); + $actualResult = $this->provider->geocode('127.0.0.1'); $this->assertSame($expectedResult, $actualResult); } @@ -64,11 +64,11 @@ public function testLocalhostDefaults() */ public function testOnlyIpAddressesCouldBeResolved() { - $this->provider->getGeocodedData('Street 123, Somewhere'); + $this->provider->geocode('Street 123, Somewhere'); } /** - * Provides data for getGeocodedData test + * Provides data for geocode test * * @return array */ @@ -133,7 +133,7 @@ public function testRetrievingGeodata($address, $adapterResponse, $expectedGeoda $adapter = $this->getGeoIP2AdapterMock($adapterResponse); $provider = new GeoIP2($adapter); - $actualGeodata = $provider->getGeocodedData($address); + $actualGeodata = $provider->geocode($address); $this->assertSame($expectedGeodata, $actualGeodata[0]); } @@ -149,7 +149,7 @@ public function testRetrievingGeodataNotExistingLocation() $provider = new GeoIP2($adapter); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** diff --git a/tests/Geocoder/Tests/Provider/GeoIPsTest.php b/tests/Geocoder/Tests/Provider/GeoIPsTest.php index 9b25bd615..fee2fc4b5 100644 --- a/tests/Geocoder/Tests/Provider/GeoIPsTest.php +++ b/tests/Geocoder/Tests/Provider/GeoIPsTest.php @@ -16,46 +16,46 @@ public function testGetName() /** * @expectedException \RuntimeException */ - public function testGetGeocodedDataWithNullApiKey() + public function testGeocodeWithNullApiKey() { $provider = new GeoIPs($this->getMockAdapter($this->never()), null); - $provider->getGeocodedData('foo'); + $provider->geocode('foo'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The GeoIPs does not support street addresses. */ - public function testGetGeocodedDataWithNull() + public function testGeocodeWithNull() { $provider = new GeoIPs($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The GeoIPs does not support street addresses. */ - public function testGetGeocodedDataWithEmpty() + public function testGeocodeWithEmpty() { $provider = new GeoIPs($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The GeoIPs does not support street addresses. */ - public function testGetGeocodedDataWithAddress() + public function testGeocodeWithAddress() { $provider = new GeoIPs($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } - public function testGetGeocodedDataWithLocalhostIPv4() + public function testGeocodeWithLocalhostIPv4() { $provider = new GeoIPs($this->getMockAdapter($this->never()), 'api_key'); - $result = $provider->getGeocodedData('127.0.0.1'); + $result = $provider->geocode('127.0.0.1'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -77,33 +77,33 @@ public function testGetGeocodedDataWithLocalhostIPv4() * @expectedException \Geocoder\Exception\UnsupportedOperation * @expectedExceptionMessage The GeoIPs does not support IPv6 addresses. */ - public function testGetGeocodedDataWithLocalhostIPv6() + public function testGeocodeWithLocalhostIPv6() { $provider = new GeoIPs($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Invalid response from GeoIPs server for query http://api.geoips.com/ip/74.200.247.59/key/api_key/output/json/timezone/true/ */ - public function testGetGeocodedDataWithRealIPv4GetsNullContent() + public function testGeocodeWithRealIPv4GetsNullContent() { $provider = new GeoIPs($this->getMockAdapterReturns(null), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult * @expectedExceptionMessage Invalid response from GeoIPs server for query http://api.geoips.com/ip/74.200.247.59/key/api_key/output/json/timezone/true/ */ - public function testGetGeocodedDataWithRealIPv4GetsEmptyContent() + public function testGeocodeWithRealIPv4GetsEmptyContent() { $provider = new GeoIPs($this->getMockAdapterReturns(''), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } - public function testGetGeocodedDataWithRealIPv4GetsFakeContentFormattedEmpty() + public function testGeocodeWithRealIPv4GetsFakeContentFormattedEmpty() { $json = '{"response":{ "status": "Propper Request", @@ -132,7 +132,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContentFormattedEmpty() }}'; $provider = new GeoIPs($this->getMockAdapterReturns($json), 'api_key'); - $result = $provider->getGeocodedData('66.147.244.214'); + $result = $provider->geocode('66.147.244.214'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -149,7 +149,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContentFormattedEmpty() $this->assertNull($result['timezone']); } - public function testGetGeocodedDataWithRealIPv4GetsFakeContent() + public function testGeocodeWithRealIPv4GetsFakeContent() { $json = '{"response":{ "status": "Propper Request", @@ -174,7 +174,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContent() }}'; $provider = new GeoIPs($this->getMockAdapterReturns($json), 'api_key'); - $result = $provider->getGeocodedData('66.147.244.214'); + $result = $provider->geocode('66.147.244.214'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -198,7 +198,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContent() * @expectedException \Geocoder\Exception\InvalidCredentials * @expectedExceptionMessage The API key associated with your request was not recognized. */ - public function testGetGeocodedDataWithRealIPv4AndInvalidApiKeyGetsFakeContent() + public function testGeocodeWithRealIPv4AndInvalidApiKeyGetsFakeContent() { $provider = new GeoIPs( $this->getMockAdapterReturns( @@ -217,14 +217,14 @@ public function testGetGeocodedDataWithRealIPv4AndInvalidApiKeyGetsFakeContent() ), 'api_key' ); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\InvalidCredentials * @expectedExceptionMessage The API key has not been approved or has been disabled. */ - public function testGetGeocodedDataWithRealIPv4AndInvalidApiKeyGetsFakeContent2() + public function testGeocodeWithRealIPv4AndInvalidApiKeyGetsFakeContent2() { $provider = new GeoIPs( $this->getMockAdapterReturns( @@ -243,14 +243,14 @@ public function testGetGeocodedDataWithRealIPv4AndInvalidApiKeyGetsFakeContent2( ), 'api_key' ); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\QuotaExceeded * @expectedExceptionMessage The service you have requested is over capacity. */ - public function testGetGeocodedDataWithRealIPv4AndQuotaExceeded() + public function testGeocodeWithRealIPv4AndQuotaExceeded() { $provider = new GeoIPs( $this->getMockAdapterReturns( @@ -269,14 +269,14 @@ public function testGetGeocodedDataWithRealIPv4AndQuotaExceeded() ), 'api_key' ); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\InvalidArgument * @expectedExceptionMessage The API call should include a valid IP address. */ - public function testGetGeocodedDataGetsFakeContentWithIpNotFound() + public function testGeocodeGetsFakeContentWithIpNotFound() { $provider = new GeoIPs( $this->getMockAdapterReturns( @@ -295,14 +295,14 @@ public function testGetGeocodedDataGetsFakeContentWithIpNotFound() ), 'api_key' ); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\InvalidCredentials * @expectedExceptionMessage The API call should include a API key parameter. */ - public function testGetGeocodedDataGetsFakeContentWithKeyNotFound() + public function testGeocodeGetsFakeContentWithKeyNotFound() { $provider = new GeoIPs( $this->getMockAdapterReturns( @@ -321,17 +321,17 @@ public function testGetGeocodedDataGetsFakeContentWithKeyNotFound() ), 'api_key' ); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } - public function testGetGeocodedDataWithRealIPv4() + public function testGeocodeWithRealIPv4() { if (!isset($_SERVER['GEOIPS_API_KEY'])) { $this->markTestSkipped('You need to configure the GEOIPS_API_KEY value in phpunit.xml'); } $provider = new GeoIPs($this->getAdapter(), $_SERVER['GEOIPS_API_KEY']); - $result = $provider->getGeocodedData('66.147.244.214'); + $result = $provider->geocode('66.147.244.214'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -354,14 +354,14 @@ public function testGetGeocodedDataWithRealIPv4() /** * @expectedException \Geocoder\Exception\NoResult */ - public function testGetGeocodedDataWithRealIPv4NoResults() + public function testGeocodeWithRealIPv4NoResults() { if (!isset($_SERVER['GEOIPS_API_KEY'])) { $this->markTestSkipped('You need to configure the GEOIPS_API_KEY value in phpunit.xml'); } $provider = new GeoIPs($this->getAdapter(), $_SERVER['GEOIPS_API_KEY']); - $result = $provider->getGeocodedData('255.255.150.96'); + $result = $provider->geocode('255.255.150.96'); } /** @@ -371,6 +371,6 @@ public function testGetGeocodedDataWithRealIPv4NoResults() public function testGetReverseData() { $provider = new GeoIPs($this->getMockAdapter($this->never()), 'api_key'); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } } diff --git a/tests/Geocoder/Tests/Provider/GeoPluginTest.php b/tests/Geocoder/Tests/Provider/GeoPluginTest.php index 49c0f6070..33d4d81a6 100644 --- a/tests/Geocoder/Tests/Provider/GeoPluginTest.php +++ b/tests/Geocoder/Tests/Provider/GeoPluginTest.php @@ -7,7 +7,7 @@ class GeoPluginTest extends TestCase { - public function testGetName() + public function testgetName() { $provider = new GeoPlugin($this->getMockAdapter($this->never())); $this->assertEquals('geo_plugin', $provider->getName()); @@ -15,122 +15,109 @@ public function testGetName() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The GeoPlugin does not support street addresses. + * @expectedExceptionMessage The GeoPlugin provider does not support street addresses, only IP addresses. */ public function testGetGeocodedDataWithNull() { $provider = new GeoPlugin($this->getMockAdapter($this->never())); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The GeoPlugin does not support street addresses. + * @expectedExceptionMessage The GeoPlugin provider does not support street addresses, only IP addresses. */ public function testGetGeocodedDataWithEmpty() { $provider = new GeoPlugin($this->getMockAdapter($this->never())); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The GeoPlugin does not support street addresses. + * @expectedExceptionMessage The GeoPlugin provider does not support street addresses, only IP addresses. */ public function testGetGeocodedDataWithAddress() { $provider = new GeoPlugin($this->getMockAdapter($this->never())); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new GeoPlugin($this->getMockAdapter($this->never())); - $result = $provider->getGeocodedData('127.0.0.1'); - - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); - - $result = $result[0]; - $this->assertInternalType('array', $result); - $this->assertArrayNotHasKey('latitude', $result); - $this->assertArrayNotHasKey('longitude', $result); - $this->assertArrayNotHasKey('postalCode', $result); - $this->assertArrayNotHasKey('timezone', $result); - - $this->assertEquals('localhost', $result['locality']); - $this->assertEquals('localhost', $result['region']); - $this->assertEquals('localhost', $result['county']); - $this->assertEquals('localhost', $result['country']); + $results = $provider->geocode('127.0.0.1'); + + $this->assertInternalType('array', $results); + $this->assertCount(1, $results); + + $result = $results[0]; + $this->assertEquals('Localhost', $result->getLocality()); + $this->assertEquals('Localhost', $result->getRegion()->getName()); + $this->assertEquals('Localhost', $result->getCounty()->getName()); + $this->assertEquals('Localhost', $result->getCountry()->getName()); } public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new GeoPlugin($this->getMockAdapter($this->never())); - $result = $provider->getGeocodedData('::1'); - - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); - - $result = $result[0]; - $this->assertInternalType('array', $result); - $this->assertArrayNotHasKey('latitude', $result); - $this->assertArrayNotHasKey('longitude', $result); - $this->assertArrayNotHasKey('postalCode', $result); - $this->assertArrayNotHasKey('timezone', $result); - - $this->assertEquals('localhost', $result['locality']); - $this->assertEquals('localhost', $result['region']); - $this->assertEquals('localhost', $result['county']); - $this->assertEquals('localhost', $result['country']); + $results = $provider->geocode('::1'); + + $this->assertInternalType('array', $results); + $this->assertCount(1, $results); + + $result = $results[0]; + $this->assertEquals('Localhost', $result->getLocality()); + $this->assertEquals('Localhost', $result->getRegion()->getName()); + $this->assertEquals('Localhost', $result->getCounty()->getName()); + $this->assertEquals('Localhost', $result->getCountry()->getName()); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://www.geoplugin.net/json.gp?ip=74.200.247.59 + * @expectedExceptionMessage Could not execute query "http://www.geoplugin.net/json.gp?ip=74.200.247.59". */ public function testGetGeocodedDataWithRealIPv4GetsNullContent() { $provider = new GeoPlugin($this->getMockAdapterReturns(null)); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://www.geoplugin.net/json.gp?ip=74.200.247.59 + * @expectedExceptionMessage Could not execute query "http://www.geoplugin.net/json.gp?ip=74.200.247.59". */ public function testGetGeocodedDataWithRealIPv4GetsEmptyContent() { $provider = new GeoPlugin($this->getMockAdapterReturns('')); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } public function testGetGeocodedDataWithRealIPv4() { $provider = new GeoPlugin($this->getAdapter()); - $result = $provider->getGeocodedData('66.147.244.214'); - - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); - - $result = $result[0]; - $this->assertInternalType('array', $result); - $this->assertEquals('Provo', $result['locality']); - $this->assertEquals(40.218102, $result['latitude'], '', 0.0001); - $this->assertEquals(-111.613297, $result['longitude'], '', 0.0001); - $this->assertEquals('UT', $result['regionCode']); - $this->assertEquals('Utah', $result['region']); - $this->assertEquals('United States', $result['country']); - $this->assertEquals('US', $result['countryCode']); + $results = $provider->geocode('66.147.244.214'); + + $this->assertInternalType('array', $results); + $this->assertCount(1, $results); + + $result = $results[0]; + $this->assertEquals('Provo', $result->getLocality()); + $this->assertEquals(40.218102, $result->getLatitude(), '', 0.0001); + $this->assertEquals(-111.613297, $result->getLongitude(), '', 0.0001); + $this->assertEquals('UT', $result->getRegion()->getCode()); + $this->assertEquals('Utah', $result->getRegion()->getName()); + $this->assertEquals('United States', $result->getCountry()->getName()); + $this->assertEquals('US', $result->getCountry()->getCode()); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The GeoPlugin is not able to do reverse geocoding. + * @expectedExceptionMessage The GeoPlugin provider is not able to do reverse geocoding. */ public function testGetReverseData() { $provider = new GeoPlugin($this->getMockAdapter($this->never())); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } } diff --git a/tests/Geocoder/Tests/Provider/GeonamesTest.php b/tests/Geocoder/Tests/Provider/GeonamesTest.php index 3b7a46d20..636a84c97 100644 --- a/tests/Geocoder/Tests/Provider/GeonamesTest.php +++ b/tests/Geocoder/Tests/Provider/GeonamesTest.php @@ -15,57 +15,57 @@ public function testGetName() /** * @expectedException \Geocoder\Exception\InvalidCredentials - * @expectedExceptionMessage No Username provided + * @expectedExceptionMessage No username provided. */ public function testGetGeocodedDataWithNullUsername() { $provider = new Geonames($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); - $provider->getGeocodedData('foo'); + $provider->geocode('foo'); } /** * @expectedException \Geocoder\Exception\InvalidCredentials - * @expectedExceptionMessage No Username provided + * @expectedExceptionMessage No username provided. */ public function testGetReversedDataWithNullUsername() { $provider = new Geonames($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); - $provider->getReversedData(array(1,2)); + $provider->reverse(1,2); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The Geonames does not support IP addresses. + * @expectedExceptionMessage The Geonames provider does not support IP addresses. */ public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new Geonames($this->getMockAdapter($this->never()), 'username'); - $provider->getGeocodedData('127.0.0.1'); + $provider->geocode('127.0.0.1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The Geonames does not support IP addresses. + * @expectedExceptionMessage The Geonames provider does not support IP addresses. */ public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new Geonames($this->getMockAdapter($this->never()), 'username'); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://api.geonames.org/searchJSON?q=&maxRows=5&style=full&username=username + * @expectedExceptionMessage Could not execute query "http://api.geonames.org/searchJSON?q=&maxRows=5&style=full&username=username". */ public function testGetGeocodedDataWithNull() { $provider = new Geonames($this->getMockAdapter(), 'username'); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage No places found for query http://api.geonames.org/searchJSON?q=BlaBlaBla&maxRows=5&style=full&username=username + * @expectedExceptionMessage No places found for query "http://api.geonames.org/searchJSON?q=BlaBlaBla&maxRows=5&style=full&username=username". * @ */ public function testGetGeocodedDataWithUnknownCity() @@ -77,7 +77,7 @@ public function testGetGeocodedDataWithUnknownCity() } JSON; $provider = new Geonames($this->getMockAdapterReturns($noPlacesFoundResponse), 'username'); - $provider->getGeocodedData('BlaBlaBla'); + $provider->geocode('BlaBlaBla'); } public function testGetGeocodedDataWithRealPlace() @@ -87,12 +87,10 @@ public function testGetGeocodedDataWithRealPlace() } $provider = new Geonames($this->getAdapter(), $_SERVER['GEONAMES_USERNAME']); - $results = $provider->getGeocodedData('London'); + $results = $provider->geocode('London'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); $this->assertEquals(51.508528775863, $results[0]['latitude'], '', 0.01); $this->assertEquals(-0.12574195861816, $results[0]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[0]['bounds']); @@ -190,7 +188,7 @@ public function testGetGeocodedDataWithRealPlaceWithLocale() } $provider = new Geonames($this->getAdapter(), $_SERVER['GEONAMES_USERNAME'], 'it_IT'); - $results = $provider->getGeocodedData('London'); + $results = $provider->geocode('London'); $this->assertInternalType('array', $results); $this->assertCount(5, $results); @@ -293,7 +291,7 @@ public function testGetReversedDataWithRealCoordinates() } $provider = new Geonames($this->getAdapter(), $_SERVER['GEONAMES_USERNAME']); - $results = $provider->getReversedData(array(51.50853, -0.12574)); + $results = $provider->reverse(array(51.50853, -0.12574)); $this->assertInternalType('array', $results); $this->assertCount(1, $results); @@ -317,7 +315,7 @@ public function testGetReversedDataWithRealCoordinatesWithLocale() } $provider = new Geonames($this->getAdapter(), $_SERVER['GEONAMES_USERNAME'], 'it_IT'); - $results = $provider->getReversedData(array(51.50853, -0.12574)); + $results = $provider->reverse(array(51.50853, -0.12574)); $this->assertInternalType('array', $results); $this->assertCount(1, $results); @@ -336,7 +334,7 @@ public function testGetReversedDataWithRealCoordinatesWithLocale() /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://api.geonames.org/findNearbyPlaceNameJSON?lat=-80.000000&lng=-170.000000&style=full&maxRows=5&username=username + * @expectedExceptionMessage Could not execute query "http://api.geonames.org/findNearbyPlaceNameJSON?lat=-80.000000&lng=-170.000000&style=full&maxRows=5&username=username". */ public function testGetReversedDataWithBadCoordinates() { @@ -346,6 +344,6 @@ public function testGetReversedDataWithBadCoordinates() } JSON; $provider = new Geonames($this->getMockAdapterReturns($badCoordinateResponse), 'username'); - $provider->getReversedData(array(-80.000000, -170.000000)); + $provider->reverse(-80.000000, -170.000000); } } diff --git a/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php b/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php index 3850e0a1e..4533e70e9 100644 --- a/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php +++ b/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php @@ -77,7 +77,7 @@ public function testGetGeocodedDataWithInvalidClientIdAndKey() { $provider = new GoogleMapsBusiness($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, true); - $provider->getGeocodedData('Columbia University'); + $provider->geocode('Columbia University'); } /** @@ -88,6 +88,6 @@ public function testGetGeocodedDataWithINvalidClientIdAndKeyNoSsl() { $provider = new GoogleMapsBusiness($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, false); - $provider->getGeocodedData('Columbia University', true); + $provider->geocode('Columbia University', true); } } diff --git a/tests/Geocoder/Tests/Provider/GoogleMapsTest.php b/tests/Geocoder/Tests/Provider/GoogleMapsTest.php index d0fd978a9..de6f9efee 100644 --- a/tests/Geocoder/Tests/Provider/GoogleMapsTest.php +++ b/tests/Geocoder/Tests/Provider/GoogleMapsTest.php @@ -20,82 +20,82 @@ public function testGetName() /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://maps.googleapis.com/maps/api/geocode/json?address=foobar + * @expectedExceptionMessage Could not execute query "http://maps.googleapis.com/maps/api/geocode/json?address=foobar". */ public function testGetGeocodedData() { $provider = new GoogleMaps($this->getMockAdapter()); - $provider->getGeocodedData('foobar'); + $provider->geocode('foobar'); } /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://maps.googleapis.com/maps/api/geocode/json?address= + * @expectedExceptionMessage Could not execute query "http://maps.googleapis.com/maps/api/geocode/json?address=". */ public function testGetGeocodedDataWithNull() { $provider = new GoogleMaps($this->getMockAdapter()); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://maps.googleapis.com/maps/api/geocode/json?address= + * @expectedExceptionMessage Could not execute query "http://maps.googleapis.com/maps/api/geocode/json?address=". */ public function testGetGeocodedDataWithEmpty() { $provider = new GoogleMaps($this->getMockAdapter()); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The GoogleMaps does not support IP addresses. + * @expectedExceptionMessage The GoogleMaps provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new GoogleMaps($this->getMockAdapter($this->never())); - $provider->getGeocodedData('127.0.0.1'); + $provider->geocode('127.0.0.1'); } /** * @expectedException Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The GoogleMaps does not support IP addresses. + * @expectedExceptionMessage The GoogleMaps provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new GoogleMaps($this->getMockAdapter($this->never())); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The GoogleMaps does not support IP addresses. + * @expectedExceptionMessage The GoogleMaps provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithRealIp() { $provider = new GoogleMaps($this->getAdapter()); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France + * @expectedExceptionMessage Could not execute query "http://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France". */ public function testGetGeocodedDataWithAddressGetsNullContent() { $provider = new GoogleMaps($this->getMockAdapterReturns(null)); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France + * @expectedExceptionMessage Could not execute query "http://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France". */ public function testGetGeocodedDataWithAddressGetsEmptyContent() { $provider = new GoogleMaps($this->getMockAdapterReturns('{"status":"OK"}')); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } /** @@ -105,19 +105,17 @@ public function testGetGeocodedDataWithAddressGetsEmptyContent() public function testGetGeocodedDataWithQuotaExceeded() { $provider = new GoogleMaps($this->getMockAdapterReturns('{"status":"OVER_QUERY_LIMIT"}')); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithRealAddress() { $provider = new GoogleMaps($this->getAdapter(), 'fr-FR', 'Île-de-France'); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); - $this->assertInternalType('array', $results); $this->assertCount(1, $results); - $result = $results[0]; - $this->assertInternalType('array', $result); + $result = $results[0]->toArray(); $this->assertEquals(48.8630462, $result['latitude'], '', 0.001); $this->assertEquals(2.3882487, $result['longitude'], '', 0.001); $this->assertArrayHasKey('south', $result['bounds']); @@ -144,13 +142,11 @@ public function testGetGeocodedDataWithRealAddress() public function testGetGeocodedDataWithRealAddressWithSsl() { $provider = new GoogleMaps($this->getAdapter(), null, null, true); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); - $this->assertInternalType('array', $results); $this->assertCount(1, $results); - $result = $results[0]; - $this->assertInternalType('array', $result); + $result = $results[0]->toArray(); $this->assertEquals(48.8630462, $result['latitude'], '', 0.001); $this->assertEquals(2.3882487, $result['longitude'], '', 0.001); $this->assertArrayHasKey('south', $result['bounds']); @@ -177,13 +173,11 @@ public function testGetGeocodedDataWithRealAddressWithSsl() public function testGetGeocodedDataBoundsWithRealAddressForNonRooftopLocation() { $provider = new GoogleMaps($this->getAdapter()); - $results = $provider->getGeocodedData('Paris, France'); + $results = $provider->geocode('Paris, France'); - $this->assertInternalType('array', $results); $this->assertCount(1, $results); - $result = $results[0]; - $this->assertInternalType('array', $result); + $result = $results[0]->toArray(); $this->assertNotNull($result['bounds']); $this->assertArrayHasKey('south', $result['bounds']); $this->assertArrayHasKey('west', $result['bounds']); @@ -198,40 +192,38 @@ public function testGetGeocodedDataBoundsWithRealAddressForNonRooftopLocation() public function testGetGeocodedDataWithRealAddressReturnsMultipleResults() { $provider = new GoogleMaps($this->getAdapter()); - $results = $provider->getGeocodedData('Paris'); + $results = $provider->geocode('Paris'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(48.856614, $results[0]['latitude'], '', 0.001); $this->assertEquals(2.3522219, $results[0]['longitude'], '', 0.001); $this->assertEquals('Paris', $results[0]['locality']); $this->assertEquals('France', $results[0]['country']); $this->assertEquals('FR', $results[0]['countryCode']); - $this->assertInternalType('array', $results[1]); $this->assertEquals(33.6609389, $results[1]['latitude'], '', 0.001); $this->assertEquals(-95.555513, $results[1]['longitude'], '', 0.001); $this->assertEquals('Paris', $results[1]['locality']); $this->assertEquals('United States', $results[1]['country']); $this->assertEquals('US', $results[1]['countryCode']); - $this->assertInternalType('array', $results[2]); $this->assertEquals(36.3020023, $results[2]['latitude'], '', 0.001); $this->assertEquals(-88.3267107, $results[2]['longitude'], '', 0.001); $this->assertEquals('Paris', $results[2]['locality']); $this->assertEquals('United States', $results[2]['country']); $this->assertEquals('US', $results[2]['countryCode']); - $this->assertInternalType('array', $results[3]); $this->assertEquals(39.611146, $results[3]['latitude'], '', 0.001); $this->assertEquals(-87.6961374, $results[3]['longitude'], '', 0.001); $this->assertEquals('Paris', $results[3]['locality']); $this->assertEquals('United States', $results[3]['country']); $this->assertEquals('US', $results[3]['countryCode']); - $this->assertInternalType('array', $results[4]); $this->assertEquals(38.2097987, $results[4]['latitude'], '', 0.001); $this->assertEquals(-84.2529869, $results[4]['longitude'], '', 0.001); $this->assertEquals('Paris', $results[4]['locality']); @@ -241,24 +233,20 @@ public function testGetGeocodedDataWithRealAddressReturnsMultipleResults() /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://maps.googleapis.com/maps/api/geocode/json?address=1.000000%2C2.000000 + * @expectedExceptionMessage Could not execute query "http://maps.googleapis.com/maps/api/geocode/json?address=1.000000%2C2.000000". */ public function testGetReversedData() { $provider = new GoogleMaps($this->getMockAdapter()); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } public function testGetReversedDataWithRealCoordinates() { $provider = new GoogleMaps($this->getAdapter()); - $result = $provider->getReversedData(array(48.8631507, 2.388911)); - - $this->assertInternalType('array', $result); - $this->assertTrue(is_array($result[0])); + $result = $provider->reverse(48.8631507, 2.388911); - $result = $result[0]; - $this->assertInternalType('array', $result); + $result = $result[0]->toArray(); $this->assertEquals(1, $result['streetNumber']); $this->assertEquals('Avenue Gambetta', $result['streetName']); $this->assertEquals(75020, $result['postalCode']); @@ -271,24 +259,22 @@ public function testGetReversedDataWithRealCoordinates() /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://maps.googleapis.com/maps/api/geocode/json?address=48.863151%2C2.388911 + * @expectedExceptionMessage Could not execute query "http://maps.googleapis.com/maps/api/geocode/json?address=48.863151%2C2.388911". */ public function testGetReversedDataWithCoordinatesGetsNullContent() { $provider = new GoogleMaps($this->getMockAdapterReturns(null)); - $provider->getReversedData(array(48.8631507, 2.388911)); + $provider->reverse(48.8631507, 2.388911); } public function testGetGeocodedDataWithCityDistrict() { $provider = new GoogleMaps($this->getAdapter()); - $results = $provider->getGeocodedData('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'); + $results = $provider->geocode('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'); - $this->assertInternalType('array', $results); $this->assertCount(1, $results); - $result = $results[0]; - $this->assertInternalType('array', $result); + $result = $results[0]->toArray(); $this->assertEquals('Kalbach-Riedberg', $result['subLocality']); } @@ -299,7 +285,7 @@ public function testGetGeocodedDataWithCityDistrict() public function testGetGeocodedDataWithInavlidApiKey() { $provider = new GoogleMaps($this->getMockAdapterReturns('{"error_message":"The provided API key is invalid.", "status":"REQUEST_DENIED"}')); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithRealValidApiKey() @@ -310,7 +296,7 @@ public function testGetGeocodedDataWithRealValidApiKey() $provider = new GoogleMaps($this->getAdapter(), null, null, true, $_SERVER['GOOGLE_GEOCODING_KEY']); - $data = $provider->getGeocodedData('Columbia University'); + $data = $provider->geocode('Columbia University'); $data = $data[0]; $this->assertNotNull($data['latitude']); @@ -332,6 +318,6 @@ public function testGetGeocodedDataWithRealInvalidApiKey() { $provider = new GoogleMaps($this->getAdapter(), null, null, true, $this->testAPIKey); - $provider->getGeocodedData('Columbia University'); + $provider->geocode('Columbia University'); } } diff --git a/tests/Geocoder/Tests/Provider/HostIpTest.php b/tests/Geocoder/Tests/Provider/HostIpTest.php index a8bef12d0..6cd88587e 100644 --- a/tests/Geocoder/Tests/Provider/HostIpTest.php +++ b/tests/Geocoder/Tests/Provider/HostIpTest.php @@ -15,38 +15,38 @@ public function testGetName() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The HostIp does not support Street addresses. + * @expectedExceptionMessage The HostIp provider does not support Street addresses. */ public function testGetGeocodedDataWithNull() { $provider = new HostIp($this->getMockAdapter($this->never())); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The HostIp does not support Street addresses. + * @expectedExceptionMessage The HostIp provider does not support Street addresses. */ public function testGetGeocodedDataWithEmpty() { $provider = new HostIp($this->getMockAdapter($this->never())); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The HostIp does not support Street addresses. + * @expectedExceptionMessage The HostIp provider does not support Street addresses. */ public function testGetGeocodedDataWithAddress() { $provider = new HostIp($this->getMockAdapter($this->never())); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new HostIp($this->getMockAdapter($this->never())); - $result = $provider->getGeocodedData('127.0.0.1'); + $result = $provider->geocode('127.0.0.1'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -66,38 +66,38 @@ public function testGetGeocodedDataWithLocalhostIPv4() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The HostIp does not support IPv6 addresses. + * @expectedExceptionMessage The HostIp provider does not support IPv6 addresses. */ public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new HostIp($this->getMockAdapter($this->never())); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://api.hostip.info/get_json.php?ip=88.188.221.14&position=true + * @expectedExceptionMessage Could not execute query "http://api.hostip.info/get_json.php?ip=88.188.221.14&position=true". */ public function testGetGeocodedDataWithRealIPv4GetsNullContent() { $provider = new HostIp($this->getMockAdapterReturns(null)); - $provider->getGeocodedData('88.188.221.14'); + $provider->geocode('88.188.221.14'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://api.hostip.info/get_json.php?ip=88.188.221.14&position=true + * @expectedExceptionMessage Could not execute query "http://api.hostip.info/get_json.php?ip=88.188.221.14&position=true". */ public function testGetGeocodedDataWithRealIPv4GetsEmptyContent() { $provider = new HostIp($this->getMockAdapterReturns('')); - $provider->getGeocodedData('88.188.221.14'); + $provider->geocode('88.188.221.14'); } public function testGetGeocodedDataWithRealIPv4() { $provider = new HostIp($this->getAdapter()); - $result = $provider->getGeocodedData('88.188.221.14'); + $result = $provider->geocode('88.188.221.14'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -115,28 +115,28 @@ public function testGetGeocodedDataWithRealIPv4() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The HostIp does not support IPv6 addresses. + * @expectedExceptionMessage The HostIp provider does not support IPv6 addresses. */ public function testGetGeocodedDataWithRealIPv6() { $provider = new HostIp($this->getAdapter()); - $provider->getGeocodedData('::ffff:88.188.221.14'); + $provider->geocode('::ffff:88.188.221.14'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The HostIp is not able to do reverse geocoding. + * @expectedExceptionMessage The HostIp provider is not able to do reverse geocoding. */ public function testGetReverseData() { $provider = new HostIp($this->getMockAdapter($this->never())); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } public function testGetGeocodedDataWithAnotherIp() { $provider = new HostIp($this->getAdapter()); - $result = $provider->getGeocodedData('33.33.33.22'); + $result = $provider->geocode('33.33.33.22'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); diff --git a/tests/Geocoder/Tests/Provider/IpInfoDbTest.php b/tests/Geocoder/Tests/Provider/IpInfoDbTest.php index 9c5193341..d3281d752 100644 --- a/tests/Geocoder/Tests/Provider/IpInfoDbTest.php +++ b/tests/Geocoder/Tests/Provider/IpInfoDbTest.php @@ -19,53 +19,53 @@ public function testGetName() public function testGetDataWithNullApiKey() { $provider = new IpInfoDb($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); - $provider->getGeocodedData('foo'); + $provider->geocode('foo'); } /** * @expectedException Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb does not support Street addresses. + * @expectedExceptionMessage The IpInfoDb provider does not support street addresses, only IPv4 addresses. */ public function testGetGeocodedDataWithRandomString() { $provider = new IpInfoDb($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('foobar'); + $provider->geocode('foobar'); } /** * @expectedException Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb does not support Street addresses. + * @expectedExceptionMessage The IpInfoDb provider does not support street addresses, only IPv4 addresses. */ public function testGetGeocodedDataWithNull() { $provider = new IpInfoDb($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb does not support Street addresses. + * @expectedExceptionMessage The IpInfoDb provider does not support street addresses, only IPv4 addresses. */ public function testGetGeocodedDataWithEmpty() { $provider = new IpInfoDb($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb does not support Street addresses. + * @expectedExceptionMessage The IpInfoDb provider does not support street addresses, only IPv4 addresses. */ public function testGetGeocodedDataWithAddress() { $provider = new IpInfoDb($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new IpInfoDb($this->getMockAdapter($this->never()), 'api_key'); - $result = $provider->getGeocodedData('127.0.0.1'); + $result = $provider->geocode('127.0.0.1'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -85,32 +85,32 @@ public function testGetGeocodedDataWithLocalhostIPv4() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb does not support IPv6 addresses. + * @expectedExceptionMessage The IpInfoDb provider does not support IPv6 addresses. */ public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new IpInfoDb($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://api.ipinfodb.com/v3/ip-city/?key=api_key&format=json&ip=74.125.45.100 + * @expectedExceptionMessage Could not execute query "http://api.ipinfodb.com/v3/ip-city/?key=api_key&format=json&ip=74.125.45.100". */ public function testGetGeocodedDataWithRealIPv4GetsNullContent() { $provider = new IpInfoDb($this->getMockAdapterReturns(null), 'api_key'); - $provider->getGeocodedData('74.125.45.100'); + $provider->geocode('74.125.45.100'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://api.ipinfodb.com/v3/ip-city/?key=api_key&format=json&ip=74.125.45.100 + * @expectedExceptionMessage Could not execute query "http://api.ipinfodb.com/v3/ip-city/?key=api_key&format=json&ip=74.125.45.100". */ public function testGetGeocodedDataWithRealIPv4GetsEmptyContent() { $provider = new IpInfoDb($this->getMockAdapterReturns(''), 'api_key'); - $provider->getGeocodedData('74.125.45.100'); + $provider->geocode('74.125.45.100'); } public function testGetGeocodedDataWithRealIPv4() @@ -120,7 +120,7 @@ public function testGetGeocodedDataWithRealIPv4() } $provider = new IpInfoDb($this->getAdapter(), $_SERVER['IPINFODB_API_KEY']); - $result = $provider->getGeocodedData('74.125.45.100'); + $result = $provider->geocode('74.125.45.100'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -139,7 +139,7 @@ public function testGetGeocodedDataWithRealIPv4() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb does not support IPv6 addresses. + * @expectedExceptionMessage The IpInfoDb provider does not support IPv6 addresses, only IPv4 addresses. */ public function testGetGeocodedDataWithRealIPv6() { @@ -148,16 +148,16 @@ public function testGetGeocodedDataWithRealIPv6() } $provider = new IpInfoDb($this->getAdapter(), $_SERVER['IPINFODB_API_KEY']); - $provider->getGeocodedData('::ffff:74.125.45.100'); + $provider->geocode('::ffff:74.125.45.100'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The IpInfoDb is not able to do reverse geocoding. + * @expectedExceptionMessage The IpInfoDb provider is not able to do reverse geocoding. */ public function testReversedData() { $provider = new IpInfoDb($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), 'api_key'); - $provider->getReversedData(array()); + $provider->reverse(null, null); } } diff --git a/tests/Geocoder/Tests/Provider/MapQuestTest.php b/tests/Geocoder/Tests/Provider/MapQuestTest.php index a855616e5..5ca46bf8a 100644 --- a/tests/Geocoder/Tests/Provider/MapQuestTest.php +++ b/tests/Geocoder/Tests/Provider/MapQuestTest.php @@ -18,34 +18,34 @@ public function testGetName() /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not find results for given query: http://open.mapquestapi.com/geocoding/v1/address?location=foobar&outFormat=json&maxResults=5&key=api_key&thumbMaps=false + * @expectedExceptionMessage Could not find results for query "http://open.mapquestapi.com/geocoding/v1/address?location=foobar&outFormat=json&maxResults=5&key=api_key&thumbMaps=false". */ public function testGetGeocodedData() { $provider = new MapQuest($this->getMockAdapterReturns('{}'), 'api_key'); - $provider->getGeocodedData('foobar'); + $provider->geocode('foobar'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query: http://open.mapquestapi.com/geocoding/v1/address?location=10+avenue+Gambetta%2C+Paris%2C+France&outFormat=json&maxResults=5&key=api_key&thumbMaps=false + * @expectedExceptionMessage Could not execute query "http://open.mapquestapi.com/geocoding/v1/address?location=10+avenue+Gambetta%2C+Paris%2C+France&outFormat=json&maxResults=5&key=api_key&thumbMaps=false". */ public function testGetGeocodedDataWithAddressGetsNullContent() { $provider = new MapQuest($this->getMockAdapterReturns(null), 'api_key'); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not find results for given query: http://open.mapquestapi.com/geocoding/v1/reverse?key=api_key&lat=123.000000&lng=456.000000 + * @expectedExceptionMessage Could not find results for query "http://open.mapquestapi.com/geocoding/v1/reverse?key=api_key&lat=123.000000&lng=456.000000". */ public function testGetNotRelevantData() { $json = '{"results":[{"locations":[{"street":"","postalCode":"","adminArea5":"","adminArea4":"","adminArea3":"","adminArea1":""}]}]}'; $provider = new MapQuest($this->getMockAdapterReturns($json), 'api_key'); - $provider->getReversedData(array(123, 456)); + $provider->reverse(123, 456); } public function testGetGeocodedDataWithRealAddress() @@ -55,7 +55,7 @@ public function testGetGeocodedDataWithRealAddress() } $provider = new MapQuest($this->getAdapter(), $_SERVER['MAPQUEST_API_KEY']); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); $this->assertInternalType('array', $results); $this->assertCount(1, $results); @@ -87,7 +87,7 @@ public function testGetReversedData() } $provider = new MapQuest($this->getMockAdapter(), $_SERVER['MAPQUEST_API_KEY']); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } public function testGetReversedDataWithRealCoordinates() @@ -97,7 +97,7 @@ public function testGetReversedDataWithRealCoordinates() } $provider = new MapQuest($this->getAdapter(), $_SERVER['MAPQUEST_API_KEY']); - $result = $provider->getReversedData(array(54.0484068, -2.7990345)); + $result = $provider->reverse(54.0484068, -2.7990345); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -126,7 +126,7 @@ public function testGetGeocodedDataWithCity() } $provider = new MapQuest($this->getAdapter(), $_SERVER['MAPQUEST_API_KEY']); - $results = $provider->getGeocodedData('Hanover'); + $results = $provider->geocode('Hanover'); $this->assertInternalType('array', $results); $this->assertCount(5, $results); @@ -170,7 +170,7 @@ public function testGetGeocodedDataWithCityDistrict() } $provider = new MapQuest($this->getAdapter(), $_SERVER['MAPQUEST_API_KEY']); - $result = $provider->getGeocodedData('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'); + $result = $provider->geocode('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -195,79 +195,41 @@ public function testGetGeocodedDataWithCityDistrict() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The MapQuest does not support IP addresses. + * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new MapQuest($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('127.0.0.1'); + $provider->geocode('127.0.0.1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The MapQuest does not support IP addresses. + * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new MapQuest($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The MapQuest does not support IP addresses. + * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithRealIPv4() { $provider = new MapQuest($this->getAdapter(), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The MapQuest does not support IP addresses. + * @expectedExceptionMessage The MapQuest provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithRealIPv6() { $provider = new MapQuest($this->getAdapter(), 'api_key'); - $provider->getGeocodedData('::ffff:74.200.247.59'); - } - - public function testLicensedVsOpenGeocodeEndpoints() - { - $licensed = false; - $provider = new MapQuestMock($this->getAdapter(), 'api_key', null, $licensed); - $queryUrl = $provider->getGeocodedData('Hanover'); - $this->assertContains('http://open.', $queryUrl); - - $licensed = true; - $provider = new MapQuestMock($this->getAdapter(), 'api_key', null, $licensed); - $queryUrl = $provider->getGeocodedData('Hanover'); - $this->assertContains('http://www.', $queryUrl); - } - - public function testLicensedVsOpenReverseGeocodeEndpoints() - { - $licensed = false; - $provider = new MapQuestMock($this->getAdapter(), 'api_key', null, $licensed); - $queryUrl = $provider->getReversedData(array(54.0484068, -2.7990345)); - $this->assertContains('http://open.', $queryUrl); - - $licensed = true; - $provider = new MapQuestMock($this->getAdapter(), 'api_key', null, $licensed); - $queryUrl = $provider->getReversedData(array(54.0484068, -2.7990345)); - $this->assertContains('http://www.', $queryUrl); - } -} - -class MapQuestMock extends MapQuest -{ - /** - * Short circuits so assertions can inspect the - * executed query URL - */ - protected function executeQuery($query) - { - return $query; + $provider->geocode('::ffff:74.200.247.59'); } } diff --git a/tests/Geocoder/Tests/Provider/MaxMindTest.php b/tests/Geocoder/Tests/Provider/MaxMindTest.php index 4dbb36edb..2f5f18865 100644 --- a/tests/Geocoder/Tests/Provider/MaxMindTest.php +++ b/tests/Geocoder/Tests/Provider/MaxMindTest.php @@ -19,79 +19,65 @@ public function testGetName() public function testGetGeocodedDataWithNullApiKey() { $provider = new MaxMind($this->getMockAdapter($this->never()), null); - $provider->getGeocodedData('foo'); + $provider->geocode('foo'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The MaxMind does not support street addresses. + * @expectedExceptionMessage The MaxMind provider does not support street addresses, only IP addresses. */ public function testGetGeocodedDataWithNull() { $provider = new MaxMind($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The MaxMind does not support street addresses. + * @expectedExceptionMessage The MaxMind provider does not support street addresses, only IP addresses. */ public function testGetGeocodedDataWithEmpty() { $provider = new MaxMind($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The MaxMind does not support street addresses. + * @expectedExceptionMessage The MaxMind provider does not support street addresses, only IP addresses. */ public function testGetGeocodedDataWithAddress() { $provider = new MaxMind($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new MaxMind($this->getMockAdapter($this->never()), 'api_key'); - $result = $provider->getGeocodedData('127.0.0.1'); + $results = $provider->geocode('127.0.0.1'); - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); + $this->assertCount(1, $results); - $result = $result[0]; - $this->assertInternalType('array', $result); - $this->assertArrayNotHasKey('latitude', $result); - $this->assertArrayNotHasKey('longitude', $result); - $this->assertArrayNotHasKey('postalCode', $result); - $this->assertArrayNotHasKey('timezone', $result); - - $this->assertEquals('localhost', $result['locality']); - $this->assertEquals('localhost', $result['region']); - $this->assertEquals('localhost', $result['county']); - $this->assertEquals('localhost', $result['country']); + $result = $results[0]->toArray(); + $this->assertEquals('Localhost', $result['locality']); + $this->assertEquals('Localhost', $result['region']); + $this->assertEquals('Localhost', $result['county']); + $this->assertEquals('Localhost', $result['country']); } public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new MaxMind($this->getMockAdapter($this->never()), 'api_key'); - $result = $provider->getGeocodedData('::1'); + $results = $provider->geocode('::1'); - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); + $this->assertCount(1, $results); - $result = $result[0]; - $this->assertInternalType('array', $result); - $this->assertArrayNotHasKey('latitude', $result); - $this->assertArrayNotHasKey('longitude', $result); - $this->assertArrayNotHasKey('postalCode', $result); - $this->assertArrayNotHasKey('timezone', $result); - - $this->assertEquals('localhost', $result['locality']); - $this->assertEquals('localhost', $result['region']); - $this->assertEquals('localhost', $result['county']); - $this->assertEquals('localhost', $result['country']); + $result = $results[0]->toArray(); + $this->assertEquals('Localhost', $result['locality']); + $this->assertEquals('Localhost', $result['region']); + $this->assertEquals('Localhost', $result['county']); + $this->assertEquals('Localhost', $result['country']); } /** @@ -101,7 +87,7 @@ public function testGetGeocodedDataWithLocalhostIPv6() public function testGetGeocodedDataWithRealIPv4AndNotSupportedService() { $provider = new MaxMind($this->getMockAdapter(), 'api_key', 'foo'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** @@ -111,67 +97,62 @@ public function testGetGeocodedDataWithRealIPv4AndNotSupportedService() public function testGetGeocodedDataWithRealIPv6AndNotSupportedService() { $provider = new MaxMind($this->getMockAdapter(), 'api_key', 12345); - $provider->getGeocodedData('::ffff:74.200.247.59'); + $provider->geocode('::ffff:74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geoip.maxmind.com/f?l=api_key&i=74.200.247.59 + * @expectedExceptionMessage Could not execute query "http://geoip.maxmind.com/f?l=api_key&i=74.200.247.59". */ public function testGetGeocodedDataWithRealIPv4GetsNullContent() { $provider = new MaxMind($this->getMockAdapterReturns(null), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geoip.maxmind.com/f?l=api_key&i=74.200.247.59 + * @expectedExceptionMessage Could not execute query "http://geoip.maxmind.com/f?l=api_key&i=74.200.247.59". */ public function testGetGeocodedDataWithRealIPv4GetsEmptyContent() { $provider = new MaxMind($this->getMockAdapterReturns(''), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } public function testGetGeocodedDataWithRealIPv4GetsFakeContentFormattedEmpty() { $provider = new MaxMind($this->getMockAdapterReturns(',,,,,,,,,'), 'api_key'); - $result = $provider->getGeocodedData('74.200.247.59'); - - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); - - $result = $result[0]; - $this->assertInternalType('array', $result); - $this->assertNull($result['country']); - $this->assertNull($result['countryCode']); - $this->assertNull($result['regionCode']); - $this->assertNull($result['locality']); - $this->assertNull($result['latitude']); - $this->assertNull($result['longitude']); - $this->assertNull($result['postalCode']); - $this->assertNull($result['bounds']); - $this->assertNull($result['streetNumber']); - $this->assertNull($result['streetName']); - $this->assertNull($result['subLocality']); - $this->assertNull($result['county']); - $this->assertNull($result['countyCode']); - $this->assertNull($result['region']); - $this->assertNull($result['timezone']); + $results = $provider->geocode('74.200.247.59'); + + $this->assertCount(1, $results); + + $result = $results[0]->toArray(); + $this->assertEmpty($result['country']); + $this->assertEmpty($result['countryCode']); + $this->assertEmpty($result['regionCode']); + $this->assertEmpty($result['locality']); + $this->assertEmpty($result['latitude']); + $this->assertEmpty($result['longitude']); + $this->assertEmpty($result['postalCode']); + $this->assertEmpty($result['streetNumber']); + $this->assertEmpty($result['streetName']); + $this->assertEmpty($result['subLocality']); + $this->assertEmpty($result['county']); + $this->assertEmpty($result['countyCode']); + $this->assertEmpty($result['region']); + $this->assertEmpty($result['timezone']); } public function testGetGeocodedDataWithRealIPv4GetsFakeContent() { $provider = new MaxMind($this->getMockAdapterReturns( 'US,TX,Plano,75093,33.034698486328,-96.813400268555,,,,'), 'api_key'); - $result = $provider->getGeocodedData('74.200.247.59'); + $results = $provider->geocode('74.200.247.59'); - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); + $this->assertCount(1, $results); - $result = $result[0]; - $this->assertInternalType('array', $result); + $result = $results[0]->toArray(); $this->assertEquals('United States', $result['country']); $this->assertEquals('US', $result['countryCode']); $this->assertEquals('TX', $result['regionCode']); @@ -179,33 +160,30 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContent() $this->assertEquals(75093, $result['postalCode']); $this->assertEquals(33.034698486328, $result['latitude'], '', 0.0001); $this->assertEquals(-96.813400268555, $result['longitude'], '', 0.0001); - $this->assertNull($result['timezone']); - $this->assertNull($result['bounds']); - $this->assertNull($result['streetNumber']); - $this->assertNull($result['streetName']); - $this->assertNull($result['subLocality']); - $this->assertNull($result['county']); - $this->assertNull($result['countyCode']); - $this->assertNull($result['region']); - $this->assertNull($result['timezone']); + $this->assertEmpty($result['timezone']); + $this->assertEmpty($result['streetNumber']); + $this->assertEmpty($result['streetName']); + $this->assertEmpty($result['subLocality']); + $this->assertEmpty($result['county']); + $this->assertEmpty($result['countyCode']); + $this->assertEmpty($result['region']); + $this->assertEmpty($result['timezone']); $provider2 = new MaxMind($this->getMockAdapterReturns('FR,,,,,,,,,'), 'api_key'); - $result2 = $provider2->getGeocodedData('74.200.247.59'); - $this->assertEquals('France', $result2[0]['country']); + $result2 = $provider2->geocode('74.200.247.59'); + $this->assertEquals('France', $result2[0]->getCountry()->getName()); $provider3 = new MaxMind($this->getMockAdapterReturns('GB,,,,,,,,,'), 'api_key'); - $result3 = $provider3->getGeocodedData('74.200.247.59'); - $this->assertEquals('United Kingdom', $result3[0]['country']); + $result3 = $provider3->geocode('74.200.247.59'); + $this->assertEquals('United Kingdom', $result3[0]->getCountry()->getName()); $provider4 = new MaxMind($this->getMockAdapterReturns( 'US,CA,San Francisco,94110,37.748402,-122.415604,807,415,"Layered Technologies","Automattic"'), 'api_key'); - $result = $provider4->getGeocodedData('74.200.247.59'); + $results = $provider4->geocode('74.200.247.59'); - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); + $this->assertCount(1, $results); - $result = $result[0]; - $this->assertInternalType('array', $result); + $result = $results[0]->toArray(); $this->assertEquals('United States', $result['country']); $this->assertEquals('US', $result['countryCode']); $this->assertEquals('CA', $result['regionCode']); @@ -213,14 +191,13 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContent() $this->assertEquals(94110, $result['postalCode']); $this->assertEquals(37.748402, $result['latitude'], '', 0.0001); $this->assertEquals(-122.415604, $result['longitude'], '', 0.0001); - $this->assertNull($result['bounds']); - $this->assertNull($result['streetNumber']); - $this->assertNull($result['streetName']); - $this->assertNull($result['subLocality']); - $this->assertNull($result['county']); - $this->assertNull($result['countyCode']); - $this->assertNull($result['region']); - $this->assertNull($result['timezone']); + $this->assertEmpty($result['streetNumber']); + $this->assertEmpty($result['streetName']); + $this->assertEmpty($result['subLocality']); + $this->assertEmpty($result['county']); + $this->assertEmpty($result['countyCode']); + $this->assertEmpty($result['region']); + $this->assertEmpty($result['timezone']); } /** @@ -230,7 +207,7 @@ public function testGetGeocodedDataWithRealIPv4GetsFakeContent() public function testGetGeocodedDataWithRealIPv4AndInvalidApiKeyGetsFakeContent() { $provider = new MaxMind($this->getMockAdapterReturns(',,,,,,,,,,INVALID_LICENSE_KEY'), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** @@ -241,7 +218,7 @@ public function testGetGeocodedOmniServiceDataWithRealIPv6AndInvalidApiKeyGetsFa { $provider = new MaxMind($this->getMockAdapterReturns(',,,,,,,,,,,,,,,,,,,,,,,,INVALID_LICENSE_KEY'), 'api_key', MaxMind::OMNI_SERVICE); - $provider->getGeocodedData('::ffff:74.200.247.59'); + $provider->geocode('::ffff:74.200.247.59'); } /** @@ -251,7 +228,7 @@ public function testGetGeocodedOmniServiceDataWithRealIPv6AndInvalidApiKeyGetsFa public function testGetGeocodedDataWithRealIPv4AndInvalidApiKey() { $provider = new MaxMind($this->getAdapter(), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** @@ -261,7 +238,7 @@ public function testGetGeocodedDataWithRealIPv4AndInvalidApiKey() public function testGetGeocodedDataWithRealIPv4AndInvalidApiKeyGetsFakeContent2() { $provider = new MaxMind($this->getMockAdapterReturns(',,,,,,,,,,LICENSE_REQUIRED'), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** @@ -272,38 +249,38 @@ public function testGetGeocodedOmniServiceDataWithRealIPv6AndInvalidApiKeyGetsFa { $provider = new MaxMind($this->getMockAdapterReturns(',,,,,,,,,,,,,,,,,,,,,,,INVALID_LICENSE_KEY'), 'api_key', MaxMind::OMNI_SERVICE); - $provider->getGeocodedData('::ffff:74.200.247.59'); + $provider->geocode('::ffff:74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not retrieve informations for the ip address provided. + * @expectedExceptionMessage Could not retrieve information for the supplied IP address. */ public function testGetGeocodedDataWithRealIPv4GetsFakeContentWithIpNotFound() { $provider = new MaxMind($this->getMockAdapterReturns(',,,,,,,,,,IP_NOT_FOUND'), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not retrieve informations for the ip address provided. + * @expectedExceptionMessage Could not retrieve information for the supplied IP address. */ public function testGetGeocodedOmniServiceDataWithRealIPv6GetsFakeContentWithIpNotFound() { $provider = new MaxMind($this->getMockAdapterReturns(',,,,,,,,,,,,,,,,,,,,,,,IP_NOT_FOUND'), 'api_key', MaxMind::OMNI_SERVICE); - $provider->getGeocodedData('::fff:74.200.247.59'); + $provider->geocode('::fff:74.200.247.59'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Invalid result returned by provider. + * @expectedExceptionMessage Invalid result returned by the API. */ public function testGetGeocodedDataGetsFakeContentWithInvalidData() { $provider = new MaxMind($this->getMockAdapterReturns(',,,,,,,,,,'), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } public function testGetGeocodedServiceWithRealIPv4() @@ -313,13 +290,11 @@ public function testGetGeocodedServiceWithRealIPv4() } $provider = new MaxMind($this->getAdapter(), $_SERVER['MAXMIND_API_KEY']); - $result = $provider->getGeocodedData('74.200.247.159'); + $result = $provider->geocode('74.200.247.159'); - $this->assertInternalType('array', $result); $this->assertCount(1, $result); $result = $result[0]; - $this->assertInternalType('array', $result); $this->assertEquals(33.034698, $result['latitude'], '', 0.1); $this->assertEquals(-96.813400, $result['longitude'], '', 0.1); $this->assertEquals('Plano', $result['locality']); @@ -345,13 +320,11 @@ public function testGetGeocodedDataOmniServiceWithRealIPv4() $provider = new MaxMind($this->getAdapter(), $_SERVER['MAXMIND_API_KEY'], MaxMind::OMNI_SERVICE); - $result = $provider->getGeocodedData('74.200.247.159'); + $result = $provider->geocode('74.200.247.159'); - $this->assertInternalType('array', $result); $this->assertCount(1, $result); $result = $result[0]; - $this->assertInternalType('array', $result); $this->assertEquals(33.0347, $result['latitude'], '', 0.1); $this->assertEquals(-96.8134, $result['longitude'], '', 0.1); $this->assertEquals('Plano', $result['locality']); @@ -376,13 +349,11 @@ public function testGetGeocodedDataWithRealIPv6() } $provider = new MaxMind($this->getAdapter(), $_SERVER['MAXMIND_API_KEY']); - $result = $provider->getGeocodedData('66.147.244.214'); + $result = $provider->geocode('66.147.244.214'); - $this->assertInternalType('array', $result); $this->assertCount(1, $result); - $result = $result[0]; - $this->assertInternalType('array', $result); + $result = $result[0]->toArray(); $this->assertEquals(40.2181, $result['latitude'], '', 0.1); $this->assertEquals(-111.6133, $result['longitude'], '', 0.1); $this->assertEquals('Provo', $result['locality']); @@ -408,7 +379,7 @@ public function testGetGeocodedDataOmniServiceWithRealIPv6WithSsl() $provider = new MaxMind($this->getAdapter(), $_SERVER['MAXMIND_API_KEY'], MaxMind::OMNI_SERVICE, true); - $result = $provider->getGeocodedData('::ffff:66.147.244.214'); + $result = $provider->geocode('::ffff:66.147.244.214'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -434,11 +405,11 @@ public function testGetGeocodedDataOmniServiceWithRealIPv6WithSsl() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The MaxMind is not able to do reverse geocoding. + * @expectedExceptionMessage The MaxMind provider is not able to do reverse geocoding. */ public function testGetReverseData() { $provider = new MaxMind($this->getMockAdapter($this->never()), 'api_key'); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } } diff --git a/tests/Geocoder/Tests/Provider/OpenCageTest.php b/tests/Geocoder/Tests/Provider/OpenCageTest.php index 928cc6ec0..027effc22 100644 --- a/tests/Geocoder/Tests/Provider/OpenCageTest.php +++ b/tests/Geocoder/Tests/Provider/OpenCageTest.php @@ -18,32 +18,32 @@ public function testGetName() /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not find results for given query: http://api.opencagedata.com/geocode/v1/json?key=api_key&query=foobar&limit=5 + * @expectedExceptionMessage Could not find results for query "http://api.opencagedata.com/geocode/v1/json?key=api_key&query=foobar&limit=5&pretty=1". */ public function testGetGeocodedData() { $provider = new OpenCage($this->getMockAdapterReturns('{}'), 'api_key'); - $provider->getGeocodedData('foobar'); + $provider->geocode('foobar'); } /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not find results for given query: https://api.opencagedata.com/geocode/v1/json?key=api_key&query=foobar&limit=5 + * @expectedExceptionMessage Could not find results for query "https://api.opencagedata.com/geocode/v1/json?key=api_key&query=foobar&limit=5&pretty=1". */ public function testSslSchema() { $provider = new OpenCage($this->getMockAdapterReturns('{}'), 'api_key', true); - $provider->getGeocodedData('foobar'); + $provider->geocode('foobar'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query: http://api.opencagedata.com/geocode/v1/json?key=api_key&query=10+avenue+Gambetta%2C+Paris%2C+France&limit=5 + * @expectedExceptionMessage Could not execute query "http://api.opencagedata.com/geocode/v1/json?key=api_key&query=10+avenue+Gambetta%2C+Paris%2C+France&limit=5&pretty=1". */ public function testGetGeocodedDataWithAddressGetsNullContent() { $provider = new OpenCage($this->getMockAdapterReturns(null), 'api_key'); - $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $provider->geocode('10 avenue Gambetta, Paris, France'); } public function testGetGeocodedDataWithRealAddress() @@ -53,7 +53,7 @@ public function testGetGeocodedDataWithRealAddress() } $provider = new OpenCage($this->getAdapter(), $_SERVER['OPENCAGE_API_KEY']); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); $this->assertInternalType('array', $results); $this->assertCount(3, $results); @@ -87,7 +87,7 @@ public function testGetReversedData() } $provider = new OpenCage($this->getMockAdapter(), $_SERVER['OPENCAGE_API_KEY']); - $provider->getReversedData(array(1, 2)); + $provider->reverse(array(1, 2)); } public function testGetReversedDataWithRealCoordinates() @@ -97,7 +97,7 @@ public function testGetReversedDataWithRealCoordinates() } $provider = new OpenCage($this->getAdapter(), $_SERVER['OPENCAGE_API_KEY']); - $result = $provider->getReversedData(array(54.0484068, -2.7990345)); + $result = $provider->reverse(54.0484068, -2.7990345); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -128,7 +128,7 @@ public function testGetGeocodedDataWithCity() } $provider = new OpenCage($this->getAdapter(), $_SERVER['OPENCAGE_API_KEY']); - $results = $provider->getGeocodedData('Hanover'); + $results = $provider->geocode('Hanover'); $this->assertInternalType('array', $results); $this->assertCount(5, $results); @@ -171,7 +171,7 @@ public function testGetGeocodedDataWithCityDistrict() } $provider = new OpenCage($this->getAdapter(), $_SERVER['OPENCAGE_API_KEY']); - $result = $provider->getGeocodedData('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'); + $result = $provider->geocode('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'); $this->assertInternalType('array', $result); $this->assertCount(2, $result); @@ -199,7 +199,7 @@ public function testGetGeocodedDataWithLocale() } $provider = new OpenCage($this->getAdapter(), $_SERVER['OPENCAGE_API_KEY'], true, 'es'); - $result = $provider->getGeocodedData('London'); + $result = $provider->geocode('London'); $this->assertInternalType('array', $result); $this->assertCount(5, $result); @@ -215,42 +215,42 @@ public function testGetGeocodedDataWithLocale() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The OpenCage does not support IP addresses. + * @expectedExceptionMessage The OpenCage provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new OpenCage($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('127.0.0.1'); + $provider->geocode('127.0.0.1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The OpenCage does not support IP addresses. + * @expectedExceptionMessage The OpenCage provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new OpenCage($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The OpenCage does not support IP addresses. + * @expectedExceptionMessage The OpenCage provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithRealIPv4() { $provider = new OpenCage($this->getAdapter(), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The OpenCage does not support IP addresses. + * @expectedExceptionMessage The OpenCage provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithRealIPv6() { $provider = new OpenCage($this->getAdapter(), 'api_key'); - $provider->getGeocodedData('::ffff:74.200.247.59'); + $provider->geocode('::ffff:74.200.247.59'); } } diff --git a/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php b/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php index a183fcb07..9491ce02e 100644 --- a/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php +++ b/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php @@ -16,12 +16,14 @@ public function testGetName() public function testGetGeocodedDataWithRealAddress() { $provider = new OpenStreetMap($this->getAdapter()); - $results = $provider->getGeocodedData('Paris'); + $results = $provider->geocode('Paris'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(48.8565056, $results[0]['latitude'], '', 0.01); $this->assertEquals(2.3521334, $results[0]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[0]['bounds']); @@ -43,7 +45,6 @@ public function testGetGeocodedDataWithRealAddress() $this->assertEquals('France métropolitaine', $results[0]['country']); $this->assertEquals('FR', $results[0]['countryCode']); - $this->assertInternalType('array', $results[1]); $this->assertEquals(48.8588408, $results[1]['latitude'], '', 0.01); $this->assertEquals(2.32003465529896, $results[1]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[1]['bounds']); @@ -65,7 +66,6 @@ public function testGetGeocodedDataWithRealAddress() $this->assertEquals('France métropolitaine', $results[1]['country']); $this->assertEquals('FR', $results[1]['countryCode']); - $this->assertInternalType('array', $results[2]); $this->assertEquals(35.28687645, $results[2]['latitude'], '', 0.01); $this->assertEquals(-93.7354879210082, $results[2]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[2]['bounds']); @@ -87,7 +87,6 @@ public function testGetGeocodedDataWithRealAddress() $this->assertEquals('United States of America', $results[2]['country']); $this->assertEquals('US', $results[2]['countryCode']); - $this->assertInternalType('array', $results[3]); $this->assertEquals(33.6751155, $results[3]['latitude'], '', 0.01); $this->assertEquals(-95.5502662477703, $results[3]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[3]['bounds']); @@ -109,7 +108,6 @@ public function testGetGeocodedDataWithRealAddress() $this->assertEquals('United States of America', $results[3]['country']); $this->assertEquals('US', $results[3]['countryCode']); - $this->assertInternalType('array', $results[4]); $this->assertEquals(38.2097987, $results[4]['latitude'], '', 0.01); $this->assertEquals(-84.2529869, $results[4]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[4]['bounds']); @@ -135,12 +133,14 @@ public function testGetGeocodedDataWithRealAddress() public function testGetGeocodedDataWithRealAddressWithLocale() { $provider = new OpenStreetMap($this->getAdapter(), 'fr_FR'); - $results = $provider->getGeocodedData('10 allée Evariste Galois, Clermont ferrand'); + $results = $provider->geocode('10 allée Evariste Galois, Clermont ferrand'); - $this->assertInternalType('array', $results); $this->assertCount(2, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(45.7586841, $results[0]['latitude'], '', 0.01); $this->assertEquals(3.1354075, $results[0]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[0]['bounds']); @@ -162,7 +162,6 @@ public function testGetGeocodedDataWithRealAddressWithLocale() $this->assertEquals('France', $results[0]['country']); $this->assertEquals('FR', $results[0]['countryCode']); - $this->assertInternalType('array', $results[1]); $this->assertEquals(45.7586841, $results[1]['latitude'], '', 0.01); $this->assertEquals(3.1354075, $results[1]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[1]['bounds']); @@ -188,13 +187,11 @@ public function testGetGeocodedDataWithRealAddressWithLocale() public function testGetReversedDataWithRealCoordinates() { $provider = new OpenStreetMap($this->getAdapter()); - $results = $provider->getReversedData(array('60.4539471728726', '22.2567841926781')); + $results = $provider->reverse(60.4539471728726, 22.2567841926781); - $this->assertInternalType('array', $results); $this->assertCount(1, $results); - $result = $results[0]; - $this->assertInternalType('array', $result); + $result = $results[0]->toArray(); $this->assertEquals(60.4539, $result['latitude'], '', 0.001); $this->assertEquals(22.2568, $result['longitude'], '', 0.001); $this->assertNull($result['bounds']); @@ -210,23 +207,25 @@ public function testGetReversedDataWithRealCoordinates() /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://nominatim.openstreetmap.org/search?q=Hammm&format=xml&addressdetails=1&limit=5 + * @expectedExceptionMessage Could not execute query "http://nominatim.openstreetmap.org/search?q=Hammm&format=xml&addressdetails=1&limit=5". */ public function testGetGeocodedDataWithUnknownCity() { $provider = new OpenStreetMap($this->getAdapter()); - $provider->getGeocodedData('Hammm'); + $provider->geocode('Hammm'); } public function testGetReversedDataWithRealCoordinatesWithLocale() { $provider = new OpenStreetMap($this->getAdapter(), 'de_DE'); - $results = $provider->getGeocodedData('Kalbacher Hauptstraße, 60437 Frankfurt, Germany'); + $results = $provider->geocode('Kalbacher Hauptstraße, 60437 Frankfurt, Germany'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(50.1856803, $results[0]['latitude'], '', 0.01); $this->assertEquals(8.6506285, $results[0]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[0]['bounds']); @@ -248,7 +247,6 @@ public function testGetReversedDataWithRealCoordinatesWithLocale() $this->assertEquals('Deutschland', $results[0]['country']); $this->assertEquals('DE', $results[0]['countryCode']); - $this->assertInternalType('array', $results[1]); $this->assertEquals(50.1845911, $results[1]['latitude'], '', 0.01); $this->assertEquals(8.6540194, $results[1]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[1]['bounds']); @@ -270,7 +268,6 @@ public function testGetReversedDataWithRealCoordinatesWithLocale() $this->assertEquals('Deutschland', $results[1]['country']); $this->assertEquals('DE', $results[1]['countryCode']); - $this->assertInternalType('array', $results[2]); $this->assertEquals(50.1862884, $results[2]['latitude'], '', 0.01); $this->assertEquals(8.6493167, $results[2]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[2]['bounds']); @@ -292,7 +289,6 @@ public function testGetReversedDataWithRealCoordinatesWithLocale() $this->assertEquals('Deutschland', $results[2]['country']); $this->assertEquals('DE', $results[2]['countryCode']); - $this->assertInternalType('array', $results[3]); $this->assertEquals(50.1861344, $results[3]['latitude'], '', 0.01); $this->assertEquals(8.649578, $results[3]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[3]['bounds']); @@ -318,43 +314,38 @@ public function testGetReversedDataWithRealCoordinatesWithLocale() public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new OpenStreetMap($this->getMockAdapter($this->never())); - $result = $provider->getGeocodedData('127.0.0.1'); - - $this->assertInternalType('array', $result); - $this->assertCount(1, $result); - - $result = $result[0]; - $this->assertInternalType('array', $result); - $this->assertArrayNotHasKey('latitude', $result); - $this->assertArrayNotHasKey('longitude', $result); - $this->assertArrayNotHasKey('postalCode', $result); - $this->assertArrayNotHasKey('timezone', $result); - - $this->assertEquals('localhost', $result['locality']); - $this->assertEquals('localhost', $result['region']); - $this->assertEquals('localhost', $result['county']); - $this->assertEquals('localhost', $result['country']); + $results = $provider->geocode('127.0.0.1'); + + $this->assertCount(1, $results); + + $result = $results[0]->toArray(); + $this->assertEquals('Localhost', $result['locality']); + $this->assertEquals('Localhost', $result['region']); + $this->assertEquals('Localhost', $result['county']); + $this->assertEquals('Localhost', $result['country']); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The Nominatim does not support IPv6 addresses. + * @expectedExceptionMessage The OpenStreetMap provider does not support IPv6 addresses. */ public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new OpenStreetMap($this->getMockAdapter($this->never())); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } public function testGetGeocodedDataWithRealIPv4() { $provider = new OpenStreetMap($this->getAdapter()); - $results = $provider->getGeocodedData('88.188.221.14'); + $results = $provider->geocode('88.188.221.14'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(43.6189768, $results[0]['latitude'], '', 0.01); $this->assertEquals(1.4564493, $results[0]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[0]['bounds']); @@ -375,22 +366,19 @@ public function testGetGeocodedDataWithRealIPv4() $this->assertNull($results[0]['regionCode']); $this->assertEquals('France métropolitaine', $results[0]['country']); $this->assertEquals('FR', $results[0]['countryCode']); - - $this->assertInternalType('array', $results[1]); - $this->assertInternalType('array', $results[2]); - $this->assertInternalType('array', $results[3]); - $this->assertInternalType('array', $results[4]); } public function testGetGeocodedDataWithRealIPv4WithLocale() { $provider = new OpenStreetMap($this->getAdapter(), 'da_DK'); - $results = $provider->getGeocodedData('88.188.221.14'); + $results = $provider->geocode('88.188.221.14'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(43.6155351, $results[0]['latitude'], '', 0.01); $this->assertEquals(1.4525647, $results[0]['longitude'], '', 0.01); $this->assertArrayHasKey('south', $results[0]['bounds']); @@ -411,46 +399,41 @@ public function testGetGeocodedDataWithRealIPv4WithLocale() $this->assertNull($results[0]['regionCode']); $this->assertEquals('Frankrig', $results[0]['country']); $this->assertEquals('FR', $results[0]['countryCode']); - - $this->assertInternalType('array', $results[1]); - $this->assertInternalType('array', $results[2]); - $this->assertInternalType('array', $results[3]); - $this->assertInternalType('array', $results[4]); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The Nominatim does not support IPv6 addresses. + * @expectedExceptionMessage The OpenStreetMap provider does not support IPv6 addresses. */ public function testGetGeocodedDataWithRealIPv6() { $provider = new OpenStreetMap($this->getAdapter()); - $provider->getGeocodedData('::ffff:88.188.221.14'); + $provider->geocode('::ffff:88.188.221.14'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not resolve address "Läntinen Pitkäkatu 35, Turku" + * @expectedExceptionMessage Could not execute query "http://nominatim.openstreetmap.org/search?q=L%C3%A4ntinen+Pitk%C3%A4katu+35%2C+Turku&format=xml&addressdetails=1&limit=5". */ public function testGetGeocodedDataWithAddressGetsNullContent() { $provider = new OpenStreetMap($this->getMockAdapterReturns(null)); - $provider->getGeocodedData('Läntinen Pitkäkatu 35, Turku'); + $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://nominatim.openstreetmap.org/search?q=L%C3%A4ntinen+Pitk%C3%A4katu+35%2C+Turku&format=xml&addressdetails=1&limit=5 + * @expectedExceptionMessage Could not execute query "http://nominatim.openstreetmap.org/search?q=L%C3%A4ntinen+Pitk%C3%A4katu+35%2C+Turku&format=xml&addressdetails=1&limit=5". */ public function testGetGeocodedDataWithAddressGetsEmptyContent() { $provider = new OpenStreetMap($this->getMockAdapterReturns('')); - $provider->getGeocodedData('Läntinen Pitkäkatu 35, Turku'); + $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://nominatim.openstreetmap.org/search?q=L%C3%A4ntinen+Pitk%C3%A4katu+35%2C+Turku&format=xml&addressdetails=1&limit=5 + * @expectedExceptionMessage Could not execute query "http://nominatim.openstreetmap.org/search?q=L%C3%A4ntinen+Pitk%C3%A4katu+35%2C+Turku&format=xml&addressdetails=1&limit=5". */ public function testGetGeocodedDataWithAddressGetsEmptyXML() { @@ -458,32 +441,32 @@ public function testGetGeocodedDataWithAddressGetsEmptyXML() XML; $provider = new OpenStreetMap($this->getMockAdapterReturns($emptyXML)); - $provider->getGeocodedData('Läntinen Pitkäkatu 35, Turku'); + $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Unable to resolve the coordinates 60.4539471728726, 22.2567841926781 + * @expectedExceptionMessage Unable to find results for coordinates [ 60.453947, 22.256784 ]. */ public function testGetReversedDataWithCoordinatesGetsNullContent() { $provider = new OpenStreetMap($this->getMockAdapterReturns(null)); - $provider->getReversedData(array('60.4539471728726', '22.2567841926781')); + $provider->reverse(60.4539471728726, 22.2567841926781); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not resolve coordinates 60.4539471728726, 22.2567841926781 + * @expectedExceptionMessage Unable to find results for coordinates [ 60.453947, 22.256784 ]. */ public function testGetReversedDataWithCoordinatesGetsEmptyContent() { $provider = new OpenStreetMap($this->getMockAdapterReturns('')); - $provider->getReversedData(array('60.4539471728726', '22.2567841926781')); + $provider->reverse(60.4539471728726, 22.2567841926781); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not resolve coordinates -80.000000, -170.000000 + * @expectedExceptionMessage Unable to find results for coordinates [ -80.000000, -170.000000 ]. */ public function testGetReversedDataWithCoordinatesGetsError() { @@ -494,14 +477,14 @@ public function testGetReversedDataWithCoordinatesGetsError() XML; $provider = new OpenStreetMap($this->getMockAdapterReturns($errorXml)); - $provider->getReversedData(array('-80.000000', '-170.000000')); + $provider->reverse(-80.000000, -170.000000); } public function testGetNodeStreetName() { $provider = new OpenStreetMap($this->getAdapter(), 'fr_FR'); - $results = $provider->getReversedData(array(48.86, 2.35)); + $results = $provider->reverse(48.86, 2.35); - $this->assertEquals('Rue Quincampoix', $results[0]['streetName']); + $this->assertEquals('Rue Quincampoix', $results[0]->getStreetName()); } } diff --git a/tests/Geocoder/Tests/Provider/TomTomTest.php b/tests/Geocoder/Tests/Provider/TomTomTest.php index adadeeca1..48ca24726 100644 --- a/tests/Geocoder/Tests/Provider/TomTomTest.php +++ b/tests/Geocoder/Tests/Provider/TomTomTest.php @@ -16,57 +16,57 @@ public function testGetName() /** * @expectedException \RuntimeException * @expectedException \Geocoder\Exception\InvalidCredentials - * @expectedExceptionMessage No Geocoding API Key provided + * @expectedExceptionMessage No API Key provided. */ public function testGetGeocodedDataWithNullApiKey() { $provider = new TomTom($this->getMockAdapter($this->never()), null); - $provider->getGeocodedData('foo'); + $provider->geocode('foo'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=&maxResults=5 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=&maxResults=5". */ public function testGetGeocodedDataWithNull() { $provider = new TomTom($this->getMockAdapter(), 'api_key'); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=&maxResults=5 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=&maxResults=5". */ public function testGetGeocodedDataWithEmpty() { $provider = new TomTom($this->getMockAdapter(), 'api_key'); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=Tagensvej%2047%2C%202200%20K%C3%B8benhavn%20N&maxResults=5 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=Tagensvej%2047%2C%202200%20K%C3%B8benhavn%20N&maxResults=5". */ public function testGetGeocodedDataWithAddressContentReturnNull() { $provider = new TomTom($this->getMockAdapterReturns(null), 'api_key'); - $provider->getGeocodedData('Tagensvej 47, 2200 København N'); + $provider->geocode('Tagensvej 47, 2200 København N'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=Tagensvej%2047%2C%202200%20K%C3%B8benhavn%20N&maxResults=5 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=Tagensvej%2047%2C%202200%20K%C3%B8benhavn%20N&maxResults=5". */ public function testGetGeocodedDataWithAddress() { $provider = new TomTom($this->getMockAdapter(), 'api_key'); - $provider->getGeocodedData('Tagensvej 47, 2200 København N'); + $provider->geocode('Tagensvej 47, 2200 København N'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=foo&maxResults=5 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/geocoding/geocode?key=api_key&query=foo&maxResults=5". */ public function testGetGeocodedDataNoResult() { @@ -75,7 +75,7 @@ public function testGetGeocodedDataNoResult() XML; $provider = new TomTom($this->getMockAdapterReturns($noResult), 'api_key'); - $provider->getGeocodedData('foo'); + $provider->geocode('foo'); } public function testGetGeocodedDataWithRealAddress() @@ -85,7 +85,7 @@ public function testGetGeocodedDataWithRealAddress() } $provider = new TomTom($this->getAdapter(), $_SERVER['TOMTOM_GEOCODING_KEY']); - $result = $provider->getGeocodedData('Tagensvej 47, 2200 København N'); + $result = $provider->geocode('Tagensvej 47, 2200 København N'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -114,7 +114,7 @@ public function testGetGeocodedDataWithRealAddressWithFrenchLocale() } $provider = new TomTom($this->getAdapter(), $_SERVER['TOMTOM_GEOCODING_KEY'], 'fr_FR'); - $result = $provider->getGeocodedData('Tagensvej 47, 2200 København N'); + $result = $provider->geocode('Tagensvej 47, 2200 København N'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -143,7 +143,7 @@ public function testGetGeocodedDataWithRealAddressWithSwidishLocale() } $provider = new TomTom($this->getAdapter(), $_SERVER['TOMTOM_GEOCODING_KEY'], 'sv-SE'); - $result = $provider->getGeocodedData('Tagensvej 47, 2200 København N'); + $result = $provider->geocode('Tagensvej 47, 2200 København N'); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -172,7 +172,7 @@ public function testGetGeocodedDataWithRealAddressReturnsMultipleResults() } $provider = new TomTom($this->getAdapter(), $_SERVER['TOMTOM_GEOCODING_KEY']); - $results = $provider->getGeocodedData('Paris'); + $results = $provider->geocode('Paris'); $this->assertInternalType('array', $results); $this->assertCount(5, $results); @@ -227,42 +227,42 @@ public function testGetGeocodedDataWithRealAddressReturnsMultipleResults() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The TomTom does not support IP addresses. + * @expectedExceptionMessage The TomTom provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new TomTom($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('127.0.0.1'); + $provider->geocode('127.0.0.1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The TomTom does not support IP addresses. + * @expectedExceptionMessage The TomTom provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new TomTom($this->getMockAdapter($this->never()), 'api_key'); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The TomTom does not support IP addresses. + * @expectedExceptionMessage The TomTom provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithIPv4() { $provider = new TomTom($this->getAdapter(), 'api_key'); - $provider->getGeocodedData('74.200.247.59'); + $provider->geocode('74.200.247.59'); } /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The TomTom does not support IP addresses. + * @expectedExceptionMessage The TomTom provider does not support IP addresses, only street addresses. */ public function testGetGeocodedDataWithIPv6() { $provider = new TomTom($this->getAdapter(), 'api_key'); - $provider->getGeocodedData('::ffff:74.200.247.59'); + $provider->geocode('::ffff:74.200.247.59'); } /** @@ -272,42 +272,42 @@ public function testGetGeocodedDataWithIPv6() public function testGetReversedDataWithoutApiKey() { $provider = new TomTom($this->getMockAdapter($this->never()), null); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } /** * @expectedException Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=api_key&point=1.000000,2.000000 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=api_key&point=1.000000,2.000000". */ public function testGetReversedData() { $provider = new TomTom($this->getMockAdapter(), 'api_key'); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=api_key&point=48.863216,2.388772 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=api_key&point=48.863216,2.388772". */ public function testGetReversedDataWithCoordinatesContentReturnNull() { $provider = new TomTom($this->getMockAdapterReturns(null), 'api_key'); - $provider->getReversedData(array(48.86321648955345, 2.3887719959020615)); + $provider->reverse(48.86321648955345, 2.3887719959020615); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=api_key&point=60.453947,22.256784 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=api_key&point=60.453947,22.256784". */ public function testGetReversedDataWithCoordinatesGetsEmptyContent() { $provider = new TomTom($this->getMockAdapterReturns(''), 'api_key'); - $provider->getReversedData(array('60.4539471728726', '22.2567841926781')); + $provider->reverse('60.4539471728726', '22.2567841926781'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=api_key&point=1.000000,2.000000 + * @expectedExceptionMessage Could not execute query "https://api.tomtom.com/lbs/services/reverseGeocode/3/xml?key=api_key&point=1.000000,2.000000". */ public function testGetReversedDataError400() { @@ -316,7 +316,7 @@ public function testGetReversedDataError400() XML; $provider = new TomTom($this->getMockAdapterReturns($error400), 'api_key'); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } /** @@ -330,7 +330,7 @@ public function testGetReversedDataError403() XML; $provider = new TomTom($this->getMockAdapterReturns($error403), 'api_key'); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } public function testGetReversedDataWithRealCoordinates() @@ -340,7 +340,7 @@ public function testGetReversedDataWithRealCoordinates() } $provider = new TomTom($this->getAdapter(), $_SERVER['TOMTOM_MAP_KEY']); - $result = $provider->getReversedData(array(48.86321648955345, 2.3887719959020615)); + $result = $provider->reverse(array(48.86321648955345, 2.3887719959020615)); $this->assertInternalType('array', $result); $this->assertCount(1, $result); @@ -369,7 +369,7 @@ public function testGetGeocodedDataWithRealCoordinates() } $provider = new TomTom($this->getAdapter(), $_SERVER['TOMTOM_MAP_KEY']); - $result = $provider->getReversedData(array(56.5231, 10.0659)); + $result = $provider->reverse(array(56.5231, 10.0659)); $this->assertInternalType('array', $result); $this->assertCount(1, $result); diff --git a/tests/Geocoder/Tests/Provider/YandexTest.php b/tests/Geocoder/Tests/Provider/YandexTest.php index 65840c2a9..3e1aba4af 100644 --- a/tests/Geocoder/Tests/Provider/YandexTest.php +++ b/tests/Geocoder/Tests/Provider/YandexTest.php @@ -23,7 +23,7 @@ public function testGetName() public function testGetGeocodedDataWithLocalhostIPv4() { $provider = new Yandex($this->getMockAdapter($this->never())); - $provider->getGeocodedData('127.0.0.1'); + $provider->geocode('127.0.0.1'); } /** @@ -33,68 +33,70 @@ public function testGetGeocodedDataWithLocalhostIPv4() public function testGetGeocodedDataWithLocalhostIPv6() { $provider = new Yandex($this->getMockAdapter($this->never())); - $provider->getGeocodedData('::1'); + $provider->geocode('::1'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geocode-maps.yandex.ru/1.x/?format=json&geocode=&results=5 + * @expectedExceptionMessage Could not execute query "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=&results=5". */ public function testGetGeocodedDataWithNull() { $provider = new Yandex($this->getMockAdapter()); - $provider->getGeocodedData(null); + $provider->geocode(null); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geocode-maps.yandex.ru/1.x/?format=json&geocode=&results=5 + * @expectedExceptionMessage Could not execute query "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=&results=5". */ public function testGetGeocodedDataWithEmpty() { $provider = new Yandex($this->getMockAdapter()); - $provider->getGeocodedData(''); + $provider->geocode(''); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geocode-maps.yandex.ru/1.x/?format=json&geocode=foobar&results=5 + * @expectedExceptionMessage Could not execute query "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=foobar&results=5". */ public function testGetGeocodedDataWithInvalidData() { $provider = new Yandex($this->getMockAdapter()); - $provider->getGeocodedData('foobar'); + $provider->geocode('foobar'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geocode-maps.yandex.ru/1.x/?format=json&geocode=Kabasakal+Caddesi%2C+Istanbul%2C+Turkey&results=5 + * @expectedExceptionMessage Could not execute query "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=Kabasakal+Caddesi%2C+Istanbul%2C+Turkey&results=5". */ public function testGetGeocodedDataWithAddressGetsNullContent() { $provider = new Yandex($this->getMockAdapterReturns(null)); - $provider->getGeocodedData('Kabasakal Caddesi, Istanbul, Turkey'); + $provider->geocode('Kabasakal Caddesi, Istanbul, Turkey'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geocode-maps.yandex.ru/1.x/?format=json&geocode=foobar&results=5 + * @expectedExceptionMessage Could not execute query "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=foobar&results=5". */ public function testGetGeocodedDataWithFakeAddress() { $provider = new Yandex($this->getAdapter()); - $provider->getGeocodedData('foobar'); + $provider->geocode('foobar'); } public function testGetGeocodedDataWithRealAddress() { $provider = new Yandex($this->getAdapter()); - $results = $provider->getGeocodedData('10 avenue Gambetta, Paris, France'); + $results = $provider->geocode('10 avenue Gambetta, Paris, France'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(48.863277, $results[0]['latitude'], '', 0.01); $this->assertEquals(2.389016, $results[0]['longitude'], '', 0.01); $this->assertEquals(48.861926, $results[0]['bounds']['south'], '', 0.01); @@ -114,19 +116,15 @@ public function testGetGeocodedDataWithRealAddress() $this->assertNull($results[0]['regionCode']); $this->assertNull($results[0]['timezone']); - $this->assertInternalType('array', $results[1]); $this->assertEquals(48.810138, $results[1]['latitude'], '', 0.01); $this->assertEquals(2.435926, $results[1]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[2]); $this->assertEquals(48.892773, $results[2]['latitude'], '', 0.01); $this->assertEquals(2.246174, $results[2]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[3]); $this->assertEquals(48.844640, $results[3]['latitude'], '', 0.01); $this->assertEquals(2.420493, $results[3]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[4]); $this->assertEquals(48.813520, $results[4]['latitude'], '', 0.01); $this->assertEquals(2.324642, $results[4]['longitude'], '', 0.01); } @@ -134,12 +132,14 @@ public function testGetGeocodedDataWithRealAddress() public function testGetGeocodedDataWithRealAddressWithUALocale() { $provider = new Yandex($this->getAdapter(), 'uk-UA'); - $results = $provider->getGeocodedData('Copenhagen, Denmark'); + $results = $provider->geocode('Copenhagen, Denmark'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(55.675682, $results[0]['latitude'], '', 0.01); $this->assertEquals(12.567602, $results[0]['longitude'], '', 0.01); $this->assertEquals(55.614999, $results[0]['bounds']['south'], '', 0.01); @@ -159,19 +159,15 @@ public function testGetGeocodedDataWithRealAddressWithUALocale() $this->assertNull($results[0]['regionCode']); $this->assertNull($results[0]['timezone']); - $this->assertInternalType('array', $results[1]); $this->assertEquals(55.614439, $results[1]['latitude'], '', 0.01); $this->assertEquals(12.645351, $results[1]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[2]); $this->assertEquals(55.713258, $results[2]['latitude'], '', 0.01); $this->assertEquals(12.534930, $results[2]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[3]); $this->assertEquals(55.698878, $results[3]['latitude'], '', 0.01); $this->assertEquals(12.578211, $results[3]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[4]); $this->assertEquals(55.690380, $results[4]['latitude'], '', 0.01); $this->assertEquals(12.554827, $results[4]['longitude'], '', 0.01); } @@ -179,12 +175,14 @@ public function testGetGeocodedDataWithRealAddressWithUALocale() public function testGetGeocodedDataWithRealAddressWithUSLocale() { $provider = new Yandex($this->getAdapter(), 'en-US'); - $results = $provider->getGeocodedData('1600 Pennsylvania Ave, Washington'); + $results = $provider->geocode('1600 Pennsylvania Ave, Washington'); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(38.898720, $results[0]['latitude'], '', 0.01); $this->assertEquals(-77.036384, $results[0]['longitude'], '', 0.01); $this->assertEquals(38.897119, $results[0]['bounds']['south'], '', 0.01); @@ -202,23 +200,16 @@ public function testGetGeocodedDataWithRealAddressWithUSLocale() $this->assertNull($results[0]['subLocality']); $this->assertNull($results[0]['regionCode']); $this->assertNull($results[0]['timezone']); - - $this->assertInternalType('array', $results[1]); - $this->assertInternalType('array', $results[2]); - $this->assertInternalType('array', $results[3]); - $this->assertInternalType('array', $results[4]); } public function testGetGeocodedDataWithRealAddressWithBYLocale() { $provider = new Yandex($this->getAdapter(), 'be-BY'); - $result = $provider->getGeocodedData('ул.Ленина, 19, Минск 220030, Республика Беларусь'); + $result = $provider->geocode('ул.Ленина, 19, Минск 220030, Республика Беларусь'); - $this->assertInternalType('array', $result); $this->assertCount(1, $result); - $result = $result[0]; - $this->assertInternalType('array', $result); + $result = $result[0]->toArray(); $this->assertEquals(53.898077, $result['latitude'], '', 0.01); $this->assertEquals(27.563673, $result['longitude'], '', 0.01); $this->assertEquals(53.896867, $result['bounds']['south'], '', 0.01); @@ -241,43 +232,45 @@ public function testGetGeocodedDataWithRealAddressWithBYLocale() /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geocode-maps.yandex.ru/1.x/?format=json&geocode=2.000000,1.000000&results=5 + * @expectedExceptionMessage Could not execute query "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=2.000000,1.000000&results=5". */ public function testGetReversedData() { $provider = new Yandex($this->getMockAdapter()); - $provider->getReversedData(array(1, 2)); + $provider->reverse(1, 2); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geocode-maps.yandex.ru/1.x/?format=json&geocode=0.000000,0.000000&results=5 + * @expectedExceptionMessage Could not execute query "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=0.000000,0.000000&results=5". */ public function testGetReversedDataWithInvalidData() { $provider = new Yandex($this->getMockAdapter()); - $provider->getReversedData(array('foo', 'bar')); + $provider->reverse('foo', 'bar'); } /** * @expectedException \Geocoder\Exception\NoResult - * @expectedExceptionMessage Could not execute query http://geocode-maps.yandex.ru/1.x/?format=json&geocode=2.388772,48.863216&results=5 + * @expectedExceptionMessage Could not execute query "http://geocode-maps.yandex.ru/1.x/?format=json&geocode=2.388772,48.863216&results=5". */ public function testGetReversedDataWithAddressGetsNullContent() { $provider = new Yandex($this->getMockAdapterReturns(null)); - $provider->getReversedData(array(48.863216489553, 2.388771995902061)); + $provider->reverse(48.863216489553, 2.388771995902061); } public function testGetReversedDataWithRealCoordinates() { $provider = new Yandex($this->getAdapter()); - $results = $provider->getReversedData(array(48.863216489553, 2.388771995902061)); + $results = $provider->reverse(48.863216489553, 2.388771995902061); - $this->assertInternalType('array', $results); $this->assertCount(3, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(48.863212, $results[0]['latitude'], '', 0.01); $this->assertEquals(2.388773, $results[0]['longitude'], '', 0.01); $this->assertEquals(48.86294, $results[0]['bounds']['south'], '', 0.01); @@ -297,11 +290,9 @@ public function testGetReversedDataWithRealCoordinates() $this->assertNull($results[0]['regionCode']); $this->assertNull($results[0]['timezone']); - $this->assertInternalType('array', $results[1]); $this->assertEquals(48.709273, $results[1]['latitude'], '', 0.01); $this->assertEquals(2.503371, $results[1]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[2]); $this->assertEquals(46.621810, $results[2]['latitude'], '', 0.01); $this->assertEquals(2.452113, $results[2]['longitude'], '', 0.01); } @@ -309,12 +300,14 @@ public function testGetReversedDataWithRealCoordinates() public function testGetReversedDataWithRealCoordinatesWithUSLocaleAndStreeToponym() { $provider = new Yandex($this->getAdapter(), 'en-US', 'street'); - $results = $provider->getReversedData(array(48.863216489553, 2.388771995902061)); + $results = $provider->reverse(48.863216489553, 2.388771995902061); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(48.87132, $results[0]['latitude'], '', 0.01); $this->assertEquals(2.404017, $results[0]['longitude'], '', 0.01); $this->assertEquals(48.86294, $results[0]['bounds']['south'], '', 0.01); @@ -334,19 +327,15 @@ public function testGetReversedDataWithRealCoordinatesWithUSLocaleAndStreeTopony $this->assertNull($results[0]['regionCode']); $this->assertNull($results[0]['timezone']); - $this->assertInternalType('array', $results[1]); $this->assertEquals(48.863230, $results[1]['latitude'], '', 0.01); $this->assertEquals(2.388261, $results[1]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[2]); $this->assertEquals(48.866022, $results[2]['latitude'], '', 0.01); $this->assertEquals(2.389662, $results[2]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[3]); $this->assertEquals(48.863918, $results[3]['latitude'], '', 0.01); $this->assertEquals(2.387767, $results[3]['longitude'], '', 0.01); - $this->assertInternalType('array', $results[4]); $this->assertEquals(48.863787, $results[4]['latitude'], '', 0.01); $this->assertEquals(2.389600, $results[4]['longitude'], '', 0.01); } @@ -354,12 +343,14 @@ public function testGetReversedDataWithRealCoordinatesWithUSLocaleAndStreeTopony public function testGetReversedDataWithRealCoordinatesWithUALocaleAndHouseToponym() { $provider = new Yandex($this->getAdapter(), 'uk-UA', 'house'); - $results = $provider->getReversedData(array(60.4539471768582, 22.2567842183875)); + $results = $provider->reverse(60.4539471768582, 22.2567842183875); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(60.454462, $results[0]['latitude'], '', 0.01); $this->assertEquals(22.256561, $results[0]['longitude'], '', 0.01); $this->assertEquals(60.45345, $results[0]['bounds']['south'], '', 0.01); @@ -383,12 +374,14 @@ public function testGetReversedDataWithRealCoordinatesWithUALocaleAndHouseTopony public function testGetReversedDataWithRealCoordinatesWithTRLocaleAndLocalityToponym() { $provider = new Yandex($this->getAdapter(), 'tr-TR', 'locality'); - $results = $provider->getReversedData(array(40.900640, 29.198184)); + $results = $provider->reverse(40.900640, 29.198184); - $this->assertInternalType('array', $results); $this->assertCount(5, $results); - $this->assertInternalType('array', $results[0]); + $results = array_map(function ($res) { + return $res->toArray(); + }, $results); + $this->assertEquals(40.909452, $results[0]['latitude'], '', 0.01); $this->assertEquals(29.138608, $results[0]['longitude'], '', 0.01); $this->assertEquals(40.860413, $results[0]['bounds']['south'], '', 0.01); @@ -407,10 +400,5 @@ public function testGetReversedDataWithRealCoordinatesWithTRLocaleAndLocalityTop $this->assertNull($results[0]['subLocality']); $this->assertNull($results[0]['regionCode']); $this->assertNull($results[0]['timezone']); - - $this->assertInternalType('array', $results[1]); - $this->assertInternalType('array', $results[2]); - $this->assertInternalType('array', $results[3]); - $this->assertInternalType('array', $results[4]); } } diff --git a/tests/Geocoder/TestsProviderBasedGeocoderTest.php b/tests/Geocoder/Tests/ProviderAggregatorTest.php similarity index 82% rename from tests/Geocoder/TestsProviderBasedGeocoderTest.php rename to tests/Geocoder/Tests/ProviderAggregatorTest.php index dec1decb9..0de4c965d 100644 --- a/tests/Geocoder/TestsProviderBasedGeocoderTest.php +++ b/tests/Geocoder/Tests/ProviderAggregatorTest.php @@ -2,16 +2,16 @@ namespace Geocoder\Tests; +use Geocoder\ProviderAggregator; use Geocoder\Model\Address; use Geocoder\Model\AddressFactory; -use Geocoder\ProviderBasedGeocoder; use Geocoder\Provider\LocaleAwareProvider; use Geocoder\Provider\Provider; /** * @author William Durand */ -class ProviderBasedGeocoderTest extends TestCase +class ProviderAggregatorTest extends TestCase { protected $geocoder; @@ -145,28 +145,9 @@ public function testReverseAlwaysReturnsArrayAndDoesNotCallProviderWihEmptyValue $this->assertEquals(0, $this->geocoder->getProvider('test2')->geocodeCount); } - public function testSetMaxResults() - { - $this->geocoder->limit(3); - $this->assertSame(3, $this->geocoder->getMaxResults()); - } - - public function testSetLocale() - { - $provider1 = new MockProvider('test1'); - $provider2 = new MockLocaleAwareProvider('test2'); - - $this->geocoder->registerProviders([$provider1, $provider2]); - $this->geocoder->setLocale('en'); - $this->assertEquals('en', $provider2->getLocale()); - - $this->geocoder->setLocale(null); - $this->assertNull($provider2->getLocale()); - } - public function testDefaultMaxResults() { - $this->assertSame(ProviderBasedGeocoder::MAX_RESULTS, $this->geocoder->getMaxResults()); + $this->assertSame(Provider::MAX_RESULTS, $this->geocoder->getLimit()); } private function getAddressMock() @@ -184,14 +165,14 @@ public function __construct($name) $this->name = $name; } - public function getGeocodedData($address) + public function geocode($address) { - return array(); + return $this->returnResult(array()); } - public function getReversedData(array $coordinates) + public function reverse($latitude, $longitude) { - return array(); + return $this->returnResult(array()); } public function getName() @@ -199,10 +180,18 @@ public function getName() return $this->name; } - public function setMaxResults($maxResults) + public function getLimit() + { + } + + public function limit($limit) { return $this; } + + public function returnResult(array $data = array()) + { + } } class MockLocaleAwareProvider extends MockProvider implements LocaleAwareProvider @@ -214,7 +203,7 @@ public function getLocale() return $this->locale; } - public function setLocale($locale = null) + public function setLocale($locale) { $this->locale = $locale; @@ -224,12 +213,12 @@ public function setLocale($locale = null) class MockProviderWithData extends MockProvider { - public function getGeocodedData($address) + public function geocode($address) { - return array( + return $this->returnResult(array( 'latitude' => 123, 'longitude' => 456 - ); + )); } } @@ -237,13 +226,15 @@ class MockProviderWithRequestCount extends MockProvider { public $geocodeCount = 0; - public function getGeocodedData($address) + public function geocode($address) { $this->geocodeCount++; + + return parent::geocode($address); } } -class TestableGeocoder extends ProviderBasedGeocoder +class TestableGeocoder extends ProviderAggregator { public $countCallGetProvider = 0; @@ -253,9 +244,4 @@ public function getProvider() return parent::getProvider(); } - - public function returnResult(array $data = array()) - { - return parent::returnResult($data); - } }