複数のresponseを受けられるように

This commit is contained in:
eai04191 2019-08-22 08:58:59 +09:00
parent 0e3878a808
commit 9bd6b23c05

View File

@ -22,12 +22,12 @@ trait CreateMockedResolver
/**
* @param string $resolverClass
* @param string $responseText
* @param string|array $response
* @param array $headers
* @param int $status
* @return Resolver
*/
protected function createResolver(string $resolverClass, string $responseText, array $headers = [], int $status = 200)
protected function createResolver(string $resolverClass, $response, array $headers = [], int $status = 200)
{
if (!$this->shouldUseMock()) {
$this->resolver = app()->make($resolverClass);
@ -39,8 +39,26 @@ trait CreateMockedResolver
'content-type' => 'text/html',
];
$mockResponse = new Response($status, $headers, $responseText);
$this->handler = new MockHandler([$mockResponse]);
if (is_array($response)) {
$responses = $response;
$mockResponses = [];
foreach ($responses as $response) {
if (is_array($response)) {
$response += [
1 => [],
2 => 200
];
array_push($mockResponses, new Response($response[2], $response[1], $response[0]));
} else {
array_push($mockResponses, new Response($status, $headers, $response));
}
}
$this->handler = new MockHandler($mockResponses);
} else {
$mockResponse = new Response($status, $headers, $response);
$this->handler = new MockHandler([$mockResponse]);
}
$client = new Client(['handler' => $this->handler]);
$this->resolver = app()->make($resolverClass, ['client' => $client]);