|
5 | 5 | namespace Phauthentic\ErrorResponse\Tests; |
6 | 6 |
|
7 | 7 | use Exception; |
8 | | -use Phauthentic\ErrorResponse\ErrorResponseExceptionBasedFactoryInterface; |
| 8 | +use Nyholm\Psr7\Factory\Psr17Factory; |
| 9 | +use Phauthentic\ErrorResponse\ErrorResponseFactory; |
9 | 10 | use Phauthentic\ErrorResponse\ErrorResponseMiddleware; |
10 | 11 | use PHPUnit\Framework\TestCase; |
11 | | -use Psr\Http\Message\ResponseFactoryInterface; |
12 | 12 | use Psr\Http\Message\ServerRequestInterface; |
13 | 13 | use Psr\Http\Server\RequestHandlerInterface; |
14 | 14 |
|
15 | 15 | class ErrorResponseMiddlewareTest extends TestCase |
16 | 16 | { |
| 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 | + |
17 | 43 | public function testProcessHandlesUnhandledException(): void |
18 | 44 | { |
19 | 45 | $this->expectException(Exception::class); |
20 | 46 | $this->expectExceptionMessage('Unhandled Exception'); |
21 | 47 |
|
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 | + ); |
26 | 53 |
|
27 | 54 | $request = $this->createMock(ServerRequestInterface::class); |
28 | 55 | $handler = $this->createMock(RequestHandlerInterface::class); |
29 | 56 |
|
30 | 57 | $unhandledException = new Exception('Unhandled Exception'); |
31 | 58 |
|
32 | | - $responseFactory->expects($this->never()) |
33 | | - ->method('createResponse'); |
34 | | - |
35 | | - $errorResponseFactory->expects($this->never()) |
36 | | - ->method('createErrorResponseFromException'); |
37 | | - |
38 | 59 | $handler->expects($this->once()) |
39 | 60 | ->method('handle') |
40 | 61 | ->with($request) |
|
0 commit comments