8000 Merge branch '4.4' into 5.1 · symfony/symfony@c367e26 · GitHub
[go: up one dir, main page]

Skip to content

Commit c367e26

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Fix CS [HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response [FrameworkBundle] Do not pass the base uri twice to scoped http clients
2 parents 5189571 + 2adacbc commit c367e26

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1927,9 +1927,12 @@ private function registerHttpClientConfiguration(array $config, ContainerBuilder
19271927
unset($scopeConfig['scope']);
19281928

19291929
if (null === $scope) {
1930+
$baseUri = $scopeConfig['base_uri'];
1931+
unset($scopeConfig['base_uri']);
1932+
19301933
$container->register($name, ScopingHttpClient::class)
19311934
->setFactory([ScopingHttpClient::class, 'forBaseUri'])
1932-
->setArguments([new Reference($httpClientId), $scopeConfig['base_uri'], $scopeConfig])
1935+
->setArguments([new Reference($httpClientId), $baseUri, $scopeConfig])
19331936
->addTag('http_client.client')
19341937
;
19351938
} else {

src/Symfony/Component/HttpClient/MockHttpClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public function request(string $method, string $url, array $options = []): Respo
7171
}
7272
++$this->requestsCount;
7373

74+
if (!$response instanceof ResponseInterface) {
75+
throw new TransportException(sprintf('The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "%s" given.', \is_object($response) ? \get_class($response) : \gettype($response)));
76+
}
77+
7478
return MockResponse::fromRequest($method, $url, $options, $response);
7579
}
7680

src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ static function (string $method, string $url, array $options = []) {
9999
];
100100
}
101101

102+
/**
103+
* @dataProvider validResponseFactoryProvider
104+
*/
105+
public function testValidResponseFactory($responseFactory)
106+
{
107+
(new MockHttpClient($responseFactory))->request('GET', 'https://foo.bar');
108+
109+
$this->addToAssertionCount(1);
110+
}
111+
112+
public function validResponseFactoryProvider()
113+
{
114+
return [
115+
[static function (): MockResponse { return new MockResponse(); }],
116+
[new MockResponse()],
117+
[[new MockResponse()]],
118+
[new \ArrayIterator([new MockResponse()])],
119+
[null],
120+
[(static function (): \Generator { yield new MockResponse(); })()],
121+
];
122+
}
123+
102124
/**
103125
* @dataProvider transportExceptionProvider
104126
*/
@@ -143,6 +165,26 @@ static function (string $method, string $url, array $options = []) {
143165
];
144166
}
145167

168+
/**
169+
* @dataProvider invalidResponseFactoryProvider
170+
*/
171+
public function testInvalidResponseFactory($responseFactory, string $expectedExceptionMessage)
172+
{
173+
$this->expectException(TransportException::class);
174+
$this->expectExceptionMessage($expectedExceptionMessage);
175+
176+
(new MockHttpClient($responseFactory))->request('GET', 'https://foo.bar');
177+
}
178+
179+
public function invalidResponseFactoryProvider()
180+
{
181+
return [
182+
[static function (): \Generator { yield new MockResponse(); }, 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "Generator" given.'],
183+
[static function (): array { return [new MockResponse()]; }, 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "array" given.'],
184+
[(static function (): \Generator { yield 'ccc'; })(), 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "string" given.'],
185+
];
186+
}
187+
146188
protected function getHttpClient(string $testCase): HttpClientInterface
147189
{
148190
$responses = [];

0 commit comments

Comments
 (0)
0