[HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response
This commit is contained in:
parent
7da56f31ba
commit
564dce39f8
@ -68,6 +68,10 @@ class MockHttpClient implements HttpClientInterface
|
|||||||
$this->responseFactory->next();
|
$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);
|
return MockResponse::fromRequest($method, $url, $options, $response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,48 @@ use Symfony\Contracts\HttpClient\ResponseInterface;
|
|||||||
|
|
||||||
class MockHttpClientTest extends HttpClientTestCase
|
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
|
protected function getHttpClient(string $testCase): HttpClientInterface
|
||||||
{
|
{
|
||||||
$responses = [];
|
$responses = [];
|
||||||
|
Reference in New Issue
Block a user