Skip to content

Commit

Permalink
Merge branch '4.4' into 5.1
Browse files Browse the repository at this point in the history
* 4.4:
  Fix CS
  [HttpClient][MockHttpClient][DX] Throw when the response factory callable does not return a valid response
  [FrameworkBundle] Do not pass the base uri twice to scoped http clients
  • Loading branch information
fabpot committed Aug 30, 2020
2 parents bb1ffde + 6a3af6c commit edf00ab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions MockHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function request(string $method, string $url, array $options = []): Respo
}
++$this->requestsCount;

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 Tests/MockHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ static function (string $method, string $url, array $options = []) {
];
}

/**
* @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 transportExceptionProvider
*/
Expand Down Expand Up @@ -143,6 +165,26 @@ static function (string $method, string $url, array $options = []) {
];
}

/**
* @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 comments on commit edf00ab

Please sign in to comment.