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

Skip to content

Commit 564dce3

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

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Symfony/Component/HttpClient/MockHttpClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ public function request(string $method, string $url, array $options = []): Respo
6868
$this->responseFactory->next();
6969
}
7070

71+
if (!$response instanceof ResponseInterface) {
72+
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)));
73+
}
74+
7175
return MockResponse::fromRequest($method, $url, $options, $response);
7276
}
7377

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,48 @@
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, string $expectedExceptionMessage)
51+
{
52+
$this->expectException(TransportException::class);
53+
$this->expectExceptionMessage($expectedExceptionMessage);
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(); }, 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "Generator" given.'],
62+
[static function (): array { return [new MockResponse()]; }, 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "array" given.'],
63+
[(static function (): \Generator { yield 'ccc'; })(), 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "string" given.'],
64+
];
65+
}
66+
2567
protected function getHttpClient(string $testCase): HttpClientInterface
2668
{
2769
$responses = [];

0 commit comments

Comments
 (0)