diff --git a/.gitignore b/.gitignore index edc927f26..5d5fa0e4a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ composer.lock composer.phar phpunit.xml php-cs-fixer.phar +.puli/ diff --git a/.travis.yml b/.travis.yml index c4cd39b92..1c6b940b1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,7 +12,6 @@ env: - deps="" php: - - 5.4 - 5.5 - 5.6 - 7.0 @@ -21,7 +20,7 @@ php: matrix: fast_finish: true include: - - php: 5.4 + - php: 5.5 env: deps="low" before_script: @@ -31,4 +30,4 @@ before_script: - if [ "$deps" = "" ]; then composer install --prefer-dist --no-interaction; fi - if [ "$TRAVIS_PHP_VERSION" == "7.0" ]; then composer require "geoip/geoip"; fi -script: phpunit --coverage-text +script: ./vendor/bin/phpunit --coverage-text diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bb806bfa..9c8724504 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,21 @@ CHANGELOG ========= +### 4.0.0 (2016-xx-xx) + +* Added: Interface for `Geocoder\Model\AddressCollection` called `Geocoder\Collection`. Public APIs are updated to type hint for `Geocoder\GeocoderResult`. +* Added: Interface for `Geocoder\Model\Address` called `Geocoder\Location`. Public APIs are updated to type hint for `Geocoder\Location`. +* Changed: `Location::getCoordinates` will return null or a `Coordinates` object with coordinates data. It will never return `Coordinates` without data. +* Changed: `Location::getBounds` will return null or a `Bounds` object with coordinates data. It will never return `Bounds` without data. +* Removed: `AdminLevel::toString` in favor for `AdminLevel::__toString`. +* Removed: `Country::toString` in favor for `Country::__toString`. +* Removed: `Address::getCountryCode` in favor for `Address::getCountry()->getCode()`. +* Removed: `Address::getLongitude` in favor for `Address::getCoordinates()->getLongitude()`. +* Removed: `Address::getLatitude` in favor for `Address::getCoordinates()->getLatitude()`. +* Removed: `Bounds::isDefined` as it is always defined. + + + ### 3.3.0 (2015-12-06) * Added: timezone field for `FreeGeoIp` provider diff --git a/README.md b/README.md index 5fb058598..1e1a4740c 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,14 @@ Downloads](https://poser.pugx.org/willdurand/Geocoder/downloads.png)](https://pa Version](https://poser.pugx.org/willdurand/Geocoder/v/stable.png)](https://packagist.org/packages/willdurand/Geocoder) ![PHP7 ready](https://img.shields.io/badge/PHP7-ready-green.svg) -> **Important:** You are browsing the documentation of Geocoder **3.x**. -Documentation for version **2.x** is available here: [Geocoder 2.x -documentation](https://github.com/geocoder-php/Geocoder/blob/2.x/README.md). +> **Important:** You are browsing the documentation of Geocoder **4.x** (not +> released yet). +> +> Documentation for version **3.x** is available here: [Geocoder 3.x +> documentation](https://github.com/geocoder-php/Geocoder/blob/3.x/README.md). +> +> Documentation for version **2.x** is available here: [Geocoder 2.x +> documentation](https://github.com/geocoder-php/Geocoder/blob/2.x/README.md). --- @@ -30,6 +35,7 @@ providing a powerful abstraction layer for geocoding manipulations. - [GeoIP2](#geoip2) - [GoogleMaps](#googlemaps) - [GoogleMapsBusiness](#googlemapsbusiness) + - [Mapzen](#mapzen) - [MaxMindBinary](#maxmindbinary) - [Nominatim](#nominatim) - [TomTom](#tomtom) @@ -46,6 +52,7 @@ providing a powerful abstraction layer for geocoding manipulations. - [Formatters](#formatters) * [Extending Things](#extending-things) * [Versioning](#versioning) +* [Cookbook](#cookbook) Installation @@ -73,8 +80,8 @@ since each HTTP-based provider implements [PSR-7](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md). ```php -$curl = new \Ivory\HttpAdapter\CurlHttpAdapter(); -$geocoder = new \Geocoder\Provider\GoogleMaps($curl); +$adapter = new \Http\Adapter\Guzzle6\Client(); +$geocoder = new \Geocoder\Provider\GoogleMaps($adapter); $geocoder->geocode(...); $geocoder->reverse(...); @@ -214,14 +221,14 @@ In order to talk to geocoding APIs, you need HTTP adapters. While it was part of the library in Geocoder 1.x and 2.x, Geocoder 3.x and upper now relies on the [PSR-7 Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md) -which defines how HTTP message should be implemented. Choose any library that -follows this PSR and implement the specified interfaces to use with Geocoder. +which defines how HTTP message should be implemented. You can use any library to send HTTP messages +that implements [php-http/client-implementation](https://packagist.org/providers/php-http/client-implementation). -As making choices is rather hard, Geocoder ships with the -[egeloen/http-adapter](https://github.com/egeloen/ivory-http-adapter) library by -default, but it is up to you to choose a different implementation. +To use Guzzle 6 you should run the follwing command: -**Note:** not all providers are HTTP-based. +``` +$ composer require php-http/guzzle6-adapter +``` ### Providers @@ -238,7 +245,8 @@ Chain | `chain` | | | | | meta provider which iterates over a list of providers [Geonames](http://www.geonames.org/commercial-webservices.html) | `geonames` | yes |no | worldwide | yes | requires registration, no free tier [Google Maps](https://developers.google.com/maps/documentation/geocoding/) | `google_maps` | yes | supported | worldwide | yes | requires API key. Limit 2500 requests per day [Google Maps for Business](https://developers.google.com/maps/documentation/business/) | `google_maps_business` | yes | supported | worldwide | yes | requires API key. Limit 100,000 requests per day -[MapQuest](http://developer.mapquest.com/web/products/dev-services/geocoding-ws) | `map_quest` | yes | no | worldwide | yes | both open and [commercial service](http://platform.mapquest.com/geocoding/) require API key +[MapQuest](http://developer.mapquest.com/web/products/dev-services/geocoding-ws) | `map_quest` | yes | no | worldwide | yes | both open and [commercial service](http://platform.mapquest.com/geocoding/) requires API key +[Mapzen](https://mapzen.com/documentation/search/) | `mapzen` | yes | supported | worldwide | yes | requires API key; limited to 6 request/sec, 30,000 request/day [Nominatim](http://wiki.openstreetmap.org/wiki/Nominatim) | `nominatim` | yes | supported | worldwide | yes | requires a domain name (e.g. local installation) [OpenCage](http://geocoder.opencagedata.com/) | `opencage` | yes | supported | worldwide | yes | requires API key. 2500 requests/day free [OpenStreetMap](http://wiki.openstreetmap.org/wiki/Nominatim) | `openstreetmap` | yes | no | worldwide | yes | heavy users (>1q/s) get banned @@ -295,6 +303,10 @@ $geocoder = new \Geocoder\Provider\GoogleMaps( A valid `Client ID` is required. The private key is optional. This provider also supports SSL, and extends the `GoogleMaps` provider. +##### Mapzen + +A valid `API key` is required. This provider also supports SSL. + ##### MaxMindBinary This provider requires a data file, and the @@ -360,7 +372,7 @@ when a provider returns a result. The result is returned by `GoogleMaps` because ``` php $geocoder = new \Geocoder\ProviderAggregator(); -$adapter = new \Ivory\HttpAdapter\CurlHttpAdapter(); +$adapter = new \Http\Adapter\Guzzle6\Client(); $chain = new \Geocoder\Provider\Chain([ new \Geocoder\Provider\FreeGeoIp($adapter), @@ -520,6 +532,14 @@ Major version `2` will reach **end of life on December 2015**. Version `3.x` is the current major stable version of Geocoder. +Cookbook +-------- + +We have a small cookbook where you can find examples on common use cases: + +* [Caching responses](/docs/cookbook/cache.md) +* [Configuring the HTTP client](/docs/cookbook/http-client.md) + Contributing ------------ diff --git a/composer.json b/composer.json index 874f346b1..8122ff7f7 100644 --- a/composer.json +++ b/composer.json @@ -12,13 +12,21 @@ } ], "require": { - "php": "^5.4|^7.0", - "egeloen/http-adapter": "~0.8|~1.0", - "igorw/get-in": "~1.0" + "php": "^5.5 || ^7.0", + "igorw/get-in": "^1.0", + "psr/http-message-implementation": "^1.0", + "php-http/client-implementation": "^1.0", + "php-http/message-factory": "^1.0.2", + "php-http/httplug": "^1.0", + "php-http/discovery": "^1.0" }, "require-dev": { + "phpunit/phpunit": "^4.8", "geoip2/geoip2": "~2.0", - "symfony/stopwatch": "~2.5" + "symfony/stopwatch": "~2.5", + "php-http/message": "^1.0", + "php-http/guzzle6-adapter": "^1.0", + "php-http/mock-client": "^0.3.0" }, "suggest": { "ext-geoip": "Enabling the geoip extension allows you to use the MaxMindProvider.", diff --git a/docs/cookbook/cache.md b/docs/cookbook/cache.md new file mode 100644 index 000000000..50d93b9e9 --- /dev/null +++ b/docs/cookbook/cache.md @@ -0,0 +1,38 @@ +# Caching responses + +Many of the APIs are not free so it is a good idea to cache the responses so you +are not paying for the same information twice. The caching is out of scope for this +library but we will show you an example how to properly cache responses with the +HTTPlug [cache plugin](http://php-http.readthedocs.io/en/latest/plugins/cache.html): + +```php +use Cache\Adapter\Redis\RedisCachePool; +use Http\Adapter\Guzzle6\Client as GuzzleClient; +use Http\Client\Common\PluginClient; +use Geocoder\Provider\GoogleMaps; + +// Get a PSR-6 cache pool +$client = new \Redis(); +$client->connect('127.0.0.1', 6379); +$pool = new RedisCachePool($client); + +// Give the cache pool to the cache plugin and congure it to ignore +// cache headers and store the response for one year. +$cachePlugin = new CachePlugin($pool, StreamFactoryDiscovery::find(), [ + 'respect_cache_headers' => false, + 'default_ttl' => null, + 'cache_lifetime' => 86400*365 +]); + +$adapter = new GuzzleClient(); +$pluginClient = new PluginClient($adapter, [$cachePlugin]); + +// Get a geocoder +$geocoder = new GoogleMaps($pluginClient, 'en', null, null, true, 'api-key'); + +// Query Google Maps servers +$result0 = $geocoder->geocode('foobar'); + +// This will be retrieved from the cache and not hit Google's servers +$result1 = $geocoder->geocode('foobar'); +``` diff --git a/docs/cookbook/http-client.md b/docs/cookbook/http-client.md new file mode 100644 index 000000000..9d3ca9155 --- /dev/null +++ b/docs/cookbook/http-client.md @@ -0,0 +1,47 @@ +# Configuring the HTTP client + +The Geocoder is decoupled from the HTTP client that sends the HTTP messages. This means +that you are responsible for configuring the HTTP client. Usually the default configuration +is good enough but sometime you may want to do something differently. + +How you configure the client differs between different clients below are two examples, +one with [Guzzle6 client](https://github.com/guzzle/guzzle) and one with the +[cURL client](https://github.com/php-http/curl-client). + +## Guzzle6 + +```php +use GuzzleHttp\Client as GuzzleClient; +use Http\Adapter\Guzzle6\Client; +use Geocoder\Provider\GoogleMaps; + +$config = [ + 'timeout' => 2.0, + 'verify' => false, +]; +$guzzle = new GuzzleClient($config); + +$adapter = new Client($guzzle); +$geocoder = new GoogleMaps($adapter); + +$geocoder->geocode(...); +``` + + +## cURL + +```php +use Http\Client\Curl\Client; +use Geocoder\Provider\GoogleMaps; + +$options = [ + CURLOPT_CONNECTTIMEOUT => 2, + CURLOPT_SSL_VERIFYPEER => false, +]; + +$adapter = new Client(null, null, $options); +$geocoder = new GoogleMaps($adapter); + +$geocoder->geocode(...); +``` + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index fb448b4b5..a25e5b27f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -27,6 +27,7 @@ + diff --git a/src/Geocoder/Assert.php b/src/Geocoder/Assert.php new file mode 100644 index 000000000..e2fe9c31b --- /dev/null +++ b/src/Geocoder/Assert.php @@ -0,0 +1,50 @@ + 90) { + throw new \InvalidArgumentException( + sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value) + ); + } + } + + /** + * @param float $value + * @param string $message + */ + public static function longitude($value, $message = '') + { + if (!is_double($value)) { + throw new \InvalidArgumentException( + sprintf($message ?: 'Expected a doable. Got: %s', self::typeToString($value)) + ); + } + + if ($value < -180 || $value > 180) { + throw new \InvalidArgumentException( + sprintf($message ?: 'Latitude should be between -90 and 90. Got: %s', $value) + ); + } + } + + private static function typeToString($value) + { + return is_object($value) ? get_class($value) : gettype($value); + } + +} diff --git a/src/Geocoder/Collection.php b/src/Geocoder/Collection.php new file mode 100644 index 000000000..c464b5183 --- /dev/null +++ b/src/Geocoder/Collection.php @@ -0,0 +1,38 @@ + + * @author Tobias Nyholm + */ +interface Collection extends \IteratorAggregate, \Countable +{ + /** + * @return Location + */ + public function first(); + + /** + * @return Location[] + */ + public function slice($offset, $length = null); + + /** + * @return bool + */ + public function has($index); + + /** + * @return Location + * @throws \OutOfBoundsException + */ + public function get($index); + + /** + * @return Location[] + */ + public function all(); +} \ No newline at end of file diff --git a/src/Geocoder/Dumper/Dumper.php b/src/Geocoder/Dumper/Dumper.php index 4357d2960..5c3fee3c2 100644 --- a/src/Geocoder/Dumper/Dumper.php +++ b/src/Geocoder/Dumper/Dumper.php @@ -10,7 +10,7 @@ namespace Geocoder\Dumper; -use Geocoder\Model\Address; +use Geocoder\Location; /** * @author William Durand @@ -18,12 +18,12 @@ interface Dumper { /** - * Dumps an `Address` object as a string representation of + * Dumps an `Location` object as a string representation of * the implemented format. * - * @param Address $address + * @param Location $location * * @return string */ - public function dump(Address $address); + public function dump(Location $location); } diff --git a/src/Geocoder/Dumper/GeoArray.php b/src/Geocoder/Dumper/GeoArray.php new file mode 100644 index 000000000..bdfe9ef3c --- /dev/null +++ b/src/Geocoder/Dumper/GeoArray.php @@ -0,0 +1,56 @@ + + */ +class GeoArray implements Dumper +{ + /** + * {@inheritDoc} + */ + public function dump(Address $address) + { + $properties = array_filter($address->toArray(), function ($value) { + return !empty($value); + }); + + unset( + $properties['latitude'], + $properties['longitude'], + $properties['bounds'] + ); + + if (0 === count($properties)) { + $properties = null; + } + + $array = [ + 'type' => 'Feature', + 'geometry' => [ + 'type' => 'Point', + 'coordinates' => [ $address->getLongitude(), $address->getLatitude() ] + ], + 'properties' => $properties, + ]; + + if (null !== $bounds = $address->getBounds()) { + if ($bounds->isDefined()) { + $array['bounds'] = $bounds->toArray(); + } + } + + return $array; + } +} diff --git a/src/Geocoder/Dumper/GeoJson.php b/src/Geocoder/Dumper/GeoJson.php index f23b818d3..d8e26b9d7 100644 --- a/src/Geocoder/Dumper/GeoJson.php +++ b/src/Geocoder/Dumper/GeoJson.php @@ -10,7 +10,7 @@ namespace Geocoder\Dumper; -use Geocoder\Model\Address; +use Geocoder\Location; /** * @author Jan Sorgalla @@ -20,9 +20,9 @@ class GeoJson implements Dumper /** * {@inheritDoc} */ - public function dump(Address $address) + public function dump(Location $location) { - $properties = array_filter($address->toArray(), function ($value) { + $properties = array_filter($location->toArray(), function ($value) { return !empty($value); }); @@ -36,19 +36,24 @@ public function dump(Address $address) $properties = null; } + $lat = 0; + $lon = 0; + if (null !== $coordinates = $location->getCoordinates()) { + $lat = $coordinates->getLatitude(); + $lon = $coordinates->getLongitude(); + } + $json = [ 'type' => 'Feature', 'geometry' => [ 'type' => 'Point', - 'coordinates' => [ $address->getLongitude(), $address->getLatitude() ] + 'coordinates' => [$lon, $lat], ], 'properties' => $properties, ]; - if (null !== $bounds = $address->getBounds()) { - if ($bounds->isDefined()) { - $json['bounds'] = $bounds->toArray(); - } + if (null !== $bounds = $location->getBounds()) { + $json['bounds'] = $bounds->toArray(); } return json_encode($json); diff --git a/src/Geocoder/Dumper/Gpx.php b/src/Geocoder/Dumper/Gpx.php index 4ce63e877..cb680258b 100644 --- a/src/Geocoder/Dumper/Gpx.php +++ b/src/Geocoder/Dumper/Gpx.php @@ -11,7 +11,7 @@ namespace Geocoder\Dumper; use Geocoder\Geocoder; -use Geocoder\Model\Address; +use Geocoder\Location; /** * @author William Durand @@ -19,11 +19,11 @@ class Gpx implements Dumper { /** - * @param Address $address + * @param Location $location * * @return string */ - public function dump(Address $address) + public function dump(Location $location) { $gpx = sprintf(<< @@ -37,8 +37,7 @@ public function dump(Address $address) GPX , Geocoder::VERSION); - if ($address->getBounds()->isDefined()) { - $bounds = $address->getBounds(); + if (null !== $bounds = $location->getBounds()) { $gpx .= sprintf(<< @@ -46,6 +45,13 @@ public function dump(Address $address) , $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth()); } + $lat = null; + $lon = null; + if (null !== $coordinates = $location->getCoordinates()) { + $lat = $coordinates->getLatitude(); + $lon = $coordinates->getLongitude(); + } + $gpx .= sprintf(<< @@ -53,7 +59,7 @@ public function dump(Address $address) GPX - , $address->getLatitude(), $address->getLongitude(), $this->formatName($address)); + , $lat, $lon, $this->formatName($location)); $gpx .= << @@ -63,11 +69,11 @@ public function dump(Address $address) } /** - * @param Address $address + * @param Location $address * * @return string */ - protected function formatName(Address $address) + protected function formatName(Location $address) { $name = []; $array = $address->toArray(); diff --git a/src/Geocoder/Dumper/Kml.php b/src/Geocoder/Dumper/Kml.php index 7f4c889b7..3568d57c7 100644 --- a/src/Geocoder/Dumper/Kml.php +++ b/src/Geocoder/Dumper/Kml.php @@ -10,7 +10,7 @@ namespace Geocoder\Dumper; -use Geocoder\Model\Address; +use Geocoder\Location; /** * @author Jan Sorgalla @@ -20,9 +20,9 @@ class Kml extends Gpx implements Dumper /** * {@inheritDoc} */ - public function dump(Address $address) + public function dump(Location $location) { - $name = $this->formatName($address); + $name = $this->formatName($location); $kml = << @@ -38,6 +38,13 @@ public function dump(Address $address) KML; - return sprintf($kml, $name, $name, $address->getLongitude(), $address->getLatitude()); + $lat = null; + $lon = null; + if (null !== $coordinates = $location->getCoordinates()) { + $lat = $coordinates->getLatitude(); + $lon = $coordinates->getLongitude(); + } + + return sprintf($kml, $name, $name, $lon, $lat); } } diff --git a/src/Geocoder/Dumper/Wkb.php b/src/Geocoder/Dumper/Wkb.php index 569327e09..0317762ff 100644 --- a/src/Geocoder/Dumper/Wkb.php +++ b/src/Geocoder/Dumper/Wkb.php @@ -10,7 +10,7 @@ namespace Geocoder\Dumper; -use Geocoder\Model\Address; +use Geocoder\Location; /** * @author Jan Sorgalla @@ -20,8 +20,15 @@ class Wkb implements Dumper /** * {@inheritDoc} */ - public function dump(Address $address) + public function dump(Location $location) { - return pack('cLdd', 1, 1, $address->getLongitude(), $address->getLatitude()); + $lat = null; + $lon = null; + if (null !== $coordinates = $location->getCoordinates()) { + $lat = $coordinates->getLatitude(); + $lon = $coordinates->getLongitude(); + } + + return pack('cLdd', 1, 1, $lon, $lat); } } diff --git a/src/Geocoder/Dumper/Wkt.php b/src/Geocoder/Dumper/Wkt.php index 40ffdc5e8..b58ec70f1 100644 --- a/src/Geocoder/Dumper/Wkt.php +++ b/src/Geocoder/Dumper/Wkt.php @@ -10,7 +10,7 @@ namespace Geocoder\Dumper; -use Geocoder\Model\Address; +use Geocoder\Location; /** * @author Jan Sorgalla @@ -20,8 +20,15 @@ class Wkt implements Dumper /** * {@inheritDoc} */ - public function dump(Address $address) + public function dump(Location $location) { - return sprintf('POINT(%F %F)', $address->getLongitude(), $address->getLatitude()); + $lat = null; + $lon = null; + if (null !== $coordinates = $location->getCoordinates()) { + $lat = $coordinates->getLatitude(); + $lon = $coordinates->getLongitude(); + } + + return sprintf('POINT(%F %F)', $lon, $lat); } } diff --git a/src/Geocoder/Formatter/StringFormatter.php b/src/Geocoder/Formatter/StringFormatter.php index 1ca09d808..39ca4f9da 100644 --- a/src/Geocoder/Formatter/StringFormatter.php +++ b/src/Geocoder/Formatter/StringFormatter.php @@ -10,9 +10,8 @@ namespace Geocoder\Formatter; -use Geocoder\Model\Address; -use Geocoder\Model\AdminLevel; use Geocoder\Model\AdminLevelCollection; +use Geocoder\Location; /** * @author William Durand @@ -42,22 +41,22 @@ class StringFormatter /** * Transform an `Address` instance into a string representation. * - * @param Address $address - * @param string $format + * @param Location $location + * @param string $format * * @return string */ - public function format(Address $address, $format) + public function format(Location $location, $format) { $replace = [ - self::STREET_NUMBER => $address->getStreetNumber(), - self::STREET_NAME => $address->getStreetName(), - self::LOCALITY => $address->getLocality(), - self::POSTAL_CODE => $address->getPostalCode(), - self::SUB_LOCALITY => $address->getSubLocality(), - self::COUNTRY => $address->getCountry()->getName(), - self::COUNTRY_CODE => $address->getCountry()->getCode(), - self::TIMEZONE => $address->getTimezone(), + self::STREET_NUMBER => $location->getStreetNumber(), + self::STREET_NAME => $location->getStreetName(), + self::LOCALITY => $location->getLocality(), + self::POSTAL_CODE => $location->getPostalCode(), + self::SUB_LOCALITY => $location->getSubLocality(), + self::COUNTRY => $location->getCountry()->getName(), + self::COUNTRY_CODE => $location->getCountry()->getCode(), + self::TIMEZONE => $location->getTimezone(), ]; for ($level = 1; $level <= AdminLevelCollection::MAX_LEVEL_DEPTH; $level ++) { @@ -65,7 +64,7 @@ public function format(Address $address, $format) $replace[self::ADMIN_LEVEL_CODE . $level] = null; } - foreach ($address->getAdminLevels() as $level => $adminLevel) { + foreach ($location->getAdminLevels() as $level => $adminLevel) { $replace[self::ADMIN_LEVEL . $level] = $adminLevel->getName(); $replace[self::ADMIN_LEVEL_CODE . $level] = $adminLevel->getCode(); } diff --git a/src/Geocoder/Geocoder.php b/src/Geocoder/Geocoder.php index d312a320b..7a90a8638 100644 --- a/src/Geocoder/Geocoder.php +++ b/src/Geocoder/Geocoder.php @@ -10,8 +10,6 @@ namespace Geocoder; -use Geocoder\Model\AddressCollection; - /** * @author William Durand */ @@ -27,8 +25,8 @@ interface Geocoder * * @param string $value * - * @return AddressCollection - * @throws Geocoder\Exception\Exception + * @return Collection + * @throws \Geocoder\Exception\Exception */ public function geocode($value); @@ -38,8 +36,8 @@ public function geocode($value); * @param double $latitude * @param double $longitude * - * @return AddressCollection - * @throws Geocoder\Exception\Exception + * @return Collection + * @throws \Geocoder\Exception\Exception */ public function reverse($latitude, $longitude); diff --git a/src/Geocoder/Location.php b/src/Geocoder/Location.php new file mode 100644 index 000000000..0c89cc61f --- /dev/null +++ b/src/Geocoder/Location.php @@ -0,0 +1,104 @@ + + * @author Tobias Nyholm + */ +interface Location +{ + /** + * Will always return the coordinates value object. + * + * This method MUST NOT return null. + * + * @return Coordinates + */ + public function getCoordinates(); + + /** + * Returns the bounds value object. + * + * This method MUST NOT return null. + * + * @return Bounds + */ + public function getBounds(); + + /** + * Returns the street number value. + * + * @return string|int + */ + public function getStreetNumber(); + + /** + * Returns the street name value. + * + * @return string + */ + public function getStreetName(); + + /** + * Returns the city or locality value. + * + * @return string + */ + public function getLocality(); + + /** + * Returns the postal code or zipcode value. + * + * @return string + */ + public function getPostalCode(); + + /** + * Returns the locality district, or + * sublocality, or neighborhood. + * + * @return string + */ + public function getSubLocality(); + + /** + * Returns the administrative levels. + * + * This method MUST NOT return null. + * + * @return AdminLevelCollection + */ + public function getAdminLevels(); + + /** + * Returns the country value object. + * + * This method MUST NOT return null. + * + * @return Country + */ + public function getCountry(); + + /** + * Returns the timezone for the Position. The timezone MUST be in the list of supported timezones. + * + * {@link http://php.net/manual/en/timezones.php} + * + * @return string + */ + public function getTimezone(); + + /** + * Returns an array with data indexed by name. + * + * @return array + */ + public function toArray(); +} \ No newline at end of file diff --git a/src/Geocoder/Model/Address.php b/src/Geocoder/Model/Address.php index 8eaf4cd47..d730da9f1 100644 --- a/src/Geocoder/Model/Address.php +++ b/src/Geocoder/Model/Address.php @@ -10,10 +10,12 @@ namespace Geocoder\Model; +use Geocoder\Location; + /** * @author William Durand */ -final class Address +final class Address implements Location { /** * @var Coordinates @@ -66,11 +68,17 @@ final class Address private $timezone; /** - * @param string $streetNumber - * @param string $streetName - * @param string $postalCode - * @param string $locality - * @param string $subLocality + * + * @param Coordinates|null $coordinates + * @param Bounds|null $bounds + * @param string|null $streetNumber + * @param string|null $streetName + * @param string|null $postalCode + * @param string|null $locality + * @param string|null $subLocality + * @param AdminLevelCollection|null $adminLevels + * @param Country|null $country + * @param string|null $timezone */ public function __construct( Coordinates $coordinates = null, @@ -97,9 +105,9 @@ public function __construct( } /** - * Returns an array of coordinates (latitude, longitude). + * Returns the coordinates for this address. * - * @return Coordinates + * @return Coordinates|null */ public function getCoordinates() { @@ -107,37 +115,9 @@ public function getCoordinates() } /** - * Returns the latitude value. - * - * @return double - */ - public function getLatitude() - { - if (null === $this->coordinates) { - return null; - } - - return $this->coordinates->getLatitude(); - } - - /** - * Returns the longitude value. - * - * @return double - */ - public function getLongitude() - { - if (null === $this->coordinates) { - return null; - } - - return $this->coordinates->getLongitude(); - } - - /** - * Returns the bounds value. + * Returns the bounds. * - * @return Bounds + * @return Bounds|null */ public function getBounds() { @@ -208,23 +188,13 @@ public function getAdminLevels() /** * Returns the country value. * - * @return Country + * @return Country|null */ public function getCountry() { return $this->country; } - /** - * Returns the country ISO code. - * - * @return string - */ - public function getCountryCode() - { - return $this->country->getCode(); - } - /** * Returns the timezone. * @@ -250,18 +220,39 @@ public function toArray() ]; } + $lat = null; + $lon = null; + if (null !== $coordinates = $this->getCoordinates()) { + $lat = $coordinates->getLatitude(); + $lon = $coordinates->getLongitude(); + } + + $countryName = null; + $countryCode = null; + if (null !== $country = $this->getCountry()) { + $countryName = $country->getName(); + $countryCode = $country->getCode(); + } + + $noBounds = [ + 'south' => null, + 'west' => null, + 'north' => null, + 'east' => null, + ]; + return array( - 'latitude' => $this->getLatitude(), - 'longitude' => $this->getLongitude(), - 'bounds' => $this->bounds->toArray(), + 'latitude' => $lat, + 'longitude' => $lon, + 'bounds' => null !== $this->bounds ? $this->bounds->toArray() : $noBounds, 'streetNumber' => $this->streetNumber, 'streetName' => $this->streetName, 'postalCode' => $this->postalCode, 'locality' => $this->locality, 'subLocality' => $this->subLocality, 'adminLevels' => $adminLevels, - 'country' => $this->country->getName(), - 'countryCode' => $this->country->getCode(), + 'country' => $countryName, + 'countryCode' => $countryCode, 'timezone' => $this->timezone, ); } diff --git a/src/Geocoder/Model/AddressCollection.php b/src/Geocoder/Model/AddressCollection.php index 1eef24945..6cdd61291 100644 --- a/src/Geocoder/Model/AddressCollection.php +++ b/src/Geocoder/Model/AddressCollection.php @@ -3,16 +3,18 @@ namespace Geocoder\Model; use Geocoder\Exception\CollectionIsEmpty; +use Geocoder\Collection; +use Geocoder\Location; -final class AddressCollection implements \IteratorAggregate, \Countable +final class AddressCollection implements Collection { /** - * @var Address[] + * @var Location[] */ private $addresses; /** - * @param Address[] $addresses + * @param Location[] $addresses */ public function __construct(array $addresses = []) { @@ -36,19 +38,19 @@ public function count() } /** - * @return Address + * @return Location */ public function first() { if (empty($this->addresses)) { - throw new CollectionIsEmpty('The AddressCollection instance is empty.'); + throw new CollectionIsEmpty('The Collection instance is empty.'); } return reset($this->addresses); } /** - * @return Address[] + * @return Location[] */ public function slice($offset, $length = null) { @@ -64,7 +66,7 @@ public function has($index) } /** - * @return Address + * @return Location * @throws \OutOfBoundsException */ public function get($index) @@ -77,7 +79,7 @@ public function get($index) } /** - * @return Address[] + * @return Location[] */ public function all() { diff --git a/src/Geocoder/Model/AddressFactory.php b/src/Geocoder/Model/AddressFactory.php index 2d28b295f..255d402a4 100644 --- a/src/Geocoder/Model/AddressFactory.php +++ b/src/Geocoder/Model/AddressFactory.php @@ -10,6 +10,8 @@ namespace Geocoder\Model; +use Geocoder\Collection; + /** * @author Markus Bachmann * @author William Durand @@ -18,7 +20,7 @@ final class AddressFactory { /** * @param array $results - * @return \Geocoder\Model\AddressCollection + * @return Collection */ public function createFromArray(array $results) { @@ -33,12 +35,13 @@ public function createFromArray(array $results) ); } + $addresses[] = new Address( $this->createCoordinates( $this->readDoubleValue($result, 'latitude'), $this->readDoubleValue($result, 'longitude') ), - new Bounds( + $this->createBounds( $this->readDoubleValue($result, 'bounds.south'), $this->readDoubleValue($result, 'bounds.west'), $this->readDoubleValue($result, 'bounds.north'), @@ -114,6 +117,8 @@ private function upperize($str) /** * @param double $latitude * @param double $longitude + * + * @return Coordinates|null */ private function createCoordinates($latitude, $longitude) { @@ -123,4 +128,20 @@ private function createCoordinates($latitude, $longitude) return new Coordinates((double) $latitude, (double) $longitude); } + + /** + * @param double $south + * @param double $west + * @param double $north + * + * @return Bounds|null + */ + private function createBounds($south, $west, $north, $east) + { + if (null === $south || null === $west || null === $north || null === $east) { + return null; + } + + return new Bounds((double) $south, (double) $west, (double) $north, (double) $east); + } } diff --git a/src/Geocoder/Model/AdminLevel.php b/src/Geocoder/Model/AdminLevel.php index f5227dc5c..88e47fc3d 100644 --- a/src/Geocoder/Model/AdminLevel.php +++ b/src/Geocoder/Model/AdminLevel.php @@ -72,16 +72,6 @@ public function getCode() return $this->code; } - /** - * Returns a string with the administrative level name. - * - * @return string - */ - public function toString() - { - return $this->getName(); - } - /** * Returns a string with the administrative level name. * @@ -89,6 +79,6 @@ public function toString() */ public function __toString() { - return $this->toString(); + return $this->getName(); } } diff --git a/src/Geocoder/Model/AdminLevelCollection.php b/src/Geocoder/Model/AdminLevelCollection.php index 489b18091..0f0ce2a64 100644 --- a/src/Geocoder/Model/AdminLevelCollection.php +++ b/src/Geocoder/Model/AdminLevelCollection.php @@ -7,7 +7,7 @@ /** * @author Giorgio Premi */ -final class AdminLevelCollection implements \IteratorAggregate, \Countable +final class AdminLevelCollection implements \IteratorAggregate, \Countable { const MAX_LEVEL_DEPTH = 5; @@ -16,6 +16,10 @@ final class AdminLevelCollection implements \IteratorAggregate, \Countable */ private $adminLevels; + /** + * + * @param AdminLevel[] $adminLevels + */ public function __construct(array $adminLevels = []) { $this->adminLevels = []; diff --git a/src/Geocoder/Model/Bounds.php b/src/Geocoder/Model/Bounds.php index 27104d333..55fa5043d 100644 --- a/src/Geocoder/Model/Bounds.php +++ b/src/Geocoder/Model/Bounds.php @@ -10,6 +10,8 @@ namespace Geocoder\Model; +use Geocoder\Assert; + /** * @author William Durand */ @@ -43,6 +45,16 @@ final class Bounds */ public function __construct($south, $west, $north, $east) { + $south = (double) $south; + $north = (double) $north; + $west = (double) $west; + $east = (double) $east; + + Assert::latitude($south); + Assert::latitude($north); + Assert::longitude($west); + Assert::longitude($east); + $this->south = $south; $this->west = $west; $this->north = $north; @@ -89,16 +101,6 @@ public function getEast() return $this->east; } - /** - * Returns whether or not bounds are defined - * - * @return bool - */ - public function isDefined() - { - return !empty($this->south) && !empty($this->east) && !empty($this->north) && !empty($this->west); - } - /** * Returns an array with bounds. * diff --git a/src/Geocoder/Model/Coordinates.php b/src/Geocoder/Model/Coordinates.php index f56be3df2..d0ca08791 100644 --- a/src/Geocoder/Model/Coordinates.php +++ b/src/Geocoder/Model/Coordinates.php @@ -10,6 +10,8 @@ namespace Geocoder\Model; +use Geocoder\Assert; + /** * @author William Durand */ @@ -31,6 +33,12 @@ final class Coordinates */ public function __construct($latitude, $longitude) { + $latitude = (double) $latitude; + $longitude = (double) $longitude; + + Assert::latitude($latitude); + Assert::longitude($longitude); + $this->latitude = $latitude; $this->longitude = $longitude; } @@ -38,7 +46,7 @@ public function __construct($latitude, $longitude) /** * Returns the latitude. * - * @return double + * @return double|null */ public function getLatitude() { @@ -48,7 +56,7 @@ public function getLatitude() /** * Returns the longitude. * - * @return double + * @return double|null */ public function getLongitude() { diff --git a/src/Geocoder/Model/Country.php b/src/Geocoder/Model/Country.php index 9cd3ba839..00ced44b7 100644 --- a/src/Geocoder/Model/Country.php +++ b/src/Geocoder/Model/Country.php @@ -10,6 +10,8 @@ namespace Geocoder\Model; +use Geocoder\Assert; + /** * @author William Durand */ @@ -55,16 +57,6 @@ public function getCode() return $this->code; } - /** - * Returns a string with the country name. - * - * @return string - */ - public function toString() - { - return $this->getName(); - } - /** * Returns a string with the country name. * @@ -72,6 +64,6 @@ public function toString() */ public function __toString() { - return $this->toString(); + return $this->getName() ?: ''; } } diff --git a/src/Geocoder/Provider/AbstractHttpProvider.php b/src/Geocoder/Provider/AbstractHttpProvider.php index e391981b7..38f9e4cb1 100644 --- a/src/Geocoder/Provider/AbstractHttpProvider.php +++ b/src/Geocoder/Provider/AbstractHttpProvider.php @@ -10,7 +10,10 @@ namespace Geocoder\Provider; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Message\MessageFactory; +use Http\Discovery\HttpClientDiscovery; +use Http\Discovery\MessageFactoryDiscovery; +use Http\Client\HttpClient; /** * @author William Durand @@ -18,27 +21,70 @@ class AbstractHttpProvider extends AbstractProvider { /** - * @var HttpAdapterInterface + * @var HttpClient */ - private $adapter; + private $client; /** - * @param HttpAdapterInterface $adapter An HTTP adapter + * @var MessageFactory */ - public function __construct(HttpAdapterInterface $adapter) + private $messageFactory; + + /** + * @param HttpClient $client + * @param MessageFactory|null $factory + */ + public function __construct(HttpClient $client, MessageFactory $factory = null) { parent::__construct(); - $this->adapter = $adapter; + $this->client = $client; + $this->messageFactory = $factory; } /** * Returns the HTTP adapter. * - * @return HttpAdapterInterface + * @return HttpClient + */ + protected function getHttpClient() + { + return $this->client; + } + + /** + * @return MessageFactory */ - public function getAdapter() + protected function getMessageFactory() { - return $this->adapter; + if ($this->messageFactory === null) { + $this->messageFactory = MessageFactoryDiscovery::find(); + } + + return $this->messageFactory; + } + + /** + * @param HttpClient $client + * + * @return AbstractHttpProvider + */ + public function setClient(HttpClient $client) + { + $this->client = $client; + + return $this; + } + + /** + * @param MessageFactory $messageFactory + * + * @return AbstractHttpProvider + */ + public function setMessageFactory(MessageFactory $messageFactory) + { + $this->messageFactory = $messageFactory; + + return $this; } } diff --git a/src/Geocoder/Provider/AbstractProvider.php b/src/Geocoder/Provider/AbstractProvider.php index ea4050fcd..cc25fb977 100644 --- a/src/Geocoder/Provider/AbstractProvider.php +++ b/src/Geocoder/Provider/AbstractProvider.php @@ -11,6 +11,7 @@ namespace Geocoder\Provider; use Geocoder\Geocoder; +use Geocoder\Collection; use Geocoder\Model\AddressFactory; /** @@ -96,7 +97,7 @@ protected function getLocalhostDefaults() /** * @param array $data An array of data. * - * @return \Geocoder\Model\AddressCollection + * @return Collection */ protected function returnResults(array $data = []) { diff --git a/src/Geocoder/Provider/ArcGISOnline.php b/src/Geocoder/Provider/ArcGISOnline.php index da3994c98..0549e3219 100644 --- a/src/Geocoder/Provider/ArcGISOnline.php +++ b/src/Geocoder/Provider/ArcGISOnline.php @@ -12,12 +12,12 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author ALKOUM Dorian */ -class ArcGISOnline extends AbstractHttpProvider implements Provider +final class ArcGISOnline extends AbstractHttpProvider implements Provider { /** * @var string @@ -40,13 +40,13 @@ class ArcGISOnline extends AbstractHttpProvider 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 HttpClient $client 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) + public function __construct(HttpClient $client, $sourceCountry = null, $useSsl = false) { - parent::__construct($adapter); + parent::__construct($client); $this->sourceCountry = $sourceCountry; $this->protocol = $useSsl ? 'https' : 'http'; @@ -167,8 +167,9 @@ private function buildQuery($query) */ private function executeQuery($query) { - $query = $this->buildQuery($query); - $content = (string) $this->getAdapter()->get($query)->getBody(); + $query = $this->buildQuery($query); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Could not execute query "%s".', $query)); diff --git a/src/Geocoder/Provider/BingMaps.php b/src/Geocoder/Provider/BingMaps.php index 296755ead..2723c18f4 100644 --- a/src/Geocoder/Provider/BingMaps.php +++ b/src/Geocoder/Provider/BingMaps.php @@ -13,12 +13,12 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author David Guyon */ -class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider +final class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider { use LocaleTrait; @@ -38,13 +38,13 @@ class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter - * @param string $apiKey An API key - * @param string $locale A locale (optional) + * @param HttpClient $client An HTTP adapter + * @param string $apiKey An API key + * @param string $locale A locale (optional) */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null) + public function __construct(HttpClient $client, $apiKey, $locale = null) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; $this->locale = $locale; @@ -100,7 +100,8 @@ private function executeQuery($query) $query = sprintf('%s&culture=%s', $query, str_replace('_', '-', $this->getLocale())); } - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Could not execute query "%s".', $query)); diff --git a/src/Geocoder/Provider/Chain.php b/src/Geocoder/Provider/Chain.php index 5abf7bfcb..8c3f7da47 100644 --- a/src/Geocoder/Provider/Chain.php +++ b/src/Geocoder/Provider/Chain.php @@ -16,7 +16,7 @@ /** * @author Markus Bachmann */ -class Chain implements LocaleAwareProvider +final class Chain implements LocaleAwareProvider { use LocaleTrait; diff --git a/src/Geocoder/Provider/FreeGeoIp.php b/src/Geocoder/Provider/FreeGeoIp.php index 9f1aeb555..4dcd4ff02 100644 --- a/src/Geocoder/Provider/FreeGeoIp.php +++ b/src/Geocoder/Provider/FreeGeoIp.php @@ -12,12 +12,13 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Geocoder\Model\AddressCollection; +use Geocoder\Collection; +use Geocoder\Model\Address; /** * @author William Durand */ -class FreeGeoIp extends AbstractHttpProvider implements Provider +final class FreeGeoIp extends AbstractHttpProvider implements Provider { /** * @var string @@ -61,11 +62,12 @@ public function getName() /** * @param string $query * - * @return AddressCollection + * @return Collection */ private function executeQuery($query) { - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Could not execute query %s', $query)); diff --git a/src/Geocoder/Provider/GeoIP2.php b/src/Geocoder/Provider/GeoIP2.php index f6f7c6d70..e0006d3de 100644 --- a/src/Geocoder/Provider/GeoIP2.php +++ b/src/Geocoder/Provider/GeoIP2.php @@ -18,7 +18,7 @@ /** * @author Jens Wiese */ -class GeoIP2 extends AbstractProvider implements LocaleAwareProvider +final class GeoIP2 extends AbstractProvider implements LocaleAwareProvider { use LocaleTrait; diff --git a/src/Geocoder/Provider/GeoIPs.php b/src/Geocoder/Provider/GeoIPs.php index 438775555..176fb8e24 100644 --- a/src/Geocoder/Provider/GeoIPs.php +++ b/src/Geocoder/Provider/GeoIPs.php @@ -15,7 +15,7 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\QuotaExceeded; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Andrea Cristaudo @@ -23,7 +23,7 @@ * * @link http://www.geoips.com/en/developer/api-guide */ -class GeoIPs extends AbstractHttpProvider implements Provider +final class GeoIPs extends AbstractHttpProvider implements Provider { /** * @var string @@ -50,12 +50,12 @@ class GeoIPs extends AbstractHttpProvider implements Provider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter - * @param string $apiKey An API key + * @param HttpClient $client An HTTP adapter + * @param string $apiKey An API key */ - public function __construct(HttpAdapterInterface $adapter, $apiKey) + public function __construct(HttpClient $client, $apiKey) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; } @@ -107,7 +107,8 @@ public function getName() */ private function executeQuery($query) { - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Invalid response from GeoIPs API for query "%s".', $query)); diff --git a/src/Geocoder/Provider/GeoPlugin.php b/src/Geocoder/Provider/GeoPlugin.php index efe2172d0..9efa88bcd 100644 --- a/src/Geocoder/Provider/GeoPlugin.php +++ b/src/Geocoder/Provider/GeoPlugin.php @@ -16,7 +16,7 @@ /** * @author Andrea Cristaudo */ -class GeoPlugin extends AbstractHttpProvider implements Provider +final class GeoPlugin extends AbstractHttpProvider implements Provider { /** * @var string @@ -62,7 +62,8 @@ public function getName() */ private function executeQuery($query) { - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Could not execute query "%s".', $query)); diff --git a/src/Geocoder/Provider/Geoip.php b/src/Geocoder/Provider/Geoip.php index 0789bce61..e594c82b9 100644 --- a/src/Geocoder/Provider/Geoip.php +++ b/src/Geocoder/Provider/Geoip.php @@ -19,7 +19,7 @@ * * @link http://php.net/manual/ref.geoip.php */ -class Geoip extends AbstractProvider implements Provider +final class Geoip extends AbstractProvider implements Provider { public function __construct() { diff --git a/src/Geocoder/Provider/Geonames.php b/src/Geocoder/Provider/Geonames.php index c7fe3425f..2649c1458 100644 --- a/src/Geocoder/Provider/Geonames.php +++ b/src/Geocoder/Provider/Geonames.php @@ -14,12 +14,12 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; use Geocoder\Model\AdminLevelCollection; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Giovanni Pirrotta */ -class Geonames extends AbstractHttpProvider implements LocaleAwareProvider +final class Geonames extends AbstractHttpProvider implements LocaleAwareProvider { /** * @var string @@ -39,13 +39,13 @@ class Geonames extends AbstractHttpProvider implements LocaleAwareProvider private $username; /** - * @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 HttpClient $client An HTTP adapter + * @param string $username Username login (Free registration at http://www.geonames.org/login) + * @param string $locale A locale (optional) */ - public function __construct(HttpAdapterInterface $adapter, $username, $locale = null) + public function __construct(HttpClient $client, $username, $locale = null) { - parent::__construct($adapter); + parent::__construct($client); $this->username = $username; $this->locale = $locale; @@ -102,7 +102,8 @@ private function executeQuery($query) $query = sprintf('%s&lang=%s', $query, substr($this->getLocale(), 0, 2)); } - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Could not execute query "%s".', $query)); diff --git a/src/Geocoder/Provider/GoogleMaps.php b/src/Geocoder/Provider/GoogleMaps.php index 031dceb76..d3bbc2ad6 100644 --- a/src/Geocoder/Provider/GoogleMaps.php +++ b/src/Geocoder/Provider/GoogleMaps.php @@ -15,22 +15,32 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\QuotaExceeded; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author William Durand */ -class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider +final class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider { /** * @var string */ - const ENDPOINT_URL = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s'; + const GEOCODE_ENDPOINT_URL = 'http://maps.googleapis.com/maps/api/geocode/json?address=%s'; /** * @var string */ - const ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s'; + const GEOCODE_ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s'; + + /** + * @var string + */ + const REVERSE_ENDPOINT_URL = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=%F,%F'; + + /** + * @var string + */ + const REVERSE_ENDPOINT_URL_SSL = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=%F,%F'; use LocaleTrait; @@ -50,15 +60,47 @@ class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider private $apiKey; /** - * @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) + * @var string + */ + private $clientId; + + /** + * @var string + */ + private $privateKey; + + /** + * Google Maps for Business + * https://developers.google.com/maps/documentation/business/ + * + * @param HttpClient $client An HTTP adapter + * @param string $clientId Your Client ID + * @param string $privateKey Your Private Key (optional) + * @param string $locale A locale (optional) + * @param string $region Region biasing (optional) + * @param bool $useSsl Whether to use an SSL connection (optional) + * @param string $apiKey Google Geocoding API key (optional) + * @return GoogleMaps + */ + public static function business(HttpClient $client, $clientId, $privateKey = null, $locale = null, $region = null, $useSsl = false, $apiKey = null) + { + $provider = new self($client, $locale, $region, $useSsl, $apiKey); + $provider->clientId = $clientId; + $provider->privateKey = $privateKey; + + return $provider; + } + + /** + * @param HttpClient $client An HTTP adapter + * @param string $locale A locale (optional) + * @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) + public function __construct(HttpClient $client, $locale = null, $region = null, $useSsl = false, $apiKey = null) { - parent::__construct($adapter); + parent::__construct($client); $this->locale = $locale; $this->region = $region; @@ -78,7 +120,7 @@ public function geocode($address) } $query = sprintf( - $this->useSsl ? self::ENDPOINT_URL_SSL : self::ENDPOINT_URL, + $this->useSsl ? self::GEOCODE_ENDPOINT_URL_SSL : self::GEOCODE_ENDPOINT_URL, rawurlencode($address) ); @@ -90,7 +132,12 @@ public function geocode($address) */ public function reverse($latitude, $longitude) { - return $this->geocode(sprintf('%F,%F', $latitude, $longitude)); + $query = sprintf( + $this->useSsl ? self::REVERSE_ENDPOINT_URL_SSL : self::REVERSE_ENDPOINT_URL, + $latitude, $longitude + ); + + return $this->executeQuery($query); } /** @@ -113,7 +160,7 @@ public function setRegion($region) * * @return string query with extra params */ - protected function buildQuery($query) + private function buildQuery($query) { if (null !== $this->getLocale()) { $query = sprintf('%s&language=%s', $query, $this->getLocale()); @@ -127,6 +174,14 @@ protected function buildQuery($query) $query = sprintf('%s&key=%s', $query, $this->apiKey); } + if (null !== $this->clientId) { + $query = sprintf('%s&client=%s', $query, $this->clientId); + + if (null !== $this->privateKey) { + $query = $this->signQuery($query); + } + } + return $query; } @@ -136,7 +191,8 @@ protected function buildQuery($query) private function executeQuery($query) { $query = $this->buildQuery($query); - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); // Throw exception if invalid clientID and/or privateKey used with GoogleMapsBusinessProvider if (strpos($content, "Provided 'signature' is not valid for the provided client ID") !== false) { @@ -268,4 +324,31 @@ private function updateAddressComponent(&$resultSet, $type, $values) return $resultSet; } + + /** + * Sign a URL with a given crypto key + * Note that this URL must be properly URL-encoded + * src: http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source + * + * @param string $query Query to be signed + * + * @return string $query Query with signature appended. + */ + private function signQuery($query) + { + $url = parse_url($query); + + $urlPartToSign = $url['path'] . '?' . $url['query']; + + // Decode the private key into its binary format + $decodedKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $this->privateKey)); + + // Create a signature using the private key and the URL-encoded + // string using HMAC SHA1. This signature will be binary. + $signature = hash_hmac('sha1', $urlPartToSign, $decodedKey, true); + + $encodedSignature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature)); + + return sprintf('%s&signature=%s', $query, $encodedSignature); + } } diff --git a/src/Geocoder/Provider/GoogleMapsBusiness.php b/src/Geocoder/Provider/GoogleMapsBusiness.php deleted file mode 100644 index 889c58cf2..000000000 --- a/src/Geocoder/Provider/GoogleMapsBusiness.php +++ /dev/null @@ -1,98 +0,0 @@ - - */ -class GoogleMapsBusiness extends GoogleMaps implements Provider -{ - /** - * @var string - */ - private $clientId; - - /** - * @var string - */ - private $privateKey; - - /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $clientId Your Client ID. - * @param string $privateKey Your Private Key (optional). - * @param string $locale A locale (optional). - * @param string $region Region biasing (optional). - * @param bool $useSsl Whether to use an SSL connection (optional) - */ - public function __construct(HttpAdapterInterface $adapter, $clientId, $privateKey = null, $locale = null, $region = null, $useSsl = false) - { - parent::__construct($adapter, $locale, $region, $useSsl); - - $this->clientId = $clientId; - $this->privateKey = $privateKey; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'google_maps_business'; - } - - /** - * {@inheritDoc} - */ - protected function buildQuery($query) - { - $query = parent::buildQuery($query); - $query = sprintf('%s&client=%s', $query, $this->clientId); - - if (null !== $this->privateKey) { - $query = $this->signQuery($query); - } - - return $query; - } - - /** - * Sign a URL with a given crypto key - * Note that this URL must be properly URL-encoded - * src: http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source - * - * @param string $query Query to be signed - * - * @return string $query Query with signature appended. - */ - private function signQuery($query) - { - $url = parse_url($query); - - $urlPartToSign = $url['path'] . '?' . $url['query']; - - // Decode the private key into its binary format - $decodedKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $this->privateKey)); - - // Create a signature using the private key and the URL-encoded - // string using HMAC SHA1. This signature will be binary. - $signature = hash_hmac('sha1', $urlPartToSign, $decodedKey, true); - - $encodedSignature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature)); - - return sprintf('%s&signature=%s', $query, $encodedSignature); - } -} diff --git a/src/Geocoder/Provider/HostIp.php b/src/Geocoder/Provider/HostIp.php index d3c258af1..2c069e6f5 100644 --- a/src/Geocoder/Provider/HostIp.php +++ b/src/Geocoder/Provider/HostIp.php @@ -12,11 +12,12 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; +use Geocoder\Collection; /** * @author William Durand */ -class HostIp extends AbstractHttpProvider implements Provider +final class HostIp extends AbstractHttpProvider implements Provider { /** * @var string @@ -65,11 +66,12 @@ public function getName() /** * @param string $query * - * @return \Geocoder\Model\AddressCollection + * @return Collection */ private function executeQuery($query) { - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); $data = json_decode($content, true); diff --git a/src/Geocoder/Provider/IpInfoDb.php b/src/Geocoder/Provider/IpInfoDb.php index a2ab2dc76..5e92d92e0 100644 --- a/src/Geocoder/Provider/IpInfoDb.php +++ b/src/Geocoder/Provider/IpInfoDb.php @@ -14,12 +14,13 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Geocoder\Collection; +use Http\Client\HttpClient; /** * @author William Durand */ -class IpInfoDb extends AbstractHttpProvider implements Provider +final class IpInfoDb extends AbstractHttpProvider implements Provider { /** * @var string @@ -42,15 +43,15 @@ class IpInfoDb extends AbstractHttpProvider implements Provider private $endpointUrl; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param string $precision The endpoint precision. Either "city" or "country" (faster) + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param string $precision The endpoint precision. Either "city" or "country" (faster) * - * @throws Geocoder\Exception\InvalidArgument + * @throws \Geocoder\Exception\InvalidArgument */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $precision = 'city') + public function __construct(HttpClient $client, $apiKey, $precision = 'city') { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; switch ($precision) { @@ -117,11 +118,12 @@ public function getName() /** * @param string $query * - * @return \Geocoder\Model\AddressCollection + * @return Collection */ private function executeQuery($query) { - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Could not execute query "%s".', $query)); diff --git a/src/Geocoder/Provider/MapQuest.php b/src/Geocoder/Provider/MapQuest.php index 0e37cac46..55e1878ad 100644 --- a/src/Geocoder/Provider/MapQuest.php +++ b/src/Geocoder/Provider/MapQuest.php @@ -13,12 +13,12 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author William Durand */ -class MapQuest extends AbstractHttpProvider implements Provider +final class MapQuest extends AbstractHttpProvider implements Provider { /** * @var string @@ -54,13 +54,13 @@ class MapQuest extends AbstractHttpProvider implements Provider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param bool $licensed True to use MapQuest's licensed endpoints, default is false to use the open endpoints (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param bool $licensed True to use MapQuest's licensed endpoints, default is false to use the open endpoints (optional). */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $licensed = false) + public function __construct(HttpClient $client, $apiKey, $licensed = false) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; $this->licensed = $licensed; @@ -120,7 +120,8 @@ public function getName() */ private function executeQuery($query) { - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Could not execute query "%s".', $query)); diff --git a/src/Geocoder/Provider/Mapzen.php b/src/Geocoder/Provider/Mapzen.php new file mode 100644 index 000000000..0641168db --- /dev/null +++ b/src/Geocoder/Provider/Mapzen.php @@ -0,0 +1,223 @@ + + */ +final class Mapzen extends AbstractHttpProvider +{ + /** + * @var string + */ + const GEOCODE_ENDPOINT_URL = '%s://search.mapzen.com/v1/search?text=%s&key=%s&size=%d'; + + /** + * @var string + */ + const REVERSE_ENDPOINT_URL = '%s://search.mapzen.com/v1/reverse?point.lat=%f&point.lon=%f&key=%s&size=%d'; + + /** + * @var string + */ + private $scheme; + + /** + * @var string + */ + private $apiKey; + + /** + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param bool $useSsl Whether to use an SSL connection (optional). + */ + public function __construct(HttpClient $client, $apiKey, $useSSL = true) + { + parent::__construct($client); + + $this->apiKey = $apiKey; + $this->scheme = $useSSL ? 'https' : 'http'; + } + + /** + * {@inheritdoc} + */ + public function geocode($address) + { + 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 Mapzen provider does not support IP addresses, only street addresses.'); + } + + $query = sprintf(self::GEOCODE_ENDPOINT_URL, $this->scheme, urlencode($address), $this->apiKey, $this->getLimit()); + + return $this->executeQuery($query); + } + + /** + * {@inheritdoc} + */ + public function reverse($latitude, $longitude) + { + if (null === $this->apiKey) { + throw new InvalidCredentials('No API Key provided.'); + } + + $query = sprintf(self::REVERSE_ENDPOINT_URL, $this->scheme, $latitude, $longitude, $this->apiKey, $this->getLimit()); + + return $this->executeQuery($query); + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'mapzen'; + } + + /** + * @param $query + * @return \Geocoder\Model\AddressCollection + */ + private function executeQuery($query) + { + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); + + if (empty($content)) { + throw new NoResult(sprintf('Could not execute query "%s".', $query)); + } + + $json = json_decode($content, true); + + // See https://mapzen.com/documentation/search/api-keys-rate-limits/ + if (isset($json['meta'])) { + switch ($json['meta']['status_code']) { + case 403: + throw new InvalidCredentials('Invalid or missing api key.'); + case 429: + throw new QuotaExceeded('Valid request but quota exceeded.'); + } + } + + if (!isset($json['type']) || $json['type'] !== 'FeatureCollection' || !isset($json['features']) || count($json['features']) === 0) { + throw new NoResult(sprintf('Could not find results for query "%s".', $query)); + } + + $locations = $json['features']; + + if (empty($locations)) { + throw new NoResult(sprintf('Could not find results for query "%s".', $query)); + } + + $results = []; + foreach ($locations as $location) { + $bounds = []; + if (isset($location['bbox'])) { + $bounds = [ + 'south' => $location['bbox'][3], + 'west' => $location['bbox'][2], + 'north' => $location['bbox'][1], + 'east' => $location['bbox'][0], + ]; + } + + $props = $location['properties']; + + $adminLevels = []; + foreach (['region', 'locality', 'macroregion', 'country'] as $i => $component) { + if (isset($props[$component])) { + $adminLevels[] = ['name' => $props[$component], 'level' => $i + 1]; + } + } + + $results[] = array_merge($this->getDefaults(), array( + 'latitude' => $location['geometry']['coordinates'][1], + 'longitude' => $location['geometry']['coordinates'][0], + 'bounds' => $bounds ?: [], + 'streetNumber' => isset($props['housenumber']) ? $props['housenumber'] : null, + 'streetName' => isset($props['street']) ? $props['street'] : null, + 'subLocality' => isset($props['neighbourhood']) ? $props['neighbourhood'] : null, + 'locality' => isset($props['locality']) ? $props['locality'] : null, + 'postalCode' => isset($props['postalcode']) ? $props['postalcode'] : null, + 'adminLevels' => $adminLevels, + 'country' => isset($props['country']) ? $props['country'] : null, + 'countryCode' => isset($props['country_a']) ? strtoupper($props['country_a']) : null + )); + } + + return $this->returnResults($results); + } + + /** + * @param array $components + * + * @return null|string + */ + protected function guessLocality(array $components) + { + $localityKeys = array('city', 'town' , 'village', 'hamlet'); + + return $this->guessBestComponent($components, $localityKeys); + } + + /** + * @param array $components + * + * @return null|string + */ + protected function guessStreetName(array $components) + { + $streetNameKeys = array('road', 'street', 'street_name', 'residential'); + + return $this->guessBestComponent($components, $streetNameKeys); + } + + /** + * @param array $components + * + * @return null|string + */ + protected function guessSubLocality(array $components) + { + $subLocalityKeys = array('neighbourhood', 'city_district'); + + return $this->guessBestComponent($components, $subLocalityKeys); + } + + /** + * @param array $components + * @param array $keys + * + * @return null|string + */ + protected function guessBestComponent(array $components, array $keys) + { + foreach ($keys as $key) { + if (isset($components[$key]) && !empty($components[$key])) { + return $components[$key]; + } + } + + return null; + } +} diff --git a/src/Geocoder/Provider/MaxMind.php b/src/Geocoder/Provider/MaxMind.php index 5b5ffee3e..5eb6e0212 100644 --- a/src/Geocoder/Provider/MaxMind.php +++ b/src/Geocoder/Provider/MaxMind.php @@ -13,12 +13,12 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Andrea Cristaudo */ -class MaxMind extends AbstractHttpProvider implements Provider +final class MaxMind extends AbstractHttpProvider implements Provider { /** * @var string Country, City, ISP and Organization @@ -56,14 +56,14 @@ class MaxMind extends AbstractHttpProvider implements Provider private $useSsl = false; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param string $service The specific Maxmind service to use (optional). - * @param bool $useSsl Whether to use an SSL connection (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @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(HttpClient $client, $apiKey, $service = self::CITY_EXTENDED_SERVICE, $useSsl = false) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; $this->service = $service; @@ -116,7 +116,8 @@ public function getName() */ private function executeQuery($query) { - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); $fields = $this->fieldsForService($this->service); if (null === $content || '' === $content) { diff --git a/src/Geocoder/Provider/MaxMindBinary.php b/src/Geocoder/Provider/MaxMindBinary.php index c7a7d19dd..7f59f147c 100644 --- a/src/Geocoder/Provider/MaxMindBinary.php +++ b/src/Geocoder/Provider/MaxMindBinary.php @@ -15,7 +15,7 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -class MaxMindBinary extends AbstractProvider implements Provider +final class MaxMindBinary extends AbstractProvider implements Provider { /** * @var string diff --git a/src/Geocoder/Provider/Nominatim.php b/src/Geocoder/Provider/Nominatim.php index d1a73d24b..d903c2ff9 100644 --- a/src/Geocoder/Provider/Nominatim.php +++ b/src/Geocoder/Provider/Nominatim.php @@ -12,12 +12,12 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Niklas Närhinen */ -class Nominatim extends AbstractHttpProvider implements LocaleAwareProvider +final class Nominatim extends AbstractHttpProvider implements LocaleAwareProvider { use LocaleTrait; @@ -27,13 +27,23 @@ class Nominatim extends AbstractHttpProvider implements LocaleAwareProvider private $rootUrl; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $rootUrl Root URL of the nominatim server - * @param string $locale A locale (optional). + * @param HttpClient $client + * @param string|null $locale + * @return Nominatim */ - public function __construct(HttpAdapterInterface $adapter, $rootUrl, $locale = null) + public static function withOpenStreetMapServer(HttpClient $client, $locale = null) { - parent::__construct($adapter); + return new self($client, 'http://nominatim.openstreetmap.org', $locale); + } + + /** + * @param HttpClient $client An HTTP adapter. + * @param string $rootUrl Root URL of the nominatim server + * @param string $locale A locale (optional). + */ + public function __construct(HttpClient $client, $rootUrl, $locale = null) + { + parent::__construct($client); $this->rootUrl = rtrim($rootUrl, '/'); $this->locale = $locale; @@ -163,7 +173,9 @@ private function executeQuery($query) $query = sprintf('%s&accept-language=%s', $query, $this->getLocale()); } - return (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + + return (string) $this->getHttpClient()->sendRequest($request)->getBody(); } private function getGeocodeEndpointUrl() diff --git a/src/Geocoder/Provider/OpenCage.php b/src/Geocoder/Provider/OpenCage.php index 5d48e9fc8..ee3e2d3e3 100644 --- a/src/Geocoder/Provider/OpenCage.php +++ b/src/Geocoder/Provider/OpenCage.php @@ -13,12 +13,13 @@ use Geocoder\Exception\QuotaExceeded; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Geocoder\Collection; +use Http\Client\HttpClient; /** * @author mtm */ -class OpenCage extends AbstractHttpProvider implements LocaleAwareProvider +final class OpenCage extends AbstractHttpProvider implements LocaleAwareProvider { /** * @var string @@ -38,14 +39,14 @@ class OpenCage extends AbstractHttpProvider implements LocaleAwareProvider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param bool $useSsl Whether to use an SSL connection (optional). - * @param string|null $locale A locale (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param bool $useSsl Whether to use an SSL connection (optional). + * @param string|null $locale A locale (optional). */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $useSsl = false, $locale = null) + public function __construct(HttpClient $client, $apiKey, $useSsl = false, $locale = null) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; $this->scheme = $useSsl ? 'https' : 'http'; @@ -91,7 +92,7 @@ public function getName() /** * @param $query - * @return \Geocoder\Model\AddressCollection + * @return Collection */ private function executeQuery($query) { @@ -99,7 +100,8 @@ private function executeQuery($query) $query = sprintf('%s&language=%s', $query, $this->getLocale()); } - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (empty($content)) { throw new NoResult(sprintf('Could not execute query "%s".', $query)); diff --git a/src/Geocoder/Provider/OpenStreetMap.php b/src/Geocoder/Provider/OpenStreetMap.php deleted file mode 100644 index 8097d3474..000000000 --- a/src/Geocoder/Provider/OpenStreetMap.php +++ /dev/null @@ -1,41 +0,0 @@ - - */ -class OpenStreetMap extends Nominatim -{ - /** - * @var string - */ - const ROOT_URL = 'http://nominatim.openstreetmap.org'; - - /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $locale A locale (optional). - */ - 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/TomTom.php b/src/Geocoder/Provider/TomTom.php index fefec969f..731d1c5f2 100644 --- a/src/Geocoder/Provider/TomTom.php +++ b/src/Geocoder/Provider/TomTom.php @@ -13,12 +13,12 @@ use Geocoder\Exception\InvalidCredentials; use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Antoine Corcy */ -class TomTom extends AbstractHttpProvider implements LocaleAwareProvider +final class TomTom extends AbstractHttpProvider implements LocaleAwareProvider { use LocaleTrait; @@ -38,13 +38,13 @@ class TomTom extends AbstractHttpProvider implements LocaleAwareProvider private $apiKey; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $apiKey An API key. - * @param string $locale A locale (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $apiKey An API key. + * @param string $locale A locale (optional). */ - public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null) + public function __construct(HttpClient $client, $apiKey, $locale = null) { - parent::__construct($adapter); + parent::__construct($client); $this->apiKey = $apiKey; $this->locale = $locale; @@ -102,7 +102,8 @@ private function executeQuery($query) $query = sprintf('%s&language=%s', $query, substr($this->getLocale(), 0, 2)); } - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); if (false !== stripos($content, "Developer Inactive")) { throw new InvalidCredentials('Map API Key provided is not valid.'); diff --git a/src/Geocoder/Provider/Yandex.php b/src/Geocoder/Provider/Yandex.php index 2a3f08d4f..e41e3d12b 100644 --- a/src/Geocoder/Provider/Yandex.php +++ b/src/Geocoder/Provider/Yandex.php @@ -12,12 +12,12 @@ use Geocoder\Exception\NoResult; use Geocoder\Exception\UnsupportedOperation; -use Ivory\HttpAdapter\HttpAdapterInterface; +use Http\Client\HttpClient; /** * @author Antoine Corcy */ -class Yandex extends AbstractHttpProvider implements LocaleAwareProvider +final class Yandex extends AbstractHttpProvider implements LocaleAwareProvider { use LocaleTrait; @@ -37,13 +37,13 @@ class Yandex extends AbstractHttpProvider implements LocaleAwareProvider private $toponym; /** - * @param HttpAdapterInterface $adapter An HTTP adapter. - * @param string $locale A locale (optional). - * @param string $toponym Toponym biasing only for reverse geocoding (optional). + * @param HttpClient $client An HTTP adapter. + * @param string $locale A locale (optional). + * @param string $toponym Toponym biasing only for reverse geocoding (optional). */ - public function __construct(HttpAdapterInterface $adapter, $locale = null, $toponym = null) + public function __construct(HttpClient $client, $locale = null, $toponym = null) { - parent::__construct($adapter); + parent::__construct($client); $this->locale = $locale; $this->toponym = $toponym; @@ -97,7 +97,8 @@ private function executeQuery($query) $query = sprintf('%s&results=%d', $query, $this->getLimit()); - $content = (string) $this->getAdapter()->get($query)->getBody(); + $request = $this->getMessageFactory()->createRequest('GET', $query); + $content = (string) $this->getHttpClient()->sendRequest($request)->getBody(); $json = (array) json_decode($content, true); if (empty($json) || isset($json['error']) || diff --git a/src/Geocoder/ProviderAggregator.php b/src/Geocoder/ProviderAggregator.php index 33073b0ab..5ecf43fd0 100644 --- a/src/Geocoder/ProviderAggregator.php +++ b/src/Geocoder/ProviderAggregator.php @@ -11,8 +11,9 @@ namespace Geocoder; use Geocoder\Exception\ProviderNotRegistered; -use Geocoder\Provider\Provider; use Geocoder\Model\AddressCollection; +use Geocoder\Provider\Provider; +use Geocoder\Model\Address; /** * @author William Durand diff --git a/tests/.cached_responses/00a3bd9f9306c9ae3b8aa7aa0c85d71d6d5de8fa b/tests/.cached_responses/00a3bd9f9306c9ae3b8aa7aa0c85d71d6d5de8fa new file mode 100644 index 000000000..2297d3097 --- /dev/null +++ b/tests/.cached_responses/00a3bd9f9306c9ae3b8aa7aa0c85d71d6d5de8fa @@ -0,0 +1 @@ +s:3418:"{"geocoding":{"version":"0.1","attribution":"https://search.mapzen.com/v1/attribution","query":{"text":"Hanover","size":5,"private":false,"querySize":10},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1473682048001},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-88.204203,42.027323]},"properties":{"id":"404497623","gid":"whosonfirst:localadmin:404497623","layer":"localadmin","source":"whosonfirst","source_id":"404497623","name":"Hanover","confidence":0.951,"country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Illinois","region_gid":"whosonfirst:region:85688697","region_a":"IL","county":"Cook County","county_gid":"whosonfirst:county:102084317","localadmin":"Hanover","localadmin_gid":"whosonfirst:localadmin:404497623","label":"Hanover, IL, USA"},"bbox":[-88.263572,41.986227,-88.14458,42.067434]},{"type":"Feature","geometry":{"type":"Point","coordinates":[-78.122906,18.393428]},"properties":{"id":"85672563","gid":"whosonfirst:region:85672563","layer":"region","source":"whosonfirst","source_id":"85672563","name":"Hanover","confidence":0.944,"country":"Jamaica","country_gid":"whosonfirst:country:85632215","country_a":"JAM","region":"Hanover","region_gid":"whosonfirst:region:85672563","label":"Hanover, Jamaica"},"bbox":[-78.3460426069,18.2897883164,-77.9214139468,18.4554710959]},{"type":"Feature","geometry":{"type":"Point","coordinates":[-76.72414,39.19289]},"properties":{"id":"4357340","gid":"geonames:locality:4357340","layer":"locality","source":"geonames","source_id":"4357340","name":"Hanover","confidence":0.734,"country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Maryland","region_gid":"whosonfirst:region:85688501","region_a":"MD","county":"Howard County","county_gid":"whosonfirst:county:102084263","locality":"Hanover","locality_gid":"whosonfirst:locality:4357340","label":"Hanover, MD, USA"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-70.81199,42.11316]},"properties":{"id":"4938836","gid":"geonames:locality:4938836","layer":"locality","source":"geonames","source_id":"4938836","name":"Hanover","confidence":0.72,"country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Massachusetts","region_gid":"whosonfirst:region:85688645","region_a":"MA","county":"Plymouth County","county_gid":"whosonfirst:county:102084367","localadmin":"Hanover","localadmin_gid":"whosonfirst:localadmin:404476511","locality":"Hanover","locality_gid":"whosonfirst:locality:4938836","label":"Hanover, MA, USA"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-76.984032,39.81179]},"properties":{"id":"101717245","gid":"whosonfirst:locality:101717245","layer":"locality","source":"whosonfirst","source_id":"101717245","name":"Hanover","confidence":0.718,"country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Pennsylvania","region_gid":"whosonfirst:region:85688481","region_a":"PA","county":"York County","county_gid":"whosonfirst:county:102080953","localadmin":"Hanover","localadmin_gid":"whosonfirst:localadmin:404487771","locality":"Hanover","locality_gid":"whosonfirst:locality:101717245","label":"Hanover, PA, USA"},"bbox":[-76.999944,39.791156,-76.963061,39.831769]}],"bbox":[-88.263572,18.2897883164,-70.81199,42.11316]}"; \ No newline at end of file diff --git a/tests/.cached_responses/404ab562295927999aba94e1e615fca96186879e b/tests/.cached_responses/404ab562295927999aba94e1e615fca96186879e new file mode 100644 index 000000000..2c0d5f923 --- /dev/null +++ b/tests/.cached_responses/404ab562295927999aba94e1e615fca96186879e @@ -0,0 +1 @@ +s:4780:"{"geocoding":{"version":"0.1","attribution":"https://search.mapzen.com/v1/attribution","query":{"text":"242 Acklam Road, London, United Kingdom","parsed_text":{"name":"242 Acklam Road","number":"242","street":"Acklam Road","regions":["London","United Kingdom"],"admin_parts":"London, United Kingdom"},"size":5,"private":false,"querySize":10},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1473674363289},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-0.203602,51.521124]},"properties":{"id":"node:3711723386","gid":"openstreetmap:address:node:3711723386","layer":"address","source":"openstreetmap","source_id":"node:3711723386","name":"240 Acklam Road","housenumber":"240","street":"Acklam Road","postalcode":"W10 5QT","confidence":0.866,"country":"United Kingdom","country_gid":"whosonfirst:country:85633159","country_a":"GBR","macroregion":"England","macroregion_gid":"whosonfirst:macroregion:404227469","region":"Kensington and Chelsea","region_gid":"whosonfirst:region:85683667","locality":"London","locality_gid":"whosonfirst:locality:101750367","neighbourhood":"Westbourne Green","neighbourhood_gid":"whosonfirst:neighbourhood:85864267","label":"240 Acklam Road, London, England, United Kingdom"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-1.282082,54.552668]},"properties":{"id":"node:1485615866","gid":"openstreetmap:address:node:1485615866","layer":"address","source":"openstreetmap","source_id":"node:1485615866","name":"Teesdale Park Acklam Road","housenumber":"Teesdale Park","street":"Acklam Road","postalcode":"TS17 7JU","confidence":0.866,"country":"United Kingdom","country_gid":"whosonfirst:country:85633159","country_a":"GBR","macroregion":"England","macroregion_gid":"whosonfirst:macroregion:404227469","region":"Stockton-on-Tees","region_gid":"whosonfirst:region:85683777","localadmin":"Thornaby","localadmin_gid":"whosonfirst:localadmin:404452219","neighbourhood":"Acklam","neighbourhood_gid":"whosonfirst:neighbourhood:85785205","label":"Teesdale Park Acklam Road, Thornaby, England, United Kingdom"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-70.568928,41.565854]},"properties":{"id":"us/ma/statewide:862559","gid":"openaddresses:address:us/ma/statewide:862559","layer":"address","source":"openaddresses","source_id":"us/ma/statewide:862559","name":"242 Acapesket Road","housenumber":"242","street":"Acapesket Road","postalcode":"02536","confidence":0.647,"country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Massachusetts","region_gid":"whosonfirst:region:85688645","region_a":"MA","county":"Barnstable County","county_gid":"whosonfirst:county:102084379","localadmin":"Falmouth","localadmin_gid":"whosonfirst:localadmin:404476181","locality":"East Falmouth","locality_gid":"whosonfirst:locality:85950545","neighbourhood":"Kingdom Hall","neighbourhood_gid":"whosonfirst:neighbourhood:85828363","label":"242 Acapesket Road, East Falmouth, MA, USA"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-72.492505,42.704231]},"properties":{"id":"us/ma/statewide:1436753","gid":"openaddresses:address:us/ma/statewide:1436753","layer":"address","source":"openaddresses","source_id":"us/ma/statewide:1436753","name":"242 Old Vernon Road","housenumber":"242","street":"Old Vernon Road","postalcode":"01360","confidence":0.647,"country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Massachusetts","region_gid":"whosonfirst:region:85688645","region_a":"MA","county":"Franklin County","county_gid":"whosonfirst:county:102084457","localadmin":"Northfield","localadmin_gid":"whosonfirst:localadmin:404475965","neighbourhood":"Satans Kingdom","neighbourhood_gid":"whosonfirst:neighbourhood:420523529","label":"242 Old Vernon Road, Northfield, MA, USA"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-70.528914,41.591705]},"properties":{"id":"us/ma/statewide:2148130","gid":"openaddresses:address:us/ma/statewide:2148130","layer":"address","source":"openaddresses","source_id":"us/ma/statewide:2148130","name":"242 Carriage Shop Road","housenumber":"242","street":"Carriage Shop Road","postalcode":"02536","confidence":0.647,"country":"United States","country_gid":"whosonfirst:country:85633793","country_a":"USA","region":"Massachusetts","region_gid":"whosonfirst:region:85688645","region_a":"MA","county":"Barnstable County","county_gid":"whosonfirst:county:102084379","localadmin":"Falmouth","localadmin_gid":"whosonfirst:localadmin:404476181","neighbourhood":"Kingdom Hall","neighbourhood_gid":"whosonfirst:neighbourhood:85828363","label":"242 Carriage Shop Road, Falmouth, MA, USA"}}],"bbox":[-72.492505,41.565854,-0.203602,54.552668]}"; \ No newline at end of file diff --git a/tests/.cached_responses/56e8d5a5657a1015d32ab35096598c62df39852a b/tests/.cached_responses/56e8d5a5657a1015d32ab35096598c62df39852a new file mode 100644 index 000000000..8f93673a4 --- /dev/null +++ b/tests/.cached_responses/56e8d5a5657a1015d32ab35096598c62df39852a @@ -0,0 +1 @@ +s:5120:"{"geocoding":{"version":"0.1","attribution":"https://search.mapzen.com/v1/attribution","query":{"text":"Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany","parsed_text":{"name":"Kalbacher Hauptstraße 10","postalcode":"60437","regions":["Kalbacher Hauptstraße 10","Frankfurt","Germany"],"admin_parts":"60437 Frankfurt, Germany"},"size":5,"private":false,"querySize":10},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1473682507033},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[8.636781,50.189017]},"properties":{"id":"de/he/city_of_frankfurtammain:72948","gid":"openaddresses:address:de/he/city_of_frankfurtammain:72948","layer":"address","source":"openaddresses","source_id":"de/he/city_of_frankfurtammain:72948","name":"Kalbacher Hauptstraße 10a","housenumber":"10a","street":"Kalbacher Hauptstraße","postalcode":"60437","confidence":0.88,"country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Hessen","region_gid":"whosonfirst:region:85682531","macrocounty":"Darmstadt","macrocounty_gid":"whosonfirst:macrocounty:404227581","county":"Frankfurt am Main","county_gid":"whosonfirst:county:102063589","locality":"Frankfurt am Main","locality_gid":"whosonfirst:locality:101913837","neighbourhood":"Römerstadt","neighbourhood_gid":"whosonfirst:neighbourhood:85796311","label":"Kalbacher Hauptstraße 10a, Frankfurt am Main, Germany"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.636575,50.189044]},"properties":{"id":"de/he/city_of_frankfurtammain:72883","gid":"openaddresses:address:de/he/city_of_frankfurtammain:72883","layer":"address","source":"openaddresses","source_id":"de/he/city_of_frankfurtammain:72883","name":"Kalbacher Hauptstraße 10","housenumber":"10","street":"Kalbacher Hauptstraße","postalcode":"60437","confidence":0.88,"country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Hessen","region_gid":"whosonfirst:region:85682531","macrocounty":"Darmstadt","macrocounty_gid":"whosonfirst:macrocounty:404227581","county":"Frankfurt am Main","county_gid":"whosonfirst:county:102063589","locality":"Frankfurt am Main","locality_gid":"whosonfirst:locality:101913837","neighbourhood":"Römerstadt","neighbourhood_gid":"whosonfirst:neighbourhood:85796311","label":"Kalbacher Hauptstraße 10, Frankfurt am Main, Germany"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.635608,50.188253]},"properties":{"id":"way:109149380","gid":"openstreetmap:address:way:109149380","layer":"address","source":"openstreetmap","source_id":"way:109149380","name":"Kalbacher Hauptstraße 5","housenumber":"5","street":"Kalbacher Hauptstraße","postalcode":"60437","confidence":0.655,"country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Hessen","region_gid":"whosonfirst:region:85682531","macrocounty":"Darmstadt","macrocounty_gid":"whosonfirst:macrocounty:404227581","county":"Frankfurt am Main","county_gid":"whosonfirst:county:102063589","locality":"Frankfurt am Main","locality_gid":"whosonfirst:locality:101913837","neighbourhood":"Römerstadt","neighbourhood_gid":"whosonfirst:neighbourhood:85796311","label":"Kalbacher Hauptstraße 5, Frankfurt am Main, Germany"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.646009,50.187199]},"properties":{"id":"way:109282050","gid":"openstreetmap:address:way:109282050","layer":"address","source":"openstreetmap","source_id":"way:109282050","name":"Kalbacher Hauptstraße 78","housenumber":"78","street":"Kalbacher Hauptstraße","postalcode":"60437","confidence":0.655,"country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Hessen","region_gid":"whosonfirst:region:85682531","macrocounty":"Darmstadt","macrocounty_gid":"whosonfirst:macrocounty:404227581","county":"Frankfurt am Main","county_gid":"whosonfirst:county:102063589","locality":"Frankfurt am Main","locality_gid":"whosonfirst:locality:101913837","neighbourhood":"Römerstadt","neighbourhood_gid":"whosonfirst:neighbourhood:85796311","label":"Kalbacher Hauptstraße 78, Frankfurt am Main, Germany"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[8.646989,50.18735]},"properties":{"id":"way:109282307","gid":"openstreetmap:address:way:109282307","layer":"address","source":"openstreetmap","source_id":"way:109282307","name":"Kalbacher Hauptstraße 115","housenumber":"115","street":"Kalbacher Hauptstraße","postalcode":"60437","confidence":0.655,"country":"Germany","country_gid":"whosonfirst:country:85633111","country_a":"DEU","region":"Hessen","region_gid":"whosonfirst:region:85682531","macrocounty":"Darmstadt","macrocounty_gid":"whosonfirst:macrocounty:404227581","county":"Frankfurt am Main","county_gid":"whosonfirst:county:102063589","locality":"Frankfurt am Main","locality_gid":"whosonfirst:locality:101913837","neighbourhood":"Römerstadt","neighbourhood_gid":"whosonfirst:neighbourhood:85796311","label":"Kalbacher Hauptstraße 115, Frankfurt am Main, Germany"}}],"bbox":[8.635608,50.187199,8.646989,50.189044]}"; \ No newline at end of file diff --git a/tests/.cached_responses/c04af40112605168a2260e602918c6ef37f235cd b/tests/.cached_responses/c04af40112605168a2260e602918c6ef37f235cd new file mode 100644 index 000000000..c4fa7d3f1 --- /dev/null +++ b/tests/.cached_responses/c04af40112605168a2260e602918c6ef37f235cd @@ -0,0 +1 @@ +s:3836:"{"geocoding":{"version":"0.1","attribution":"https://search.mapzen.com/v1/attribution","query":{"size":5,"private":false,"point.lat":54.048407,"point.lon":-2.799034,"boundary.circle.radius":1,"boundary.circle.lat":54.048407,"boundary.circle.lon":-2.799034,"querySize":10},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1473679050630},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[-2.798955,54.048412]},"properties":{"id":"node:3930594967","gid":"openstreetmap:venue:node:3930594967","layer":"venue","source":"openstreetmap","source_id":"node:3930594967","name":"Collegian W.M.C","street":"Gage Street","confidence":0.9,"distance":0.005,"country":"United Kingdom","country_gid":"whosonfirst:country:85633159","country_a":"GBR","macroregion":"England","macroregion_gid":"whosonfirst:macroregion:404227469","region":"Lancashire","region_gid":"whosonfirst:region:85683853","locality":"Lancaster","locality_gid":"whosonfirst:locality:101873271","label":"Collegian W.M.C, Lancaster, England, United Kingdom"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-2.79913,54.048378]},"properties":{"id":"node:3930587961","gid":"openstreetmap:venue:node:3930587961","layer":"venue","source":"openstreetmap","source_id":"node:3930587961","name":"Stable End Curio's","confidence":0.9,"distance":0.007,"country":"United Kingdom","country_gid":"whosonfirst:country:85633159","country_a":"GBR","macroregion":"England","macroregion_gid":"whosonfirst:macroregion:404227469","region":"Lancashire","region_gid":"whosonfirst:region:85683853","locality":"Lancaster","locality_gid":"whosonfirst:locality:101873271","label":"Stable End Curio's, Lancaster, England, United Kingdom"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-2.798922,54.04823]},"properties":{"id":"node:985894837","gid":"openstreetmap:venue:node:985894837","layer":"venue","source":"openstreetmap","source_id":"node:985894837","name":"The Tap House","confidence":0.8,"distance":0.021,"country":"United Kingdom","country_gid":"whosonfirst:country:85633159","country_a":"GBR","macroregion":"England","macroregion_gid":"whosonfirst:macroregion:404227469","region":"Lancashire","region_gid":"whosonfirst:region:85683853","locality":"Lancaster","locality_gid":"whosonfirst:locality:101873271","label":"The Tap House, Lancaster, England, United Kingdom"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-2.798828,54.04823]},"properties":{"id":"node:3930587962","gid":"openstreetmap:venue:node:3930587962","layer":"venue","source":"openstreetmap","source_id":"node:3930587962","name":"The Elles Gallery","street":"Gage Street","confidence":0.8,"distance":0.024,"country":"United Kingdom","country_gid":"whosonfirst:country:85633159","country_a":"GBR","macroregion":"England","macroregion_gid":"whosonfirst:macroregion:404227469","region":"Lancashire","region_gid":"whosonfirst:region:85683853","locality":"Lancaster","locality_gid":"whosonfirst:locality:101873271","label":"The Elles Gallery, Lancaster, England, United Kingdom"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-2.798939,54.04813]},"properties":{"id":"node:3930587255","gid":"openstreetmap:venue:node:3930587255","layer":"venue","source":"openstreetmap","source_id":"node:3930587255","name":"Nicholas Evans Printing","street":"Mary Street","confidence":0.8,"distance":0.031,"country":"United Kingdom","country_gid":"whosonfirst:country:85633159","country_a":"GBR","macroregion":"England","macroregion_gid":"whosonfirst:macroregion:404227469","region":"Lancashire","region_gid":"whosonfirst:region:85683853","locality":"Lancaster","locality_gid":"whosonfirst:locality:101873271","label":"Nicholas Evans Printing, Lancaster, England, United Kingdom"}}],"bbox":[-2.79913,54.04813,-2.798828,54.048412]}"; \ No newline at end of file diff --git a/tests/.cached_responses/e472c244bd0475c86ecc1803d7555f1209010733 b/tests/.cached_responses/e472c244bd0475c86ecc1803d7555f1209010733 new file mode 100644 index 000000000..21b7882bb --- /dev/null +++ b/tests/.cached_responses/e472c244bd0475c86ecc1803d7555f1209010733 @@ -0,0 +1 @@ +s:5153:"{"geocoding":{"version":"0.1","attribution":"https://search.mapzen.com/v1/attribution","query":{"size":5,"private":false,"point.lat":49.139092,"point.lon":1.657246,"boundary.circle.radius":1,"boundary.circle.lat":49.139092,"boundary.circle.lon":1.657246,"querySize":10},"engine":{"name":"Pelias","author":"Mapzen","version":"1.0"},"timestamp":1473681887087},"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[1.657246,49.139092]},"properties":{"id":"node:1564292039","gid":"openstreetmap:venue:node:1564292039","layer":"venue","source":"openstreetmap","source_id":"node:1564292039","name":"Les Jardins d'Épicure","confidence":1,"distance":0,"country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Île-De-France","macroregion_gid":"whosonfirst:macroregion:404227465","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","macrocounty":"Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-En-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bus-Saint-Rémy","locality_gid":"whosonfirst:locality:101879825","label":"Les Jardins d'Épicure, Bus-Saint-Rémy, France"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.657036,49.139443]},"properties":{"id":"fr/val_doise:24453","gid":"openaddresses:address:fr/val_doise:24453","layer":"address","source":"openaddresses","source_id":"fr/val_doise:24453","name":"16 Grande Rue","housenumber":"16","street":"Grande Rue","postalcode":"95710","confidence":0.8,"distance":0.042,"country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Île-De-France","macroregion_gid":"whosonfirst:macroregion:404227465","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","macrocounty":"Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-En-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bus-Saint-Rémy","locality_gid":"whosonfirst:locality:101879825","label":"16 Grande Rue, Bus-Saint-Rémy, France"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.657518,49.13963]},"properties":{"id":"fr/val_doise:173518","gid":"openaddresses:address:fr/val_doise:173518","layer":"address","source":"openaddresses","source_id":"fr/val_doise:173518","name":"23 Grande Rue","housenumber":"23","street":"Grande Rue","postalcode":"95710","confidence":0.8,"distance":0.063,"country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Île-De-France","macroregion_gid":"whosonfirst:macroregion:404227465","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","macrocounty":"Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-En-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bus-Saint-Rémy","locality_gid":"whosonfirst:locality:101879825","label":"23 Grande Rue, Bus-Saint-Rémy, France"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.657782,49.139601]},"properties":{"id":"fr/val_doise:173511","gid":"openaddresses:address:fr/val_doise:173511","layer":"address","source":"openaddresses","source_id":"fr/val_doise:173511","name":"18 Grande Rue","housenumber":"18","street":"Grande Rue","postalcode":"95710","confidence":0.8,"distance":0.069,"country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Île-De-France","macroregion_gid":"whosonfirst:macroregion:404227465","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","macrocounty":"Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-En-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bus-Saint-Rémy","locality_gid":"whosonfirst:locality:101879825","label":"18 Grande Rue, Bus-Saint-Rémy, France"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[1.658133,49.13903]},"properties":{"id":"fr/val_doise:170018","gid":"openaddresses:address:fr/val_doise:170018","layer":"address","source":"openaddresses","source_id":"fr/val_doise:170018","name":"2 Rue Des Prés","housenumber":"2","street":"Rue Des Prés","postalcode":"95710","confidence":0.8,"distance":0.065,"country":"France","country_gid":"whosonfirst:country:85633147","country_a":"FRA","macroregion":"Île-De-France","macroregion_gid":"whosonfirst:macroregion:404227465","region":"Val-d'Oise","region_gid":"whosonfirst:region:85683451","macrocounty":"Pontoise","macrocounty_gid":"whosonfirst:macrocounty:404228193","county":"Magny-En-Vexin","county_gid":"whosonfirst:county:102067333","localadmin":"Bray-Et-Lu","localadmin_gid":"whosonfirst:localadmin:404408311","locality":"Bus-Saint-Rémy","locality_gid":"whosonfirst:locality:101879825","label":"2 Rue Des Prés, Bus-Saint-Rémy, France"}}],"bbox":[1.657036,49.13903,1.658133,49.13963]}"; \ No newline at end of file diff --git a/tests/.cached_responses/e6ec14e48d1de32a5cfde0ba8b43d63eee1ab473 b/tests/.cached_responses/e6ec14e48d1de32a5cfde0ba8b43d63eee1ab473 new file mode 100644 index 000000000..4b54aa8fb --- /dev/null +++ b/tests/.cached_responses/e6ec14e48d1de32a5cfde0ba8b43d63eee1ab473 @@ -0,0 +1,487 @@ +s:14981:"{ + "results" : [ + { + "address_components" : [ + { + "long_name" : "12", + "short_name" : "12", + "types" : [ "street_number" ] + }, + { + "long_name" : "Avenue Gambetta", + "short_name" : "Avenue Gambetta", + "types" : [ "route" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "locality", "political" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "long_name" : "Île-de-France", + "short_name" : "Île-de-France", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + }, + { + "long_name" : "75020", + "short_name" : "75020", + "types" : [ "postal_code" ] + } + ], + "formatted_address" : "12 Avenue Gambetta, 75020 Paris, France", + "geometry" : { + "location" : { + "lat" : 48.8630886, + "lng" : 2.3889524 + }, + "location_type" : "ROOFTOP", + "viewport" : { + "northeast" : { + "lat" : 48.86443758029149, + "lng" : 2.390301380291502 + }, + "southwest" : { + "lat" : 48.8617396197085, + "lng" : 2.387603419708498 + } + } + }, + "place_id" : "ChIJ9aLL3vJt5kcR61GCze3v6fg", + "types" : [ "street_address" ] + }, + { + "address_components" : [ + { + "long_name" : "Père-Lachaise", + "short_name" : "Père-Lachaise", + "types" : [ "neighborhood", "political" ] + }, + { + "long_name" : "20th arrondissement", + "short_name" : "20th arrondissement", + "types" : [ "political", "sublocality", "sublocality_level_1" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "locality", "political" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "long_name" : "Île-de-France", + "short_name" : "Île-de-France", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + }, + { + "long_name" : "75020", + "short_name" : "75020", + "types" : [ "postal_code" ] + } + ], + "formatted_address" : "Père-Lachaise, 75020 Paris, France", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 48.8707461, + "lng" : 2.4064679 + }, + "southwest" : { + "lat" : 48.856442, + "lng" : 2.383115 + } + }, + "location" : { + "lat" : 48.8627872, + "lng" : 2.3928087 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 48.8707461, + "lng" : 2.4064679 + }, + "southwest" : { + "lat" : 48.856442, + "lng" : 2.383115 + } + } + }, + "place_id" : "ChIJ8eVDMoxt5kcRHsuLm96IHm8", + "types" : [ "neighborhood", "political" ] + }, + { + "address_components" : [ + { + "long_name" : "20th arrondissement", + "short_name" : "20th arrondissement", + "types" : [ "political", "sublocality", "sublocality_level_1" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "locality", "political" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "long_name" : "Île-de-France", + "short_name" : "Île-de-France", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + }, + { + "long_name" : "75020", + "short_name" : "75020", + "types" : [ "postal_code" ] + } + ], + "formatted_address" : "20th arrondissement, 75020 Paris, France", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 48.8784419, + "lng" : 2.4163301 + }, + "southwest" : { + "lat" : 48.846621, + "lng" : 2.376885 + } + }, + "location" : { + "lat" : 48.8599825, + "lng" : 2.4066412 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 48.8784419, + "lng" : 2.4163301 + }, + "southwest" : { + "lat" : 48.846621, + "lng" : 2.376885 + } + } + }, + "place_id" : "ChIJ6_zai45t5kcRgBuUaMOCCwU", + "types" : [ "political", "sublocality", "sublocality_level_1" ] + }, + { + "address_components" : [ + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "locality", "political" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "long_name" : "Île-de-France", + "short_name" : "Île-de-France", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + } + ], + "formatted_address" : "Paris, France", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 48.9021449, + "lng" : 2.4699208 + }, + "southwest" : { + "lat" : 48.815573, + "lng" : 2.224199 + } + }, + "location" : { + "lat" : 48.856614, + "lng" : 2.3522219 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 48.9021449, + "lng" : 2.4699208 + }, + "southwest" : { + "lat" : 48.815573, + "lng" : 2.225193 + } + } + }, + "place_id" : "ChIJD7fiBh9u5kcRYJSMaMOCCwQ", + "types" : [ "locality", "political" ] + }, + { + "address_components" : [ + { + "long_name" : "75020", + "short_name" : "75020", + "types" : [ "postal_code" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "locality", "political" ] + }, + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "long_name" : "Île-de-France", + "short_name" : "Île-de-France", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + } + ], + "formatted_address" : "75020 Paris, France", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 48.8784077, + "lng" : 2.4164596 + }, + "southwest" : { + "lat" : 48.8465946, + "lng" : 2.3768622 + } + }, + "location" : { + "lat" : 48.8599825, + "lng" : 2.4066412 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 48.8784077, + "lng" : 2.4164596 + }, + "southwest" : { + "lat" : 48.8465946, + "lng" : 2.3768622 + } + } + }, + "place_id" : "ChIJ6_zai45t5kcRMFLY4caCCxw", + "types" : [ "postal_code" ] + }, + { + "address_components" : [ + { + "long_name" : "Paris", + "short_name" : "Paris", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "long_name" : "Île-de-France", + "short_name" : "Île-de-France", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + } + ], + "formatted_address" : "Paris, France", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 48.9021449, + "lng" : 2.4699208 + }, + "southwest" : { + "lat" : 48.815573, + "lng" : 2.224199 + } + }, + "location" : { + "lat" : 48.8565823, + "lng" : 2.3522148 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 48.9021449, + "lng" : 2.4699208 + }, + "southwest" : { + "lat" : 48.815573, + "lng" : 2.225193 + } + } + }, + "place_id" : "ChIJD7fiBh9u5kcRMCqLaMOCCwM", + "types" : [ "administrative_area_level_2", "political" ] + }, + { + "address_components" : [ + { + "long_name" : "Île-de-France", + "short_name" : "Île-de-France", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + } + ], + "formatted_address" : "Île-de-France, France", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 49.241504, + "lng" : 3.5590069 + }, + "southwest" : { + "lat" : 48.1200811, + "lng" : 1.44617 + } + }, + "location" : { + "lat" : 48.8499198, + "lng" : 2.6370411 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 49.241504, + "lng" : 3.5590069 + }, + "southwest" : { + "lat" : 48.1200811, + "lng" : 1.44617 + } + } + }, + "place_id" : "ChIJF4ymA8Th5UcRcCWLaMOCCwE", + "types" : [ "administrative_area_level_1", "political" ] + }, + { + "address_components" : [ + { + "long_name" : "Paris Metropolitan Area", + "short_name" : "Paris Metropolitan Area", + "types" : [ "political" ] + }, + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + } + ], + "formatted_address" : "Paris Metropolitan Area, France", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 49.44956, + "lng" : 3.5590339 + }, + "southwest" : { + "lat" : 48.065382, + "lng" : 1.3557489 + } + }, + "location" : { + "lat" : 48.8575712, + "lng" : 2.2771715 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 49.44956, + "lng" : 3.5590339 + }, + "southwest" : { + "lat" : 48.065382, + "lng" : 1.3557489 + } + } + }, + "place_id" : "ChIJW36m20gL5kcRiMnAEpHgyq8", + "types" : [ "political" ] + }, + { + "address_components" : [ + { + "long_name" : "France", + "short_name" : "FR", + "types" : [ "country", "political" ] + } + ], + "formatted_address" : "France", + "geometry" : { + "bounds" : { + "northeast" : { + "lat" : 51.1241999, + "lng" : 9.6624999 + }, + "southwest" : { + "lat" : 41.3253001, + "lng" : -5.5591 + } + }, + "location" : { + "lat" : 46.227638, + "lng" : 2.213749 + }, + "location_type" : "APPROXIMATE", + "viewport" : { + "northeast" : { + "lat" : 51.0891285, + "lng" : 9.5597934 + }, + "southwest" : { + "lat" : 41.3423369, + "lng" : -5.142141899999999 + } + } + }, + "place_id" : "ChIJMVd4MymgVA0R99lHx5Y__Ws", + "types" : [ "country", "political" ] + } + ], + "status" : "OK" +} +"; \ No newline at end of file diff --git a/tests/Geocoder/Tests/CachedResponseAdapter.php b/tests/Geocoder/Tests/CachedResponseAdapter.php deleted file mode 100644 index 3ef242d67..000000000 --- a/tests/Geocoder/Tests/CachedResponseAdapter.php +++ /dev/null @@ -1,69 +0,0 @@ -adapter = $adapter; - $this->useCache = $useCache; - $this->apiKey = $apiKey; - $this->cacheDir = $cacheDir; - } - - /** - * {@inheritDoc} - */ - public function getName() - { - return 'cached_response'; - } - - /** - * {@inheritDoc} - */ - protected function sendInternalRequest(InternalRequestInterface $internalRequest) - { - $url = (string) $internalRequest->getUri(); - if ($this->apiKey) { - $url = str_replace($this->apiKey, '[apikey]', $url); - } - - $file = sprintf('%s/%s/%s', realpath(__DIR__ . '/../../'), $this->cacheDir, sha1($url)); - - if ($this->useCache && is_file($file) && is_readable($file)) { - $content = unserialize(file_get_contents($file)); - - if (!empty($content)) { - return $this->adapter - ->getConfiguration() - ->getMessageFactory() - ->createResponse(200, RequestInterface::PROTOCOL_VERSION_1_1, [], $content); - } - } - - $response = $this->adapter->get($url); - - if ($this->useCache) { - file_put_contents($file, serialize((string) $response->getBody())); - } - - return $response; - } -} diff --git a/tests/Geocoder/Tests/CachedResponseClient.php b/tests/Geocoder/Tests/CachedResponseClient.php new file mode 100644 index 000000000..7f600baad --- /dev/null +++ b/tests/Geocoder/Tests/CachedResponseClient.php @@ -0,0 +1,52 @@ +delegate = $delegate; + $this->useCache = $useCache; + $this->apiKey = $apiKey; + $this->cacheDir = $cacheDir; + } + + /** + * {@inheritDoc} + */ + public function sendRequest(RequestInterface $request) + { + $url = (string) $request->getUri(); + if ($this->apiKey) { + $url = str_replace($this->apiKey, '[apikey]', $url); + } + + $file = sprintf('%s/%s/%s', realpath(__DIR__ . '/../../'), $this->cacheDir, sha1($url)); + + if ($this->useCache && is_file($file) && is_readable($file)) { + return new Response(200, [], Psr7\stream_for(unserialize(file_get_contents($file)))); + } + + $response = $this->delegate->sendRequest($request); + + if ($this->useCache) { + file_put_contents($file, serialize($response->getBody()->getContents())); + } + + return $response; + } +} diff --git a/tests/Geocoder/Tests/Dumper/GeoArrayTest.php b/tests/Geocoder/Tests/Dumper/GeoArrayTest.php new file mode 100644 index 000000000..cf571786d --- /dev/null +++ b/tests/Geocoder/Tests/Dumper/GeoArrayTest.php @@ -0,0 +1,133 @@ + + * @author William Durand + */ +class GeoArrayTest extends TestCase +{ + private $dumper; + + protected function setUp() + { + $this->dumper = new GeoArray(); + } + + public function testDump() + { + $address = $this->createAddress([]); + $expected = array( + 'type' => 'Feature', + 'geometry' => array( + 'type' => 'Point', + 'coordinates' => array(0, 0) + ), + 'properties' => null + ); + + $result = $this->dumper->dump($address); + + $this->assertInternalType('array', $result); + $this->assertEquals($expected, $result); + } + + public function testDumpWithData() + { + $address = $this->createAddress([ + 'latitude' => 48.8631507, + 'longitude' => 2.3889114 + ]); + + $expected = array( + 'type' => 'Feature', + 'geometry' => array( + 'type' => 'Point', + 'coordinates' => array(2.3889114, 48.8631507) + ), + 'properties' => null + ); + + $result = $this->dumper->dump($address); + + $this->assertInternalType('array', $result); + $this->assertEquals($expected, $result); + } + + public function testDumpWithBounds() + { + $address = $this->createAddress([ + 'latitude' => 48.8631507, + 'longitude' => 2.3889114, + 'bounds' => [ + 'south' => 48.8631507, + 'west' => 2.3889114, + 'north' => 48.8631507, + 'east' => 2.388911 + ] + ]); + + $expected = array( + 'type' => 'Feature', + 'geometry' => array( + 'type' => 'Point', + 'coordinates' => array(2.3889114, 48.8631507) + ), + 'properties' => null, + 'bounds' => array( + 'south' => 48.8631507, + 'west' => 2.3889114, + 'north' => 48.8631507, + 'east' => 2.388911 + ) + ); + + $result = $this->dumper->dump($address); + + $this->assertInternalType('array', $result); + $this->assertEquals($expected, $result); + } + + public function testDumpWithProperties() + { + $address = $this->createAddress([ + 'latitude' => 48.8631507, + 'longitude' => 2.3889114, + 'bounds' => [ + 'south' => 48.8631507, + 'west' => 2.3889114, + 'north' => 48.8631507, + 'east' => 2.388911 + ], + 'locality' => 'Paris', + 'country' => 'France' + ]); + + $expected = array( + 'type' => 'Feature', + 'geometry' => array( + 'type' => 'Point', + 'coordinates' => array(2.3889114, 48.8631507) + ), + 'properties' => array( + 'locality' => 'Paris', + 'country' => 'France' + ), + 'bounds' => array( + 'south' => 48.8631507, + 'west' => 2.3889114, + 'north' => 48.8631507, + 'east' => 2.388911 + ) + ); + + $result = $this->dumper->dump($address); + + $this->assertInternalType('array', $result); + $this->assertEquals($expected, $result); + } +} diff --git a/tests/Geocoder/Tests/Dumper/GpxTest.php b/tests/Geocoder/Tests/Dumper/GpxTest.php index f8514753a..beefb57da 100644 --- a/tests/Geocoder/Tests/Dumper/GpxTest.php +++ b/tests/Geocoder/Tests/Dumper/GpxTest.php @@ -64,7 +64,7 @@ public function testDumpWithData() GPX - , Geocoder::VERSION, $address->getLatitude(), $address->getLongitude()); + , Geocoder::VERSION, $address->getCoordinates()->getLatitude(), $address->getCoordinates()->getLongitude()); $result = $this->dumper->dump($address); diff --git a/tests/Geocoder/Tests/Model/AddressFactoryTest.php b/tests/Geocoder/Tests/Model/AddressFactoryTest.php index 015b80c13..265121bdc 100644 --- a/tests/Geocoder/Tests/Model/AddressFactoryTest.php +++ b/tests/Geocoder/Tests/Model/AddressFactoryTest.php @@ -3,6 +3,7 @@ namespace Geocoder\Tests\Model; use Geocoder\Model\AddressFactory; +use Geocoder\Location; use Geocoder\Tests\TestCase; /** @@ -11,6 +12,7 @@ */ class AddressFactoryTest extends TestCase { + /** @var AddressFactory */ private $factory; public function setUp() @@ -31,18 +33,19 @@ public function testCreateFromArray() $this->assertCount(3, $addresses); $i = 1; - foreach ($addresses as $address) { - $this->assertInstanceOf('Geocoder\Model\Address', $address); - $this->assertInstanceOf('Geocoder\Model\Country', $address->getCountry()); - $this->assertNull($address->getCoordinates()); + foreach ($addresses as $location) { + /** @var $location Location */ + $this->assertInstanceOf('Geocoder\Model\Address', $location); + $this->assertInstanceOf('Geocoder\Model\Country', $location->getCountry()); + $this->assertNull($location->getCoordinates()); - foreach ($address->getAdminLevels() as $level => $adminLevel) { + foreach ($location->getAdminLevels() as $level => $adminLevel) { $this->assertInstanceOf('Geocoder\Model\AdminLevel', $adminLevel); $this->assertSame($level, $adminLevel->getLevel()); $this->assertEquals('admin ' . $level, $adminLevel->getName()); } - $this->assertEquals($i++, $address->getStreetNumber()); + $this->assertEquals($i++, $location->getStreetNumber()); } } diff --git a/tests/Geocoder/Tests/Model/AddressTest.php b/tests/Geocoder/Tests/Model/AddressTest.php new file mode 100644 index 000000000..9e74bf6e3 --- /dev/null +++ b/tests/Geocoder/Tests/Model/AddressTest.php @@ -0,0 +1,34 @@ + NULL, + 'longitude' => NULL, + 'bounds' => array ( + 'south' => NULL, + 'west' => NULL, + 'north' => NULL, + 'east' => NULL, + ), + 'streetNumber' => NULL, + 'streetName' => NULL, + 'postalCode' => NULL, + 'locality' => NULL, + 'subLocality' => NULL, + 'adminLevels' => array(), + 'country' => NULL, + 'countryCode' => NULL, + 'timezone' => NULL, + ); + + $address = new Address(); + $this->assertEquals($address->toArray(), $expected); + } +} \ No newline at end of file diff --git a/tests/Geocoder/Tests/Provider/AbstractProviderTest.php b/tests/Geocoder/Tests/Provider/AbstractProviderTest.php index be774d220..36df0f355 100644 --- a/tests/Geocoder/Tests/Provider/AbstractProviderTest.php +++ b/tests/Geocoder/Tests/Provider/AbstractProviderTest.php @@ -3,10 +3,8 @@ namespace Geocoder\Tests\Provider; use Geocoder\Tests\TestCase; - +use Http\Mock\Client; use Geocoder\Provider\AbstractProvider; -use Ivory\HttpAdapter\AbstractHttpAdapter; -use Ivory\HttpAdapter\Message\InternalRequestInterface; /** * @author William Durand @@ -15,8 +13,8 @@ class AbstractProviderTest extends TestCase { public function testGetLocalhostDefaults() { - $adapter = new MockHttpAdapter(); - $provider = new MockProvider($adapter); + $client = $this->prophesize('Http\Client\HttpClient'); + $provider = new MockProvider($client->reveal()); $result = $provider->getLocalhostDefaults(); $this->assertEquals(2, count($result)); @@ -32,15 +30,3 @@ public function getLocalhostDefaults() return parent::getLocalhostDefaults(); } } - -class MockHttpAdapter extends AbstractHttpAdapter -{ - public function getName() - { - return 'mock_http_adapter'; - } - - protected function sendInternalRequest(InternalRequestInterface $internalRequest) - { - } -} diff --git a/tests/Geocoder/Tests/Provider/ArcGISOnlineTest.php b/tests/Geocoder/Tests/Provider/ArcGISOnlineTest.php index db69cd09b..9b93882be 100644 --- a/tests/Geocoder/Tests/Provider/ArcGISOnlineTest.php +++ b/tests/Geocoder/Tests/Provider/ArcGISOnlineTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\ArcGISOnline; @@ -80,11 +81,11 @@ public function testGeocodeWithRealAddress() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.863279997000461, $result->getLatitude(), '', 0.0001); - $this->assertEquals(2.3890199980004354, $result->getLongitude(), '', 0.0001); + $this->assertEquals(48.863279997000461, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(2.3890199980004354, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals(10, $result->getStreetNumber()); $this->assertEquals('10 Avenue Gambetta, 75020, 20e Arrondissement, Paris, Île-de-France', $result->getStreetName()); $this->assertEquals(75020, $result->getPostalCode()); @@ -94,7 +95,7 @@ public function testGeocodeWithRealAddress() $this->assertEquals('Paris', $result->getAdminLevels()->get(2)->getName()); $this->assertEquals('FRA', $result->getCountry()->getCode()); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertNull($result->getBounds()); $this->assertNull($result->getSubLocality()); $this->assertNull($result->getAdminLevels()->get(2)->getCode()); $this->assertNull($result->getAdminLevels()->get(1)->getCode()); @@ -113,8 +114,8 @@ public function testGeocodeWithRealAddressAndHttps() /** @var \Geocoder\Model\Address $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.863279997000461, $result->getLatitude(), '', 0.0001); - $this->assertEquals(2.3890199980004354, $result->getLongitude(), '', 0.0001); + $this->assertEquals(48.863279997000461, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(2.3890199980004354, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals(10, $result->getStreetNumber()); $this->assertEquals('10 Avenue Gambetta, 75020, 20e Arrondissement, Paris, Île-de-France', $result->getStreetName()); $this->assertEquals(75020, $result->getPostalCode()); @@ -125,7 +126,7 @@ public function testGeocodeWithRealAddressAndHttps() $this->assertEquals('FRA', $result->getCountry()->getCode()); $this->assertEquals(10, $result->getStreetNumber()); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertNull($result->getBounds()); $this->assertNull($result->getSubLocality()); $this->assertNull($result->getAdminLevels()->get(2)->getCode()); $this->assertNull($result->getAdminLevels()->get(1)->getCode()); @@ -181,18 +182,18 @@ public function testReverseWithRealCoordinates() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.863279997000461, $result->getLatitude(), '', 0.0001); - $this->assertEquals(2.3890199980004354, $result->getLongitude(), '', 0.0001); + $this->assertEquals(48.863279997000461, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(2.3890199980004354, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getStreetNumber()); $this->assertEquals('3 Avenue Gambetta', $result->getStreetName()); $this->assertEquals(75020, $result->getPostalCode()); $this->assertEquals('Paris', $result->getLocality()); $this->assertEquals('FRA', $result->getCountry()->getCode()); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertNull($result->getBounds()); $this->assertNull($result->getSubLocality()); $this->assertEmpty($result->getAdminLevels()); $this->assertNull($result->getCountry()->getName()); @@ -210,15 +211,15 @@ public function testReverseWithRealCoordinatesWithHttps() /** @var \Geocoder\Model\Address $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.863279997000461, $result->getLatitude(), '', 0.0001); - $this->assertEquals(2.3890199980004354, $result->getLongitude(), '', 0.0001); + $this->assertEquals(48.863279997000461, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(2.3890199980004354, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getStreetNumber()); $this->assertEquals('3 Avenue Gambetta', $result->getStreetName()); $this->assertEquals(75020, $result->getPostalCode()); $this->assertEquals('Paris', $result->getLocality()); $this->assertEquals('FRA', $result->getCountry()->getCode()); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertNull($result->getBounds()); $this->assertNull($result->getSubLocality()); $this->assertEmpty($result->getAdminLevels()); $this->assertNull($result->getCountry()->getName()); @@ -233,11 +234,11 @@ public function testGeocodeWithCity() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(52.370518568000477, $result->getLatitude(), '', 0.0001); - $this->assertEquals(9.7332166860004463, $result->getLongitude(), '', 0.0001); + $this->assertEquals(52.370518568000477, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(9.7332166860004463, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getStreetNumber()); $this->assertEquals('Hannover, Niedersachsen, Deutschland', $result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -246,49 +247,49 @@ public function testGeocodeWithCity() $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('DEU', $result->getCountry()->getCode()); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertNull($result->getBounds()); $this->assertNull($result->getSubLocality()); $this->assertNull($result->getCountry()->getName()); $this->assertNull($result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(47.111386795000499, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-101.4265391569997, $result->getLongitude(), '', 0.0001); + $this->assertEquals(47.111386795000499, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-101.4265391569997, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals('Hannover, North Dakota, United States', $result->getStreetName()); $this->assertNull($result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('North Dakota', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('USA', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(39.391768472000479, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-77.440257128999633, $result->getLongitude(), '', 0.0001); + $this->assertEquals(39.391768472000479, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-77.440257128999633, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals('Hannover, Maryland, United States', $result->getStreetName()); $this->assertNull($result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Maryland', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('USA', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(53.174198173, $result->getLatitude(), '', 0.0001); - $this->assertEquals(8.5069383810005, $result->getLongitude(), '', 0.0001); + $this->assertEquals(53.174198173, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(8.5069383810005, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals('Hannöver, Niedersachsen, Deutschland', $result->getStreetName()); $this->assertNull($result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); $this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('DEU', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(-26.281805980999593, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-48.849389793999649, $result->getLongitude(), '', 0.0001); + $this->assertEquals(-26.281805980999593, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-48.849389793999649, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals('Hannover', $result->getStreetName()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Sul', $result->getAdminLevels()->get(1)->getName()); diff --git a/tests/Geocoder/Tests/Provider/BingMapsTest.php b/tests/Geocoder/Tests/Provider/BingMapsTest.php index ae0054d09..61757e96b 100644 --- a/tests/Geocoder/Tests/Provider/BingMapsTest.php +++ b/tests/Geocoder/Tests/Provider/BingMapsTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\BingMaps; @@ -94,12 +95,12 @@ public function testGeocodeReturnsMultipleResults() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(3, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.86321675999999, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.3887721299999995, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.86321675999999, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.3887721299999995, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.859354042429, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.3809438666389, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.867079477571, $result->getBounds()->getNorth(), '', 0.01); @@ -116,12 +117,12 @@ public function testGeocodeReturnsMultipleResults() $this->assertNull($result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.81342781, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.32503767, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.81342781, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.32503767, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.809565092429, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.3172171827738, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.817290527571, $result->getBounds()->getNorth(), '', 0.01); @@ -136,12 +137,12 @@ public function testGeocodeReturnsMultipleResults() $this->assertEquals('France', $result->getCountry()->getName()); $this->assertEquals('FR', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.81014147, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.43568048, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.81014147, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.43568048, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.806278752429, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.4278605052897, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.814004187571, $result->getBounds()->getNorth(), '', 0.01); @@ -169,12 +170,12 @@ public function testReverseReturnsSingleResult() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.86321648955345, $result->getLatitude(), '', 0.0001); - $this->assertEquals(2.3887719959020615, $result->getLongitude(), '', 0.0001); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.86321648955345, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(2.3887719959020615, $result->getCoordinates()->getLongitude(), '', 0.0001); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.859353771983, $result->getBounds()->getSouth(), '', 0.0001); $this->assertEquals(2.3809437325833, $result->getBounds()->getWest(), '', 0.0001); $this->assertEquals(48.867079207124, $result->getBounds()->getNorth(), '', 0.0001); @@ -204,12 +205,12 @@ public function testGeocodeWithRealAddressReturnsSingleResults() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.86321675999999, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.3887721299999995, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.86321675999999, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.3887721299999995, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.859354042429, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.3809438666389, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.867079477571, $result->getBounds()->getNorth(), '', 0.01); @@ -242,12 +243,12 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(0); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(44.786701202393, $result->getLatitude(), '', 0.01); - $this->assertEquals(8.2841901779175, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(44.786701202393, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(8.2841901779175, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(44.775325775146, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(8.2711343765259, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(44.795879364014, $result->getBounds()->getNorth(), '', 0.01); @@ -262,12 +263,12 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() $this->assertEquals('Italie', $result->getCountry()->getName()); $this->assertEquals('IT', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(46.05179977417, $result->getLatitude(), '', 0.01); - $this->assertEquals(11.497699737549, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(46.05179977417, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(11.497699737549, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(46.029235839844, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(11.473880767822, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(46.07377243042, $result->getBounds()->getNorth(), '', 0.01); @@ -282,12 +283,12 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() $this->assertEquals('Italie', $result->getCountry()->getName()); $this->assertEquals('IT', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(44.987880706787, $result->getLatitude(), '', 0.01); - $this->assertEquals(9.442440032959, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(44.987880706787, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(9.442440032959, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(44.958910323795, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(9.3878520826907, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(45.01685108978, $result->getBounds()->getNorth(), '', 0.01); @@ -302,12 +303,12 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() $this->assertEquals('Italie', $result->getCountry()->getName()); $this->assertEquals('IT', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(43.82638168335, $result->getLatitude(), '', 0.01); - $this->assertEquals(11.068260192871, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(43.82638168335, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(11.068260192871, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(43.797411300357, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(11.014744487393, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(43.855352066342, $result->getBounds()->getNorth(), '', 0.01); @@ -322,12 +323,12 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() $this->assertEquals('Italie', $result->getCountry()->getName()); $this->assertEquals('IT', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(42.295810699463, $result->getLatitude(), '', 0.01); - $this->assertEquals(13.626440048218, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(42.295810699463, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(13.626440048218, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(42.26684031647, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(13.574242599134, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(42.324781082455, $result->getBounds()->getNorth(), '', 0.01); @@ -375,12 +376,12 @@ public function testReverseWithRealCoordinatesReturnsSingleResult() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.86321648955345, $result->getLatitude(), '', 0.0001); - $this->assertEquals(2.3887719959020615, $result->getLongitude(), '', 0.0001); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.86321648955345, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(2.3887719959020615, $result->getCoordinates()->getLongitude(), '', 0.0001); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.859353771983, $result->getBounds()->getSouth(), '', 0.0001); $this->assertEquals(2.3809437325833, $result->getBounds()->getWest(), '', 0.0001); $this->assertEquals(48.867079207124, $result->getBounds()->getNorth(), '', 0.0001); diff --git a/tests/Geocoder/Tests/Provider/FreeGeoIpTest.php b/tests/Geocoder/Tests/Provider/FreeGeoIpTest.php index b36c24c20..a77f9e261 100644 --- a/tests/Geocoder/Tests/Provider/FreeGeoIpTest.php +++ b/tests/Geocoder/Tests/Provider/FreeGeoIpTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\FreeGeoIp; @@ -51,7 +52,7 @@ public function testGeocodeWithLocalhostIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('localhost', $result->getLocality()); @@ -66,7 +67,7 @@ public function testGeocodeWithLocalhostIPv6() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('localhost', $result->getLocality()); @@ -101,11 +102,11 @@ public function testGeocodeWithRealIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(33.0347, $result->getLatitude(), '', 0.01); - $this->assertEquals(-96.8134, $result->getLongitude(), '', 0.01); + $this->assertEquals(33.0347, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-96.8134, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals(75093, $result->getPostalCode()); $this->assertEquals('Plano', $result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); @@ -122,11 +123,11 @@ public function testGeocodeWithRealIPv6() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(33.0347, $result->getLatitude(), '', 0.01); - $this->assertEquals(-96.8134, $result->getLongitude(), '', 0.01); + $this->assertEquals(33.0347, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-96.8134, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals(75093, $result->getPostalCode()); $this->assertEquals('Plano', $result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); diff --git a/tests/Geocoder/Tests/Provider/GeoIP2Test.php b/tests/Geocoder/Tests/Provider/GeoIP2Test.php index f9bebb1f6..3474ab72a 100644 --- a/tests/Geocoder/Tests/Provider/GeoIP2Test.php +++ b/tests/Geocoder/Tests/Provider/GeoIP2Test.php @@ -11,6 +11,7 @@ namespace Geocoder\Tests\Provider; use Geocoder\Exception\NoResult; +use Geocoder\Location; use Geocoder\Provider\GeoIP2; use Geocoder\Tests\TestCase; @@ -50,7 +51,7 @@ public function testGeocodeWithLocalhostIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('localhost', $result->getLocality()); @@ -162,12 +163,22 @@ public function testRetrievingGeodata($address, $adapterResponse, $expectedGeoda $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals($expectedGeodata['latitude'], $result->getLatitude()); - $this->assertEquals($expectedGeodata['longitude'], $result->getLongitude()); - $this->assertEquals($expectedGeodata['boundsDefined'], $result->getBounds()->isDefined()); + if (isset($expectedGeodata['latitude'])) { + $this->assertEquals($expectedGeodata['latitude'], $result->getCoordinates()->getLatitude()); + $this->assertEquals($expectedGeodata['longitude'], $result->getCoordinates()->getLongitude()); + } else { + $this->assertNull($result->getCoordinates()); + } + + if ($expectedGeodata['boundsDefined']) { + $this->assertNotNull($result->getBounds()); + } else { + $this->assertNull($result->getBounds()); + } + $this->assertEquals($expectedGeodata['streetNumber'], $result->getStreetNumber()); $this->assertEquals($expectedGeodata['streetName'], $result->getStreetName()); $this->assertEquals($expectedGeodata['locality'], $result->getLocality()); diff --git a/tests/Geocoder/Tests/Provider/GeoIPsTest.php b/tests/Geocoder/Tests/Provider/GeoIPsTest.php index 9d1f3e0aa..d79338aa8 100644 --- a/tests/Geocoder/Tests/Provider/GeoIPsTest.php +++ b/tests/Geocoder/Tests/Provider/GeoIPsTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\GeoIPs; @@ -60,7 +61,7 @@ public function testGeocodeWithLocalhostIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('localhost', $result->getLocality()); @@ -131,11 +132,11 @@ public function testGeocodeWithRealIPv4GetsFakeContentFormattedEmpty() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertNull($result->getLatitude()); - $this->assertNull($result->getLongitude()); + $this->assertNull($result->getCoordinates()); + $this->assertNull($result->getPostalCode()); $this->assertNull($result->getLocality()); $this->assertEmpty($result->getAdminLevels()); @@ -174,11 +175,11 @@ public function testGeocodeWithRealIPv4GetsFakeContent() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(40.3402, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-111.6073, $result->getLongitude(), '', 0.0001); + $this->assertEquals(40.3402, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-111.6073, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getStreetName()); $this->assertNull($result->getPostalCode()); $this->assertEquals('PROVO', $result->getLocality()); @@ -333,11 +334,11 @@ public function testGeocodeWithRealIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(40.3402, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-111.6073, $result->getLongitude(), '', 0.0001); + $this->assertEquals(40.3402, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-111.6073, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getStreetName()); $this->assertNull($result->getPostalCode()); $this->assertEquals('PROVO', $result->getLocality()); diff --git a/tests/Geocoder/Tests/Provider/GeoPluginTest.php b/tests/Geocoder/Tests/Provider/GeoPluginTest.php index 5241ddb4b..fec1ec348 100644 --- a/tests/Geocoder/Tests/Provider/GeoPluginTest.php +++ b/tests/Geocoder/Tests/Provider/GeoPluginTest.php @@ -99,8 +99,8 @@ public function testGeocodeWithRealIPv4() $result = $results->first(); - $this->assertEquals(40.711101999999997, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-73.946899000000002, $result->getLongitude(), '', 0.0001); + $this->assertEquals(40.711101999999997, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-73.946899000000002, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); $this->assertEquals('New York', $result->getAdminLevels()->get(1)->getName()); diff --git a/tests/Geocoder/Tests/Provider/GeoipTest.php b/tests/Geocoder/Tests/Provider/GeoipTest.php index 722572f70..a42696eed 100644 --- a/tests/Geocoder/Tests/Provider/GeoipTest.php +++ b/tests/Geocoder/Tests/Provider/GeoipTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\Geoip; @@ -60,11 +61,11 @@ public function testGeocodeWithLocalhostIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertNull($result->getLatitude()); - $this->assertNull($result->getLongitude()); + $this->assertNull($result->getCoordinates()); + $this->assertNull($result->getPostalCode()); $this->assertNull($result->getTimezone()); $this->assertEmpty($result->getAdminLevels()); diff --git a/tests/Geocoder/Tests/Provider/GeonamesTest.php b/tests/Geocoder/Tests/Provider/GeonamesTest.php index d54cedf18..a3706ac40 100644 --- a/tests/Geocoder/Tests/Provider/GeonamesTest.php +++ b/tests/Geocoder/Tests/Provider/GeonamesTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\Geonames; @@ -19,7 +20,7 @@ public function testGetName() */ public function testGeocodeWithNullUsername() { - $provider = new Geonames($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); + $provider = new Geonames($this->getMock('Http\Client\HttpClient'), null); $provider->geocode('foo'); } @@ -29,7 +30,7 @@ public function testGeocodeWithNullUsername() */ public function testReverseWithNullUsername() { - $provider = new Geonames($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); + $provider = new Geonames($this->getMock('Http\Client\HttpClient'), null); $provider->reverse(1,2); } @@ -91,12 +92,12 @@ public function testGeocodeWithRealPlace() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(51.508528775863, $result->getLatitude(), '', 0.01); - $this->assertEquals(-0.12574195861816, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(51.508528775863, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-0.12574195861816, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(51.151689398345, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-0.70360885396019, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(51.865368153381, $result->getBounds()->getNorth(), '', 0.01); @@ -109,12 +110,12 @@ public function testGeocodeWithRealPlace() $this->assertEquals('GB', $result->getCountryCode()); $this->assertEquals('Europe/London', $result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(-33.015285093464, $result->getLatitude(), '', 0.01); - $this->assertEquals(27.911624908447, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(-33.015285093464, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(27.911624908447, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(-33.104996458003, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(27.804746435655, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(-32.925573728925, $result->getBounds()->getNorth(), '', 0.01); @@ -128,12 +129,12 @@ public function testGeocodeWithRealPlace() $this->assertEquals('ZA', $result->getCountryCode()); $this->assertEquals('Africa/Johannesburg', $result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(51.512788890295, $result->getLatitude(), '', 0.01); - $this->assertEquals(-0.091838836669922, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(51.512788890295, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-0.091838836669922, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(51.155949512764, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-0.66976046752962, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(51.869628267826, $result->getBounds()->getNorth(), '', 0.01); @@ -147,12 +148,12 @@ public function testGeocodeWithRealPlace() $this->assertEquals('GB', $result->getCountryCode()); $this->assertEquals('Europe/London', $result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(42.983389283, $result->getLatitude(), '', 0.01); - $this->assertEquals(-81.233042387, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(42.983389283, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-81.233042387, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(42.907075642763, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-81.337489676463, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(43.059702923237, $result->getBounds()->getNorth(), '', 0.01); @@ -164,12 +165,12 @@ public function testGeocodeWithRealPlace() $this->assertEquals('CA', $result->getCountryCode()); $this->assertEquals('America/Toronto', $result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(41.3556539, $result->getLatitude(), '', 0.01); - $this->assertEquals(-72.0995209, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(41.3556539, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-72.0995209, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(41.334087887904, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-72.128261254846, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(41.377219912096, $result->getBounds()->getNorth(), '', 0.01); @@ -195,12 +196,12 @@ public function testGeocodeWithRealPlaceWithLocale() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(51.50853, $result->getLatitude(), '', 0.01); - $this->assertEquals(-0.12574, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(51.50853, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-0.12574, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(51.15169, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-0.70361, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(51.86537, $result->getBounds()->getNorth(), '', 0.01); @@ -213,12 +214,12 @@ public function testGeocodeWithRealPlaceWithLocale() $this->assertEquals('GB', $result->getCountryCode()); $this->assertEquals('Europe/London', $result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(-33.015285093464, $result->getLatitude(), '', 0.01); - $this->assertEquals(27.911624908447, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(-33.015285093464, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(27.911624908447, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(-33.104996458003, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(27.804746435655, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(-32.925573728925, $result->getBounds()->getNorth(), '', 0.01); @@ -232,12 +233,12 @@ public function testGeocodeWithRealPlaceWithLocale() $this->assertEquals('ZA', $result->getCountryCode()); $this->assertEquals('Africa/Johannesburg', $result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(51.512788890295, $result->getLatitude(), '', 0.01); - $this->assertEquals(-0.091838836669922, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(51.512788890295, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-0.091838836669922, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(51.155949512764, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-0.66976046752962, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(51.869628267826, $result->getBounds()->getNorth(), '', 0.01); @@ -251,12 +252,12 @@ public function testGeocodeWithRealPlaceWithLocale() $this->assertEquals('GB', $result->getCountryCode()); $this->assertEquals('Europe/London', $result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(42.983389283, $result->getLatitude(), '', 0.01); - $this->assertEquals(-81.233042387, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(42.983389283, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-81.233042387, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(42.907075642763, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-81.337489676463, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(43.059702923237, $result->getBounds()->getNorth(), '', 0.01); @@ -268,12 +269,12 @@ public function testGeocodeWithRealPlaceWithLocale() $this->assertEquals('CA', $result->getCountryCode()); $this->assertEquals('America/Toronto', $result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(41.3556539, $result->getLatitude(), '', 0.01); - $this->assertEquals(-72.0995209, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(41.3556539, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-72.0995209, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(41.334087887904, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-72.128261254846, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(41.377219912096, $result->getBounds()->getNorth(), '', 0.01); @@ -299,11 +300,11 @@ public function testReverseWithRealCoordinates() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(51.50853, $result->getLatitude(), '', 0.01); - $this->assertEquals(-0.12574, $result->getLongitude(), '', 0.01); + $this->assertEquals(51.50853, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-0.12574, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('London', $result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Greater London', $result->getAdminLevels()->get(2)->getName()); @@ -325,11 +326,11 @@ public function testReverseWithRealCoordinatesWithLocale() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(51.50853, $result->getLatitude(), '', 0.01); - $this->assertEquals(-0.12574, $result->getLongitude(), '', 0.01); + $this->assertEquals(51.50853, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-0.12574, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('Londra', $result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Greater London', $result->getAdminLevels()->get(2)->getName()); diff --git a/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php b/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php deleted file mode 100644 index fc5323721..000000000 --- a/tests/Geocoder/Tests/Provider/GoogleMapsBusinessTest.php +++ /dev/null @@ -1,93 +0,0 @@ -getMockAdapter($this->never()), $this->testClientId); - $this->assertEquals('google_maps_business', $provider->getName()); - } - - public function testBuildQueryWithNoPrivateKey() - { - $method = new \ReflectionMethod( - 'Geocoder\Provider\GoogleMapsBusiness', 'buildQuery' - ); - - $method->setAccessible(true); - - $provider = new GoogleMapsBusiness($this->getMockAdapter($this->never()), $this->testClientId); - $query = 'http://maps.googleapis.com/maps/api/geocode/json?address=blah'; - - $this->assertEquals($query.'&client=foo', $method->invoke($provider, $query)); - } - - public function testBuildQueryWithPrivateKey() - { - $method = new \ReflectionMethod( - 'Geocoder\Provider\GoogleMapsBusiness', 'buildQuery' - ); - - $method->setAccessible(true); - - $provider = new GoogleMapsBusiness( - $this->getMockAdapter($this->never()), - $this->testClientId, - $this->testPrivateKey - ); - - $query = 'http://maps.googleapis.com/maps/api/geocode/json?address=blah'; - - $this->assertEquals($query.'&client=foo&signature=9G2weMhhd4E2ciR681gp9YabvUg=', $method->invoke($provider, $query)); - } - - public function testSignQuery() - { - $method = new \ReflectionMethod( - 'Geocoder\Provider\GoogleMapsBusiness', 'signQuery' - ); - - $method->setAccessible(true); - - $provider = new GoogleMapsBusiness( - $this->getMockAdapter($this->never()), - $this->testClientId, - $this->testPrivateKey - ); - - $query = 'http://maps.googleapis.com/maps/api/geocode/json?address=10%20avenue%20Gambetta%2C%20Paris%2C%20France'; - - $this->assertEquals($query.'&signature=7oRS85BVVpPUsyrd4MWFGMJNWok=', $method->invoke($provider, $query)); - } - - /** - * @expectedException \Geocoder\Exception\InvalidCredentials - * @expectedExceptionMessage Invalid client ID / API Key https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&client=foo&signature=9dJq1hPF7_iwafUpnqXUqEkP0gY= - */ - public function testGeocodeWithInvalidClientIdAndKey() - { - $provider = new GoogleMapsBusiness($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, true); - - $provider->geocode('Columbia University'); - } - - /** - * @expectedException \Geocoder\Exception\InvalidCredentials - * @expectedExceptionMessage Invalid client ID / API Key http://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&client=foo&signature=9dJq1hPF7_iwafUpnqXUqEkP0gY= - */ - public function testGeocodeWithInvalidClientIdAndKeyNoSsl() - { - $provider = new GoogleMapsBusiness($this->getAdapter(), $this->testClientId, $this->testPrivateKey, null, null, false); - - $provider->geocode('Columbia University', true); - } -} diff --git a/tests/Geocoder/Tests/Provider/GoogleMapsTest.php b/tests/Geocoder/Tests/Provider/GoogleMapsTest.php index 5e89c2ca6..b99ce8321 100644 --- a/tests/Geocoder/Tests/Provider/GoogleMapsTest.php +++ b/tests/Geocoder/Tests/Provider/GoogleMapsTest.php @@ -2,8 +2,12 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Exception\NoResult; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\GoogleMaps; +use Http\Client\HttpClient; +use Psr\Http\Message\RequestInterface; class GoogleMapsTest extends TestCase { @@ -116,12 +120,12 @@ public function testGeocodeWithRealAddress() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.8630462, $result->getLatitude(), '', 0.001); - $this->assertEquals(2.3882487, $result->getLongitude(), '', 0.001); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.8630462, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(2.3882487, $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.8630462, $result->getBounds()->getSouth(), '', 0.001); $this->assertEquals(2.3882487, $result->getBounds()->getWest(), '', 0.001); $this->assertEquals(48.8630462, $result->getBounds()->getNorth(), '', 0.001); @@ -147,12 +151,12 @@ public function testGeocodeWithRealAddressWithSsl() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.8630462, $result->getLatitude(), '', 0.001); - $this->assertEquals(2.3882487, $result->getLongitude(), '', 0.001); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.8630462, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(2.3882487, $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.8630462, $result->getBounds()->getSouth(), '', 0.001); $this->assertEquals(2.3882487, $result->getBounds()->getWest(), '', 0.001); $this->assertEquals(48.8630462, $result->getBounds()->getNorth(), '', 0.001); @@ -179,10 +183,10 @@ public function testGeocodeBoundsWithRealAddressForNonRooftopLocation() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.815573, $result->getBounds()->getSouth(), '', 0.0001); $this->assertEquals(2.224199, $result->getBounds()->getWest(), '', 0.0001); $this->assertEquals(48.902145, $result->getBounds()->getNorth(), '', 0.0001); @@ -197,47 +201,47 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.856614, $result->getLatitude(), '', 0.001); - $this->assertEquals(2.3522219, $result->getLongitude(), '', 0.001); + $this->assertEquals(48.856614, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(2.3522219, $result->getCoordinates()->getLongitude(), '', 0.001); $this->assertEquals('Paris', $result->getLocality()); $this->assertEquals('France', $result->getCountry()->getName()); $this->assertEquals('FR', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(33.6609389, $result->getLatitude(), '', 0.001); - $this->assertEquals(-95.555513, $result->getLongitude(), '', 0.001); + $this->assertEquals(33.6609389, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(-95.555513, $result->getCoordinates()->getLongitude(), '', 0.001); $this->assertEquals('Paris', $result->getLocality()); $this->assertEquals('United States', $result->getCountry()->getName()); $this->assertEquals('US', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(36.3020023, $result->getLatitude(), '', 0.001); - $this->assertEquals(-88.3267107, $result->getLongitude(), '', 0.001); + $this->assertEquals(36.3020023, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(-88.3267107, $result->getCoordinates()->getLongitude(), '', 0.001); $this->assertEquals('Paris', $result->getLocality()); $this->assertEquals('United States', $result->getCountry()->getName()); $this->assertEquals('US', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(39.611146, $result->getLatitude(), '', 0.001); - $this->assertEquals(-87.6961374, $result->getLongitude(), '', 0.001); + $this->assertEquals(39.611146, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(-87.6961374, $result->getCoordinates()->getLongitude(), '', 0.001); $this->assertEquals('Paris', $result->getLocality()); $this->assertEquals('United States', $result->getCountry()->getName()); $this->assertEquals('US', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(38.2097987, $result->getLatitude(), '', 0.001); - $this->assertEquals(-84.2529869, $result->getLongitude(), '', 0.001); + $this->assertEquals(38.2097987, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(-84.2529869, $result->getCoordinates()->getLongitude(), '', 0.001); $this->assertEquals('Paris', $result->getLocality()); $this->assertEquals('United States',$result->getCountry()->getName()); $this->assertEquals('US', $result->getCountry()->getCode()); @@ -245,7 +249,7 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() /** * @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?latlng=1.000000,2.000000". */ public function testReverse() { @@ -261,10 +265,10 @@ public function testReverseWithRealCoordinates() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(1, $result->getStreetNumber()); + $this->assertEquals(12, $result->getStreetNumber()); $this->assertEquals('Avenue Gambetta', $result->getStreetName()); $this->assertEquals(75020, $result->getPostalCode()); $this->assertEquals('Paris', $result->getLocality()); @@ -277,7 +281,7 @@ public function testReverseWithRealCoordinates() /** * @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?latlng=48.863151,2.388911". */ public function testReverseWithCoordinatesGetsNullContent() { @@ -293,7 +297,7 @@ public function testGeocodeWithCityDistrict() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('Kalbach-Riedberg', $result->getSubLocality()); @@ -322,12 +326,12 @@ public function testGeocodeWithRealValidApiKey() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertNotNull($result->getLatitude()); - $this->assertNotNull($result->getLongitude()); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertNotNull($result->getCoordinates()->getLatitude()); + $this->assertNotNull($result->getCoordinates()->getLongitude()); + $this->assertNotNull($result->getBounds()); $this->assertEquals('New York', $result->getLocality()); $this->assertEquals('Manhattan', $result->getSubLocality()); $this->assertCount(2, $result->getAdminLevels()); @@ -353,9 +357,75 @@ public function testGeocodePostalTown() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('Pontypridd', $result->getLocality()); } + + public function testBusinessQueryWithoutPrivateKey() + { + $uri = ''; + + $provider = GoogleMaps::business( + $this->getMockAdapterWithRequestCallback( + function (RequestInterface $request) use (&$uri) { + $uri = $request->getUri(); + } + ), + 'foo' + ); + + try { + $provider->geocode('blah'); + } catch (NoResult $e) { + } + + $this->assertEquals('http://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo', $uri); + } + + public function testBusinessQueryWithPrivateKey() + { + $uri = ''; + + $provider = GoogleMaps::business( + $this->getMockAdapterWithRequestCallback( + function (RequestInterface $request) use (&$uri) { + $uri = (string)$request->getUri(); + } + ), + 'foo', + 'bogus' + ); + + try { + $provider->geocode('blah'); + } catch (NoResult $e) { + } + + $this->assertEquals( + 'http://maps.googleapis.com/maps/api/geocode/json?address=blah&client=foo&signature=9G2weMhhd4E2ciR681gp9YabvUg=', + $uri + ); + } + + /** + * @expectedException \Geocoder\Exception\InvalidCredentials + * @expectedExceptionMessage Invalid client ID / API Key https://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&client=foo&signature=9dJq1hPF7_iwafUpnqXUqEkP0gY= + */ + public function testGeocodeWithInvalidClientIdAndKey() + { + $provider = GoogleMaps::business($this->getAdapter(), 'foo', 'bogus', null, null, true); + $provider->geocode('Columbia University'); + } + + /** + * @expectedException \Geocoder\Exception\InvalidCredentials + * @expectedExceptionMessage Invalid client ID / API Key http://maps.googleapis.com/maps/api/geocode/json?address=Columbia%20University&client=foo&signature=9dJq1hPF7_iwafUpnqXUqEkP0gY= + */ + public function testGeocodeWithInvalidClientIdAndKeyNoSsl() + { + $provider = GoogleMaps::business($this->getAdapter(), 'foo', 'bogus', null, null, false); + $provider->geocode('Columbia University'); + } } diff --git a/tests/Geocoder/Tests/Provider/HostIpTest.php b/tests/Geocoder/Tests/Provider/HostIpTest.php index 51bec9d7e..b4ea57521 100644 --- a/tests/Geocoder/Tests/Provider/HostIpTest.php +++ b/tests/Geocoder/Tests/Provider/HostIpTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\HostIp; @@ -51,11 +52,11 @@ public function testGeocodeWithLocalhostIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertNull($result->getLatitude()); - $this->assertNull($result->getLongitude()); + $this->assertNull($result->getCoordinates()); + $this->assertNull($result->getPostalCode()); $this->assertNull($result->getTimezone()); $this->assertEmpty($result->getAdminLevels()); @@ -102,11 +103,11 @@ public function testGeocodeWithRealIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(45.5333, $result->getLatitude(), '', 0.0001); - $this->assertEquals(2.6167, $result->getLongitude(), '', 0.0001); + $this->assertEquals(45.5333, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(2.6167, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getPostalCode()); $this->assertEquals('Aulnat', $result->getLocality()); $this->assertEmpty($result->getAdminLevels()); @@ -142,10 +143,10 @@ public function testGeocodeWithAnotherIp() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertNull($result->getLatitude()); - $this->assertNull($result->getLongitude()); + $this->assertNull($result->getCoordinates()); + } } diff --git a/tests/Geocoder/Tests/Provider/IpInfoDbTest.php b/tests/Geocoder/Tests/Provider/IpInfoDbTest.php index 80897f3fc..a15418f42 100644 --- a/tests/Geocoder/Tests/Provider/IpInfoDbTest.php +++ b/tests/Geocoder/Tests/Provider/IpInfoDbTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\IpInfoDb; @@ -27,7 +28,7 @@ public function testGetName() */ public function testGetDataWithNullApiKey() { - $provider = new IpInfoDb($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), null); + $provider = new IpInfoDb($this->getMock('Http\Client\HttpClient'), null); $provider->geocode('foo'); } @@ -79,11 +80,11 @@ public function testGeocodeWithLocalhostIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertNull($result->getLatitude()); - $this->assertNull($result->getLongitude()); + $this->assertNull($result->getCoordinates()); + $this->assertNull($result->getPostalCode()); $this->assertNull($result->getTimezone()); $this->assertEmpty($result->getAdminLevels()); @@ -134,11 +135,11 @@ public function testGeocodeWithRealIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(37.406, $result->getLatitude(), '', 0.001); - $this->assertEquals(-122.079, $result->getLongitude(), '', 0.001); + $this->assertEquals(37.406, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(-122.079, $result->getCoordinates()->getLongitude(), '', 0.001); $this->assertEquals(94043, $result->getPostalCode()); $this->assertEquals('Mountain View', $result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); @@ -177,11 +178,11 @@ public function testGetGeocodedDataWithCountryPrecision() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertNull($result->getLatitude()); - $this->assertNull($result->getLongitude()); + $this->assertNull($result->getCoordinates()); + $this->assertNull($result->getPostalCode()); $this->assertNull($result->getLocality()); $this->assertEmpty($result->getAdminLevels()); @@ -196,7 +197,7 @@ public function testGetGeocodedDataWithCountryPrecision() */ public function testReverse() { - $provider = new IpInfoDb($this->getMock('\Ivory\HttpAdapter\HttpAdapterInterface'), 'api_key'); + $provider = new IpInfoDb($this->getMock('Http\Client\HttpClient'), 'api_key'); $provider->reverse(null, null); } } diff --git a/tests/Geocoder/Tests/Provider/MapQuestTest.php b/tests/Geocoder/Tests/Provider/MapQuestTest.php index 74b4f8837..d65f5c6f8 100644 --- a/tests/Geocoder/Tests/Provider/MapQuestTest.php +++ b/tests/Geocoder/Tests/Provider/MapQuestTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\MapQuest; @@ -60,11 +61,11 @@ public function testGeocodeWithRealAddress() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.866205, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.389089, $result->getLongitude(), '', 0.01); + $this->assertEquals(48.866205, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.389089, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('10 Avenue Gambetta', $result->getStreetName()); $this->assertEquals(75020, $result->getPostalCode()); $this->assertEquals('Paris', $result->getLocality()); @@ -74,7 +75,7 @@ public function testGeocodeWithRealAddress() $this->assertEquals('FR', $result->getCountry()->getName()); $this->assertEquals('FR', $result->getCountry()->getCode()); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getAdminLevels()->get(1)->getCode()); $this->assertNull($result->getTimezone()); @@ -105,11 +106,11 @@ public function testReverseWithRealCoordinates() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(54.0484068, $result->getLatitude(), '', 0.001); - $this->assertEquals(-2.7990345, $result->getLongitude(), '', 0.001); + $this->assertEquals(54.0484068, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(-2.7990345, $result->getCoordinates()->getLongitude(), '', 0.001); $this->assertEquals('Lancaster Gate', $result->getStreetName()); $this->assertEquals('LA1 1LZ', $result->getPostalCode()); $this->assertEquals('Lancaster', $result->getLocality()); @@ -119,7 +120,7 @@ public function testReverseWithRealCoordinates() $this->assertEquals('GB', $result->getCountry()->getName()); $this->assertEquals('GB', $result->getCountry()->getCode()); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getAdminLevels()->get(1)->getCode()); $this->assertNull($result->getTimezone()); @@ -137,11 +138,11 @@ public function testGeocodeWithCity() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(52.374478, $result->getLatitude(), '', 0.01); - $this->assertEquals(9.738553, $result->getLongitude(), '', 0.01); + $this->assertEquals(52.374478, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(9.738553, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); @@ -149,11 +150,11 @@ public function testGeocodeWithCity() $this->assertEquals('DE', $result->getCountry()->getName()); $this->assertEquals('DE', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(52.374478000000003, $result->getLatitude(), '', 0.01); - $this->assertEquals(9.7385529999999996, $result->getLongitude(), '', 0.01); + $this->assertEquals(52.374478000000003, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(9.7385529999999996, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); @@ -161,11 +162,11 @@ public function testGeocodeWithCity() $this->assertEquals('DE', $result->getCountry()->getName()); $this->assertEquals('DE', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(52.374478000000003, $result->getLatitude(), '', 0.01); - $this->assertEquals(9.7385529999999996, $result->getLongitude(), '', 0.01); + $this->assertEquals(52.374478000000003, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(9.7385529999999996, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); @@ -173,11 +174,11 @@ public function testGeocodeWithCity() $this->assertEquals('DE', $result->getCountry()->getName()); $this->assertEquals('DE', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(52.374478000000003, $result->getLatitude(), '', 0.01); - $this->assertEquals(9.7385529999999996, $result->getLongitude(), '', 0.01); + $this->assertEquals(52.374478000000003, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(9.7385529999999996, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); @@ -198,11 +199,11 @@ public function testGeocodeWithCityDistrict() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(50.189062, $result->getLatitude(), '', 0.01); - $this->assertEquals(8.636567, $result->getLongitude(), '', 0.01); + $this->assertEquals(50.189062, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(8.636567, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('Kalbacher Hauptstraße 10', $result->getStreetName()); $this->assertEquals(60437, $result->getPostalCode()); $this->assertEquals('Frankfurt', $result->getLocality()); @@ -211,7 +212,7 @@ public function testGeocodeWithCityDistrict() $this->assertEquals('DE', $result->getCountry()->getName()); $this->assertEquals('DE', $result->getCountry()->getCode()); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getAdminLevels()->get(1)->getCode()); $this->assertNull($result->getTimezone()); diff --git a/tests/Geocoder/Tests/Provider/MapzenTest.php b/tests/Geocoder/Tests/Provider/MapzenTest.php new file mode 100644 index 000000000..dea610bf5 --- /dev/null +++ b/tests/Geocoder/Tests/Provider/MapzenTest.php @@ -0,0 +1,300 @@ + + */ +class MapzenTest extends TestCase +{ + public function testGetName() + { + $provider = new Mapzen($this->getMockAdapter($this->never()), 'api_key'); + $this->assertEquals('mapzen', $provider->getName()); + } + + /** + * @expectedException \Geocoder\Exception\NoResult + * @expectedExceptionMessage Could not find results for query "https://search.mapzen.com/v1/search?text=foobar&key=api_key&size=5". + */ + public function testGeocode() + { + $provider = new Mapzen($this->getMockAdapterReturns('{}'), 'api_key'); + $provider->geocode('foobar'); + } + + /** + * @expectedException \Geocoder\Exception\NoResult + * @expectedExceptionMessage Could not find results for query "https://search.mapzen.com/v1/search?text=foobar&key=api_key&size=5". + */ + public function testSslSchema() + { + $provider = new Mapzen($this->getMockAdapterReturns('{}'), 'api_key', true); + $provider->geocode('foobar'); + } + + /** + * @expectedException \Geocoder\Exception\NoResult + * @expectedExceptionMessage Could not execute query "https://search.mapzen.com/v1/search?text=242+Acklam+Road%2C+London%2C+United+Kingdom&key=api_key&size=5". + */ + public function testGeocodeWithAddressGetsNullContent() + { + $provider = new Mapzen($this->getMockAdapterReturns(null), 'api_key'); + $provider->geocode('242 Acklam Road, London, United Kingdom'); + } + + public function testGeocodeWithRealAddress() + { + if (!isset($_SERVER['MAPZEN_API_KEY'])) { + $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml'); + } + + $provider = new Mapzen($this->getAdapter($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']); + $results = $provider->geocode('242 Acklam Road, London, United Kingdom'); + + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); + $this->assertCount(5, $results); + + /** @var \Geocoder\Model\Address $result */ + $result = $results->first(); + $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertEquals(51.521124, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-0.20360200000000001, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertEquals(240, $result->getStreetNumber()); + $this->assertEquals('Acklam Road', $result->getStreetName()); + $this->assertEquals('W10 5QT', $result->getPostalCode()); + $this->assertEquals('London', $result->getLocality()); + $this->assertCount(4, $result->getAdminLevels()); + $this->assertEquals('London', $result->getAdminLevels()->get(2)->getName()); + $this->assertEquals('Kensington and Chelsea', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('United Kingdom', $result->getCountry()->getName()); + $this->assertEquals('GBR', $result->getCountry()->getCode()); + } + + /** + * @expectedException \Geocoder\Exception\NoResult + */ + public function testReverse() + { + if (!isset($_SERVER['MAPZEN_API_KEY'])) { + $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml'); + } + + $provider = new Mapzen($this->getMockAdapter(), $_SERVER['MAPZEN_API_KEY']); + $provider->reverse(1, 2); + } + + public function testReverseWithRealCoordinates() + { + if (!isset($_SERVER['MAPZEN_API_KEY'])) { + $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml'); + } + + $provider = new Mapzen($this->getAdapter($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']); + $results = $provider->reverse(54.0484068, -2.7990345); + + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); + $this->assertCount(5, $results); + + /** @var \Geocoder\Model\Address $result */ + $result = $results->first(); + $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertEquals(54.048411999999999, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(-2.7989549999999999, $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNull($result->getStreetNumber()); + $this->assertEquals('Gage Street', $result->getStreetName()); + $this->assertNull($result->getPostalCode()); + $this->assertEquals('Lancaster', $result->getLocality()); + $this->assertCount(4, $result->getAdminLevels()); + $this->assertEquals('Lancashire', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('England', $result->getAdminLevels()->get(3)->getName()); + $this->assertEquals('United Kingdom', $result->getCountry()->getName()); + $this->assertEquals('GBR', $result->getCountry()->getCode()); + } + + public function testReverseWithVillage() + { + if (!isset($_SERVER['MAPZEN_API_KEY'])) { + $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml'); + } + + $provider = new Mapzen($this->getAdapter($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']); + $results = $provider->reverse(49.1390924, 1.6572462); + + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); + $this->assertCount(5, $results); + + /** @var \Geocoder\Model\Address $result */ + $result = $results->first(); + $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertEquals('Bus-Saint-Rémy', $result->getLocality()); + } + + public function testGeocodeWithCity() + { + if (!isset($_SERVER['MAPZEN_API_KEY'])) { + $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml'); + } + + $provider = new Mapzen($this->getAdapter($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']); + $results = $provider->geocode('Hanover'); + + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); + $this->assertCount(5, $results); + + /** @var \Geocoder\Model\Address $result */ + $result = $results->first(); + $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertEquals(42.027323000000003, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-88.204203000000007, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNull($result->getLocality()); + $this->assertCount(2, $result->getAdminLevels()); + $this->assertEquals('United States', $result->getAdminLevels()->get(4)->getName()); + $this->assertEquals('Illinois', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('United States', $result->getCountry()->getName()); + + /** @var \Geocoder\Model\Address $result */ + $result = $results->get(1); + $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertEquals(18.393428, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-78.122906, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNull($result->getLocality()); + $this->assertCount(2, $result->getAdminLevels()); + $this->assertEquals('Hanover', $result->getAdminLevels()->get(1)->getName()); + $this->assertEquals('Jamaica', $result->getCountry()->getName()); + + /** @var \Geocoder\Model\Address $result */ + $result = $results->get(2); + $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertEquals(39.192889999999998, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-76.724140000000006, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertEquals('Hanover', $result->getLocality()); + $this->assertTrue( $result->getAdminLevels()->has(4)); + $this->assertEquals('Hanover', $result->getAdminLevels()->get(2)->getName()); + $this->assertEquals('United States', $result->getCountry()->getName()); + } + + public function testGeocodeWithCityDistrict() + { + if (!isset($_SERVER['MAPZEN_API_KEY'])) { + $this->markTestSkipped('You need to configure the MAPZEN_API_KEY value in phpunit.xml'); + } + + $provider = new Mapzen($this->getAdapter($_SERVER['MAPZEN_API_KEY']), $_SERVER['MAPZEN_API_KEY']); + $results = $provider->geocode('Kalbacher Hauptstraße 10, 60437 Frankfurt, Germany'); + + $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); + $this->assertCount(5, $results); + + /** @var \Geocoder\Model\Address $result */ + $result = $results->first(); + $this->assertInstanceOf('\Geocoder\Model\Address', $result); + $this->assertEquals(50.189017, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(8.6367809999999992, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertEquals('10a', $result->getStreetNumber()); + $this->assertEquals('Kalbacher Hauptstraße', $result->getStreetName()); + $this->assertEquals(60437, $result->getPostalCode()); + $this->assertEquals('Frankfurt am Main', $result->getLocality()); + $this->assertCount(3, $result->getAdminLevels()); + $this->assertEquals('Frankfurt am Main', $result->getAdminLevels()->get(2)->getName()); + $this->assertEquals('Hessen', $result->getAdminLevels()->get(1)->getName()); + $this->assertNull($result->getAdminLevels()->get(1)->getCode()); + $this->assertEquals('Germany', $result->getCountry()->getName()); + $this->assertEquals('DEU', $result->getCountry()->getCode()); + } + + /** + * @expectedException \Geocoder\Exception\QuotaExceeded + * @expectedExceptionMessage Valid request but quota exceeded. + */ + public function testGeocodeQuotaExceeded() + { + $provider = new Mapzen( + $this->getMockAdapterReturns( + '{ + "meta": { + "version": 1, + "status_code": 429 + }, + "results": { + "error": { + "type": "QpsExceededError", + "message": "Queries per second exceeded: Queries exceeded (6 allowed)." + } + } + }' + ), + 'api_key' + ); + $provider->geocode('New York'); + } + + /** + * @expectedException \Geocoder\Exception\InvalidCredentials + * @expectedExceptionMessage Invalid or missing api key. + */ + public function testGeocodeInvalidApiKey() + { + $provider = new Mapzen( + $this->getMockAdapterReturns( + '{ + "meta": { + "version": 1, + "status_code": 403 + }, + "results": { + "error": { + "type": "KeyError", + "message": "No api_key specified." + } + } + }' + ), + 'api_key' + ); + $provider->geocode('New York'); + } + + /** + * @expectedException \Geocoder\Exception\UnsupportedOperation + * @expectedExceptionMessage The Mapzen provider does not support IP addresses, only street addresses. + */ + public function testGeocodeWithLocalhostIPv4() + { + $provider = new Mapzen($this->getMockAdapter($this->never()), 'api_key'); + $provider->geocode('127.0.0.1'); + } + + /** + * @expectedException \Geocoder\Exception\UnsupportedOperation + * @expectedExceptionMessage The Mapzen provider does not support IP addresses, only street addresses. + */ + public function testGeocodeWithLocalhostIPv6() + { + $provider = new Mapzen($this->getMockAdapter($this->never()), 'api_key'); + $provider->geocode('::1'); + } + + /** + * @expectedException \Geocoder\Exception\UnsupportedOperation + * @expectedExceptionMessage The Mapzen provider does not support IP addresses, only street addresses. + */ + public function testGeocodeWithRealIPv4() + { + $provider = new Mapzen($this->getAdapter(), 'api_key'); + $provider->geocode('74.200.247.59'); + } + + /** + * @expectedException \Geocoder\Exception\UnsupportedOperation + * @expectedExceptionMessage The Mapzen provider does not support IP addresses, only street addresses. + */ + public function testGeocodeWithRealIPv6() + { + $provider = new Mapzen($this->getAdapter(), 'api_key'); + $provider->geocode('::ffff:74.200.247.59'); + } +} diff --git a/tests/Geocoder/Tests/Provider/MaxMindBinaryTest.php b/tests/Geocoder/Tests/Provider/MaxMindBinaryTest.php index 33ab95b65..c690371ed 100644 --- a/tests/Geocoder/Tests/Provider/MaxMindBinaryTest.php +++ b/tests/Geocoder/Tests/Provider/MaxMindBinaryTest.php @@ -1,6 +1,7 @@ assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals('43.089200000000005', $result->getLatitude(), '', 0.001); - $this->assertEquals('-76.025000000000006', $result->getLongitude(), '', 0.001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals('43.089200000000005', $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals('-76.025000000000006', $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -77,13 +78,13 @@ public function testLocationResultContainsExpectedFieldsForASpanishIp() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals('41.543299999999988', $result->getLatitude(), '', 0.001); - $this->assertEquals('2.1093999999999937', $result->getLongitude(), '', 0.001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals('41.543299999999988', $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals('2.1093999999999937', $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -108,7 +109,7 @@ public function testFindLocationByIp($ip, $expectedCity, $expectedCountry) $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals($expectedCity, $result->getLocality()); @@ -120,7 +121,7 @@ public function testShouldReturnResultsAsUtf8Encoded() $provider = new MaxMindBinary($this->binaryFile); $results = $provider->geocode('212.51.181.237'); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertSame('Châlette-sur-loing', $result->getLocality()); diff --git a/tests/Geocoder/Tests/Provider/MaxMindTest.php b/tests/Geocoder/Tests/Provider/MaxMindTest.php index cbe313465..b100128ad 100644 --- a/tests/Geocoder/Tests/Provider/MaxMindTest.php +++ b/tests/Geocoder/Tests/Provider/MaxMindTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\MaxMind; @@ -60,7 +61,7 @@ public function testGeocodeWithLocalhostIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('localhost', $result->getLocality()); @@ -75,7 +76,7 @@ public function testGeocodeWithLocalhostIPv6() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('localhost', $result->getLocality()); @@ -130,11 +131,11 @@ public function testGeocodeWithRealIPv4GetsFakeContentFormattedEmpty() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertNull($result->getLatitude()); - $this->assertNull($result->getLongitude()); + $this->assertNull($result->getCoordinates()); + $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -155,11 +156,11 @@ public function testGeocodeWithRealIPv4GetsFakeContent() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(33.034698486328, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-96.813400268555, $result->getLongitude(), '', 0.0001); + $this->assertEquals(33.034698486328, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-96.813400268555, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertEquals(75093, $result->getPostalCode()); @@ -187,11 +188,11 @@ public function testGeocodeWithRealIPv4GetsFakeContent() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(37.748402, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-122.415604, $result->getLongitude(), '', 0.0001); + $this->assertEquals(37.748402, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-122.415604, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertEquals(94110, $result->getPostalCode()); @@ -300,12 +301,12 @@ public function testGeocodeServiceWithRealIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(33.034698, $result->getLatitude(), '', 0.1); - $this->assertEquals(-96.813400, $result->getLongitude(), '', 0.1); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(33.034698, $result->getCoordinates()->getLatitude(), '', 0.1); + $this->assertEquals(-96.813400, $result->getCoordinates()->getLongitude(), '', 0.1); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertEquals(75093, $result->getPostalCode()); @@ -332,12 +333,12 @@ public function testGeocodeOmniServiceWithRealIPv4() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(33.0347, $result->getLatitude(), '', 0.1); - $this->assertEquals(-96.8134, $result->getLongitude(), '', 0.1); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(33.0347, $result->getCoordinates()->getLatitude(), '', 0.1); + $this->assertEquals(-96.8134, $result->getCoordinates()->getLongitude(), '', 0.1); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertEquals(75093, $result->getPostalCode()); @@ -364,12 +365,12 @@ public function testGeocodeOmniServiceWithRealIPv4WithSslAndEncoding() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(-27.5833, $result->getLatitude(), '', 0.1); - $this->assertEquals(-48.5666, $result->getLongitude(), '', 0.1); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(-27.5833, $result->getCoordinates()->getLatitude(), '', 0.1); + $this->assertEquals(-48.5666, $result->getCoordinates()->getLongitude(), '', 0.1); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -395,12 +396,12 @@ public function testGeocodeWithRealIPv6() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(40.2181, $result->getLatitude(), '', 0.1); - $this->assertEquals(-111.6133, $result->getLongitude(), '', 0.1); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(40.2181, $result->getCoordinates()->getLatitude(), '', 0.1); + $this->assertEquals(-111.6133, $result->getCoordinates()->getLongitude(), '', 0.1); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertEquals(84606, $result->getPostalCode()); @@ -427,12 +428,12 @@ public function testGeocodeOmniServiceWithRealIPv6WithSsl() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(40.2181, $result->getLatitude(), '', 0.1); - $this->assertEquals(-111.6133, $result->getLongitude(), '', 0.1); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(40.2181, $result->getCoordinates()->getLatitude(), '', 0.1); + $this->assertEquals(-111.6133, $result->getCoordinates()->getLongitude(), '', 0.1); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertEquals(84606, $result->getPostalCode()); diff --git a/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php b/tests/Geocoder/Tests/Provider/NominatimTest.php similarity index 79% rename from tests/Geocoder/Tests/Provider/OpenStreetMapTest.php rename to tests/Geocoder/Tests/Provider/NominatimTest.php index 1c28b5389..5d2430696 100644 --- a/tests/Geocoder/Tests/Provider/OpenStreetMapTest.php +++ b/tests/Geocoder/Tests/Provider/NominatimTest.php @@ -2,31 +2,26 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; +use Geocoder\Provider\Nominatim; use Geocoder\Tests\TestCase; -use Geocoder\Provider\OpenStreetMap; -class OpenStreetMapTest extends TestCase +class NominatimTest extends TestCase { - public function testGetName() - { - $provider = new OpenStreetMap($this->getMockAdapter($this->never())); - $this->assertEquals('openstreetmap', $provider->getName()); - } - public function testGeocodeWithRealAddress() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $results = $provider->geocode('Paris'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.8565056, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.3521334, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.8565056, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.3521334, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.8155250549316, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.22412180900574, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.902156829834, $result->getBounds()->getNorth(), '', 0.01); @@ -43,12 +38,12 @@ public function testGeocodeWithRealAddress() $this->assertEquals('France', $result->getCountry()->getName()); $this->assertEquals('FR', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.8588408, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.32003465529896, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.8588408, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.32003465529896, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.8155250549316, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.22412180900574, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.902156829834, $result->getBounds()->getNorth(), '', 0.01); @@ -65,12 +60,12 @@ public function testGeocodeWithRealAddress() $this->assertEquals('France', $result->getCountry()->getName()); $this->assertEquals('FR', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(35.28687645, $result->getLatitude(), '', 0.01); - $this->assertEquals(-93.7354879210082, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(35.28687645, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-93.7354879210082, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(35.2672462463379, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-93.7618103027344, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(35.3065032958984, $result->getBounds()->getNorth(), '', 0.01); @@ -87,12 +82,12 @@ public function testGeocodeWithRealAddress() $this->assertEquals('United States of America', $result->getCountry()->getName()); $this->assertEquals('US', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(33.6751155, $result->getLatitude(), '', 0.01); - $this->assertEquals(-95.5502662477703, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(33.6751155, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-95.5502662477703, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(33.6118507385254, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-95.6279296875, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(33.7383804321289, $result->getBounds()->getNorth(), '', 0.01); @@ -109,12 +104,12 @@ public function testGeocodeWithRealAddress() $this->assertEquals('United States of America', $result->getCountry()->getName()); $this->assertEquals('US', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(38.2097987, $result->getLatitude(), '', 0.01); - $this->assertEquals(-84.2529869, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(38.2097987, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-84.2529869, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(38.1649208068848, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-84.3073272705078, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(38.2382736206055, $result->getBounds()->getNorth(), '', 0.01); @@ -134,18 +129,18 @@ public function testGeocodeWithRealAddress() public function testGeocodeWithRealAddressWithLocale() { - $provider = new OpenStreetMap($this->getAdapter(), 'fr_FR'); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter(), 'fr_FR'); $results = $provider->geocode('10 allée Evariste Galois, Clermont ferrand'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(2, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(45.7586841, $result->getLatitude(), '', 0.01); - $this->assertEquals(3.1354075, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(45.7586841, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(3.1354075, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(45.7576484680176, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(3.13258004188538, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(45.7595367431641, $result->getBounds()->getNorth(), '', 0.01); @@ -162,12 +157,12 @@ public function testGeocodeWithRealAddressWithLocale() $this->assertEquals('France', $result->getCountry()->getName()); $this->assertEquals('FR', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(45.7586841, $result->getLatitude(), '', 0.01); - $this->assertEquals(3.1354075, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(45.7586841, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(3.1354075, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(45.7576484680176, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(3.13258004188538, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(45.7595367431641, $result->getBounds()->getNorth(), '', 0.01); @@ -187,18 +182,18 @@ public function testGeocodeWithRealAddressWithLocale() public function testReverseWithRealCoordinates() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $results = $provider->reverse(60.4539471728726, 22.2567841926781); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(60.4539, $result->getLatitude(), '', 0.001); - $this->assertEquals(22.2568, $result->getLongitude(), '', 0.001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(60.4539, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(22.2568, $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNull($result->getBounds()); $this->assertEquals(35, $result->getStreetNumber()); $this->assertEquals('Läntinen Pitkäkatu', $result->getStreetName()); $this->assertEquals(20100, $result->getPostalCode()); @@ -218,24 +213,24 @@ public function testReverseWithRealCoordinates() */ public function testGeocodeWithUnknownCity() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $provider->geocode('Hammm'); } public function testReverseWithRealCoordinatesWithLocale() { - $provider = new OpenStreetMap($this->getAdapter(), 'de_DE'); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter(), 'de_DE'); $results = $provider->geocode('Kalbacher Hauptstraße, 60437 Frankfurt, Germany'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(50.1856803, $result->getLatitude(), '', 0.01); - $this->assertEquals(8.6506285, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(50.1856803, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(8.6506285, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(50.1851196289062, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(8.64984607696533, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(50.1860122680664, $result->getBounds()->getNorth(), '', 0.01); @@ -251,12 +246,12 @@ public function testReverseWithRealCoordinatesWithLocale() $this->assertEquals('Deutschland', $result->getCountry()->getName()); $this->assertEquals('DE', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(50.1845911, $result->getLatitude(), '', 0.01); - $this->assertEquals(8.6540194, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(50.1845911, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(8.6540194, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(50.1840019226074, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(8.65207481384277, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(50.1851234436035, $result->getBounds()->getNorth(), '', 0.01); @@ -272,12 +267,12 @@ public function testReverseWithRealCoordinatesWithLocale() $this->assertEquals('Deutschland', $result->getCountry()->getName()); $this->assertEquals('DE', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(50.1862884, $result->getLatitude(), '', 0.01); - $this->assertEquals(8.6493167, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(50.1862884, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(8.6493167, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(50.1862106323242, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(8.64931583404541, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(50.1862907409668, $result->getBounds()->getNorth(), '', 0.01); @@ -293,12 +288,12 @@ public function testReverseWithRealCoordinatesWithLocale() $this->assertEquals('Deutschland', $result->getCountry()->getName()); $this->assertEquals('DE', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(50.1861344, $result->getLatitude(), '', 0.01); - $this->assertEquals(8.649578, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(50.1861344, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(8.649578, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(50.1860084533691, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(8.64943885803223, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(50.1862144470215, $result->getBounds()->getNorth(), '', 0.01); @@ -317,13 +312,13 @@ public function testReverseWithRealCoordinatesWithLocale() public function testGeocodeWithLocalhostIPv4() { - $provider = new OpenStreetMap($this->getMockAdapter($this->never())); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapter($this->never())); $results = $provider->geocode('127.0.0.1'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('localhost', $result->getLocality()); @@ -333,28 +328,28 @@ public function testGeocodeWithLocalhostIPv4() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The Geocoder\Provider\OpenStreetMap provider does not support IPv6 addresses. + * @expectedExceptionMessage The Geocoder\Provider\Nominatim provider does not support IPv6 addresses. */ public function testGeocodeWithLocalhostIPv6() { - $provider = new OpenStreetMap($this->getMockAdapter($this->never())); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapter($this->never())); $provider->geocode('::1'); } public function testGeocodeWithRealIPv4() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $results = $provider->geocode('88.188.221.14'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(43.6189768, $result->getLatitude(), '', 0.01); - $this->assertEquals(1.4564493, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(43.6189768, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(1.4564493, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(43.6159553527832, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(1.45302963256836, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(43.623119354248, $result->getBounds()->getNorth(), '', 0.01); @@ -374,18 +369,18 @@ public function testGeocodeWithRealIPv4() public function testGeocodeWithRealIPv4WithLocale() { - $provider = new OpenStreetMap($this->getAdapter(), 'da_DK'); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter(), 'da_DK'); $results = $provider->geocode('88.188.221.14'); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(43.6155351, $result->getLatitude(), '', 0.01); - $this->assertEquals(1.4525647, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(43.6155351, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(1.4525647, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(43.6154556274414, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(1.4524964094162, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(43.6156005859375, $result->getBounds()->getNorth(), '', 0.01); @@ -405,11 +400,11 @@ public function testGeocodeWithRealIPv4WithLocale() /** * @expectedException \Geocoder\Exception\UnsupportedOperation - * @expectedExceptionMessage The Geocoder\Provider\OpenStreetMap provider does not support IPv6 addresses. + * @expectedExceptionMessage The Geocoder\Provider\Nominatim provider does not support IPv6 addresses. */ public function testGeocodeWithRealIPv6() { - $provider = new OpenStreetMap($this->getAdapter()); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter()); $provider->geocode('::ffff:88.188.221.14'); } @@ -419,7 +414,7 @@ public function testGeocodeWithRealIPv6() */ public function testGeocodeWithAddressGetsNullContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns(null)); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns(null)); $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } @@ -429,7 +424,7 @@ public function testGeocodeWithAddressGetsNullContent() */ public function testGeocodeWithAddressGetsEmptyContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns('')); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns('')); $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } @@ -442,7 +437,7 @@ public function testGeocodeWithAddressGetsEmptyXML() $emptyXML = << XML; - $provider = new OpenStreetMap($this->getMockAdapterReturns($emptyXML)); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns($emptyXML)); $provider->geocode('Läntinen Pitkäkatu 35, Turku'); } @@ -452,7 +447,7 @@ public function testGeocodeWithAddressGetsEmptyXML() */ public function testReverseWithCoordinatesGetsNullContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns(null)); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns(null)); $provider->reverse(60.4539471728726, 22.2567841926781); } @@ -462,7 +457,7 @@ public function testReverseWithCoordinatesGetsNullContent() */ public function testReverseWithCoordinatesGetsEmptyContent() { - $provider = new OpenStreetMap($this->getMockAdapterReturns('')); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns('')); $provider->reverse(60.4539471728726, 22.2567841926781); } @@ -478,19 +473,19 @@ public function testReverseWithCoordinatesGetsError() Unable to geocode XML; - $provider = new OpenStreetMap($this->getMockAdapterReturns($errorXml)); + $provider = Nominatim::withOpenStreetMapServer($this->getMockAdapterReturns($errorXml)); $provider->reverse(-80.000000, -170.000000); } public function testGetNodeStreetName() { - $provider = new OpenStreetMap($this->getAdapter(), 'fr_FR'); + $provider = Nominatim::withOpenStreetMapServer($this->getAdapter(), 'fr_FR'); $results = $provider->reverse(48.86, 2.35); $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('Rue Quincampoix', $result->getStreetName()); diff --git a/tests/Geocoder/Tests/Provider/OpenCageTest.php b/tests/Geocoder/Tests/Provider/OpenCageTest.php index db0ef0bed..cd895df56 100644 --- a/tests/Geocoder/Tests/Provider/OpenCageTest.php +++ b/tests/Geocoder/Tests/Provider/OpenCageTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\OpenCage; @@ -58,12 +59,12 @@ public function testGeocodeWithRealAddress() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(3, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.866205, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.389089, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.866205, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.389089, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.863142699999997, $result->getBounds()->getSouth()); $this->assertEquals(2.3890394000000001, $result->getBounds()->getWest()); $this->assertEquals(48.863242700000001, $result->getBounds()->getNorth()); @@ -105,12 +106,12 @@ public function testReverseWithRealCoordinates() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(54.0484068, $result->getLatitude(), '', 0.001); - $this->assertEquals(-2.7990345, $result->getLongitude(), '', 0.001); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(54.0484068, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(-2.7990345, $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNotNull($result->getBounds()); $this->assertEquals(54.048273100000003, $result->getBounds()->getSouth()); $this->assertEquals(-2.7998815000000001, $result->getBounds()->getWest()); $this->assertEquals(54.0494992, $result->getBounds()->getNorth()); @@ -139,7 +140,7 @@ public function testReverseWithVillage() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('Bray-et-Lû', $result->getLocality()); @@ -157,42 +158,42 @@ public function testGeocodeWithCity() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(52.374478, $result->getLatitude(), '', 0.01); - $this->assertEquals(9.738553, $result->getLongitude(), '', 0.01); + $this->assertEquals(52.374478, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(9.738553, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Region Hannover', $result->getAdminLevels()->get(2)->getName()); $this->assertEquals('Lower Saxony', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('Germany', $result->getCountry()->getName()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(37.744783, $result->getLatitude(), '', 0.01); - $this->assertEquals(-77.4464165, $result->getLongitude(), '', 0.01); + $this->assertEquals(37.744783, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-77.4464165, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertNull($result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Hanover', $result->getAdminLevels()->get(2)->getName()); $this->assertEquals('United States of America', $result->getCountry()->getName()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(18.3840489, $result->getLatitude(), '', 0.01); - $this->assertEquals(-78.131485, $result->getLongitude(), '', 0.01); + $this->assertEquals(18.3840489, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-78.131485, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertNull($result->getLocality()); $this->assertTrue( $result->getAdminLevels()->has(2)); $this->assertEquals('Hanover', $result->getAdminLevels()->get(2)->getName()); $this->assertEquals('Jamaica', $result->getCountry()->getName()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(43.7033073, $result->getLatitude(), '', 0.01); - $this->assertEquals(-72.2885663, $result->getLongitude(), '', 0.01); + $this->assertEquals(43.7033073, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-72.2885663, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals('Hanover', $result->getLocality()); $this->assertCount(2, $result->getAdminLevels()); $this->assertEquals('Grafton County', $result->getAdminLevels()->get(2)->getName()); @@ -212,11 +213,11 @@ public function testGeocodeWithCityDistrict() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(2, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(50.189062, $result->getLatitude(), '', 0.01); - $this->assertEquals(8.636567, $result->getLongitude(), '', 0.01); + $this->assertEquals(50.189062, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(8.636567, $result->getCoordinates()->getLongitude(), '', 0.01); $this->assertEquals(10, $result->getStreetNumber()); $this->assertEquals('Kalbacher Hauptstraße', $result->getStreetName()); $this->assertEquals(60437, $result->getPostalCode()); @@ -242,7 +243,7 @@ public function testGeocodeWithLocale() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); $this->assertEquals('Londres', $result->getLocality()); @@ -334,15 +335,3 @@ public function testGeocodeWithRealIPv6() $provider->geocode('::ffff:74.200.247.59'); } } - -class OpenCageMock extends OpenCage -{ - /** - * Short circuits so assertions can inspect the - * executed query URL - */ - protected function executeQuery($query) - { - return $query; - } -} diff --git a/tests/Geocoder/Tests/Provider/TomTomTest.php b/tests/Geocoder/Tests/Provider/TomTomTest.php index 2c67feeaf..36462b7a2 100644 --- a/tests/Geocoder/Tests/Provider/TomTomTest.php +++ b/tests/Geocoder/Tests/Provider/TomTomTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\TomTom; @@ -90,12 +91,12 @@ public function testGeocodeWithRealAddress() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(55.704389, $result->getLatitude(), '', 0.0001); - $this->assertEquals(12.546129, $result->getLongitude(), '', 0.0001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(55.704389, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(12.546129, $result->getCoordinates()->getLongitude(), '', 0.0001); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertEquals('Tagensvej', $result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -119,12 +120,12 @@ public function testGeocodeWithRealAddressWithFrenchLocale() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(55.704389, $result->getLatitude(), '', 0.0001); - $this->assertEquals(12.546129, $result->getLongitude(), '', 0.0001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(55.704389, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(12.546129, $result->getCoordinates()->getLongitude(), '', 0.0001); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertEquals('Tagensvej', $result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -148,12 +149,12 @@ public function testGeocodeWithRealAddressWithSwedishLocale() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(55.704389, $result->getLatitude(), '', 0.0001); - $this->assertEquals(12.546129, $result->getLongitude(), '', 0.0001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(55.704389, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(12.546129, $result->getCoordinates()->getLongitude(), '', 0.0001); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertEquals('Tagensvej', $result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -177,12 +178,12 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.856898, $result->getLatitude(), '', 0.0001); - $this->assertEquals(2.350844, $result->getLongitude(), '', 0.0001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(48.856898, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(2.350844, $result->getCoordinates()->getLongitude(), '', 0.0001); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertNull($result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -195,44 +196,44 @@ public function testGeocodeWithRealAddressReturnsMultipleResults() $this->assertEquals('FRA', $result->getCountry()->getCode()); $this->assertNull($result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(33.661426, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-95.556321, $result->getLongitude(), '', 0.0001); + $this->assertEquals(33.661426, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-95.556321, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals('Paris', $result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); $this->assertEquals('Texas', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('United States',$result->getCountry()->getName()); $this->assertEquals('USA', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(36.302754, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-88.326359, $result->getLongitude(), '', 0.0001); + $this->assertEquals(36.302754, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-88.326359, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals('Paris', $result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); $this->assertEquals('Tennessee', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('United States', $result->getCountry()->getName()); $this->assertEquals('USA', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(-19.039448, $result->getLatitude(), '', 0.0001); - $this->assertEquals(29.560445, $result->getLongitude(), '', 0.0001); + $this->assertEquals(-19.039448, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(29.560445, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals('Paris', $result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); $this->assertEquals('Midlands', $result->getAdminLevels()->get(1)->getName()); $this->assertEquals('Zimbabwe', $result->getCountry()->getName()); $this->assertEquals('ZWE', $result->getCountry()->getCode()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(35.292105, $result->getLatitude(), '', 0.0001); - $this->assertEquals(-93.729922, $result->getLongitude(), '', 0.0001); + $this->assertEquals(35.292105, $result->getCoordinates()->getLatitude(), '', 0.0001); + $this->assertEquals(-93.729922, $result->getCoordinates()->getLongitude(), '', 0.0001); $this->assertEquals('Paris', $result->getLocality()); $this->assertCount(1, $result->getAdminLevels()); $this->assertEquals('Arkansas', $result->getAdminLevels()->get(1)->getName()); @@ -360,12 +361,12 @@ public function testReverseWithRealCoordinates() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(48.86323, $result->getLatitude(), '', 0.001); - $this->assertEquals(2.38877, $result->getLongitude(), '', 0.001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(48.86323, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(2.38877, $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertEquals('Avenue Gambetta', $result->getStreetName()); $this->assertNull($result->getPostalCode()); @@ -389,12 +390,12 @@ public function testGeocodeWithRealCoordinates() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('\Geocoder\Model\Address', $result); - $this->assertEquals(56.52435, $result->getLatitude(), '', 0.001); - $this->assertEquals(10.06744, $result->getLongitude(), '', 0.001); - $this->assertFalse($result->getBounds()->isDefined()); + $this->assertEquals(56.52435, $result->getCoordinates()->getLatitude(), '', 0.001); + $this->assertEquals(10.06744, $result->getCoordinates()->getLongitude(), '', 0.001); + $this->assertNull($result->getBounds()); $this->assertNull($result->getStreetNumber()); $this->assertEquals('Stabelsvej', $result->getStreetName()); $this->assertNull($result->getPostalCode()); diff --git a/tests/Geocoder/Tests/Provider/YandexTest.php b/tests/Geocoder/Tests/Provider/YandexTest.php index 4a5b42e6e..138b06393 100644 --- a/tests/Geocoder/Tests/Provider/YandexTest.php +++ b/tests/Geocoder/Tests/Provider/YandexTest.php @@ -2,6 +2,7 @@ namespace Geocoder\Tests\Provider; +use Geocoder\Location; use Geocoder\Tests\TestCase; use Geocoder\Provider\Yandex; @@ -94,12 +95,12 @@ public function testGeocodeWithRealAddress() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.863277, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.389016, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.863277, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.389016, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.861926, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.386967, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.864629, $result->getBounds()->getNorth(), '', 0.01); @@ -129,12 +130,12 @@ public function testGeocodeWithRealAddressWithUALocale() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results);; - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(55.675676, $result->getLatitude(), '', 0.01); - $this->assertEquals(12.567593, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(55.675676, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(12.567593, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(55.614999, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(12.45295, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(55.73259, $result->getBounds()->getNorth(), '', 0.01); @@ -153,29 +154,29 @@ public function testGeocodeWithRealAddressWithUALocale() $this->assertNull($result->getAdminLevels()->get(1)->getCode()); $this->assertNull($result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(55.455739, $result->getLatitude(), '', 0.01); - $this->assertEquals(9.972854, $result->getLongitude(), '', 0.01); + $this->assertEquals(55.455739, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(9.972854, $result->getCoordinates()->getLongitude(), '', 0.01); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(55.713258, $result->getLatitude(), '', 0.01); - $this->assertEquals(12.534930, $result->getLongitude(), '', 0.01); + $this->assertEquals(55.713258, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(12.534930, $result->getCoordinates()->getLongitude(), '', 0.01); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(55.698878, $result->getLatitude(), '', 0.01); - $this->assertEquals(12.578211, $result->getLongitude(), '', 0.01); + $this->assertEquals(55.698878, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(12.578211, $result->getCoordinates()->getLongitude(), '', 0.01); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(55.690380, $result->getLatitude(), '', 0.01); - $this->assertEquals(12.554827, $result->getLongitude(), '', 0.01); + $this->assertEquals(55.690380, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(12.554827, $result->getCoordinates()->getLongitude(), '', 0.01); } public function testGeocodeWithRealAddressWithUSLocale() @@ -186,12 +187,12 @@ public function testGeocodeWithRealAddressWithUSLocale() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(38.897695, $result->getLatitude(), '', 0.01); - $this->assertEquals(-77.038692, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(38.897695, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(-77.038692, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(38.891265, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(-77.046921, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(38.904125, $result->getBounds()->getNorth(), '', 0.01); @@ -220,12 +221,12 @@ public function testGeocodeWithRealAddressWithBYLocale() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(1, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(53.898077, $result->getLatitude(), '', 0.01); - $this->assertEquals(27.563673, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(53.898077, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(27.563673, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(53.896867, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(27.561624, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(53.899286, $result->getBounds()->getNorth(), '', 0.01); @@ -282,12 +283,12 @@ public function testReverseWithRealCoordinates() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.863212, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.388773, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.863212, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.388773, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.86294, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.387497, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.877038, $result->getBounds()->getNorth(), '', 0.01); @@ -308,17 +309,17 @@ public function testReverseWithRealCoordinates() $this->assertNull($result->getAdminLevels()->get(1)->getCode()); $this->assertNull($result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.864848, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.3993549, $result->getLongitude(), '', 0.01); + $this->assertEquals(48.864848, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.3993549, $result->getCoordinates()->getLongitude(), '', 0.01); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.856929, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.341197, $result->getLongitude(), '', 0.01); + $this->assertEquals(48.856929, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.341197, $result->getCoordinates()->getLongitude(), '', 0.01); } public function testReverseWithRealCoordinatesWithUSLocaleAndStreeToponym() @@ -329,12 +330,12 @@ public function testReverseWithRealCoordinatesWithUSLocaleAndStreeToponym() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.87132, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.404017, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(48.87132, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.404017, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(48.86294, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(2.387497, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(48.877038, $result->getBounds()->getNorth(), '', 0.01); @@ -355,29 +356,29 @@ public function testReverseWithRealCoordinatesWithUSLocaleAndStreeToponym() $this->assertNull($result->getAdminLevels()->get(1)->getCode()); $this->assertNull($result->getTimezone()); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(1); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.863230, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.388261, $result->getLongitude(), '', 0.01); + $this->assertEquals(48.863230, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.388261, $result->getCoordinates()->getLongitude(), '', 0.01); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(2); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.866022, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.389662, $result->getLongitude(), '', 0.01); + $this->assertEquals(48.866022, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.389662, $result->getCoordinates()->getLongitude(), '', 0.01); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(3); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.863918, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.387767, $result->getLongitude(), '', 0.01); + $this->assertEquals(48.863918, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.387767, $result->getCoordinates()->getLongitude(), '', 0.01); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->get(4); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(48.863787, $result->getLatitude(), '', 0.01); - $this->assertEquals(2.389600, $result->getLongitude(), '', 0.01); + $this->assertEquals(48.863787, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(2.389600, $result->getCoordinates()->getLongitude(), '', 0.01); } public function testReverseWithRealCoordinatesWithUALocaleAndHouseToponym() @@ -388,12 +389,12 @@ public function testReverseWithRealCoordinatesWithUALocaleAndHouseToponym() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(60.454462, $result->getLatitude(), '', 0.01); - $this->assertEquals(22.256561, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(60.454462, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(22.256561, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(60.45345, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(22.254513, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(60.455474, $result->getBounds()->getNorth(), '', 0.01); @@ -423,12 +424,12 @@ public function testReverseWithRealCoordinatesWithTRLocaleAndLocalityToponym() $this->assertInstanceOf('Geocoder\Model\AddressCollection', $results); $this->assertCount(5, $results); - /** @var \Geocoder\Model\Address $result */ + /** @var Location $result */ $result = $results->first(); $this->assertInstanceOf('Geocoder\Model\Address', $result); - $this->assertEquals(40.874651, $result->getLatitude(), '', 0.01); - $this->assertEquals(29.129562, $result->getLongitude(), '', 0.01); - $this->assertTrue($result->getBounds()->isDefined()); + $this->assertEquals(40.874651, $result->getCoordinates()->getLatitude(), '', 0.01); + $this->assertEquals(29.129562, $result->getCoordinates()->getLongitude(), '', 0.01); + $this->assertNotNull($result->getBounds()); $this->assertEquals(40.860413, $result->getBounds()->getSouth(), '', 0.01); $this->assertEquals(29.107230, $result->getBounds()->getWest(), '', 0.01); $this->assertEquals(40.876111, $result->getBounds()->getNorth(), '', 0.01); diff --git a/tests/Geocoder/Tests/TestCase.php b/tests/Geocoder/Tests/TestCase.php index d65fcaeeb..6af5baba8 100644 --- a/tests/Geocoder/Tests/TestCase.php +++ b/tests/Geocoder/Tests/TestCase.php @@ -3,8 +3,12 @@ namespace Geocoder\Tests; use Geocoder\Model\AddressFactory; -use Ivory\HttpAdapter\HttpAdapterInterface; -use Ivory\HttpAdapter\CurlHttpAdapter; +use GuzzleHttp\Psr7\Response; +use Http\Client\HttpClient; +use Http\Mock\Client as MockClient; +use Http\Adapter\Guzzle6\Client as GuzzleClient; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; /** * @author William Durand @@ -13,7 +17,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase { /** * @param null|object $expects - * @return HttpAdapterInterface + * @return HttpClient */ protected function getMockAdapter($expects = null) { @@ -23,61 +27,65 @@ protected function getMockAdapter($expects = null) $stream = $this->getMock('Psr\Http\Message\StreamInterface'); $stream - ->expects($this->any()) + ->expects($expects) ->method('__toString') ->will($this->returnValue('')); - $response = $this->getMock('Psr\Http\Message\MessageInterface'); - $response - ->expects($this->any()) - ->method('getBody') - ->will($this->returnValue($stream)); - - $adapter = $this->getMock('Ivory\HttpAdapter\HttpAdapterInterface'); - $adapter - ->expects($expects) - ->method('get') - ->will($this->returnValue($response)); + $client = new MockClient(); + $client->addResponse(new Response(200, [], $stream)); - return $adapter; + return $client; } /** * @param $returnValue - * @return HttpAdapterInterface + * @return HttpClient */ protected function getMockAdapterReturns($returnValue) { - $body = $this->getMock('Psr\Http\Message\StreamInterface'); - $body - ->expects($this->once()) - ->method('__toString') - ->will($this->returnValue((string) $returnValue)); + $client = new MockClient(); + $client->addResponse(new Response(200, [], (string) $returnValue)); - $response = $this->getMock('Psr\Http\Message\MessageInterface'); - $response - ->expects($this->once()) - ->method('getBody') - ->will($this->returnValue($body)); + return $client; + } - $adapter = $this->getMock('Ivory\HttpAdapter\HttpAdapterInterface'); - $adapter + /** + * @param callable $requestCallback + * @return HttpClient + */ + protected function getMockAdapterWithRequestCallback(callable $requestCallback) + { + $client = $this->getMockForAbstractClass(HttpClient::class); + + $client ->expects($this->once()) - ->method('get') - ->will($this->returnValue($response)); + ->method('sendRequest') + ->willReturnCallback(function (RequestInterface $request) use ($requestCallback) { + $response = $requestCallback($request); + + if (!$response instanceof ResponseInterface) { + $response = new Response(200, [], (string) $response); + } + + return $response; + }); - return $adapter; + return $client; } /** * Because I was bored to fix the test suite because of * a change in a third-party API... * - * @return HttpAdapterInterface + * @return HttpClient */ protected function getAdapter($apiKey = null) { - return new CachedResponseAdapter(new CurlHttpAdapter(), $this->useCache(), $apiKey); + return new CachedResponseClient( + new GuzzleClient(), + $this->useCache(), + $apiKey + ); } /**