diff --git a/http_client.rst b/http_client.rst index 33e6c9732dd..5eb3b09f711 100644 --- a/http_client.rst +++ b/http_client.rst @@ -1373,6 +1373,82 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts $mockResponse = new MockResponse($body()); +Using the Symfony Framework, if you want to use your callback in functional tests, you can do as follow: + +First, create an invokable or iterable class responsible of generating the response:: + + namespace App\Tests; + + use Symfony\Contracts\HttpClient\ResponseInterface; + use Symfony\Component\HttpClient\Response\MockResponse; + + class MockClientCallback + { + public function __invoke(string $method, string $url, array $options = []): ResponseInterface + { + // load a fixture file or generate data + // ... + return new MockResponse($data); + } + } + +Then configure the framework to use your callback: + +.. configuration-block:: + + .. code-block:: yaml + + # config/services_test.yaml + services: + # ... + App\Tests\MockClientCallback: ~ + + # config/packages/test/framework.yaml + framework: + http_client: + mock_response_factory: App\Tests\MockClientCallback + + .. code-block:: xml + + + + + + + + + + + + + + + + + + + + + + .. code-block:: php + + // config/packages/framework.php + $container->loadFromExtension('framework', [ + 'http_client' => [ + 'mock_response_factory' => MockClientCallback::class, + ], + ]); + + +The ``MockHttpClient`` will now be used in test environment with your callback to generate responses. + .. _`cURL PHP extension`: https://www.php.net/curl .. _`PSR-17`: https://www.php-fig.org/psr/psr-17/ .. _`PSR-18`: https://www.php-fig.org/psr/psr-18/