Skip to content

Commit 938724b

Browse files
committed
feature #35893 [HttpClient][DI] Add an option to use the MockClient in functional tests (GaryPEGEOT)
This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- [HttpClient][DI] Add an option to use the MockClient in functional tests | 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: ```php 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: ```yaml # 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: ```php $client = static::createClient(); // No live HTTP connection made $client->request('GET', '/path-with-http-call'); ``` Commits ------- b53739c79d [HttpClient][DI] Add an option to use the MockClient in functional tests
2 parents 28d1377 + fe7cada commit 938724b

File tree

7 files changed

+52
-0
lines changed

7 files changed

+52
-0
lines changed

DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,9 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode)
14501450
->end()
14511451
->end()
14521452
->end()
1453+
->scalarNode('mock_response_factory')
1454+
->info('The id of the service that should generate mock responses. It should be either an invokable or an iterable.')
1455+
->end()
14531456
->arrayNode('scoped_clients')
14541457
->useAttributeAsKey('name')
14551458
->normalizeKeys(false)

DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
use Symfony\Component\Form\FormTypeExtensionInterface;
6464
use Symfony\Component\Form\FormTypeGuesserInterface;
6565
use Symfony\Component\Form\FormTypeInterface;
66+
use Symfony\Component\HttpClient\MockHttpClient;
6667
use Symfony\Component\HttpClient\ScopingHttpClient;
6768
use Symfony\Component\HttpFoundation\Request;
6869
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
@@ -2010,6 +2011,12 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
20102011
$container->registerAliasForArgument('psr18.'.$name, ClientInterface::class, $name);
20112012
}
20122013
}
2014+
2015+
if ($responseFactoryId = $config['mock_response_factory'] ?? null) {
2016+
$container->getDefinition($httpClientId)
2017+
->setClass(MockHttpClient::class)
2018+
->setArguments([new Reference($responseFactoryId)]);
2019+
}
20132020
}
20142021

20152022
private function registerMailerConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)

Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@
510510
</xsd:sequence>
511511
<xsd:attribute name="enabled" type="xsd:boolean" />
512512
<xsd:attribute name="max-host-connections" type="xsd:integer" />
513+
<xsd:attribute name="mock-response-factory" type="xsd:string" />
513514
</xsd:complexType>
514515

515516
<xsd:complexType name="http_client_default_options" mixed="true">
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'http_client' => [
5+
'default_options' => null,
6+
'mock_response_factory' => 'my_response_factory',
7+
],
8+
]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<framework:config>
9+
<framework:http-client mock-response-factory="my_response_factory">
10+
<framework:default-options />
11+
</framework:http-client>
12+
</framework:config>
13+
</container>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
http_client:
3+
default_options: ~
4+
mock_response_factory: my_response_factory

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use Symfony\Component\DependencyInjection\Reference;
4242
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4343
use Symfony\Component\Form\Serializer\FormErrorNormalizer;
44+
use Symfony\Component\HttpClient\MockHttpClient;
4445
use Symfony\Component\HttpClient\ScopingHttpClient;
4546
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
4647
use Symfony\Component\Messenger\Transport\TransportFactory;
@@ -1567,6 +1568,21 @@ public function testMailerWithSpecificMessageBus(): void
15671568
$this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('mailer.mailer')->getArgument(1));
15681569
}
15691570

1571+
public function testHttpClientMockResponseFactory()
1572+
{
1573+
$container = $this->createContainerFromFile('http_client_mock_response_factory');
1574+
1575+
$definition = $container->getDefinition('http_client');
1576+
1577+
$this->assertSame(MockHttpClient::class, $definition->getClass());
1578+
$this->assertCount(1, $definition->getArguments());
1579+
1580+
$argument = $definition->getArgument(0);
1581+
1582+
$this->assertInstanceOf(Reference::class, $argument);
1583+
$this->assertSame('my_response_factory', (string) $argument);
1584+
}
1585+
15701586
protected function createContainer(array $data = [])
15711587
{
15721588
return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([

0 commit comments

Comments
 (0)