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

[HttpClient][DI] Add an option to use the MockClient in functional tests #35893

Merged
merged 1 commit into from Aug 27, 2020

Conversation

GaryPEGEOT
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
Deprecations? no
Tickets
License MIT
Doc PR TODO

Let's say you want to mock HTTP responses in your test or dev environment:

First, you create an invokable class responsible of generating the response:

namespace App\Tests;

use Symfony\Contracts\HttpClient\ResponseInterface;

class MyAwesomeCallback
{
    public function __invoke(string $method, string $url, array $options = []): ResponseInterface
    {
        // load a fixture file or generate dynamic data
    }
}

Then configure it:

# config/services_test.yaml
services:
   # ...
   App\Tests\MyAwesomeCallback: ~

# config/packages/test/framework.yaml
framework:
    http_client:
        mock_response_factory: App\Tests\MyAwesomeCallback

The HttpClient will now be using MockHttpClient in your functional test:

$client = static::createClient();

// No live HTTP connection made
$client->request('GET', '/path-with-http-call');

@nicolas-grekas
Copy link
Member

Very related to #35651 and #35677

@fabpot
Copy link
Member

fabpot commented Aug 11, 2020

@nicolas-grekas Can you have a look at this one as it would unblock some other PRs as well?

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I just have minor notes.

@fabpot fabpot force-pushed the feature/response-factory-di branch from fa4906f to b53739c Compare August 27, 2020 14:44
@fabpot
Copy link
Member

fabpot commented Aug 27, 2020

Thank you @GaryPEGEOT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants