From 564dce39f84f3e3a9e39189da494cb6bc35bfc49 Mon Sep 17 00:00:00 2001 From: Thomas Calvet Date: Thu, 27 Aug 2020 17:46:33 +0200 Subject: [PATCH] [HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response --- .../Component/HttpClient/MockHttpClient.php | 4 ++ .../HttpClient/Tests/MockHttpClientTest.php | 42 +++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/Symfony/Component/HttpClient/MockHttpClient.php b/src/Symfony/Component/HttpClient/MockHttpClient.php index 07d53cc149..168e3077bb 100644 --- a/src/Symfony/Component/HttpClient/MockHttpClient.php +++ b/src/Symfony/Component/HttpClient/MockHttpClient.php @@ -68,6 +68,10 @@ class MockHttpClient implements HttpClientInterface $this->responseFactory->next(); } + if (!$response instanceof ResponseInterface) { + 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))); + } + return MockResponse::fromRequest($method, $url, $options, $response); } diff --git a/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php index f81322e0bb..bfed5ce145 100644 --- a/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php @@ -22,6 +22,48 @@ use Symfony\Contracts\HttpClient\ResponseInterface; class MockHttpClientTest extends HttpClientTestCase { + /** + * @dataProvider validResponseFactoryProvider + */ + public function testValidResponseFactory($responseFactory) + { + (new MockHttpClient($responseFactory))->request('GET', 'https://foo.bar'); + + $this->addToAssertionCount(1); + } + + public function validResponseFactoryProvider() + { + return [ + [static function (): MockResponse { return new MockResponse(); }], + [new MockResponse()], + [[new MockResponse()]], + [new \ArrayIterator([new MockResponse()])], + [null], + [(static function (): \Generator { yield new MockResponse(); })()], + ]; + } + + /** + * @dataProvider invalidResponseFactoryProvider + */ + public function testInvalidResponseFactory($responseFactory, string $expectedExceptionMessage) + { + $this->expectException(TransportException::class); + $this->expectExceptionMessage($expectedExceptionMessage); + + (new MockHttpClient($responseFactory))->request('GET', 'https://foo.bar'); + } + + public function invalidResponseFactoryProvider() + { + return [ + [static function (): \Generator { yield new MockResponse(); }, 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "Generator" given.'], + [static function (): array { return [new MockResponse()]; }, 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "array" given.'], + [(static function (): \Generator { yield 'ccc'; })(), 'The response factory passed to MockHttpClient must return/yield an instance of ResponseInterface, "string" given.'], + ]; + } + protected function getHttpClient(string $testCase): HttpClientInterface { $responses = [];