-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Description
Hi,
I would like to test an 'Internal Server Error' from a LocalWebTestCase but I do not know how to do it.
First I added this error handler in SlimApp:
// Error handler
$container['errorHandler'] = function ($c) {
return function ($request, $response, $exception) use ($c) {
$c->logger->error('Internal Server Error');
$data = array('message' => 'Internal Server Error');
return $c['response']->withJson($data, 500);
};
};I added a specific route only for testing purpose:
// Route for testing server internal error (ie: $container['errorHandler'])
$app->get('/internalerror', function ($request, $response, $args) {
$this->logger->info("Route '/' internalerror");
throw new \Exception('Testing /internalerror.');
return $response;
});In a test case I do:
public function testInternalError()
{
try {
$this->client->get('/internalerror');
} catch (\Exception $e) {
}
$response = $this->client->response;
$this->assertEquals(500, $response->getStatusCode());
$this->assertEquals('Internal Server Error', $response->getReasonPhrase());
$this->assertEquals('application/json;charset=utf-8', $response->getHeader('Content-Type')[0]);
$data = json_decode($response->getBody());
$this->assertEquals('Internal Server Error', $data->message);
}But $this->client->response return null.
My aim would be to test the message sent by the server defined in the error handler.
In a production environment, this route is working fine and I have the message 'Internal Server Error' in json.
http://localhost:8080/internalerror
{"message":"Internal Server Error"}Thanks for your help!
Metadata
Metadata
Assignees
Labels
No labels