Skip to content

Commit e945ce5

Browse files
Adding tests
1 parent a9b7b2d commit e945ce5

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/ErrorResponseMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected function isAnInterceptableException(Exception $exception): bool
5454

5555
protected function errorResponseToJson(ErrorResponseInterface $errorResponse): string
5656
{
57-
return json_encode($errorResponse, JSON_THROW_ON_ERROR);
57+
return json_encode($errorResponse->toArray(), JSON_THROW_ON_ERROR);
5858
}
5959

6060
public function createResponse(ErrorResponseInterface $errorResponse): ResponseInterface

tests/ErrorResponseMiddlewareTest.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,57 @@
55
namespace Phauthentic\ErrorResponse\Tests;
66

77
use Exception;
8-
use Phauthentic\ErrorResponse\ErrorResponseExceptionBasedFactoryInterface;
8+
use Nyholm\Psr7\Factory\Psr17Factory;
9+
use Phauthentic\ErrorResponse\ErrorResponseFactory;
910
use Phauthentic\ErrorResponse\ErrorResponseMiddleware;
1011
use PHPUnit\Framework\TestCase;
11-
use Psr\Http\Message\ResponseFactoryInterface;
1212
use Psr\Http\Message\ServerRequestInterface;
1313
use Psr\Http\Server\RequestHandlerInterface;
1414

1515
class ErrorResponseMiddlewareTest extends TestCase
1616
{
17+
public function testProcessErrorResponse(): void
18+
{
19+
$middleware = new ErrorResponseMiddleware(
20+
new Psr17Factory(),
21+
new ErrorResponseFactory(),
22+
[Exception::class]
23+
);
24+
25+
$request = $this->createMock(ServerRequestInterface::class);
26+
$handler = $this->createMock(RequestHandlerInterface::class);
27+
28+
$handler->expects($this->once())
29+
->method('handle')
30+
->with($request)
31+
->willThrowException(new Exception('Handled Exception'));
32+
33+
$response = $middleware->process($request, $handler);
34+
35+
$this->assertStringContainsString('Handled Exception', (string)$response->getBody());
36+
$this->assertSame(
37+
'{"type":"about:blank","status":500,"title":"Handled Exception","detail":null,"instance":null}',
38+
(string)$response->getBody()
39+
);
40+
$this->assertSame(500, $response->getStatusCode());
41+
}
42+
1743
public function testProcessHandlesUnhandledException(): void
1844
{
1945
$this->expectException(Exception::class);
2046
$this->expectExceptionMessage('Unhandled Exception');
2147

22-
$responseFactory = $this->createMock(ResponseFactoryInterface::class);
23-
$errorResponseFactory = $this->createMock(ErrorResponseExceptionBasedFactoryInterface::class);
24-
25-
$middleware = new ErrorResponseMiddleware($responseFactory, $errorResponseFactory, [CustomException::class]);
48+
$middleware = new ErrorResponseMiddleware(
49+
new Psr17Factory(),
50+
new ErrorResponseFactory(),
51+
[CustomException::class]
52+
);
2653

2754
$request = $this->createMock(ServerRequestInterface::class);
2855
$handler = $this->createMock(RequestHandlerInterface::class);
2956

3057
$unhandledException = new Exception('Unhandled Exception');
3158

32-
$responseFactory->expects($this->never())
33-
->method('createResponse');
34-
35-
$errorResponseFactory->expects($this->never())
36-
->method('createErrorResponseFromException');
37-
3859
$handler->expects($this->once())
3960
->method('handle')
4061
->with($request)

0 commit comments

Comments
 (0)