-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Configure MockClient
if mock_response_factory has been set on a scoped client
#52265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tarjei
wants to merge
4
commits into
symfony:7.4
Choose a base branch
from
tarjei:52257_http_client_mocks
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5fd1b32
Configure mockclient if mock_response_factory has been set on a scope…
tarjei 8b43395
Improve testcoverage and add mock_client boolean parameter.
tarjei 12fab44
Fix test
tarjei 776e650
Move mock_ options out of default_options and update tests accordingly
tarjei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)); | ||
|
@@ -2468,18 +2476,28 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder | |
$retryOptions = $scopeConfig['retry_failed'] ?? ['enabled' => false]; | ||
unset($scopeConfig['retry_failed']); | ||
|
||
$httpClientTransport = new Reference('http_client.transport'); | ||
|
||
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($mockClientArguments); | ||
} | ||
|
||
if (null === $scope) { | ||
$baseUri = $scopeConfig['base_uri']; | ||
unset($scopeConfig['base_uri']); | ||
|
||
$container->register($name, ScopingHttpClient::class) | ||
->setFactory([ScopingHttpClient::class, 'forBaseUri']) | ||
->setArguments([new Reference('http_client.transport'), $baseUri, $scopeConfig]) | ||
->setArguments([$httpClientTransport, $baseUri, $scopeConfig]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this change is needed. Sure you save a few objects and memory but only during build time and this is like 3 objects. |
||
->addTag('http_client.client') | ||
; | ||
} else { | ||
$container->register($name, ScopingHttpClient::class) | ||
->setArguments([new Reference('http_client.transport'), [$scope => $scopeConfig], $scope]) | ||
->setArguments([$httpClientTransport, [$scope => $scopeConfig], $scope]) | ||
->addTag('http_client.client') | ||
; | ||
} | ||
|
@@ -2501,6 +2519,7 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder | |
$defaultUriTemplateVars, | ||
]); | ||
|
||
|
||
$container->registerAliasForArgument($name, HttpClientInterface::class); | ||
|
||
if ($hasPsr18) { | ||
|
@@ -2518,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([new Reference($responseFactoryId)]); | ||
} | ||
|
||
} | ||
|
||
private function registerThrottlingHttpClient(string $rateLimiter, string $name, ContainerBuilder $container): void | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...ymfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/http_client_mock.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
d<?php | ||
|
||
$container->loadFromExtension('framework', [ | ||
'annotations' => false, | ||
'http_method_override' => false, | ||
'handle_all_throwables' => true, | ||
'php_errors' => ['log' => true], | ||
'http_client' => [ | ||
'default_options' => null, | ||
'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' | ||
], | ||
|
||
] | ||
], | ||
]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ymfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/http_client_mock.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?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 http-method-override="false" handle-all-throwables="true"> | ||
<framework:annotations enabled="false" /> | ||
<framework:php-errors log="true" /> | ||
<framework:http-client mock-client="true"> | ||
<framework:default-options /> | ||
<framework:scoped-client name="notMocked" base-uri="https://symfony.com" mock-client="false"/> | ||
<framework:scoped-client name="mocked" base-uri="https://symfony.com" /> | ||
<framework:scoped-client name="mocked_with_factory" base-uri="https://symfony.com" mock-response-factory="my_response_factory" /> | ||
</framework:http-client> | ||
</framework:config> | ||
</container> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ymfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/http_client_mock.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...undle/Tests/DependencyInjection/Fixtures/yml/http_client_scoped_mock_response_factory.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
framework: | ||
annotations: false | ||
http_method_override: false | ||
handle_all_throwables: true | ||
php_errors: | ||
log: true | ||
http_client: | ||
default_options: ~ | ||
mock_response_factory: ~ | ||
|
||
scoped_clients: | ||
notMocked: | ||
base_uri: https://symfony.com | ||
mocked: | ||
base_uri: https://symfony.com | ||
mock_response_factory: true |
16 changes: 16 additions & 0 deletions
16
...sts/DependencyInjection/Fixtures/yml/http_client_scoped_mock_response_factory_service.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
framework: | ||
annotations: false | ||
http_method_override: false | ||
handle_all_throwables: true | ||
php_errors: | ||
log: true | ||
http_client: | ||
default_options: ~ | ||
mock_response_factory: ~ | ||
|
||
scoped_clients: | ||
notMocked: | ||
base_uri: https://symfony.com | ||
mocked: | ||
base_uri: https://symfony.com | ||
mock_response_factory: "my_response_factory" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is also decorated with priority
10
for the globalmock_response_factory
. Maybe worth having a lower priority here so it superseeds the global one ?