|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VM\ErrorHandler\Tests\Middlewares; |
|
6
|
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
|
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
9
|
|
|
use TypeError; |
|
10
|
|
|
use VM\ErrorHandler\Exceptions\Http\Forbidden; |
|
11
|
|
|
use VM\ErrorHandler\Interfaces\ErrorHandler; |
|
12
|
|
|
use VM\ErrorHandler\Middlewares\ErrorProcessor; |
|
13
|
|
|
use VM\Psr15Mocks\Middleware; |
|
14
|
|
|
|
|
15
|
|
|
class ErrorProcessorTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
use Middleware; |
|
18
|
|
|
|
|
19
|
|
|
private $errorHandler; |
|
20
|
|
|
|
|
21
|
|
|
public function setUp(): void |
|
22
|
|
|
{ |
|
23
|
|
|
$this->buildRequest(); |
|
24
|
|
|
$this->buildResponse(); |
|
25
|
|
|
$this->buildRequestHandler(); |
|
26
|
|
|
$this->buildErrorHandler(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function tearDown(): void |
|
30
|
|
|
{ |
|
31
|
|
|
$this->destroyErrorHandler(); |
|
32
|
|
|
$this->destroyRequestHandler(); |
|
33
|
|
|
$this->destroyResponse(); |
|
34
|
|
|
$this->destroyRequest(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testItShouldDoNothingWhenThereWasNoError(): void |
|
38
|
|
|
{ |
|
39
|
|
|
$this->requestHandler->expects($this->once())->method('handle')->with($this->request)->willReturn($this->response); |
|
40
|
|
|
$this->errorHandler->expects($this->never())->method('handle'); |
|
41
|
|
|
$this->errorHandler->expects($this->never())->method('handleHttpException'); |
|
42
|
|
|
|
|
43
|
|
|
$middleware = new ErrorProcessor($this->errorHandler); |
|
44
|
|
|
$response = $middleware->process($this->request, $this->requestHandler); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function testItShouldCallUponItsErrorHandlerForHttpExceptions(): void |
|
50
|
|
|
{ |
|
51
|
|
|
$httpException = new Forbidden(); |
|
52
|
|
|
$this->requestHandler->expects($this->once())->method('handle')->with($this->request)->willThrowException($httpException); |
|
53
|
|
|
$this->errorHandler->expects($this->never())->method('handle'); |
|
54
|
|
|
$this->errorHandler->expects($this->once())->method('handleHttpException')->with($httpException)->willReturn($this->response); |
|
55
|
|
|
|
|
56
|
|
|
$middleware = new ErrorProcessor($this->errorHandler); |
|
57
|
|
|
$response = $middleware->process($this->request, $this->requestHandler); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function testItShouldCallUponItsErrorHandlerForErrors(): void |
|
63
|
|
|
{ |
|
64
|
|
|
$exception = new TypeError(); |
|
65
|
|
|
$this->requestHandler->expects($this->once())->method('handle')->with($this->request)->willThrowException($exception); |
|
66
|
|
|
$this->errorHandler->expects($this->never())->method('handleHttpException'); |
|
67
|
|
|
$this->errorHandler->expects($this->once())->method('handle')->with($exception)->willReturn($this->response); |
|
68
|
|
|
|
|
69
|
|
|
$middleware = new ErrorProcessor($this->errorHandler); |
|
70
|
|
|
$response = $middleware->process($this->request, $this->requestHandler); |
|
71
|
|
|
|
|
72
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function buildErrorHandler(): void |
|
76
|
|
|
{ |
|
77
|
|
|
$this->errorHandler = $this->getMockBuilder(ErrorHandler::class) |
|
78
|
|
|
->disableOriginalConstructor() |
|
79
|
|
|
->setMethods(['handle', 'handleHttpException']) |
|
80
|
|
|
->getMock() |
|
81
|
|
|
; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function destroyErrorHandler(): void |
|
85
|
|
|
{ |
|
86
|
|
|
$this->errorHandler = null; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|