Skip to content

[HttpClient] Allow pass array of callable to the mocking http client #34871

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 1 commit into from
Feb 2, 2020
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
12 changes: 10 additions & 2 deletions src/Symfony/Component/HttpClient/MockHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class MockHttpClient implements HttpClientInterface

private $responseFactory;
private $baseUri;
private $requestsCount = 0;

/**
* @param callable|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory
* @param callable|callable[]|ResponseInterface|ResponseInterface[]|iterable|null $responseFactory
*/
public function __construct($responseFactory = null, string $baseUri = null)
{
Expand Down Expand Up @@ -64,9 +65,11 @@ public function request(string $method, string $url, array $options = []): Respo
} elseif (!$this->responseFactory->valid()) {
throw new TransportException('The response factory iterator passed to MockHttpClient is empty.');
} else {
$response = $this->responseFactory->current();
$responseFactory = $this->responseFactory->current();
$response = \is_callable($responseFactory) ? $responseFactory($method, $url, $options) : $responseFactory;
$this->responseFactory->next();
}
++$this->requestsCount;

return MockResponse::fromRequest($method, $url, $options, $response);
}
Expand All @@ -84,4 +87,9 @@ public function stream($responses, float $timeout = null): ResponseStreamInterfa

return new ResponseStream(MockResponse::stream($responses, $timeout));
}

public function getRequestsCount(): int
{
return $this->requestsCount;
}
}
121 changes: 121 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,127 @@

class MockHttpClientTest extends HttpClientTestCase
{
/**
* @dataProvider mockingProvider
*/
public function testMocking($factory, array $expectedResponses)
{
$client = new MockHttpClient($factory, 'https://example.com/');
$this->assertSame(0, $client->getRequestsCount());

$urls = ['/foo', '/bar'];
foreach ($urls as $i => $url) {
$response = $client->request('POST', $url, ['body' => 'payload']);
$this->assertEquals($expectedResponses[$i], $response->getContent());
}

$this->assertSame(2, $client->getRequestsCount());
}

public function mockingProvider(): iterable
{
yield 'callable' => [
static function (string $method, string $url, array $options = []) {
return new MockResponse($method.': '.$url.' (body='.$options['body'].')');
},
[
'POST: https://example.com/foo (body=payload)',
'POST: https://example.com/bar (body=payload)',
],
];

yield 'array of callable' => [
[
static function (string $method, string $url, array $options = []) {
return new MockResponse($method.': '.$url.' (body='.$options['body'].') [1]');
},
static function (string $method, string $url, array $options = []) {
return new MockResponse($method.': '.$url.' (body='.$options['body'].') [2]');
},
],
[
'POST: https://example.com/foo (body=payload) [1]',
'POST: https://example.com/bar (body=payload) [2]',
],
];

yield 'array of response objects' => [
[
new MockResponse('static response [1]'),
new MockResponse('static response [2]'),
],
[
'static response [1]',
'static response [2]',
],
];

yield 'iterator' => [
new \ArrayIterator(
[
new MockResponse('static response [1]'),
new MockResponse('static response [2]'),
]
),
[
'static response [1]',
'static response [2]',
],
];

yield 'null' => [
null,
[
'',
'',
],
];
}

/**
* @dataProvider transportExceptionProvider
*/
public function testTransportExceptionThrowsIfPerformedMoreRequestsThanConfigured($factory)
{
$client = new MockHttpClient($factory, 'https://example.com/');

$client->request('POST', '/foo');
$client->request('POST', '/foo');

$this->expectException(TransportException::class);
$client->request('POST', '/foo');
}

public function transportExceptionProvider(): iterable
{
yield 'array of callable' => [
[
static function (string $method, string $url, array $options = []) {
return new MockResponse();
},
static function (string $method, string $url, array $options = []) {
return new MockResponse();
},
],
];

yield 'array of response objects' => [
[
new MockResponse(),
new MockResponse(),
],
];

yield 'iterator' => [
new \ArrayIterator(
[
new MockResponse(),
new MockResponse(),
]
),
];
}

protected function getHttpClient(string $testCase): HttpClientInterface
{
$responses = [];
Expand Down