Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use service container to instantiate adapter #191

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

JaZo
Copy link
Contributor

@JaZo JaZo commented Mar 15, 2022

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!

This allows you to specify how the adapter should be instantiated. You could, for example, use a different adapter in tests, to mock requests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant