From 21768be3f8097d220075fbcd6f8975be9a96452b Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 29 Sep 2016 19:42:43 -0700 Subject: [PATCH 01/25] Updating for Laravel 5.3 --- composer.json | 72 ++++++++++++++++++--------------- config/geocoder.php | 6 +-- src/GeocoderServiceProvider.php | 42 +++++++++---------- 3 files changed, 61 insertions(+), 59 deletions(-) diff --git a/composer.json b/composer.json index d959567..07d9ced 100644 --- a/composer.json +++ b/composer.json @@ -1,44 +1,50 @@ { - "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", + "phpunit/phpunit": "~4.5" }, - - "autoload" : { - "psr-4" : { - "Toin0u\\Geocoder\\" : "src/" + "autoload": { + "psr-4": { + "Toin0u\\Geocoder\\": "src/" } }, - - "autoload-dev" : { + "autoload-dev": { "psr-4": { - "Toin0u\\Tests\\Geocoder\\" : "tests/" + "Toin0u\\Tests\\Geocoder\\": "tests/" } }, - - "extra" : { - "branch-alias" : { - "dev-master" : "0.6-dev" + "extra": { + "branch-alias": { + "dev-master": "0.7-dev" } } } diff --git a/config/geocoder.php b/config/geocoder.php index 61624ea..33f2af9 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -13,8 +13,8 @@ // 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, + 'Geocoder\Provider\GoogleMaps' => ['fr-FR', 'France', true], + 'Geocoder\Provider\FreeGeoIp' => null, ], - 'adapter' => 'Geocoder\HttpAdapter\CurlHttpAdapter', + 'adapter' => 'Ivory\HttpAdapter\Guzzle6HttpAdapter', ]; diff --git a/src/GeocoderServiceProvider.php b/src/GeocoderServiceProvider.php index b843f4f..84ab75c 100644 --- a/src/GeocoderServiceProvider.php +++ b/src/GeocoderServiceProvider.php @@ -11,8 +11,9 @@ namespace Toin0u\Geocoder; -use Geocoder\Geocoder; -use Geocoder\Provider\ChainProvider; +use Geocoder\ProviderAggregator; +use Geocoder\Provider\Chain; +use ReflectionClass; /** * Geocoder service provider @@ -30,7 +31,7 @@ public function boot() { $source = realpath(__DIR__ . '/../config/geocoder.php'); - $this->publishes([$source => config_path('geocoder.php')]); + $this->publishes([$source => config_path('geocoder.php')], 'config'); $this->mergeConfigFrom($source, 'geocoder'); } @@ -42,35 +43,30 @@ public function boot() */ public function register() { - $this->app->singleton('geocoder.adapter', function($app) { - $adapter = $app['config']->get('geocoder.adapter'); + $this->app->singleton('geocoder.adapter', function ($app) { + $adapter = config('geocoder.adapter'); return new $adapter; }); - $this->app->singleton('geocoder.chain', function($app) { - $providers = []; + $this->app->singleton('geocoder.chain', function ($app) { + $providers = collect(config('geocoder.providers')) + ->map(function ($arguments, $provider) { + $reflection = new ReflectionClass($provider); - 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 - ); + if (is_array($arguments)) { + array_unshift($arguments, $this->app['geocoder.adapter']); + return $reflection->newInstanceArgs($arguments); + } - continue; - } + return $reflection->newInstance($this->app['geocoder.adapter']); + }); - $providers[] = new $provider($app['geocoder.adapter']); - } - - return new ChainProvider($providers); + return new Chain($providers->toArray()); }); - $this->app['geocoder'] = $this->app->share(function($app) { - $geocoder = new Geocoder; + $this->app->singleton('geocoder', function ($app) { + $geocoder = new ProviderAggregator(); $geocoder->registerProvider($app['geocoder.chain']); return $geocoder; From 3f1d8b6abbb0531988851e8843a6448a21c7a2aa Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 29 Sep 2016 21:29:57 -0700 Subject: [PATCH 02/25] Updated config --- config/geocoder.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/config/geocoder.php b/config/geocoder.php index 33f2af9..7a87dc4 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -1,5 +1,10 @@ [ - 'Geocoder\Provider\GoogleMaps' => ['fr-FR', 'France', true], - 'Geocoder\Provider\FreeGeoIp' => null, + GoogleMaps::class => [ + 'en_US', + null, + true, + env('GOOGLE_MAPS_API_KEY'), + ], + BingMaps::class => [ + 'en_US', + env('BING_MAPS_API_KEY'), + ], + FreeGeoIp::class => null, ], - 'adapter' => 'Ivory\HttpAdapter\Guzzle6HttpAdapter', + 'adapter' => CurlHttpAdapter::class, ]; From 7f857c77392343431d2542f82a5e121e4e01dbce Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Fri, 30 Sep 2016 15:15:47 -0700 Subject: [PATCH 03/25] Update publish command in readme Fixes #46 --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f5aaad3..d33a8a2 100644 --- a/README.md +++ b/README.md @@ -68,13 +68,11 @@ Find the `aliases` array key in `config/app.php` and register the **Geocoder Fac ) ``` -Configuration -------------- - +## Configuration Publish and edit the configuration file -```bash -$ php artisan vendor:publish --provider="toin0u/geocoder-laravel" +```sh +php artisan vendor:publish --provider="Toin0u\Geocoder\GeocoderServiceProvider" --tags="config" ``` The service provider creates the following services: From a6d9e34dcb06146a3e46f03b6c8acc93472d821b Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Fri, 30 Sep 2016 19:49:30 -0700 Subject: [PATCH 04/25] Re-arrange tests, make sure IP addresses are geocoded. Fixes #34 --- composer.json | 6 +++-- config/geocoder.php | 9 ++----- phpunit.xml.dist => phpunit.xml | 5 ++-- src/GeocoderServiceProvider.php | 3 ++- tests/{ => Geocoder}/Facade/GeocoderTest.php | 0 .../GeocoderServiceProviderTest.php | 0 tests/{ => Geocoder}/TestCase.php | 0 .../Providers/GeocoderServiceProviderTest.php | 25 +++++++++++++++++++ tests/Laravel5_3/TestCase.php | 24 ++++++++++++++++++ 9 files changed, 60 insertions(+), 12 deletions(-) rename phpunit.xml.dist => phpunit.xml (80%) rename tests/{ => Geocoder}/Facade/GeocoderTest.php (100%) rename tests/{ => Geocoder}/GeocoderServiceProviderTest.php (100%) rename tests/{ => Geocoder}/TestCase.php (100%) create mode 100644 tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php create mode 100644 tests/Laravel5_3/TestCase.php diff --git a/composer.json b/composer.json index 07d9ced..d861f6f 100644 --- a/composer.json +++ b/composer.json @@ -34,12 +34,14 @@ }, "autoload": { "psr-4": { - "Toin0u\\Geocoder\\": "src/" + "Toin0u\\Geocoder\\": "src/", + "Toin0u\\GeocoderLaravel\\Tests\\Laravel5_3\\": "tests/Laravel5_3/" } }, "autoload-dev": { "psr-4": { - "Toin0u\\Tests\\Geocoder\\": "tests/" + "Toin0u\\Tests\\Geocoder\\": "tests/Geocoder/", + "Toin0u\\GeocoderLaravel\\Tests\\Laravel5_3\\": "tests/Laravel5_3/" } }, "extra": { diff --git a/config/geocoder.php b/config/geocoder.php index 7a87dc4..ce26e47 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -18,14 +18,9 @@ // Providers get called in the chain order given here. // The first one to return a result will be used. 'providers' => [ - GoogleMaps::class => [ - 'en_US', - null, - true, - env('GOOGLE_MAPS_API_KEY'), - ], + GoogleMaps::class => null, BingMaps::class => [ - 'en_US', + 'en-US', env('BING_MAPS_API_KEY'), ], FreeGeoIp::class => null, diff --git a/phpunit.xml.dist b/phpunit.xml similarity index 80% rename from phpunit.xml.dist rename to phpunit.xml index 7e2c52c..daa6b46 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 diff --git a/src/GeocoderServiceProvider.php b/src/GeocoderServiceProvider.php index 84ab75c..b650780 100644 --- a/src/GeocoderServiceProvider.php +++ b/src/GeocoderServiceProvider.php @@ -13,6 +13,7 @@ use Geocoder\ProviderAggregator; use Geocoder\Provider\Chain; +use Illuminate\Support\ServiceProvider; use ReflectionClass; /** @@ -20,7 +21,7 @@ * * @author Antoine Corcy */ -class GeocoderServiceProvider extends \Illuminate\Support\ServiceProvider +class GeocoderServiceProvider extends ServiceProvider { /** * Bootstrap the application events. diff --git a/tests/Facade/GeocoderTest.php b/tests/Geocoder/Facade/GeocoderTest.php similarity index 100% rename from tests/Facade/GeocoderTest.php rename to tests/Geocoder/Facade/GeocoderTest.php diff --git a/tests/GeocoderServiceProviderTest.php b/tests/Geocoder/GeocoderServiceProviderTest.php similarity index 100% rename from tests/GeocoderServiceProviderTest.php rename to tests/Geocoder/GeocoderServiceProviderTest.php diff --git a/tests/TestCase.php b/tests/Geocoder/TestCase.php similarity index 100% rename from tests/TestCase.php rename to tests/Geocoder/TestCase.php diff --git a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php new file mode 100644 index 0000000..fde0ad7 --- /dev/null +++ b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php @@ -0,0 +1,25 @@ +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()); + } +} diff --git a/tests/Laravel5_3/TestCase.php b/tests/Laravel5_3/TestCase.php new file mode 100644 index 0000000..37d1327 --- /dev/null +++ b/tests/Laravel5_3/TestCase.php @@ -0,0 +1,24 @@ +make(Kernel::class)->bootstrap(); + + return $app; + } +} From 245ff56ceb084178f253c5405b7c6fec7e4cc638 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Fri, 30 Sep 2016 21:08:02 -0700 Subject: [PATCH 05/25] Implement fix for specialized providers Fixes #24 --- config/geocoder.php | 14 ++++- src/GeocoderServiceProvider.php | 62 +++++++++++++++++-- .../Providers/GeocoderServiceProviderTest.php | 22 +++++++ 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/config/geocoder.php b/config/geocoder.php index ce26e47..1d385d4 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -1,9 +1,11 @@ [ - GoogleMaps::class => null, + GoogleMaps::class => [ + 'de-DE', + 'Wien, Österreich', + true, + env('GOOGLE_MAPS_API_KEY'), + ], BingMaps::class => [ 'en-US', env('BING_MAPS_API_KEY'), ], - FreeGeoIp::class => null, + FreeGeoIp::class => [], ], - 'adapter' => CurlHttpAdapter::class, + // 'adapter' => CurlHttpAdapter::class, + 'adapter' => Guzzle6HttpAdapter::class, ]; diff --git a/src/GeocoderServiceProvider.php b/src/GeocoderServiceProvider.php index b650780..862aa3c 100644 --- a/src/GeocoderServiceProvider.php +++ b/src/GeocoderServiceProvider.php @@ -53,14 +53,10 @@ public function register() $this->app->singleton('geocoder.chain', function ($app) { $providers = collect(config('geocoder.providers')) ->map(function ($arguments, $provider) { + $arguments = $this->prepArguments($arguments, $provider); $reflection = new ReflectionClass($provider); - if (is_array($arguments)) { - array_unshift($arguments, $this->app['geocoder.adapter']); - return $reflection->newInstanceArgs($arguments); - } - - return $reflection->newInstance($this->app['geocoder.adapter']); + return $reflection->newInstanceArgs($arguments); }); return new Chain($providers->toArray()); @@ -74,6 +70,60 @@ public function register() }); } + private function prepArguments(array $arguments, $provider) + { + $specificAdapter = $this->providerRequiresSpecificAdapter($provider); + + if ($specificAdapter) { + array_unshift($arguments, $specificAdapter); + + return $arguments; + } + + if ($this->providerRequiresAdapter($provider)) { + array_unshift($arguments, $this->app['geocoder.adapter']); + + return $arguments; + } + + return $arguments; + } + + private function providerRequiresSpecificAdapter($provider) + { + $specificAdapters = collect([ + 'Geocoder\Provider\GeoIP2' => 'Geocoder\Adapter\GeoIP2Adapter', + ]); + + return $specificAdapters->get($provider); + } + + private function providerRequiresAdapter($provider) + { + $providersRequiringAdapter = collect([ + 'Geocoder\Provider\ArcGISOnline', + 'Geocoder\Provider\BingMaps', + 'Geocoder\Provider\FreeGeoIp', + 'Geocoder\Provider\GeoIPs', + 'Geocoder\Provider\Geonames', + 'Geocoder\Provider\GeoPlugin', + 'Geocoder\Provider\GoogleMaps', + 'Geocoder\Provider\GoogleMapsBusiness', + 'Geocoder\Provider\HostIp', + 'Geocoder\Provider\IpInfoDb', + 'Geocoder\Provider\MapQuest', + 'Geocoder\Provider\MaxMind', + 'Geocoder\Provider\Nominatim', + 'Geocoder\Provider\OpenCage', + 'Geocoder\Provider\OpenStreetMap', + 'Geocoder\Provider\Provider', + 'Geocoder\Provider\TomTom', + 'Geocoder\Provider\Yandex', + ]); + + return $providersRequiringAdapter->contains($provider); + } + /** * Get the services provided by the provider. * diff --git a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php index fde0ad7..0ea5871 100644 --- a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php +++ b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php @@ -22,4 +22,26 @@ public function testItResolvesAGivenIPAddress() ->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()); + } } From 492630242b9f7320e3570ebce050402097021011 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Fri, 30 Sep 2016 22:09:16 -0700 Subject: [PATCH 06/25] Implement multiple providers Fixes #47 --- config/geocoder.php | 19 ++++++--- src/GeocoderServiceProvider.php | 41 ++++++++++++------- .../Providers/GeocoderServiceProviderTest.php | 14 +++++++ 3 files changed, 55 insertions(+), 19 deletions(-) diff --git a/config/geocoder.php b/config/geocoder.php index 1d385d4..e3ef56c 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -2,6 +2,7 @@ use Ivory\HttpAdapter\CurlHttpAdapter; use Ivory\HttpAdapter\Guzzle6HttpAdapter; +use Geocoder\Provider\Chain; use Geocoder\Provider\BingMaps; use Geocoder\Provider\FreeGeoIp; use Geocoder\Provider\GoogleMaps; @@ -20,17 +21,25 @@ // Providers get called in the chain order given here. // The first one to return a result will be used. 'providers' => [ + Chain::class => [ + GoogleMaps::class => [ + 'de-DE', + 'Wien, Österreich', + true, + env('GOOGLE_MAPS_API_KEY'), + ], + BingMaps::class => [ + 'en-US', + env('BING_MAPS_API_KEY'), + ], + FreeGeoIp::class => [], + ], GoogleMaps::class => [ 'de-DE', 'Wien, Österreich', true, env('GOOGLE_MAPS_API_KEY'), ], - BingMaps::class => [ - 'en-US', - env('BING_MAPS_API_KEY'), - ], - FreeGeoIp::class => [], ], // 'adapter' => CurlHttpAdapter::class, 'adapter' => Guzzle6HttpAdapter::class, diff --git a/src/GeocoderServiceProvider.php b/src/GeocoderServiceProvider.php index 862aa3c..556be47 100644 --- a/src/GeocoderServiceProvider.php +++ b/src/GeocoderServiceProvider.php @@ -13,6 +13,7 @@ use Geocoder\ProviderAggregator; use Geocoder\Provider\Chain; +use Illuminate\Support\Collection; use Illuminate\Support\ServiceProvider; use ReflectionClass; @@ -50,26 +51,32 @@ public function register() return new $adapter; }); - $this->app->singleton('geocoder.chain', function ($app) { - $providers = collect(config('geocoder.providers')) - ->map(function ($arguments, $provider) { - $arguments = $this->prepArguments($arguments, $provider); - $reflection = new ReflectionClass($provider); - - return $reflection->newInstanceArgs($arguments); - }); - - return new Chain($providers->toArray()); - }); - $this->app->singleton('geocoder', function ($app) { $geocoder = new ProviderAggregator(); - $geocoder->registerProvider($app['geocoder.chain']); + $geocoder->registerProviders( + $this->getProviders(collect(config('geocoder.providers'))) + ); return $geocoder; }); } + private function getProviders(Collection $providers) + { + $providers = $providers->map(function ($arguments, $provider) { + $arguments = $this->prepArguments($arguments, $provider); + $reflection = new ReflectionClass($provider); + + if ($provider === 'Geocoder\Provider\Chain') { + return $reflection->newInstance($arguments); + } + + return $reflection->newInstanceArgs($arguments); + }); + + return $providers->toArray(); + } + private function prepArguments(array $arguments, $provider) { $specificAdapter = $this->providerRequiresSpecificAdapter($provider); @@ -81,11 +88,17 @@ private function prepArguments(array $arguments, $provider) } if ($this->providerRequiresAdapter($provider)) { - array_unshift($arguments, $this->app['geocoder.adapter']); + array_unshift($arguments, app('geocoder.adapter')); return $arguments; } + if ($provider === 'Geocoder\Provider\Chain') { + return $this->getProviders( + collect(config('geocoder.providers.Geocoder\Provider\Chain')) + ); + } + return $arguments; } diff --git a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php index 0ea5871..5131e48 100644 --- a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php +++ b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php @@ -7,6 +7,7 @@ class GeocoderServiceProviderTest extends TestCase public function testItResolvesAGivenAddress() { $result = app('geocoder') + ->using('chain') ->geocode('1600 Pennsylvania Ave., Washington, DC USA') ->all(); $this->assertEquals('1600', $result[0]->getStreetNumber()); @@ -44,4 +45,17 @@ public function testItCanUseMaxMindBinaryWithoutProvider() $this->assertEquals('Washington', $result[0]->getLocality()); $this->assertEquals('20003', $result[0]->getPostalCode()); } + + public function testItCanUseASpecificProvider() + { + // dd(config('geocoder.providers')); + $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()); + } } From 1b259521600d93c352ee996d4a489340005041c5 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 1 Oct 2016 09:09:47 -0700 Subject: [PATCH 07/25] Refactor and clean up the service provider --- src/GeocoderServiceProvider.php | 100 +++++++++++++------------------- 1 file changed, 39 insertions(+), 61 deletions(-) diff --git a/src/GeocoderServiceProvider.php b/src/GeocoderServiceProvider.php index 556be47..66c6c62 100644 --- a/src/GeocoderServiceProvider.php +++ b/src/GeocoderServiceProvider.php @@ -1,4 +1,4 @@ - - */ class GeocoderServiceProvider extends ServiceProvider { /** @@ -32,9 +25,7 @@ class GeocoderServiceProvider extends ServiceProvider public function boot() { $source = realpath(__DIR__ . '/../config/geocoder.php'); - $this->publishes([$source => config_path('geocoder.php')], 'config'); - $this->mergeConfigFrom($source, 'geocoder'); } @@ -45,13 +36,7 @@ public function boot() */ public function register() { - $this->app->singleton('geocoder.adapter', function ($app) { - $adapter = config('geocoder.adapter'); - - return new $adapter; - }); - - $this->app->singleton('geocoder', function ($app) { + $this->app->singleton('geocoder', function () { $geocoder = new ProviderAggregator(); $geocoder->registerProviders( $this->getProviders(collect(config('geocoder.providers'))) @@ -61,10 +46,16 @@ public function register() }); } + /** + * 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->prepArguments($arguments, $provider); + $arguments = $this->getArguments($arguments, $provider); $reflection = new ReflectionClass($provider); if ($provider === 'Geocoder\Provider\Chain') { @@ -77,64 +68,51 @@ private function getProviders(Collection $providers) return $providers->toArray(); } - private function prepArguments(array $arguments, $provider) + /** + * 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) { - $specificAdapter = $this->providerRequiresSpecificAdapter($provider); - - if ($specificAdapter) { - array_unshift($arguments, $specificAdapter); - - return $arguments; - } - - if ($this->providerRequiresAdapter($provider)) { - array_unshift($arguments, app('geocoder.adapter')); - - return $arguments; - } - 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; } - private function providerRequiresSpecificAdapter($provider) + /** + * 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, ]); - return $specificAdapters->get($provider); - } - - private function providerRequiresAdapter($provider) - { - $providersRequiringAdapter = collect([ - 'Geocoder\Provider\ArcGISOnline', - 'Geocoder\Provider\BingMaps', - 'Geocoder\Provider\FreeGeoIp', - 'Geocoder\Provider\GeoIPs', - 'Geocoder\Provider\Geonames', - 'Geocoder\Provider\GeoPlugin', - 'Geocoder\Provider\GoogleMaps', - 'Geocoder\Provider\GoogleMapsBusiness', - 'Geocoder\Provider\HostIp', - 'Geocoder\Provider\IpInfoDb', - 'Geocoder\Provider\MapQuest', - 'Geocoder\Provider\MaxMind', - 'Geocoder\Provider\Nominatim', - 'Geocoder\Provider\OpenCage', - 'Geocoder\Provider\OpenStreetMap', - 'Geocoder\Provider\Provider', - 'Geocoder\Provider\TomTom', - 'Geocoder\Provider\Yandex', - ]); + if ($specificAdapters->has($provider)) { + return $specificAdapters->get($provider); + } - return $providersRequiringAdapter->contains($provider); + return config('geocoder.adapter'); } /** @@ -144,6 +122,6 @@ private function providerRequiresAdapter($provider) */ public function provides() { - return ['geocoder', 'geocoder.adapter', 'geocoder.chain']; + return ['geocoder']; } } From f1151483cdf4068c15eeba5dc4acdcb13687b7ed Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 1 Oct 2016 11:11:18 -0700 Subject: [PATCH 08/25] Create chainable provider with dump functionality Fixes #16 --- composer.json | 3 +- config/geocoder.php | 3 +- src/Exceptions/InvalidDumperException.php | 24 +++++ src/GeocoderServiceProvider.php | 9 +- src/ProviderAndDumperAggregator.php | 100 ++++++++++++++++++ .../Providers/GeocoderServiceProviderTest.php | 25 ++++- 6 files changed, 157 insertions(+), 7 deletions(-) create mode 100644 src/Exceptions/InvalidDumperException.php create mode 100644 src/ProviderAndDumperAggregator.php diff --git a/composer.json b/composer.json index d861f6f..d1ae9f1 100644 --- a/composer.json +++ b/composer.json @@ -34,8 +34,7 @@ }, "autoload": { "psr-4": { - "Toin0u\\Geocoder\\": "src/", - "Toin0u\\GeocoderLaravel\\Tests\\Laravel5_3\\": "tests/Laravel5_3/" + "Toin0u\\Geocoder\\": "src/" } }, "autoload-dev": { diff --git a/config/geocoder.php b/config/geocoder.php index e3ef56c..15c25e4 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -41,6 +41,5 @@ env('GOOGLE_MAPS_API_KEY'), ], ], - // 'adapter' => CurlHttpAdapter::class, - 'adapter' => Guzzle6HttpAdapter::class, + 'adapter' => CurlHttpAdapter::class, ]; diff --git a/src/Exceptions/InvalidDumperException.php b/src/Exceptions/InvalidDumperException.php new file mode 100644 index 0000000..62e355b --- /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/GeocoderServiceProvider.php b/src/GeocoderServiceProvider.php index 66c6c62..793be54 100644 --- a/src/GeocoderServiceProvider.php +++ b/src/GeocoderServiceProvider.php @@ -9,12 +9,17 @@ * file that was distributed with this source code. */ -use Geocoder\ProviderAggregator; use Geocoder\Provider\Chain; use Illuminate\Support\Collection; use Illuminate\Support\ServiceProvider; use ReflectionClass; +/** + * Geocoder service provider + * + * @author Antoine Corcy + * @author Mike Bronner + */ class GeocoderServiceProvider extends ServiceProvider { /** @@ -37,7 +42,7 @@ public function boot() public function register() { $this->app->singleton('geocoder', function () { - $geocoder = new ProviderAggregator(); + $geocoder = new ProviderAndDumperAggregator(); $geocoder->registerProviders( $this->getProviders(collect(config('geocoder.providers'))) ); diff --git a/src/ProviderAndDumperAggregator.php b/src/ProviderAndDumperAggregator.php new file mode 100644 index 0000000..b995c34 --- /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/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php index 5131e48..5f721bf 100644 --- a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php +++ b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php @@ -1,6 +1,7 @@ using('google_maps') ->geocode('1600 Pennsylvania Ave., Washington, DC USA') @@ -58,4 +58,27 @@ public function testItCanUseASpecificProvider() $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); + } } From 379682d127e0741ce188d9655aaefa5fe980b627 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 1 Oct 2016 11:27:33 -0700 Subject: [PATCH 09/25] Update tests --- tests/Geocoder/Facade/GeocoderTest.php | 25 ------- .../Geocoder/GeocoderServiceProviderTest.php | 72 ------------------- tests/Geocoder/TestCase.php | 38 ---------- .../Providers/GeocoderServiceProviderTest.php | 28 ++++++++ 4 files changed, 28 insertions(+), 135 deletions(-) delete mode 100644 tests/Geocoder/Facade/GeocoderTest.php delete mode 100644 tests/Geocoder/GeocoderServiceProviderTest.php delete mode 100644 tests/Geocoder/TestCase.php diff --git a/tests/Geocoder/Facade/GeocoderTest.php b/tests/Geocoder/Facade/GeocoderTest.php deleted file mode 100644 index 079e650..0000000 --- a/tests/Geocoder/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/Geocoder/GeocoderServiceProviderTest.php b/tests/Geocoder/GeocoderServiceProviderTest.php deleted file mode 100644 index 57c9d45..0000000 --- a/tests/Geocoder/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/Geocoder/TestCase.php b/tests/Geocoder/TestCase.php deleted file mode 100644 index 31efb9b..0000000 --- a/tests/Geocoder/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', - ]; - } -} diff --git a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php index 5f721bf..2f19d35 100644 --- a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php +++ b/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php @@ -2,6 +2,12 @@ use Toin0u\GeocoderLaravel\Tests\Laravel5_3\TestCase; use Toin0u\Geocoder\Exceptions\InvalidDumperException; +use Toin0u\Geocoder\ProviderAndDumperAggregator; +use Toin0u\Geocoder\GeocoderServiceProvider; +use Geocoder\Provider\Chain; +use Geocoder\Provider\FreeGeoIp; +use Geocoder\Provider\GoogleMaps; +use Ivory\HttpAdapter\CurlHttpAdapter; class GeocoderServiceProviderTest extends TestCase { @@ -81,4 +87,26 @@ public function testItThrowsAnExceptionForInvalidDumper() $this->assertEquals('1600', $jsonAddress->properties->streetNumber); } + + public function testConfig() + { + $this->assertTrue(is_array($providers = $this->app['config']->get('geocoder.providers'))); + $this->assertCount(2, $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(GeocoderServiceProvider::class, $loadedProviders); + $this->assertTrue($loadedProviders[GeocoderServiceProvider::class]); + } + + public function testGeocoder() + { + $this->assertInstanceOf(ProviderAndDumperAggregator::class, app('geocoder')); + } } From 31d18191e7e512b5ed17dee20023dc870ef2d475 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 1 Oct 2016 15:17:36 -0700 Subject: [PATCH 10/25] Update documentation, changelog, change namespace --- CHANGELOG.md | 102 +++++---- README.md | 211 ++++++++---------- composer.json | 6 +- config/geocoder.php | 34 ++- src/Exceptions/InvalidDumperException.php | 2 +- src/{Facade => Facades}/Geocoder.php | 6 +- src/ProviderAndDumperAggregator.php | 4 +- .../GeocoderService.php} | 15 +- ...oviderTest.php => GeocoderServiceTest.php} | 18 +- tests/Laravel5_3/TestCase.php | 2 +- 10 files changed, 202 insertions(+), 198 deletions(-) rename src/{Facade => Facades}/Geocoder.php (81%) rename src/{GeocoderServiceProvider.php => Providers/GeocoderService.php} (86%) rename tests/Laravel5_3/Providers/{GeocoderServiceProviderTest.php => GeocoderServiceTest.php} (87%) 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 d33a8a2..968e4dd 100644 --- a/README.md +++ b/README.md @@ -1,145 +1,126 @@ -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) +use Toin0u\Geocoder\GeocoderServiceProvider; [![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). - -Edit `composer.json` and add: +> 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). -```json -{ - "require": { - "toin0u/geocoder-laravel": "@stable" - } -} -``` +** Version 0.7.0 is a backwards-compatibility-breaking update. Please review + this documentation, especially the _Usage_ section before installing. ** -**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. +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' => [ + Toin0u\GeocoderLaravel\Providers\GeocoderService::class, + // ]; + ``` -And install dependencies: -```bash -$ composer update +## 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="Toin0u\Geocoder\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 - -```sh -php artisan vendor:publish --provider="Toin0u\Geocoder\GeocoderServiceProvider" --tags="config" -``` - -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('Los Angeles, CA')->all(); ``` -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')->reverse('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 d1ae9f1..50ba676 100644 --- a/composer.json +++ b/composer.json @@ -34,13 +34,13 @@ }, "autoload": { "psr-4": { - "Toin0u\\Geocoder\\": "src/" + "Geocoder\\Laravel\\": "src/", + "Geocoder\\Laravel\\Tests\\": "tests/" } }, "autoload-dev": { "psr-4": { - "Toin0u\\Tests\\Geocoder\\": "tests/Geocoder/", - "Toin0u\\GeocoderLaravel\\Tests\\Laravel5_3\\": "tests/Laravel5_3/" + "Geocoder\\Laravel\\Tests\\": "tests/" } }, "extra": { diff --git a/config/geocoder.php b/config/geocoder.php index 15c25e4..2992e9c 100644 --- a/config/geocoder.php +++ b/config/geocoder.php @@ -1,13 +1,5 @@ [ Chain::class => [ GoogleMaps::class => [ - 'de-DE', - 'Wien, Österreich', + 'en', + 'us', true, env('GOOGLE_MAPS_API_KEY'), ], - BingMaps::class => [ - 'en-US', - env('BING_MAPS_API_KEY'), - ], FreeGeoIp::class => [], ], + BingMaps::class => [ + 'en-US', + env('BING_MAPS_API_KEY'), + ], GoogleMaps::class => [ - 'de-DE', - 'Wien, Österreich', + 'en', + 'us', true, env('GOOGLE_MAPS_API_KEY'), ], diff --git a/src/Exceptions/InvalidDumperException.php b/src/Exceptions/InvalidDumperException.php index 62e355b..fa6939c 100644 --- a/src/Exceptions/InvalidDumperException.php +++ b/src/Exceptions/InvalidDumperException.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/ProviderAndDumperAggregator.php b/src/ProviderAndDumperAggregator.php index b995c34..da5019f 100644 --- a/src/ProviderAndDumperAggregator.php +++ b/src/ProviderAndDumperAggregator.php @@ -1,4 +1,4 @@ - diff --git a/src/GeocoderServiceProvider.php b/src/Providers/GeocoderService.php similarity index 86% rename from src/GeocoderServiceProvider.php rename to src/Providers/GeocoderService.php index 793be54..9da2384 100644 --- a/src/GeocoderServiceProvider.php +++ b/src/Providers/GeocoderService.php @@ -1,4 +1,4 @@ - * @author Mike Bronner */ -class GeocoderServiceProvider extends ServiceProvider +class GeocoderService extends ServiceProvider { /** * Bootstrap the application events. @@ -29,9 +32,9 @@ class GeocoderServiceProvider extends ServiceProvider */ public function boot() { - $source = realpath(__DIR__ . '/../config/geocoder.php'); - $this->publishes([$source => config_path('geocoder.php')], 'config'); - $this->mergeConfigFrom($source, 'geocoder'); + $configPath = __DIR__ . '/../../config/geocoder.php'; + $this->publishes([$configPath => config_path('geocoder.php')], 'config'); + $this->mergeConfigFrom($configPath, 'geocoder'); } /** @@ -41,6 +44,8 @@ public function boot() */ public function register() { + AliasLoader::getInstance()->alias('Geocoder', Geocoder::class); + $this->app->singleton('geocoder', function () { $geocoder = new ProviderAndDumperAggregator(); $geocoder->registerProviders( diff --git a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php b/tests/Laravel5_3/Providers/GeocoderServiceTest.php similarity index 87% rename from tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php rename to tests/Laravel5_3/Providers/GeocoderServiceTest.php index 2f19d35..10f6996 100644 --- a/tests/Laravel5_3/Providers/GeocoderServiceProviderTest.php +++ b/tests/Laravel5_3/Providers/GeocoderServiceTest.php @@ -1,15 +1,15 @@ -assertTrue(is_array($providers = $this->app['config']->get('geocoder.providers'))); - $this->assertCount(2, $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')); @@ -101,8 +101,8 @@ public function testLoadedProviders() { $loadedProviders = $this->app->getLoadedProviders(); - $this->assertArrayHasKey(GeocoderServiceProvider::class, $loadedProviders); - $this->assertTrue($loadedProviders[GeocoderServiceProvider::class]); + $this->assertArrayHasKey(GeocoderService::class, $loadedProviders); + $this->assertTrue($loadedProviders[GeocoderService::class]); } public function testGeocoder() diff --git a/tests/Laravel5_3/TestCase.php b/tests/Laravel5_3/TestCase.php index 37d1327..b48f091 100644 --- a/tests/Laravel5_3/TestCase.php +++ b/tests/Laravel5_3/TestCase.php @@ -1,4 +1,4 @@ - Date: Sat, 1 Oct 2016 15:26:43 -0700 Subject: [PATCH 11/25] Fix namespaces in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 968e4dd..ce61642 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ This package allows you to use [**Geocoder**](http://geocoder-php.org/Geocoder/) Service Provider**: ```php // 'providers' => [ - Toin0u\GeocoderLaravel\Providers\GeocoderService::class, + Geocoder\Laravel\Providers\GeocoderService::class, // ]; ``` @@ -80,7 +80,7 @@ return [ If you would like to make changes to the default configuration, publish and edit the configuration file: ```sh -php artisan vendor:publish --provider="Toin0u\Geocoder\GeocoderServiceProvider" --tags="config" +php artisan vendor:publish --provider="Geocoder\Laravel\GeocoderServiceProvider" --tags="config" ``` ## Usage From 964d30c7d4c0998b209920f3e9c0466035c00ba5 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 1 Oct 2016 15:54:55 -0700 Subject: [PATCH 12/25] Fix examples in readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ce61642..07d3dbc 100644 --- a/README.md +++ b/README.md @@ -100,12 +100,12 @@ app('geocoder')->geocode('Los Angeles, CA')->all(); #### Reverse-Geocoding ```php -app('geocoder')->reverse('Los Angeles, CA')->all(); +app('geocoder')->reverse(43.882587,-103.454067)->get(); ``` #### Dumping Results ```php -app('geocoder')->reverse('Los Angeles, CA')->dump('kml'); +app('geocoder')->geocode('Los Angeles, CA')->dump('kml'); ``` ## Changelog From 4a4e152d6cb65a948d7439bbfa0f2c72bb77e84f Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 1 Oct 2016 15:56:14 -0700 Subject: [PATCH 13/25] Remove erroneous use statement from readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 07d3dbc..0067b46 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ [![Latest StableVersion](https://poser.pugx.org/toin0u/geocoder-laravel/v/stable.png)](https://packagist.org/packages/toin0u/geocoder-laravel) -use Toin0u\Geocoder\GeocoderServiceProvider; [![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) From 8f07737f5c84d61b36c970b86b8640bfc2a3f26c Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 1 Oct 2016 16:59:27 -0700 Subject: [PATCH 14/25] Implement stand-along package tests --- composer.json | 12 +++++++++--- phpunit.xml | 9 +++++++++ tests/Laravel5_3/TestCase.php | 4 +++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 50ba676..dc117c8 100644 --- a/composer.json +++ b/composer.json @@ -30,16 +30,22 @@ "require-dev": { "orchestra/testbench": "~3.0", "satooshi/php-coveralls": "~0.6", - "phpunit/phpunit": "~4.5" + "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": { - "Geocoder\\Laravel\\": "src/", - "Geocoder\\Laravel\\Tests\\": "tests/" + "Geocoder\\Laravel\\": "src/" } }, "autoload-dev": { "psr-4": { + "App\\": "vendor/laravel/laravel/app/", "Geocoder\\Laravel\\Tests\\": "tests/" } }, diff --git a/phpunit.xml b/phpunit.xml index daa6b46..2c2aa40 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -21,4 +21,13 @@ ./src/ + + + + + + + + + diff --git a/tests/Laravel5_3/TestCase.php b/tests/Laravel5_3/TestCase.php index b48f091..673af25 100644 --- a/tests/Laravel5_3/TestCase.php +++ b/tests/Laravel5_3/TestCase.php @@ -1,5 +1,6 @@ make(Kernel::class)->bootstrap(); + $app->register(GeocoderService::class); return $app; } From c4b423c84159ccc17fcec966c57b9703da7cc7f8 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 1 Oct 2016 17:23:35 -0700 Subject: [PATCH 15/25] Update travis config --- .travis.yml | 5 +---- tests/Laravel5_3/TestCase.php | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) 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/tests/Laravel5_3/TestCase.php b/tests/Laravel5_3/TestCase.php index 673af25..de4abfa 100644 --- a/tests/Laravel5_3/TestCase.php +++ b/tests/Laravel5_3/TestCase.php @@ -15,7 +15,7 @@ abstract class TestCase extends BaseTestCase protected $baseUrl = 'http://localhost'; - public function createApplication() : Application + public function createApplication() { $app = require __DIR__ . '/../../vendor/laravel/laravel/bootstrap/app.php'; $app->make(Kernel::class)->bootstrap(); From cfd14a76c02b0bfe8433f0b2463a67fdc5c3489a Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 3 Oct 2016 08:35:25 -0700 Subject: [PATCH 16/25] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index dc117c8..9fbf5d7 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ }, "extra": { "branch-alias": { - "dev-master": "0.7-dev" + "dev-master": "1.0.0-dev" } } } From 49e32f9674bda85b7e27af201639f784548d2d45 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 3 Oct 2016 18:31:36 -0700 Subject: [PATCH 17/25] WIP - configuring PHPCI --- .phpci.yml | 0 .travis.yml | 13 ------------- CHANGELOG.md | 5 ++++- README.md | 4 +--- composer.json | 9 ++++++--- 5 files changed, 11 insertions(+), 20 deletions(-) create mode 100644 .phpci.yml delete mode 100644 .travis.yml diff --git a/.phpci.yml b/.phpci.yml new file mode 100644 index 0000000..e69de29 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index a8852d4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,13 +0,0 @@ -language: php - -php: - - 5.6 - - 7.0 - -before_script: - - travis_retry composer self-update - - travis_retry composer install --no-interaction --prefer-source --dev - -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 0e8a518..dcde2cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ 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 +## [1.0.0] - 3 Oct 2016 ### Added - ability to dump results #16. - ability to use multiple providers in addition to the chain provider #47. @@ -23,6 +23,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). - MaxMindBinary Provider being instantiated with an Adapter #24. - GeoIP2 Provider being instantiated with a generic Adapter. +### Removed +- TravisCI and Coveralls CI in favor of PHPCI. + ## [0.6.0] - TBD diff --git a/README.md b/README.md index 0067b46..61dbbea 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ [![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 > 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). -** Version 0.7.0 is a backwards-compatibility-breaking update. Please review +** Version 1.0.0 is a backwards-compatibility-breaking update. Please review this documentation, especially the _Usage_ section before installing. ** This package allows you to use [**Geocoder**](http://geocoder-php.org/Geocoder/) diff --git a/composer.json b/composer.json index 9fbf5d7..5d017c9 100644 --- a/composer.json +++ b/composer.json @@ -28,15 +28,18 @@ "willdurand/geocoder": "~3.3" }, "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" + "laravel/laravel": "^5.3", + "phploc/phploc": "^3.0", + "squizlabs/php_codesniffer": "^2.7", + "phpmd/phpmd": "^2.4", + "pdepend/pdepend": "^2.2", + "sebastian/phpcpd": "^2.0" }, "autoload": { "psr-4": { From 00bedffa1ee83fc12e5a4233abfa9ae208fa8b75 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 3 Oct 2016 18:54:46 -0700 Subject: [PATCH 18/25] Fix phpci config filename --- .phpci.yml => phpci.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .phpci.yml => phpci.yml (100%) diff --git a/.phpci.yml b/phpci.yml similarity index 100% rename from .phpci.yml rename to phpci.yml From e01edb862148f85389ae3798af7097c2e4696cfa Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 3 Oct 2016 19:17:15 -0700 Subject: [PATCH 19/25] WIP - getting PHPCI to respect configuration --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5d017c9..fefb9f2 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,8 @@ "squizlabs/php_codesniffer": "^2.7", "phpmd/phpmd": "^2.4", "pdepend/pdepend": "^2.2", - "sebastian/phpcpd": "^2.0" + "sebastian/phpcpd": "^2.0", + "jakub-onderka/php-parallel-lint": "^0.9.2" }, "autoload": { "psr-4": { From c63423eacb6896b8999f243190d5966af9607d6d Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Mon, 3 Oct 2016 19:28:18 -0700 Subject: [PATCH 20/25] WIP - remove phpci config file, update readme with build status icon and link --- README.md | 1 + phpci.yml | 0 2 files changed, 1 insertion(+) delete mode 100644 phpci.yml diff --git a/README.md b/README.md index 61dbbea..1548a5f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ [![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://ci.genealabs.com/build-status/image/1)](https://ci.genealabs.com/build-status/view/1) # Geocoder for Lavarel diff --git a/phpci.yml b/phpci.yml deleted file mode 100644 index e69de29..0000000 From 6c4ac313810697327af566ddce4f282bdc0e17f5 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 4 Oct 2016 18:31:10 -0700 Subject: [PATCH 21/25] Improve quality of some of the integration tests --- src/Providers/GeocoderService.php | 2 + .../Providers/GeocoderServiceTest.php | 62 +++++++++++++++++-- tests/Laravel5_3/TestCase.php | 1 - 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/Providers/GeocoderService.php b/src/Providers/GeocoderService.php index 9da2384..d63d23e 100644 --- a/src/Providers/GeocoderService.php +++ b/src/Providers/GeocoderService.php @@ -25,6 +25,8 @@ */ class GeocoderService extends ServiceProvider { + protected $defer = false; + /** * Bootstrap the application events. * diff --git a/tests/Laravel5_3/Providers/GeocoderServiceTest.php b/tests/Laravel5_3/Providers/GeocoderServiceTest.php index 10f6996..9f4158a 100644 --- a/tests/Laravel5_3/Providers/GeocoderServiceTest.php +++ b/tests/Laravel5_3/Providers/GeocoderServiceTest.php @@ -7,16 +7,30 @@ use Geocoder\Provider\Chain; use Geocoder\Provider\FreeGeoIp; use Geocoder\Provider\GoogleMaps; +use Geocoder\Provider\MaxMindBinary; +use Geocoder\Exception\FunctionNotFound; use Ivory\HttpAdapter\CurlHttpAdapter; class GeocoderServiceTest extends TestCase { + public function setUp() + { + parent::setUp(); + + app()->register(GeocoderService::class); + } + public function testItResolvesAGivenAddress() { + // Arrange + + // Act $result = app('geocoder') ->using('chain') ->geocode('1600 Pennsylvania Ave., Washington, DC USA') ->all(); + + // Assert $this->assertEquals('1600', $result[0]->getStreetNumber()); $this->assertEquals('Pennsylvania Avenue Southeast', $result[0]->getStreetName()); $this->assertEquals('Washington', $result[0]->getLocality()); @@ -25,32 +39,68 @@ public function testItResolvesAGivenAddress() public function testItResolvesAGivenIPAddress() { + // Arrange + + // Act $result = app('geocoder') ->geocode('8.8.8.8') ->all(); + + // Assert $this->assertEquals('US', $result[0]->getCountry()->getCode()); } public function testItResolvesAGivenAddressWithUmlauts() { + // Arrange + + // Act $result = app('geocoder') ->geocode('Obere Donaustrasse 22, Wien, Österreich') ->all(); + + // Assert $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() + public function testItResolvesAGivenAddressWithUmlautsInRegion() { + // Arrange + config()->set('geocoder.providers.Geocoder\Provider\Chain.Geocoder\Provider\GoogleMaps', [ + 'de-DE', + 'Wien, Österreich', + true, + null, + ]); + app()->register(GeocoderService::class); + + // Act $result = app('geocoder') - ->geocode('1600 Pennsylvania Ave., Washington, DC USA') + ->geocode('Obere Donaustrasse 22, Wien, Österreich') ->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()); + + // Assert + $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() + { + //Arrange + $this->expectException(FunctionNotFound::class); + $provider = new MaxMindBinary('dummy'); + + // Act + app('geocoder')->registerProvider($provider); + + // Assert + // By getting past the constructor parameters requirements, we know we + // are instantiating the provider correctly. } public function testItCanUseASpecificProvider() diff --git a/tests/Laravel5_3/TestCase.php b/tests/Laravel5_3/TestCase.php index de4abfa..da35bc3 100644 --- a/tests/Laravel5_3/TestCase.php +++ b/tests/Laravel5_3/TestCase.php @@ -19,7 +19,6 @@ public function createApplication() { $app = require __DIR__ . '/../../vendor/laravel/laravel/bootstrap/app.php'; $app->make(Kernel::class)->bootstrap(); - $app->register(GeocoderService::class); return $app; } From 1f35c25a4e609e238c61c9cc6414e9d2f1929992 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 4 Oct 2016 20:52:48 -0700 Subject: [PATCH 22/25] Update phpunit config and add link to code coverage --- README.md | 1 + phpunit.xml | 7 +++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1548a5f..e5a530e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ [![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://ci.genealabs.com/build-status/image/1)](https://ci.genealabs.com/build-status/view/1) +[Code Coverage](https://ci.genealabs.com/coverage/1) # Geocoder for Lavarel diff --git a/phpunit.xml b/phpunit.xml index 2c2aa40..fac3ed4 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -8,17 +8,16 @@ processIsolation="false" stopOnFailure="false" syntaxCheck="false" - bootstrap="vendor/autoload.php" + bootstrap="vendor/laravel/laravel/bootstrap/autoload.php" > - ./tests/Geocoder ./tests/Laravel5_3 - - ./src/ + + ./src From b46213ec78987af70815874938d2265c46f0210a Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Tue, 4 Oct 2016 20:57:28 -0700 Subject: [PATCH 23/25] Fix autoload declaration --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index fac3ed4..15b2aec 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -8,7 +8,7 @@ processIsolation="false" stopOnFailure="false" syntaxCheck="false" - bootstrap="vendor/laravel/laravel/bootstrap/autoload.php" + bootstrap="vendor/autoload.php" > From 9ce156fd83151ea1194a36de1ef64d06ffcf9941 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 13 Oct 2016 20:02:16 -0700 Subject: [PATCH 24/25] Add travis config file back in --- .travis.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..3dff9db --- /dev/null +++ b/.travis.yml @@ -0,0 +1,16 @@ +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 + +after_script: php vendor/bin/coveralls From 090bc89a3771b8c456115cf476968b6cfd96cea4 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Thu, 13 Oct 2016 20:02:33 -0700 Subject: [PATCH 25/25] Place namespace declaration below file comment --- src/Exceptions/InvalidDumperException.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Exceptions/InvalidDumperException.php b/src/Exceptions/InvalidDumperException.php index fa6939c..d3b58a3 100644 --- a/src/Exceptions/InvalidDumperException.php +++ b/src/Exceptions/InvalidDumperException.php @@ -1,14 +1,16 @@ - + * (c) Mike Bronner * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ +namespace Geocoder\Laravel\Exceptions; + use Geocoder\Exception\Exception; use Exception as BaseException;