geocoder-php / GeocoderLaravel

Geocoder service provider for Laravel
http://geocoder-php.org/GeocoderLaravel/
MIT License
704 stars 102 forks source link

Use service container to instantiate adapter #191

Open JaZo opened 2 years ago

JaZo commented 2 years ago

This allows you to specify how the adapter should be instantiated. You could, for example, set Guzzle options or use a different adapter in tests, to mock requests.

$this->app->bind(\Http\Client\HttpClient::class, function ($app) {
    if ($app->environment('testing')) {
        return new \Swis\Http\Fixture\Client(
            new \Swis\Http\Fixture\ResponseBuilder('/path/to/fixtures')
        );
    } else {
        return \Http\Adapter\Guzzle7\Client::createWithConfig(
            [
                'timeout' => 2,
            ]
        );
    }
});

N.B. This example uses our swisnl/php-http-fixture-client when in testing environment. This package allows you to easily mock requests with static fixtures. Definitely worth a try!