1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
<?php
namespace Engelsystem\Test\Unit\Middleware;
use Engelsystem\Http\Exceptions\HttpException;
use Engelsystem\Http\Response;
use Engelsystem\Middleware\ErrorHandler;
use Engelsystem\Test\Unit\Middleware\Stub\ReturnResponseMiddlewareHandler;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Twig_LoaderInterface as TwigLoader;
class ErrorHandlerTest extends TestCase
{
/**
* @covers \Engelsystem\Middleware\ErrorHandler::__construct
* @covers \Engelsystem\Middleware\ErrorHandler::process
* @covers \Engelsystem\Middleware\ErrorHandler::selectView
*/
public function testProcess()
{
/** @var TwigLoader|MockObject $twigLoader */
$twigLoader = $this->createMock(TwigLoader::class);
/** @var ServerRequestInterface|MockObject $request */
$request = $this->createMock(ServerRequestInterface::class);
/** @var ResponseInterface|MockObject $psrResponse */
$psrResponse = $this->getMockForAbstractClass(ResponseInterface::class);
$returnResponseHandler = new ReturnResponseMiddlewareHandler($psrResponse);
$psrResponse->expects($this->once())
->method('getStatusCode')
->willReturn(505);
$errorHandler = new ErrorHandler($twigLoader);
$return = $errorHandler->process($request, $returnResponseHandler);
$this->assertEquals($psrResponse, $return, 'Plain PSR-7 Response should be passed directly');
/** @var Response|MockObject $response */
$response = $this->createMock(Response::class);
$response->expects($this->exactly(3))
->method('getStatusCode')
->willReturnOnConsecutiveCalls(
200,
418,
505
);
$returnResponseHandler->setResponse($response);
$return = $errorHandler->process($request, $returnResponseHandler);
$this->assertEquals($response, $return, 'Only Responses >= 400 should be processed');
$twigLoader->expects($this->exactly(4))
->method('exists')
->withConsecutive(
['errors/418'],
['errors/4'],
['errors/400'],
['errors/505']
)
->willReturnOnConsecutiveCalls(
false,
false,
false,
true
);
$response->expects($this->exactly(2))
->method('getContent')
->willReturnOnConsecutiveCalls(
'Teapot',
'Internal Error!'
);
$response->expects($this->exactly(2))
->method('withView')
->withConsecutive(
['errors/default', ['status' => 418, 'content' => 'Teapot'], 418],
['errors/505', ['status' => 505, 'content' => 'Internal Error!'], 505]
)
->willReturn($response);
$errorHandler->process($request, $returnResponseHandler);
$errorHandler->process($request, $returnResponseHandler);
}
/**
* @covers \Engelsystem\Middleware\ErrorHandler::process
*/
public function testProcessException()
{
/** @var ServerRequestInterface|MockObject $request */
$request = $this->createMock(ServerRequestInterface::class);
/** @var ResponseInterface|MockObject $psrResponse */
$psrResponse = $this->getMockForAbstractClass(ResponseInterface::class);
/** @var ReturnResponseMiddlewareHandler|MockObject $returnResponseHandler */
$returnResponseHandler = $this->getMockBuilder(ReturnResponseMiddlewareHandler::class)
->disableOriginalConstructor()
->getMock();
$psrResponse->expects($this->once())
->method('getStatusCode')
->willReturn(300);
$returnResponseHandler->expects($this->once())
->method('handle')
->willReturnCallback(function () {
throw new HttpException(300, 'Some response', ['lor' => 'em']);
});
/** @var ErrorHandler|MockObject $errorHandler */
$errorHandler = $this->getMockBuilder(ErrorHandler::class)
->disableOriginalConstructor()
->setMethods(['createResponse'])
->getMock();
$errorHandler->expects($this->once())
->method('createResponse')
->with('Some response', 300, ['lor' => 'em'])
->willReturn($psrResponse);
$return = $errorHandler->process($request, $returnResponseHandler);
$this->assertEquals($psrResponse, $return);
}
}
|