diff --git a/.travis.yml b/.travis.yml index 3dff9db..a8852d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,13 @@ language: php php: - - 5.4 - - 5.5 - 5.6 - 7.0 - - hhvm before_script: - travis_retry composer self-update - travis_retry composer install --no-interaction --prefer-source --dev -script: phpunit --coverage-text --coverage-clover ./build/logs/clover.xml +script: vendor/bin/phpunit --coverage-text --coverage-clover ./build/logs/clover.xml after_script: php vendor/bin/coveralls diff --git a/CHANGELOG.md b/CHANGELOG.md index 55a655b..0e8a518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,41 +1,61 @@ -CHANGELOG -========= - -0.5.0 (2015-03-11) ------------------- - -* [BC] the package is now compatible for Laravel 5 -* improved the doc -* add code of conduct - -0.4.1 (2014-06-23) ------------------- - -* fix the way to implode provider's arguments + unit tests - -0.4.0 (2014-04-13) ------------------- - -* use Geocoder 2.4.* - - -0.3.0 (2014-04-13) ------------------- - -* support provider's arugments (BC break) - - -0.2.0 (2013-11-16) ------------------- - -* use Geocoder 2.3.x -* use config file -* use singleton instead of share -* improve tests - - -0.1.0 (2013-09-16) ------------------- - -* add badges -* initial import +# Geocoder for Laravel Changelog +All notable changes to this project will be documented in this file. +This project adheres to [Semantic Versioning](http://semver.org/). + +## [0.7.0] - 1 Oct 2016 +### Added +- ability to dump results #16. +- ability to use multiple providers in addition to the chain provider #47. +- more integration tests. +- special aggregator that allows chaining of `geocode()` and other methods. + +### Changed +- README documentation. +- to use Geocoder 3.3.x. +- namespace to `Geocoder\Laravel\...`. +- service provider to auto-load the facade. +- config file format. +- geocoding commands necessary to obtain results (must use `->all()`, `->get()`, + or `->dump()`) after the respective command. +- the service provider architecture. + +### Fixed +- MaxMindBinary Provider being instantiated with an Adapter #24. +- GeoIP2 Provider being instantiated with a generic Adapter. + +## [0.6.0] +- TBD + +## [0.5.0] - 11 Mar 2015 +### Added +- code of conduct message. +- Laravel 5 compatibility [BC]. + +### Updated +- documentation. + +## [0.4.1] - 23 Jun 2014 +### Fixed +- the way to implode provider's arguments + unit tests. + +## [0.4.0] - 13 Apr 2014 +### Updated +- to use Geocoder 2.4.x. + +## [0.3.0] - 13 Apr 2014 +### Added +- support for Provider arguments (backwards-compatibility break). + +## [0.2.0] - 16 Nov 2013 +### Added +- config file. + +### Updated +- to use Geocoder 2.3.x. +- to use singleton instead of share. +- tests. + +## [0.1.0] - 16 Sep 2013 +### Added +- badges. +- initial package. diff --git a/README.md b/README.md index f5aaad3..0067b46 100644 --- a/README.md +++ b/README.md @@ -1,147 +1,125 @@ -Geocoder for Lavarel 5 -====================== - -If you still use **Laravel 4**, please check out the `0.4.x` branch [here](https://github.com/geocoder-php/GeocoderLaravel/tree/0.4.x). - -This package allows you to use [**Geocoder**](http://geocoder-php.org/Geocoder/) -in [**Laravel 5**](http://laravel.com/). - [![Latest StableVersion](https://poser.pugx.org/toin0u/geocoder-laravel/v/stable.png)](https://packagist.org/packages/toin0u/geocoder-laravel) [![Total Downloads](https://poser.pugx.org/toin0u/geocoder-laravel/downloads.png)](https://packagist.org/packages/toin0u/geocoder-laravel) [![Build Status](https://secure.travis-ci.org/geocoder-php/GeocoderLaravel.png)](http://travis-ci.org/geocoder-php/GeocoderLaravel) [![Coverage Status](https://coveralls.io/repos/geocoder-php/GeocoderLaravel/badge.png)](https://coveralls.io/r/geocoder-php/GeocoderLaravel) +# Geocoder for Lavarel -Installation ------------- - -It can be found on [Packagist](https://packagist.org/packages/toin0u/geocoder-laravel). -The recommended way is through [composer](http://getcomposer.org). +> If you still use **Laravel 4**, please check out the `0.4.x` branch + [here](https://github.com/geocoder-php/GeocoderLaravel/tree/0.4.x). -Edit `composer.json` and add: +** Version 0.7.0 is a backwards-compatibility-breaking update. Please review + this documentation, especially the _Usage_ section before installing. ** -```json -{ - "require": { - "toin0u/geocoder-laravel": "@stable" - } -} -``` - -**Protip:** you should browse the -[`toin0u/geocoder-laravel`](https://packagist.org/packages/toin0u/geocoder-laravel) -page to choose a stable version to use, avoid the `@stable` meta constraint. - -And install dependencies: -```bash -$ composer update +This package allows you to use [**Geocoder**](http://geocoder-php.org/Geocoder/) + in [**Laravel 5**](http://laravel.com/). + +## Installation +1. Install the package via composer: + ```sh + composer require toin0u/geocoder-laravel + ``` + +2. Find the `providers` array key in `config/app.php` and register the **Geocoder + Service Provider**: + ```php + // 'providers' => [ + Geocoder\Laravel\Providers\GeocoderService::class, + // ]; + ``` + +## Configuration +Pay special attention to the language and region values if you are using them. + For example, the GoogleMaps provider uses TLDs for region values, and the + following for language values: https://developers.google.com/maps/faq#languagesupport. + +Further, a special note on the GoogleMaps provider: if you are using an API key, + you must also use set HTTPS to true. (Best is to leave it true always, unless + there is a special requirement not to.) + +See the [Geocoder documentation](http://geocoder-php.org/Geocoder/) for a list + of available adapters and providers. + +### Default Settings +By default, the configuration specifies a Chain Provider as the first provider, + containing GoogleMaps and FreeGeoIp providers. The first to return a result + will be returned. After the Chain Provider, we have added the BingMaps provider + for use in specific situations (providers contained in the Chain provider will + be run by default, providers not in the Chain provider need to be called + explicitly). The second GoogleMaps Provider outside of the Chain Provider is + there just to illustrate this point (and is used by the PHPUnit tests). +```php +return [ + 'providers' => [ + Chain::class => [ + GoogleMaps::class => [ + 'en', + 'us', + true, + env('GOOGLE_MAPS_API_KEY'), + ], + FreeGeoIp::class => [], + ], + BingMaps::class => [ + 'en-US', + env('BING_MAPS_API_KEY'), + ], + GoogleMaps::class => [ + 'en', + 'us', + true, + env('GOOGLE_MAPS_API_KEY'), + ], + ], + 'adapter' => CurlHttpAdapter::class, +]; ``` -If you do not have [**Composer**](https://getcomposer.org) installed, run these two commands: - -```bash -$ curl -sS https://getcomposer.org/installer | php -$ php composer.phar install +### Customization +If you would like to make changes to the default configuration, publish and + edit the configuration file: +```sh +php artisan vendor:publish --provider="Geocoder\Laravel\GeocoderServiceProvider" --tags="config" ``` +## Usage +The service provider initializes the `geocoder` service, accessible via the + facade `Geocoder::...` or the application helper `app('geocoder')->...`. -Usage ------ - -Find the `providers` array key in `config/app.php` and register the **Geocoder Service Provider**. - +### Geocoding Addresses +#### Get Collection of Addresses ```php -'providers' => array( - // ... - - Toin0u\Geocoder\GeocoderServiceProvider::class, -) +app('geocoder')->geocode('Los Angeles, CA')->get(); ``` -Find the `aliases` array key in `config/app.php` and register the **Geocoder Facade**. - +#### Get Array of Addresses ```php -'aliases' => array( - // ... - - 'Geocoder' => Toin0u\Geocoder\Facade\Geocoder::class, -) +app('geocoder')->geocode('Los Angeles, CA')->all(); ``` -Configuration -------------- - -Publish and edit the configuration file - -```bash -$ php artisan vendor:publish --provider="toin0u/geocoder-laravel" -``` - -The service provider creates the following services: - -* `geocoder`: the Geocoder instance. -* `geocoder.chain`: the chain provider used by Geocoder. -* `geocoder.adapter`: the HTTP adapter used to get data from remotes APIs. - -By default, the `geocoder.chain` service contains `GoogleMapsProvider` and `FreeGeoIpProvider`. -The `geocoder.adapter` service uses the cURL adapter. Override these services to use the -adapter/providers you want by editing `config/geocoder.php`: - +#### Reverse-Geocoding ```php -return [ - 'providers' => [ - '\Geocoder\Provider\GoogleMapsProvider' => ['en_EN', 'my-region', $ssl = false, 'MY_API_KEY'], - '\Geocoder\Provider\GoogleMapsBusinessProvider' => ['my-locale', 'my-region', $ssl = true, 'MY_API_KEY'], - ], - 'adapter' => '\Geocoder\HttpAdapter\CurlHttpAdapter' -]; +app('geocoder')->reverse(43.882587,-103.454067)->get(); ``` -NB: As you can see the array value of the provider is the constructor arguments. - -See [the Geocoder documentation](http://geocoder-php.org/Geocoder/) for a list of available adapters and providers. - - -Example with Facade -------------------- - +#### Dumping Results ```php -getMessage(); -} +app('geocoder')->geocode('Los Angeles, CA')->dump('kml'); ``` +## Changelog +https://github.com/geocoder-php/GeocoderLaravel/blob/master/CHANGELOG.md -Changelog ---------- - -[See the CHANGELOG file](https://github.com/geocoder-php/GeocoderLaravel/blob/master/CHANGELOG.md) - - -Support -------- - -[Please open an issue on GitHub](https://github.com/geocoder-php/GeocoderLaravel/issues) - - -Contributor Code of Conduct ---------------------------- - -Please note that this project is released with a Contributor Code of Conduct. -By participating in this project you agree to abide by its terms. - +## Support +If you are experiencing difficulties, please please open an issue on GitHub: + https://github.com/geocoder-php/GeocoderLaravel/issues. -License -------- +## Contributor Code of Conduct +Please note that this project is released with a + [Contributor Code of Conduct](https://github.com/geocoder-php/Geocoder#contributor-code-of-conduct). + By participating in this project you agree to abide by its terms. +## License GeocoderLaravel is released under the MIT License. See the bundled -[LICENSE](https://github.com/geocoder-php/GeocoderLaravel/blob/master/LICENSE) -file for details. + [LICENSE](https://github.com/geocoder-php/GeocoderLaravel/blob/master/LICENSE) + file for details. diff --git a/composer.json b/composer.json index d959567..9fbf5d7 100644 --- a/composer.json +++ b/composer.json @@ -1,44 +1,57 @@ { - "name" : "toin0u/geocoder-laravel", - "description" : "Geocoder Service provider for Laravel 4", - "keywords" : ["laravel", "geocoder", "geocoding"], - "homepage" : "http://geocoder-php.org/", - "license" : "MIT", - - "authors" : [{ - "name" : "Antoine Corcy", - "email" : "contact@sbin.dk", - "homepage" : "http://sbin.dk", - "role" : "Developer" - }], - - "require" : { - "php" : ">=5.4", - "illuminate/support" : "~5.0", - "willdurand/geocoder" : "~2.4" + "name": "toin0u/geocoder-laravel", + "description": "Geocoder Service Provider for Laravel", + "keywords": [ + "laravel", + "geocoder", + "geocoding" + ], + "homepage": "http://geocoder-php.org/", + "license": "MIT", + "authors": [ + { + "name": "Antoine Corcy", + "email": "contact@sbin.dk", + "homepage": "http://sbin.dk", + "role": "Developer" + }, + { + "name": "Mike Bronner", + "email": "hello@genealabs.com", + "homepage": "https://genealabs.com", + "role": "developer" + } + ], + "require": { + "php": ">=5.4", + "illuminate/support": "~5.0", + "willdurand/geocoder": "~3.3" }, - - "require-dev" : { - "orchestra/testbench" : "~3.0", - "satooshi/php-coveralls" : "~0.6", - "phpunit/phpunit" : "~4.5" + "require-dev": { + "orchestra/testbench": "~3.0", + "satooshi/php-coveralls": "~0.6", + "fzaninotto/faker": "~1.4", + "mockery/mockery": "0.9.*", + "phpunit/phpunit": "~5.0", + "symfony/css-selector": "3.1.*", + "symfony/dom-crawler": "3.1.*", + "doctrine/dbal": "^2.5", + "laravel/laravel": "^5.3" }, - - "autoload" : { - "psr-4" : { - "Toin0u\\Geocoder\\" : "src/" + "autoload": { + "psr-4": { + "Geocoder\\Laravel\\": "src/" } }, - - "autoload-dev" : { + "autoload-dev": { "psr-4": { - "Toin0u\\Tests\\Geocoder\\" : "tests/" + "App\\": "vendor/laravel/laravel/app/", + "Geocoder\\Laravel\\Tests\\": "tests/" } }, - - "extra" : { - "branch-alias" : { - "dev-master" : "0.6-dev" + "extra": { + "branch-alias": { + "dev-master": "1.0.0-dev" } } } diff --git a/config/geocoder.php b/config/geocoder.php index 61624ea..2992e9c 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -9,12 +9,35 @@ * file that was distributed with this source code. */ +use Ivory\HttpAdapter\CurlHttpAdapter; +use Ivory\HttpAdapter\Guzzle6HttpAdapter; +use Geocoder\Provider\Chain; +use Geocoder\Provider\BingMaps; +use Geocoder\Provider\FreeGeoIp; +use Geocoder\Provider\GoogleMaps; +use Geocoder\Provider\MaxMindBinary; + return [ - // Providers get called in the chain order given here. - // The first one to return a result will be used. 'providers' => [ - 'Geocoder\Provider\GoogleMapsProvider' => ['fr-FR', 'Île-de-France', true], - 'Geocoder\Provider\FreeGeoIpProvider' => null, + Chain::class => [ + GoogleMaps::class => [ + 'en', + 'us', + true, + env('GOOGLE_MAPS_API_KEY'), + ], + FreeGeoIp::class => [], + ], + BingMaps::class => [ + 'en-US', + env('BING_MAPS_API_KEY'), + ], + GoogleMaps::class => [ + 'en', + 'us', + true, + env('GOOGLE_MAPS_API_KEY'), + ], ], - 'adapter' => 'Geocoder\HttpAdapter\CurlHttpAdapter', + 'adapter' => CurlHttpAdapter::class, ]; diff --git a/phpunit.xml.dist b/phpunit.xml similarity index 51% rename from phpunit.xml.dist rename to phpunit.xml index 7e2c52c..2c2aa40 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml @@ -9,10 +9,11 @@ stopOnFailure="false" syntaxCheck="false" bootstrap="vendor/autoload.php" - > +> - ./tests/ + ./tests/Geocoder + ./tests/Laravel5_3 @@ -20,4 +21,13 @@ ./src/ + + + + + + + + + diff --git a/src/Exceptions/InvalidDumperException.php b/src/Exceptions/InvalidDumperException.php new file mode 100644 index 0000000..fa6939c --- /dev/null +++ b/src/Exceptions/InvalidDumperException.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Geocoder\Exception\Exception; +use Exception as BaseException; + +/** + * Exception to indicate an invalidly specified dumper identifier when calling + * the `dump()` method on the ProviderAndDumperAggregator class. + * + * @author Mike Bronner + */ +class InvalidDumperException extends BaseException implements Exception +{ + +} diff --git a/src/Facade/Geocoder.php b/src/Facades/Geocoder.php similarity index 81% rename from src/Facade/Geocoder.php rename to src/Facades/Geocoder.php index d549e00..749e852 100644 --- a/src/Facade/Geocoder.php +++ b/src/Facades/Geocoder.php @@ -1,4 +1,4 @@ - */ -class Geocoder extends \Illuminate\Support\Facades\Facade +class Geocoder extends Facade { /** * Get the registered name of the component. diff --git a/src/GeocoderServiceProvider.php b/src/GeocoderServiceProvider.php deleted file mode 100644 index b843f4f..0000000 --- a/src/GeocoderServiceProvider.php +++ /dev/null @@ -1,89 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Toin0u\Geocoder; - -use Geocoder\Geocoder; -use Geocoder\Provider\ChainProvider; - -/** - * Geocoder service provider - * - * @author Antoine Corcy - */ -class GeocoderServiceProvider extends \Illuminate\Support\ServiceProvider -{ - /** - * Bootstrap the application events. - * - * @return void - */ - public function boot() - { - $source = realpath(__DIR__ . '/../config/geocoder.php'); - - $this->publishes([$source => config_path('geocoder.php')]); - - $this->mergeConfigFrom($source, 'geocoder'); - } - - /** - * Register the service provider. - * - * @return void - */ - public function register() - { - $this->app->singleton('geocoder.adapter', function($app) { - $adapter = $app['config']->get('geocoder.adapter'); - - return new $adapter; - }); - - $this->app->singleton('geocoder.chain', function($app) { - $providers = []; - - foreach($app['config']->get('geocoder.providers') as $provider => $arguments) { - if (0 !== count($arguments)) { - $providers[] = call_user_func_array( - function ($arg1 = null, $arg2 = null, $arg3 = null, $arg4 = null) use ($app, $provider) { - return new $provider($app['geocoder.adapter'], $arg1, $arg2, $arg3, $arg4); - }, - $arguments - ); - - continue; - } - - $providers[] = new $provider($app['geocoder.adapter']); - } - - return new ChainProvider($providers); - }); - - $this->app['geocoder'] = $this->app->share(function($app) { - $geocoder = new Geocoder; - $geocoder->registerProvider($app['geocoder.chain']); - - return $geocoder; - }); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return ['geocoder', 'geocoder.adapter', 'geocoder.chain']; - } -} diff --git a/src/ProviderAndDumperAggregator.php b/src/ProviderAndDumperAggregator.php new file mode 100644 index 0000000..da5019f --- /dev/null +++ b/src/ProviderAndDumperAggregator.php @@ -0,0 +1,100 @@ + + */ +class ProviderAndDumperAggregator extends ProviderAggregator implements Geocoder +{ + /** + * @var AddressCollection + */ + protected $results; + + /** + * @return array + */ + public function all() + { + return $this->results->all(); + } + + /** + * @param string + * @return Collection + */ + public function dump($dumper) + { + $dumperClasses = collect([ + 'geojson' => GeoJson::class, + 'gpx' => Gpx::class, + 'kml' => Kml::class, + 'wkb' => Wkb::class, + 'wkt' => Wkt::class, + ]); + + if (! $dumperClasses->has($dumper)) { + $errorMessage = implode('', [ + "The dumper specified ('{$dumper}') is invalid. Valid dumpers ", + "are: geojson, gpx, kml, wkb, wkt.", + ]); + throw new InvalidDumperException($errorMessage); + } + + $dumperClass = $dumperClasses->get($dumper); + $dumper = new $dumperClass; + $results = collect($this->results->all()); + + return $results->map(function ($result) use ($dumper) { + return $dumper->dump($result); + }); + } + + /** + * @param string + * @return ProviderAndDumperAggregator + */ + public function geocode($value) + { + $this->results = parent::geocode($value); + + return $this; + } + + /** + * @return ProviderAndDumperAggregator + */ + public function get() + { + return $this->results; + } + + /** + * @param float + * @param float + * @return ProviderAndDumperAggregator + */ + public function reverse($latitude, $longitude) + { + $this->results = parent::reverse($latitude, $longitude); + + return $this; + } +} diff --git a/src/Providers/GeocoderService.php b/src/Providers/GeocoderService.php new file mode 100644 index 0000000..9da2384 --- /dev/null +++ b/src/Providers/GeocoderService.php @@ -0,0 +1,137 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Geocoder\Laravel\Facades\Geocoder; +use Geocoder\Laravel\ProviderAndDumperAggregator; +use Geocoder\Provider\Chain; +use Illuminate\Support\Collection; +use Illuminate\Support\ServiceProvider; +use Illuminate\Foundation\AliasLoader; +use ReflectionClass; + +/** + * Geocoder service provider + * + * @author Antoine Corcy + * @author Mike Bronner + */ +class GeocoderService extends ServiceProvider +{ + /** + * Bootstrap the application events. + * + * @return void + */ + public function boot() + { + $configPath = __DIR__ . '/../../config/geocoder.php'; + $this->publishes([$configPath => config_path('geocoder.php')], 'config'); + $this->mergeConfigFrom($configPath, 'geocoder'); + } + + /** + * Register the service provider. + * + * @return void + */ + public function register() + { + AliasLoader::getInstance()->alias('Geocoder', Geocoder::class); + + $this->app->singleton('geocoder', function () { + $geocoder = new ProviderAndDumperAggregator(); + $geocoder->registerProviders( + $this->getProviders(collect(config('geocoder.providers'))) + ); + + return $geocoder; + }); + } + + /** + * Instantiate the configured Providers, as well as the Chain Provider. + * + * @param Collection + * @return array + */ + private function getProviders(Collection $providers) + { + $providers = $providers->map(function ($arguments, $provider) { + $arguments = $this->getArguments($arguments, $provider); + $reflection = new ReflectionClass($provider); + + if ($provider === 'Geocoder\Provider\Chain') { + return $reflection->newInstance($arguments); + } + + return $reflection->newInstanceArgs($arguments); + }); + + return $providers->toArray(); + } + + /** + * Insert the required Adapter instance (if required) as the first element + * of the arguments array. + * + * @param array + * @param string + * @return string + */ + private function getArguments(array $arguments, $provider) + { + if ($provider === 'Geocoder\Provider\Chain') { + return $this->getProviders( + collect(config('geocoder.providers.Geocoder\Provider\Chain')) + ); + } + + $adapter = $this->getAdapterClass($provider); + + if ($adapter) { + array_unshift($arguments, (new $adapter)); + } + + return $arguments; + } + + /** + * Get the required Adapter class name for the current provider. It will + * select a specific adapter if required, handle the Chain provider, and + * return the default configured adapter if non of the above are true. + * + * @param string + * @return string + */ + private function getAdapterClass($provider) + { + $specificAdapters = collect([ + 'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter', + 'Geocoder\Provider\MaxMindBinary' => null, + ]); + + if ($specificAdapters->has($provider)) { + return $specificAdapters->get($provider); + } + + return config('geocoder.adapter'); + } + + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return ['geocoder']; + } +} diff --git a/tests/Facade/GeocoderTest.php b/tests/Facade/GeocoderTest.php deleted file mode 100644 index 079e650..0000000 --- a/tests/Facade/GeocoderTest.php +++ /dev/null @@ -1,25 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Toin0u\Tests\Geocoder\Facade; - -/** - * @author Antoine Corcy - */ -class GeocoderTest extends \Toin0u\Tests\Geocoder\TestCase -{ - public function testGeocoderFacade() - { - $this->assertTrue(is_array($providers = \Geocoder::getProviders())); - $this->assertArrayHasKey('chain', $providers); - $this->assertInstanceOf('Geocoder\\Provider\\ChainProvider', $providers['chain']); - } -} diff --git a/tests/GeocoderServiceProviderTest.php b/tests/GeocoderServiceProviderTest.php deleted file mode 100644 index 57c9d45..0000000 --- a/tests/GeocoderServiceProviderTest.php +++ /dev/null @@ -1,72 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Toin0u\Tests\Geocoder; - -/** - * @author Antoine Corcy - */ -class GeocoderServiceProviderTest extends TestCase -{ - public function testConfig() - { - $this->assertTrue(is_array($providers = $this->app['config']->get('geocoder.providers'))); - $this->assertCount(2, $providers); - $this->assertArrayHasKey('Geocoder\\Provider\\GoogleMapsProvider', $providers); - $this->assertArrayHasKey('Geocoder\\Provider\\FreeGeoIpProvider', $providers); - $this->assertSame('Geocoder\\HttpAdapter\\CurlHttpAdapter', $this->app['config']->get('geocoder.adapter')); - } - - public function testLoadedProviders() - { - $loadedProviders = $this->app->getLoadedProviders(); - - $this->assertArrayHasKey('Toin0u\\Geocoder\\GeocoderServiceProvider', $loadedProviders); - $this->assertTrue($loadedProviders['Toin0u\\Geocoder\\GeocoderServiceProvider']); - } - - public function testGeocoderDefaultAdapter() - { - $this->assertInstanceOf('Geocoder\\HttpAdapter\\CurlHttpAdapter', $this->app['geocoder.adapter']); - } - - public function testGeocoderChainProvider() - { - $this->assertInstanceOf('Geocoder\\Provider\\ChainProvider', $this->app['geocoder.chain']); - } - - public function testGeocoderDefaultProvider() - { - $providers = $this->getProtectedProperty($this->app['geocoder.chain'], 'providers'); - - $this->assertInstanceOf('Geocoder\\Provider\\GoogleMapsProvider', $providers[0]); - $this->assertSame('fr-FR', $providers[0]->getLocale()); - $this->assertInstanceOf('Geocoder\\HttpAdapter\\CurlHttpAdapter', $providers[0]->getAdapter()); - - $this->assertInstanceOf('Geocoder\\Provider\\FreeGeoIpProvider', $providers[1]); - $this->assertNull($providers[1]->getLocale()); - $this->assertInstanceOf('Geocoder\\HttpAdapter\\CurlHttpAdapter', $providers[1]->getAdapter()); - } - - public function testGeocoder() - { - $this->assertInstanceOf('Geocoder\\Geocoder', $this->app['geocoder']); - } - - protected function getProtectedProperty($testObj, $propertyName) - { - $reflection = new \ReflectionClass($testObj); - $property = $reflection->getProperty($propertyName); - $property->setAccessible(true); - - return $property->getValue($testObj); - } -} diff --git a/tests/Laravel5_3/Providers/GeocoderServiceTest.php b/tests/Laravel5_3/Providers/GeocoderServiceTest.php new file mode 100644 index 0000000..10f6996 --- /dev/null +++ b/tests/Laravel5_3/Providers/GeocoderServiceTest.php @@ -0,0 +1,112 @@ +using('chain') + ->geocode('1600 Pennsylvania Ave., Washington, DC USA') + ->all(); + $this->assertEquals('1600', $result[0]->getStreetNumber()); + $this->assertEquals('Pennsylvania Avenue Southeast', $result[0]->getStreetName()); + $this->assertEquals('Washington', $result[0]->getLocality()); + $this->assertEquals('20003', $result[0]->getPostalCode()); + } + + public function testItResolvesAGivenIPAddress() + { + $result = app('geocoder') + ->geocode('8.8.8.8') + ->all(); + $this->assertEquals('US', $result[0]->getCountry()->getCode()); + } + + public function testItResolvesAGivenAddressWithUmlauts() + { + $result = app('geocoder') + ->geocode('Obere Donaustrasse 22, Wien, Österreich') + ->all(); + $this->assertEquals('22', $result[0]->getStreetNumber()); + $this->assertEquals('Obere Donaustraße', $result[0]->getStreetName()); + $this->assertEquals('Wien', $result[0]->getLocality()); + $this->assertEquals('1020', $result[0]->getPostalCode()); + } + + public function testItCanUseMaxMindBinaryWithoutProvider() + { + $result = app('geocoder') + ->geocode('1600 Pennsylvania Ave., Washington, DC USA') + ->all(); + $this->assertEquals('1600', $result[0]->getStreetNumber()); + $this->assertEquals('Pennsylvania Avenue Southeast', $result[0]->getStreetName()); + $this->assertEquals('Washington', $result[0]->getLocality()); + $this->assertEquals('20003', $result[0]->getPostalCode()); + } + + public function testItCanUseASpecificProvider() + { + $result = app('geocoder') + ->using('google_maps') + ->geocode('1600 Pennsylvania Ave., Washington, DC USA') + ->all(); + $this->assertEquals('1600', $result[0]->getStreetNumber()); + $this->assertEquals('Pennsylvania Avenue Southeast', $result[0]->getStreetName()); + $this->assertEquals('Washington', $result[0]->getLocality()); + $this->assertEquals('20003', $result[0]->getPostalCode()); + } + + public function testItDumpsAndAddress() + { + $result = app('geocoder') + ->using('google_maps') + ->geocode('1600 Pennsylvania Ave., Washington, DC USA') + ->dump('geojson'); + $jsonAddress = json_decode($result->first()); + + $this->assertEquals('1600', $jsonAddress->properties->streetNumber); + } + + public function testItThrowsAnExceptionForInvalidDumper() + { + $this->expectException(InvalidDumperException::class); + $result = app('geocoder') + ->using('google_maps') + ->geocode('1600 Pennsylvania Ave., Washington, DC USA') + ->dump('test'); + $jsonAddress = json_decode($result->first()); + + $this->assertEquals('1600', $jsonAddress->properties->streetNumber); + } + + public function testConfig() + { + $this->assertTrue(is_array($providers = $this->app['config']->get('geocoder.providers'))); + $this->assertCount(3, $providers); + $this->assertArrayHasKey(GoogleMaps::class, $providers[Chain::class]); + $this->assertArrayHasKey(FreeGeoIp::class, $providers[Chain::class]); + $this->assertSame(CurlHttpAdapter::class, $this->app['config']->get('geocoder.adapter')); + } + + public function testLoadedProviders() + { + $loadedProviders = $this->app->getLoadedProviders(); + + $this->assertArrayHasKey(GeocoderService::class, $loadedProviders); + $this->assertTrue($loadedProviders[GeocoderService::class]); + } + + public function testGeocoder() + { + $this->assertInstanceOf(ProviderAndDumperAggregator::class, app('geocoder')); + } +} diff --git a/tests/Laravel5_3/TestCase.php b/tests/Laravel5_3/TestCase.php new file mode 100644 index 0000000..de4abfa --- /dev/null +++ b/tests/Laravel5_3/TestCase.php @@ -0,0 +1,26 @@ +make(Kernel::class)->bootstrap(); + $app->register(GeocoderService::class); + + return $app; + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php deleted file mode 100644 index 31efb9b..0000000 --- a/tests/TestCase.php +++ /dev/null @@ -1,38 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Toin0u\Tests\Geocoder; - -/** - * @author Antoine Corcy - */ -class TestCase extends \Orchestra\Testbench\TestCase -{ - /** - * {@inheritDoc} - */ - protected function getPackageProviders($app) - { - return [ - 'Toin0u\Geocoder\GeocoderServiceProvider', - ]; - } - - /** - * {@inheritDoc} - */ - protected function getPackageAliases($app) - { - return [ - 'Geocoder' => 'Toin0u\Geocoder\Facade\Geocoder', - ]; - } -}