/** * Ensure population of errors * * @covers HtmlValidator\Response::__construct * @covers HtmlValidator\Response::validateResponse * @covers HtmlValidator\Response::parse * @covers HtmlValidator\Response::getErrors * @covers HtmlValidator\Response::hasErrors */ public function testWillPopulateErrors() { $data = ['messages' => [['type' => 'error', 'firstLine' => 1, 'lastLine' => 2, 'firstColumn' => 3, 'lastColumn' => 4, 'hiliteStart' => 5, 'hiliteLength' => 6, 'message' => 'Foobar', 'extract' => '<strong>Foo</strong>'], ['type' => 'error', 'firstLine' => 9, 'lastLine' => 8, 'firstColumn' => 7, 'lastColumn' => 6, 'hiliteStart' => 5, 'hiliteLength' => 4, 'message' => 'Pimp Pelican', 'extract' => '<em>Pelican</em>']]]; $responseMock = $this->getGuzzleResponseMock(true); $responseMock->expects($this->any())->method('getBody')->will($this->returnValue(json_encode($data))); $response = new Response($responseMock); $errors = $response->getErrors(); $this->assertTrue($response->hasErrors()); $this->assertSame(2, count($errors)); $this->assertSame($data['messages'][0]['message'], $errors[0]->getText()); $this->assertSame($data['messages'][1]['message'], $errors[1]->getText()); }
/** * Throw exception * * @param Response $response * * @throws Exception\ExceptionInterface * * @codeCoverageIgnore */ protected function throwExceptions(Response $response) { if ($response->hasErrors()) { throw $response->getLastError(); } $message = 'Error while execute request, code: ' . $response->getCode(); switch ($response->getCode()) { case 200: case 201: break; case 204: $message = 'No content'; break; case 400: $message = 'Bad Request'; break; case 401: $message = 'Unauthorized'; break; case 404: $message = 'Not Found'; break; case 405: $message = 'Method Not Allowed'; break; case 500: $message = 'Internal Server Error'; break; } throw new Exception\HttpException($message, $response->getCode()); }
function testHasErrors() { $req = Request::get(TEST_URL); $four_oh_four_headers = "HTTP/1.1 404 Not Found\nContent-Type: application/json"; $res = new Response(SAMPLE_JSON_RESPONSE, $four_oh_four_headers, $req); assert(true === $res->hasErrors()); $two_oh_oh = "HTTP/1.1 200 OK\nContent-Type: application/json"; $res = new Response(SAMPLE_JSON_RESPONSE, $two_oh_oh, $req); assert(false === $res->hasErrors()); }