diff options
author | Arnold Daniels <arnold@jasny.net> | 2017-01-25 01:50:31 +0100 |
---|---|---|
committer | Arnold Daniels <arnold@jasny.net> | 2017-01-25 02:27:01 +0100 |
commit | b7f9fab71b4c876ba0e3cd0fa4e4f5f9c03d2fcb (patch) | |
tree | cbf6bc6be07047549b510cad2a4bb13694fab8e8 | |
parent | 67ff2b6acfd1923faf577b828e7e3d3ba9007d15 (diff) | |
download | error-handler-b7f9fab71b4c876ba0e3cd0fa4e4f5f9c03d2fcb.zip error-handler-b7f9fab71b4c876ba0e3cd0fa4e4f5f9c03d2fcb.tar.gz error-handler-b7f9fab71b4c876ba0e3cd0fa4e4f5f9c03d2fcb.tar.bz2 |
Check if response implements `Jasny\GlobalEnvironmentInterface` rather than an implementation.
-rw-r--r-- | .travis.yml | 1 | ||||
-rw-r--r-- | composer.json | 3 | ||||
-rw-r--r-- | src/ErrorHandler/Middleware.php | 8 | ||||
-rw-r--r-- | tests/ErrorHandler/MiddlewareTest.php | 43 |
4 files changed, 45 insertions, 10 deletions
diff --git a/.travis.yml b/.travis.yml index 46cfdc3..04a1d32 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ language: php php: - 5.6 - 7.0 + - 7.1 branches: only: diff --git a/composer.json b/composer.json index 1e1e412..c1c0086 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "psr/log": "^1.0" }, "require-dev": { - "jasny/php-code-quality": "^2.0" + "jasny/php-code-quality": "^2.0", + "jasny/http-message": "^1.3" }, "autoload": { "psr-4": { diff --git a/src/ErrorHandler/Middleware.php b/src/ErrorHandler/Middleware.php index 7288b56..d96925a 100644 --- a/src/ErrorHandler/Middleware.php +++ b/src/ErrorHandler/Middleware.php @@ -5,7 +5,7 @@ namespace Jasny\ErrorHandler; use Jasny\ErrorHandler; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; -use Jasny\HttpMessage\Response as JasnyResponse; +use Jasny\HttpMessage\GlobalEnvironmentInterface; /** * Use error handler as middleware @@ -69,7 +69,11 @@ class Middleware */ protected function errorResponse(ServerRequestInterface $request, ResponseInterface $response) { - if (class_exists(JasnyResponse::class) && $response instanceof JasnyResponse && $response->isStale()) { + if ( + interface_exists(GlobalEnvironmentInterface::class, false) && + $response instanceof GlobalEnvironmentInterface && + $response->isStale() + ) { $response = $response->revive(); } diff --git a/tests/ErrorHandler/MiddlewareTest.php b/tests/ErrorHandler/MiddlewareTest.php index 8e62a1f..414d817 100644 --- a/tests/ErrorHandler/MiddlewareTest.php +++ b/tests/ErrorHandler/MiddlewareTest.php @@ -4,21 +4,22 @@ namespace Jasny\ErrorHandler; use Jasny\ErrorHandler; use Jasny\ErrorHandler\Middleware; - +use Jasny\HttpMessage\Response as Response; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; - use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; - use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Jasny\TestHelper; /** * @covers Jasny\ErrorHandler\Middleware */ class MiddlewareTest extends \PHPUnit_Framework_TestCase { + use TestHelper; + /** * @var ErrorHandler|MockObject */ @@ -126,7 +127,7 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase $errorResponse = $this->createMock(ResponseInterface::class); $stream = $this->createMock(StreamInterface::class); - $stream->expects($this->once())->method('write')->with('Unexpected error'); + $stream->expects($this->once())->method('write')->with('An unexpected error occured'); $request->expects($this->once())->method('getProtocolVersion')->willReturn('1.1'); @@ -153,15 +154,43 @@ class MiddlewareTest extends \PHPUnit_Framework_TestCase $this->assertEquals("Call to undefined function this_function_does_not_exist()", $error->getMessage()); } + public function testInvokeRevive() + { + $request = $this->createMock(ServerRequestInterface::class); + $response = $this->createMock(Response::class); + $revivedResponse = $this->createMock(Response::class); + $stream = $this->createMock(StreamInterface::class); + + $exception = $this->createMock(\Exception::class); + + $response->method('isStale')->willReturn(true); + $response->expects($this->once())->method('revive')->willReturn($revivedResponse); + $response->expects($this->never())->method('withProtocolVersion'); + + $revivedResponse->expects($this->once())->method('withProtocolVersion')->willReturnSelf(); + $revivedResponse->expects($this->once())->method('withStatus')->willReturnSelf(); + $revivedResponse->expects($this->once())->method('getBody')->willReturn($stream); + + $next = $this->createCallbackMock($this->once(), function ($method) use ($request, $response, $exception) { + $method->with($request, $response)->willThrowException($exception); + }); + + $middleware = $this->middleware; + + $result = $middleware($request, $response, $next); + + $this->assertSame($revivedResponse, $result); + } + public function testInvokeLog() { $request = $this->createMock(ServerRequestInterface::class); $response = $this->createMock(ResponseInterface::class); $stream = $this->createMock(StreamInterface::class); - $response->method('withProtocolVersion')->willReturnSelf(); - $response->method('withStatus')->willReturnSelf(); - $response->method('getBody')->willReturn($stream); + $response->method('withProtocolVersion')->id('withProtocolVersion')->willReturnSelf(); + $response->method('withStatus')->id('withStatus')->after('withProtocolVersion')->willReturnSelf(); + $response->method('getBody')->after('withStatus')->willReturn($stream); $exception = $this->createMock(\Exception::class); |