summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArnold Daniels <arnold@jasny.net>2016-10-30 11:28:54 +0100
committerArnold Daniels <arnold@jasny.net>2016-10-30 11:28:54 +0100
commitf7135f61882288f45afaeea9efdd5ea8b248de91 (patch)
treecf5f7ac9f5df4e02496aded2901b77f6e0c5440d
parent780a4648b1e2eac3fcf8b7831807529d1455e647 (diff)
downloadrouter-f7135f61882288f45afaeea9efdd5ea8b248de91.zip
router-f7135f61882288f45afaeea9efdd5ea8b248de91.tar.gz
router-f7135f61882288f45afaeea9efdd5ea8b248de91.tar.bz2
Error handler has moved to it's own library
-rw-r--r--composer.json3
-rw-r--r--src/Router/Middleware/ErrorHandler.php54
-rw-r--r--tests/Router/Middleware/ErrorHandlerTest.php86
-rw-r--r--tests/support/TestHelpers.php3
4 files changed, 4 insertions, 142 deletions
diff --git a/composer.json b/composer.json
index 147e6ea..ad3d7c8 100644
--- a/composer.json
+++ b/composer.json
@@ -17,7 +17,8 @@
"require": {
"php": ">=5.6.0",
"jasny/php-functions": "^2.0",
- "psr/http-message": "^1.0"
+ "psr/http-message": "^1.0",
+ "psr/log": "^1.0"
},
"require-dev": {
"jasny/php-code-quality": "^2.0"
diff --git a/src/Router/Middleware/ErrorHandler.php b/src/Router/Middleware/ErrorHandler.php
deleted file mode 100644
index 789c455..0000000
--- a/src/Router/Middleware/ErrorHandler.php
+++ /dev/null
@@ -1,54 +0,0 @@
-<?php
-
-namespace Jasny\Router\Middleware;
-
-use Psr\Http\Message\ServerRequestInterface;
-use Psr\Http\Message\ResponseInterface;
-
-/**
- * Handle error in following middlewares/app actions
- */
-class ErrorHandler
-{
- /**
- * Run middleware action
- *
- * @param ServerRequestInterface $request
- * @param ResponseInterface $response
- * @param callback $next
- * @return ResponseInterface
- */
- public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)
- {
- if ($next && !is_callable($next)) {
- throw new \InvalidArgumentException("'next' should be a callback");
- }
-
- $error = false;
-
- try {
- $response = $next ? call_user_func($next, $request, $response) : $response;
- } catch(\Throwable $e) {
- $error = true;
- } catch(\Exception $e) { #This block can be removed when migrating to PHP7, because Throwable represents both Exception and Error
- $error = true;
- }
-
- return $error ? $this->handleError($response) : $response;
- }
-
- /**
- * Handle caught error
- *
- * @param ResponseInterface $response
- * @return ResponseInterface
- */
- protected function handleError($response)
- {
- $body = $response->getBody();
- $body->rewind();
- $body->write('Unexpected error');
-
- return $response->withStatus(500, 'Internal Server Error')->withBody($body);
- }
-}
diff --git a/tests/Router/Middleware/ErrorHandlerTest.php b/tests/Router/Middleware/ErrorHandlerTest.php
deleted file mode 100644
index c4b6313..0000000
--- a/tests/Router/Middleware/ErrorHandlerTest.php
+++ /dev/null
@@ -1,86 +0,0 @@
-<?php
-
-use Jasny\Router\Middleware\ErrorHandler;
-use Psr\Http\Message\ServerRequestInterface;
-use Psr\Http\Message\ResponseInterface;
-use Psr\Http\Message\StreamInterface;
-
-class ErrorHandlerTest extends PHPUnit_Framework_TestCase
-{
- /**
- * Test invoke with invalid 'next' param
- */
- public function testInvokeInvalidNext()
- {
- $middleware = new ErrorHandler();
- list($request, $response) = $this->getRequests();
-
- $this->expectException(\InvalidArgumentException::class);
-
- $result = $middleware($request, $response, 'not_callable');
- }
-
- /**
- * Test that exception in 'next' callback is caught
- */
- public function testInvokeCatchError()
- {
- $middleware = new ErrorHandler();
- list($request, $response) = $this->getRequests();
-
- $this->expectCatchError($response);
-
- $result = $middleware($request, $response, function($request, $response) {
- throw new Exception('Test exception');
- });
-
- $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object");
- }
-
- /**
- * Test case when there is no error
- */
- public function testInvokeNoError()
- {
- $middleware = new ErrorHandler();
- list($request, $response) = $this->getRequests();
-
- $result = $middleware($request, $response, function($request, $response) {
- $response->nextCalled = true;
-
- return $response;
- });
-
- $this->assertEquals(get_class($response), get_class($result), "Middleware should return response object");
- $this->assertTrue($result->nextCalled, "'next' was not called");
- }
-
- /**
- * Get requests for testing
- *
- * @return array
- */
- public function getRequests()
- {
- $request = $this->createMock(ServerRequestInterface::class);
- $response = $this->createMock(ResponseInterface::class);
-
- return [$request, $response];
- }
-
- /**
- * Expect for error
- *
- * @param ResponseInterface $response
- */
- public function expectCatchError($response)
- {
- $stream = $this->createMock(StreamInterface::class);
- $stream->expects($this->once())->method('rewind');
- $stream->expects($this->once())->method('write')->with($this->equalTo('Unexpected error'));
-
- $response->method('getBody')->will($this->returnValue($stream));
- $response->expects($this->once())->method('withBody')->with($this->equalTo($stream))->will($this->returnSelf());
- $response->expects($this->once())->method('withStatus')->with($this->equalTo(500), $this->equalTo('Internal Server Error'))->will($this->returnSelf());
- }
-}
diff --git a/tests/support/TestHelpers.php b/tests/support/TestHelpers.php
index 5604a37..e4fdff7 100644
--- a/tests/support/TestHelpers.php
+++ b/tests/support/TestHelpers.php
@@ -3,6 +3,7 @@
namespace Jasny\Router;
use PHPUnit_Framework_MockObject_Matcher_Invocation as Invocation;
+use PHPUnit_Framework_MockObject_MockObject as MockObject;
/**
* Helper methods for PHPUnit tests
@@ -15,7 +16,7 @@ trait TestHelpers
* @param Invocation $matcher
* @param array $with With arguments
* @param mixed $return
- * @return \PHPUnit_Framework_MockObject_MockObject
+ * @return MockObject
*/
protected function createCallbackMock(Invocation $matcher, $with = [], $return = null)
{