@@ -1373,6 +1373,82 @@ However, using ``MockResponse`` allows simulating chunked responses and timeouts
1373
1373
1374
1374
$mockResponse = new MockResponse($body());
1375
1375
1376
+ Using the Symfony Framework, if you want to use your callback in functional tests, you can do as follow:
1377
+
1378
+ First, create an invokable or iterable class responsible of generating the response::
1379
+
1380
+ namespace App\Tests;
1381
+
1382
+ use Symfony\Contracts\HttpClient\ResponseInterface;
1383
+ use Symfony\Component\HttpClient\Response\MockResponse;
1384
+
1385
+ class MockClientCallback
1386
+ {
1387
+ public function __invoke(string $method, string $url, array $options = []): ResponseInterface
1388
+ {
1389
+ // load a fixture file or generate data
1390
+ // ...
1391
+ return new MockResponse($data);
1392
+ }
1393
+ }
1394
+
1395
+ Then configure the framework to use your callback:
1396
+
1397
+ .. configuration-block ::
1398
+
1399
+ .. code-block :: yaml
1400
+
1401
+ # config/services_test.yaml
1402
+ services :
1403
+ # ...
1404
+ App\Tests\MockClientCallback : ~
1405
+
1406
+ # config/packages/test/framework.yaml
1407
+ framework :
1408
+ http_client :
1409
+ mock_response_factory : App\Tests\MockClientCallback
1410
+
1411
+ .. code-block :: xml
1412
+
1413
+ <!-- config/services_test.xml -->
1414
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1415
+ <container xmlns =" http://symfony.com/schema/dic/services"
1416
+ xmlns : xsd =" http://www.w3.org/2001/XMLSchema-instance"
1417
+ xsd : schemaLocation =" http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd" >
1418
+
1419
+ <services >
1420
+ <service id =" App\Tests\MockClientCallback" />
1421
+ </services >
1422
+ </container >
1423
+
1424
+ <!-- config/packages/framework.xml -->
1425
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
1426
+ <container xmlns =" http://symfony.com/schema/dic/services"
1427
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
1428
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
1429
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
1430
+ https://symfony.com/schema/dic/services/services-1.0.xsd
1431
+ http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
1432
+
1433
+ <framework : config >
1434
+ <framework : http-client mock-response-factory =" App\Tests\MockClientCallback" >
1435
+ <!-- ... -->
1436
+ </framework-http-client >
1437
+ </framework : config >
1438
+ </container >
1439
+
1440
+ .. code-block :: php
1441
+
1442
+ // config/packages/framework.php
1443
+ $container->loadFromExtension('framework', [
1444
+ 'http_client' => [
1445
+ 'mock_response_factory' => MockClientCallback::class,
1446
+ ],
1447
+ ]);
1448
+
1449
+
1450
+ The ``MockHttpClient `` will now be used in test environment with your callback to generate responses.
1451
+
1376
1452
.. _`cURL PHP extension` : https://www.php.net/curl
1377
1453
.. _`PSR-17` : https://www.php-fig.org/psr/psr-17/
1378
1454
.. _`PSR-18` : https://www.php-fig.org/psr/psr-18/
0 commit comments