10000 [HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response by fancyweb · Pull Request #37966 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response #37966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/HttpClient/MockHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function request(string $method, string $url, array $options = []): Respo
$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);
}

Expand Down
42 changes: 42 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,48 @@

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 = [];
Expand Down
0