diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 29becf671d19..2a130706a297 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -1866,8 +1866,12 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $e ->append($this->createHttpClientRetrySection()) ->end() ->end() + ->booleanNode('mock_client') + ->info('Inject a mock client.') + ->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 or a boolean to just inject the mock client.') + ->defaultNull() + ->info('The id of the service that should generate mock responses. It should be either an invokable or an iterable. Requires mock_client = true') ->end() ->arrayNode('scoped_clients') ->useAttributeAsKey('name') @@ -2006,8 +2010,11 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode, callable $e ->variableNode('md5')->end() ->end() ->end() + ->booleanNode('mock_client') + ->info('Inject a mock client.') + ->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 or a boolean to just inject the mock client.') + ->info('The id of the service that should generate mock responses. It should be either an invokable or an iterable') ->end() ->arrayNode('extra') ->info('Extra options for specific HTTP client') diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index a76d3c5fc415..0a6f509aa5d1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -2456,6 +2456,14 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder ->getDefinition('http_client.uri_template') ->setArgument(2, $defaultUriTemplateVars); + $mockByDefault = $config['mock_client'] ?? false; + $defaultMockResponseFactory = $config['mock_response_factory'] ?? null; + if ($mockByDefault) { + $container->register('http_client.mock_client', MockHttpClient::class) + ->setDecoratedService('http_client.transport', null, -10) // lower priority than TraceableHttpClient (5) + ->setArguments($defaultMockResponseFactory === null ? [] : [new Reference($defaultMockResponseFactory)]); + } + foreach ($config['scoped_clients'] as $name => $scopeConfig) { if ($container->has($name)) { throw new InvalidArgumentException(sprintf('Invalid scope name: "%s" is reserved.', $name)); @@ -2470,11 +2478,12 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder $httpClientTransport = new Reference('http_client.transport'); - if ($responseFactoryId = $scopeConfig['mock_response_factory'] ?? null) { + if ($scopeConfig['mock_client'] ?? $mockByDefault) { + $mockResponseFactory = $scopeConfig['mock_response_factory'] ?? $defaultMockResponseFactory; + $mockClientArguments = ($mockResponseFactory) ? [new Reference($mockResponseFactory)]: []; $container->register('http_client.mock_client.'.$name, MockHttpClient::class) ->setDecoratedService($httpClientTransport, null, -10) // lower priority than TraceableHttpClient (5) - ->setArguments( - true === $responseFactoryId ? [] : [new Reference($responseFactoryId)]); + ->setArguments($mockClientArguments); } if (null === $scope) { @@ -2510,6 +2519,7 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder $defaultUriTemplateVars, ]); + $container->registerAliasForArgument($name, HttpClientInterface::class); if ($hasPsr18) { @@ -2527,11 +2537,7 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder } } - if ($responseFactoryId = $config['mock_response_factory'] ?? null) { - $container->register('http_client.mock_client', MockHttpClient::class) - ->setDecoratedService('http_client.transport', null, -10) // lower priority than TraceableHttpClient (5) - ->setArguments(true === $responseFactoryId ? [] : [new Reference($responseFactoryId)]); - } + } private function registerThrottlingHttpClient(string $rateLimiter, string $name, ContainerBuilder $container): void 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 98392c6c2e18..fe856463e118 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 @@ -640,6 +640,7 @@ + @@ -697,7 +698,8 @@ - + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_scoped_mock_response_factory_service.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock.php similarity index 74% rename from src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_scoped_mock_response_factory_service.php rename to src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock.php index dcbd1cb0e76d..0496fd4e21bf 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_scoped_mock_response_factory_service.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock.php @@ -1,4 +1,4 @@ -loadFromExtension('framework', [ 'annotations' => false, @@ -7,14 +7,20 @@ 'php_errors' => ['log' => true], 'http_client' => [ 'default_options' => null, + 'mock_client' => true, 'scoped_clients' => [ 'notMocked' => [ - 'base_uri' => 'https://symfony.com' + 'base_uri' => 'https://symfony.com', + 'mock_client' => false, ], 'mocked' => [ + 'base_uri' => 'https://symfony.com' + ], + 'mocked_with_factory' => [ 'base_uri' => 'https://symfony.com', 'mock_response_factory' => 'my_response_factory' ], + ] ], ]); 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 index c0b9cf5ef7f6..57cc4290d255 100644 --- 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 @@ -7,6 +7,20 @@ 'php_errors' => ['log' => true], 'http_client' => [ 'default_options' => null, - 'mock_response_factory' => true, + 'mock_client' => true, + 'mock_response_factory' => 'my_factory', + 'scoped_clients' => [ + 'notMocked' => [ + 'base_uri' => 'https://symfony.com', + 'mock_client' => false, + ], + 'mocked' => [ + 'base_uri' => 'https://symfony.com' + ], + 'mocked_custom_factory' => [ + 'base_uri' => 'https://symfony.com', + 'mock_response_factory' => 'my_other_factory' + ] + ] ], ]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock_response_factory_service.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock_response_factory_service.php deleted file mode 100644 index 326f0d25db50..000000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock_response_factory_service.php +++ /dev/null @@ -1,12 +0,0 @@ -loadFromExtension('framework', [ - 'annotations' => false, - 'http_method_override' => false, - 'handle_all_throwables' => true, - 'php_errors' => ['log' => true], - 'http_client' => [ - 'default_options' => null, - 'mock_response_factory' => 'my_response_factory', - ], -]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_scoped_mock_response_factory.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_scoped_mock_response_factory.php deleted file mode 100644 index dbba92de997f..000000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_scoped_mock_response_factory.php +++ /dev/null @@ -1,20 +0,0 @@ -loadFromExtension('framework', [ - 'annotations' => false, - 'http_method_override' => false, - 'handle_all_throwables' => true, - 'php_errors' => ['log' => true], - 'http_client' => [ - 'default_options' => null, - 'scoped_clients' => [ - 'notMocked' => [ - 'base_uri' => 'https://symfony.com' - ], - 'mocked' => [ - 'base_uri' => 'https://symfony.com', - 'mock_response_factory' => true, - ], - ] - ], -]); diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_scoped_mock_response_factory.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock.xml similarity index 76% rename from src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_scoped_mock_response_factory.xml rename to src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock.xml index b4b64ffb5496..0dd3d74ee002 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_scoped_mock_response_factory.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock.xml @@ -8,10 +8,11 @@ - + - - + + + 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 index c5238a5d922f..b99a4ed206f3 100644 --- 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 @@ -8,8 +8,12 @@ - + + + + + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock_response_factory_service.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock_response_factory_service.xml deleted file mode 100644 index d2bc058fb17e..000000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock_response_factory_service.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_scoped_mock_response_factory_service.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_scoped_mock_response_factory_service.xml deleted file mode 100644 index 7d2f04a21ae0..000000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_scoped_mock_response_factory_service.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock.yml new file mode 100644 index 000000000000..a6365674c348 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock.yml @@ -0,0 +1,18 @@ +framework: + annotations: false + http_method_override: false + handle_all_throwables: true + php_errors: + log: true + http_client: + default_options: ~ + mock_client: true + scoped_clients: + notMocked: + base_uri : https://symfony.com + mock_client : false + mocked: + base_uri: https://symfony.com + mocked_with_factory: + base_uri: https://symfony.com + mock_response_factory: 'my_response_factory' 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 index a10995312c9d..6283b80d26fc 100644 --- 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 @@ -6,4 +6,14 @@ framework: log: true http_client: default_options: ~ - mock_response_factory: true + mock_client: true + mock_response_factory: 'my_factory' + scoped_clients: + notMocked: + base_uri : https://symfony.com + mock_client : false + mocked: + base_uri: https://symfony.com + mocked_custom_factory: + base_uri: https://symfony.com + mock_response_factory: 'my_other_factory' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock_response_factory_service.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock_response_factory_service.yml deleted file mode 100644 index 92c40b4591b1..000000000000 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock_response_factory_service.yml +++ /dev/null @@ -1,9 +0,0 @@ -framework: - annotations: false - http_method_override: false - handle_all_throwables: true - php_errors: - log: true - http_client: - default_options: ~ - mock_response_factory: my_response_factory diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php index fc5a0baefbd4..540be789d0bf 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php @@ -2095,21 +2095,19 @@ public function testMailerWithSpecificMessageBus() $this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('mailer.mailer')->getArgument(1)); } - public function testHttpClientMockResponseFactoryNoService() + public function testHttpClientNoMock() { - $container = $this->createContainerFromFile('http_client_scoped_mock_response_factory'); - - $this->assertFalse($container->hasDefinition('http_client.mock_client.notMocked')); + $container = $this->createContainerFromFile('http_client_scoped_without_query_option'); - $definition = $container->getDefinition('http_client.mock_client.mocked'); + $this->assertFalse($container->hasDefinition('http_client.mock_client.foo')); + $this->assertFalse($container->hasDefinition('http_client.mock_client')); - $this->assertSame(MockHttpClient::class, $definition->getClass()); - $this->assertCount(0, $definition->getArguments()); } - public function testHttpClientMockResponseFactoryWithService() + public function testHttpClientMockResponseFactory() { - $container = $this->createContainerFromFile('http_client_scoped_mock_response_factory_service'); + $container = $this->createContainerFromFile('http_client_mock_response_factory'); + $this->assertFalse($container->hasDefinition('http_client.mock_client.notMocked')); $definition = $container->getDefinition('http_client.mock_client.mocked'); @@ -2118,36 +2116,42 @@ public function testHttpClientMockResponseFactoryWithService() $this->assertCount(1, $definition->getArguments()); $argument = $definition->getArgument(0); - $this->assertInstanceOf(Reference::class, $argument); $this->assertSame('http_client.transport', current($definition->getDecoratedService())); - $this->assertSame('my_response_factory', (string) $argument); - } + $this->assertSame('my_factory', (string) $argument); - public function testHttpClientMockResponseFactoryService() - { - $container = $this->createContainerFromFile('http_client_mock_response_factory_service'); - - $definition = $container->getDefinition('http_client.mock_client'); + $definition = $container->getDefinition('http_client.mock_client.mocked_custom_factory'); $this->assertSame(MockHttpClient::class, $definition->getClass()); $this->assertCount(1, $definition->getArguments()); $argument = $definition->getArgument(0); - $this->assertInstanceOf(Reference::class, $argument); $this->assertSame('http_client.transport', current($definition->getDecoratedService())); - $this->assertSame('my_response_factory', (string) $argument); + $this->assertSame('my_other_factory', (string) $argument); } - public function testHttpClientUseMockResponseFactory() + public function testHttpClientUseMockClientButOverrideInScopedClientsAndEnableFactories() { - $container = $this->createContainerFromFile('http_client_mock_response_factory'); + $container = $this->createContainerFromFile('http_client_mock'); $definition = $container->getDefinition('http_client.mock_client'); $this->assertSame(MockHttpClient::class, $definition->getClass()); $this->assertCount(0, $definition->getArguments()); + + $this->assertFalse($container->hasDefinition('http_client.mock_client.notMocked')); + + $definition = $container->getDefinition('http_client.mock_client.mocked'); + + $this->assertSame(MockHttpClient::class, $definition->getClass()); + $this->assertCount(0, $definition->getArguments()); + + $definition = $container->getDefinition('http_client.mock_client.mocked_with_factory'); + + $this->assertSame(MockHttpClient::class, $definition->getClass()); + $this->assertCount(1, $definition->getArguments()); + } public function testRegisterParameterCollectingBehaviorDescribingTags()