public function it_performs_a_HTTP_request_to_the_given_url_and_raises_an_error_if_failed(HttpMethodsClient $client, ResponseInterface $response, HttpException $e) { $client->sendRequest(Argument::type(RequestInterface::class))->willThrow($e->getWrappedObject()); $e->getResponse()->willReturn($response); $response->getStatusCode()->willReturn(500); $response->getBody()->willReturn('{"error":"foo"}'); $this->shouldThrow(new CommunicationException('500 : foo'))->during('request', ['GET', 'bar']); }
function it_throws_exception_when_request_returns_not_found_error(HttpClientInterface $httpClient, RequestFactoryInterface $requestFactory, RequestInterface $request, ResponseInterface $response, HttpException $httpException) { $apiHttpPathAndQuery = ApiHttpPathAndQuery::createForPath(self::API_PATH); $response->getStatusCode()->willReturn(404); $requestFactory->createRequest(Argument::any(), Argument::any(), Argument::any())->willReturn($request); $httpException->getResponse()->willReturn($response); $httpClient->sendRequest($request)->willThrow($httpException->getWrappedObject()); $this->shouldThrow(ApiHttpClientNotFoundException::class)->duringGet($apiHttpPathAndQuery); }
/** * builds the error exception. * * @param PlugException $e * * @return CommunicationException */ private function buildRequestError(PlugException $e) { $data = $e->getResponse() ? json_decode($e->getResponse()->getBody(), true) : ['error' => $e->getMessage()]; $message = isset($data['error']) ? $data['error'] : 'Server Error'; $status = $e->getResponse() ? $e->getResponse()->getStatusCode() : 500; return new CommunicationException(sprintf('%s : %s', $status, $message)); }