Skip to content

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
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.

4 participants