From b53739c79d99d876d96f7a6420a20f8cf182dee4 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Fri, 28 Feb 2020 10:44:48 +0100 Subject: [PATCH] [HttpClient][DI] Add an option to use the MockClient in functional tests --- .../DependencyInjection/Configuration.php | 3 +++ .../DependencyInjection/FrameworkExtension.php | 7 +++++++ .../Resources/config/schema/symfony-1.0.xsd | 1 + .../php/http_client_mock_response_factory.php | 8 ++++++++ .../xml/http_client_mock_response_factory.xml | 13 +++++++++++++ .../yml/http_client_mock_response_factory.yml | 4 ++++ .../FrameworkExtensionTest.php | 16 ++++++++++++++++ 7 files changed, 52 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock_response_factory.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock_response_factory.xml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock_response_factory.yml diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 4e47cbf75d..085ceb5daf 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -1450,6 +1450,9 @@ class Configuration implements ConfigurationInterface ->end() ->end() ->end() + ->scalarNode('mock_response_factory') + ->info('The id of the service that should generate mock responses. It should be either an invokable or an iterable.') + ->end() ->arrayNode('scoped_clients') ->useAttributeAsKey('name') ->normalizeKeys(false) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index f922448df7..9e96263ddc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -63,6 +63,7 @@ use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator; use Symfony\Component\Form\FormTypeExtensionInterface; use Symfony\Component\Form\FormTypeGuesserInterface; use Symfony\Component\Form\FormTypeInterface; +use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\ScopingHttpClient; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface; @@ -2009,6 +2010,12 @@ class FrameworkExtension extends Extension $container->registerAliasForArgument('psr18.'.$name, ClientInterface::class, $name); } } + + if ($responseFactoryId = $config['mock_response_factory'] ?? null) { + $container->getDefinition($httpClientId) + ->setClass(MockHttpClient::class) + ->setArguments([new Reference($responseFactoryId)]); + } } private function registerMailerConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 9a03ede460..08cea8ecee 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -510,6 +510,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock_response_factory.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock_response_factory.php new file mode 100644 index 0000000000..5b64c3ae0a --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock_response_factory.php @@ -0,0 +1,8 @@ +loadFromExtension('framework', [ + 'http_client' => [ + 'default_options' => null, + 'mock_response_factory' => 'my_response_factory', + ], +]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock_response_factory.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock_response_factory.xml new file mode 100644 index 0000000000..6835b2f4b7 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock_response_factory.xml @@ -0,0 +1,13 @@ + + + + + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock_response_factory.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock_response_factory.yml new file mode 100644 index 0000000000..b958591084 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock_response_factory.yml @@ -0,0 +1,4 @@ +framework: + http_client: + default_options: ~ + mock_response_factory: my_response_factory diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 2c14a2ad0b..7044b52bfb 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -41,6 +41,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBa use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Serializer\FormErrorNormalizer; +use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\HttpClient\ScopingHttpClient; use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass; use Symfony\Component\Messenger\Transport\TransportFactory; @@ -1567,6 +1568,21 @@ abstract class FrameworkExtensionTest extends TestCase $this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('mailer.mailer')->getArgument(1)); } + public function testHttpClientMockResponseFactory() + { + $container = $this->createContainerFromFile('http_client_mock_response_factory'); + + $definition = $container->getDefinition('http_client'); + + $this->assertSame(MockHttpClient::class, $definition->getClass()); + $this->assertCount(1, $definition->getArguments()); + + $argument = $definition->getArgument(0); + + $this->assertInstanceOf(Reference::class, $argument); + $this->assertSame('my_response_factory', (string) $argument); + } + protected function createContainer(array $data = []) { return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([