Closed suadhuskic closed 3 years ago
I guess what you want is providing a fake response to any request? Which mocking framework do you use? Or do you intend to write a simple mock implementation yourself that fulfills your requirements?
Yeah, i guess you can say that. We are using Mockery but i doubt that would work here? I haven't actually tried it.
Here is an example of a function that is proxying back specific exceptions when we get a 400 from an external API. I want to test that the correct exceptions are being thrown depending on the status code & response body:
public function callSomeExternalApi(
array $payload,
?CancellationToken $cancellationToken = null
): Promise {
$url = self::BASE_URL;
$request = new Request($url, 'POST', '');
$request->setBody(http_build_query(payload));
$request->addHeader('Content-Type', 'application/x-www-form-urlencoded');
return call(function () use ($request, $cancellationToken) {
$response = yield $this->httpClient->request($request, $cancellationToken);
$body = yield $response->getBody()->buffer();
$responseBody = json_decode($body, true);
if ($response->getStatus() === Response::HTTP_OK) {
return responseBody;
} elseif ($response->getStatus() === Response::HTTP_BAD_REQUEST) {
$errorCode = $responseBody['code'] ?? null;
if ($errorCode === self::ERROR_CODE_1) {
throw new ERROR_CODE_1Exception();
}
if ($errorCode === self::ERROR_CODE_2) {
throw new ERROR_CODE_2Exception();
}
throw new BadRequestException('Failed to fetch external stuff..');
} else {
throw new ExternalServerError('There was a problem.');
}
});
}
$this->httpClient
is an instanceof Amp\Http\Client\HttpClient::class
with a pre authorization header set, nothing else.
Actually, let me try creating a ApplicationInterceptor
and returning early
You can either do that or mock DelegateHttpClient
and then use new HttpClient($mock)
. It all depends on the level you'd like to mock.
It seems like all questions have been answered. If not, please respond with further questions, the issue can always be re-opened. :+1:
Are there examples on how to mock out sending requests? I looked through the examples folder and website, but couldn't find anything