Skip to content

Commit

Permalink
[HttpClient][DI] Add an option to use the MockHTTPClient in functiona…
Browse files Browse the repository at this point in the history
…l tests
  • Loading branch information
GaryPEGEOT committed Apr 28, 2020
1 parent c699b9c commit bd11b47
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 0 deletions.
Expand Up @@ -1339,6 +1339,9 @@ private function addHttpClientSection(ArrayNodeDefinition $rootNode)
->end()
->end()
->end()
->scalarNode('mock_response_factory')
->info('ID of the service used to generate mock responses. Must be invokable.')
->end()
->arrayNode('scoped_clients')
->useAttributeAsKey('name')
->normalizeKeys(false)
Expand Down
Expand Up @@ -63,6 +63,7 @@
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\HttpKernel\CacheClearer\CacheClearerInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
Expand Down Expand Up @@ -1947,6 +1948,12 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
$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, XmlFileLoader $loader)
Expand Down
Expand Up @@ -483,6 +483,7 @@
</xsd:sequence>
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="max-host-connections" type="xsd:integer" />
<xsd:attribute name="mock-response-factory" type="xsd:string" />
</xsd:complexType>

<xsd:complexType name="http_client_default_options" mixed="true">
Expand Down
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('framework', [
'http_client' => [
'default_options' => null,
'mock_response_factory' => 'my_response_factory',
],
]);
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:http-client mock-response-factory="my_response_factory">
<framework:default-options />
</framework:http-client>
</framework:config>
</container>
@@ -0,0 +1,4 @@
framework:
http_client:
default_options: ~
mock_response_factory: my_response_factory
Expand Up @@ -37,6 +37,7 @@
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
use Symfony\Component\Messenger\Transport\TransportFactory;
Expand Down Expand Up @@ -1423,6 +1424,21 @@ public function testHttpClientFullDefaultOptions()
], $defaultOptions['peer_fingerprint']);
}

public function testHttpClientMockResponseFactory(): void
{
$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);
}

public function testMailer(): void
{
$container = $this->createContainerFromFile('mailer');
Expand Down

0 comments on commit bd11b47

Please sign in to comment.