10000 bug #37966 [HttpClient][MockHttpClient][DX] Throw when the response f… · symfony/symfony@4d6ea77 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d6ea77

Browse files
committed
bug #37966 [HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response (fancyweb)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - The current message is `TypeError: Argument 4 passed to Symfony\Component\HttpClient\Response\MockResponse::fromRequest() must implement interface Symfony\Contracts\HttpClient\ResponseInterface, instance of Generator given`. I lost some time with this because I was passing a callable that returns a \Generator instead of passing the resulting \Generator directly. We could support that case but I guess with the added exception message, it is clear we don't support it at all. Commits ------- 564dce3 [HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response
2 parents 943cbd8 + 564dce3 commit 4d6ea77

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)
0