From fda0179b13a6fb2d2195603c9d7707207f8f48b4 Mon Sep 17 00:00:00 2001 From: Starli0n Date: Fri, 2 Sep 2016 19:13:56 +0200 Subject: [PATCH] Fix Issue #34: Testing 'Internal Server Error (500)' --- composer.json | 2 +- src/There4/Slim/Test/WebTestClient.php | 4 ++-- .../Slim/Test/WebTestClientTest.php | 22 +++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 37c21cd..a30c204 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "source": "https://github.com/there4/slim-test-helpers" }, "require": { - "slim/slim": "3.*", + "slim/slim": "~3.1", "phpunit/phpunit": "^4.8|5.*", "phpunit/dbunit": "2.*", "illuminate/database": ">=4.0" diff --git a/src/There4/Slim/Test/WebTestClient.php b/src/There4/Slim/Test/WebTestClient.php index 9e56f00..d0f995c 100644 --- a/src/There4/Slim/Test/WebTestClient.php +++ b/src/There4/Slim/Test/WebTestClient.php @@ -102,9 +102,9 @@ private function request($method, $path, $data = array(), $optionalHeaders = arr $this->request = new Request($method, $uri, $headers, $cookies, $serverParams, $body); $response = new Response(); - // Invoke request + // Process request $app = $this->app; - $this->response = $app($this->request, $response); + $this->response = $app->process($this->request, $response); // Return the application output. return (string)$this->response->getBody(); diff --git a/tests/There4Test/Slim/Test/WebTestClientTest.php b/tests/There4Test/Slim/Test/WebTestClientTest.php index 890923b..4405f33 100644 --- a/tests/There4Test/Slim/Test/WebTestClientTest.php +++ b/tests/There4Test/Slim/Test/WebTestClientTest.php @@ -140,4 +140,26 @@ private function getValidUri() { return '/testing'; } + + public function testInternalError() + { + $this->getSlimInstance()->get('/internalerror', function ($request, $response, $args) { + throw new \Exception('Testing /internalerror.'); + return $response; + }); + + $container = $this->getSlimInstance()->getContainer(); + $container['errorHandler'] = function ($c) { + return function ($request, $response, $exception) use ($c) { + $data = array('message' => 'Internal Server Error'); + return $c['response']->withJson($data, 500); + }; + }; + + $client = new WebTestClient($this->getSlimInstance()); + $client->get('/internalerror'); + self::assertEquals(500, $client->response->getStatusCode()); + $data = json_decode($client->response->getBody()); + self::assertEquals('Internal Server Error', $data->message); + } }