Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fef0c62

Browse files
committed
[HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response
1 parent 7da56f3 commit fef0c62

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Symfony/Component/HttpClient/MockHttpClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public function request(string $method, string $url, array $options = []): Respo
6161
$response = new MockResponse();
6262
} elseif (\is_callable($this->responseFactory)) {
6363
$response = ($this->responseFactory)($method, $url, $options);
64+
if (!$response instanceof ResponseInterface) {
65+
throw new TransportException('The response factory callable passed to MockHttpClient must return an instance of ResponseInterface.');
66+
}
6467
} elseif (!$this->responseFactory->valid()) {
6568
throw new TransportException('The response factory iterator passed to MockHttpClient is empty.');
6669
} else {

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,47 @@
2222

2323
class MockHttpClientTest extends HttpClientTestCase
2424
{
25+
/**
26+
* @dataProvider validResponseFactoryProvider
27+
*/
28+
public function testValidResponseFactory($responseFactory)
29+
{
30+
(new MockHttpClient($responseFactory))->request('GET', 'https://foo.bar');
31+
32+
$this->addToAssertionCount(1);
33+
}
34+
35+
public function validResponseFactoryProvider()
36+
{
37+
return [
38+
[static function (): MockResponse { return new MockResponse(); }],
39+
[new MockResponse()],
40+
[[new MockResponse()]],
41+
[new \ArrayIterator([new MockResponse()])],
42+
[null],
43+
[(static function (): \Generator { yield new MockResponse(); })()],
44+
];
45+
}
46+
47+
/**
48+
* @dataProvider invalidResponseFactoryProvider
49+
*/
50+
public function testInvalidResponseFactory($responseFactory)
51+
{
52+
$this->expectException(TransportException::class);
53+
$this->expectExceptionMessage('The response factory callable passed to MockHttpClient must return an instance of ResponseInterface.');
54+
55+
(new MockHttpClient($responseFactory))->request('GET', 'https://foo.bar');
56+
}
57+
58+
public function invalidResponseFactoryProvider()
59+
{
60+
return [
61+
[static function (): \Generator { yield new MockResponse(); }],
62+
[static function (): array { return [new MockResponse()]; }],
63+
];
64+
}
65+
2566
protected function getHttpClient(string $testCase): HttpClientInterface
2667
{
2768
$responses = [];

0 commit comments

Comments
 (0)