@@ -1462,6 +1462,82 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
14621462
14631463 $mockResponse = new MockResponse($body());
14641464
1465+ Using the Symfony Framework, if you want to use your callback in functional tests, you can do as follow:
1466+
1467+ First, create an invokable or iterable class responsible of generating the response::
1468+
1469+ namespace App\Tests;
1470+
1471+ use Symfony\Contracts\HttpClient\ResponseInterface;
1472+ use Symfony\Component\HttpClient\Response\MockResponse;
1473+
1474+ class MockClientCallback
1475+ {
1476+ public function __invoke(string $method, string $url, array $options = []): ResponseInterface
1477+ {
1478+ // load a fixture file or generate data
1479+ // ...
1480+ return new MockResponse($data);
1481+ }
1482+ }
1483+
1484+ Then configure the framework to use your callback:
1485+
1486+ .. configuration-block ::
1487+
1488+ .. code-block :: yaml
1489+
1490+ # config/services_test.yaml
1491+ services :
1492+ # ...
1493+ App\Tests\MockClientCallback : ~
1494+
1495+ # config/packages/test/framework.yaml
1496+ framework :
1497+ http_client :
1498+ mock_response_factory : App\Tests\MockClientCallback
1499+
1500+ .. code-block :: xml
1501+
1502+ <!-- config/services_test.xml -->
1503+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1504+ <container xmlns =" http://symfony.com/schema/dic/services"
1505+ xmlns : xsd =" http://www.w3.org/2001/XMLSchema-instance"
1506+ xsd : schemaLocation =" http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd" >
1507+
1508+ <services >
1509+ <service id =" App\Tests\MockClientCallback" />
1510+ </services >
1511+ </container >
1512+
1513+ <!-- config/packages/framework.xml -->
1514+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1515+ <container xmlns =" http://symfony.com/schema/dic/services"
1516+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1517+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
1518+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
1519+ https://symfony.com/schema/dic/services/services-1.0.xsd
1520+ http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
1521+
1522+ <framework : config >
1523+ <framework : http-client mock-response-factory =" App\Tests\MockClientCallback" >
1524+ <!-- ... -->
1525+ </framework-http-client >
1526+ </framework : config >
1527+ </container >
1528+
1529+ .. code-block :: php
1530+
1531+ // config/packages/framework.php
1532+ $container->loadFromExtension('framework', [
1533+ 'http_client' => [
1534+ 'mock_response_factory' => MockClientCallback::class,
1535+ ],
1536+ ]);
1537+
1538+
1539+ The ``MockHttpClient `` will now be used in test environment with your callback to generate responses.
1540+
14651541.. _`cURL PHP extension` : https://www.php.net/curl
14661542.. _`PSR-17` : https://www.php-fig.org/psr/psr-17/
14671543.. _`PSR-18` : https://www.php-fig.org/psr/psr-18/
0 commit comments