-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathExceptionHandlerTest.php
More file actions
244 lines (199 loc) · 8.62 KB
/
ExceptionHandlerTest.php
File metadata and controls
244 lines (199 loc) · 8.62 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace Themosis\Tests\Core;
use Illuminate\Config\Repository;
use Illuminate\Container\Container;
use Illuminate\Contracts\Support\Responsable;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Routing\ResponseFactory;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\ValidationException;
use Illuminate\Validation\Validator;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Themosis\Core\Exceptions\Handler;
class ExceptionHandlerTest extends TestCase
{
/**
* @var Container
*/
protected $container;
/**
* @var Handler
*/
protected $handler;
protected $request;
/**
* @var Repository
*/
protected $config;
public function setUp(): void
{
$this->container = Container::setInstance(new Container());
$this->request = $this->getMockBuilder('stdClass')
->setMethods(['expectsJson'])
->getMock();
$this->config = $config = $this->getMockBuilder(Repository::class)
->setMethods(['get'])
->getMock();
$this->container->singleton('config', function () use ($config) {
return $config;
});
$viewFactory = $this->getMockBuilder(Factory::class)
->disableOriginalConstructor()
->getMock();
$redirector = $this->getMockBuilder(Redirector::class)
->disableOriginalConstructor()
->getMock();
$this->container->singleton(
'Illuminate\Contracts\Routing\ResponseFactory',
function () use ($viewFactory, $redirector) {
return new ResponseFactory(
$viewFactory,
$redirector,
);
},
);
$this->handler = new Handler($this->container);
}
public function testHandlerReportExceptionAsContext()
{
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$this->container->instance(LoggerInterface::class, $logger);
$exception = new \RuntimeException('Exception message');
$logger->expects($this->once())
->method('error')
->with(
$this->equalTo('Exception message'),
[
'exception' => $exception,
],
);
$this->handler->report($exception);
}
public function testReturnsJsonWithStackTraceWhenAjaxRequestAndDebugTrue()
{
$this->config->expects($this->once())
->method('get')
->with('app.debug', $this->equalTo(null))
->will($this->returnValue(true));
$this->request->expects($this->once())->method('expectsJson')->will($this->returnValue(true));
$response = $this->handler->render(
$this->request,
new \Exception('Custom error message'),
)->getContent();
$this->assertFalse(strpos($response, '<!DOCTYPE html>'));
$this->assertTrue(false !== strpos($response, '"message": "Custom error message"'));
$this->assertTrue(false !== strpos($response, '"file":'));
$this->assertTrue(false !== strpos($response, '"line":'));
$this->assertTrue(false !== strpos($response, '"trace":'));
}
public function testReturnsCustomResponseWhenExceptionImplementResponsable()
{
$response = $this->handler->render($this->request, new CustomException())->getContent();
$this->assertSame('{"response":"Custom exception response"}', $response);
}
public function testReturnsJsonWithoutStackTraceWhenAjaxRequestAndDebugFalseAndExceptionMessageIsMasked()
{
$this->config->expects($this->once())
->method('get')
->with('app.debug', $this->equalTo(null))
->will($this->returnValue(false));
$this->request->expects($this->once())->method('expectsJson')->will($this->returnValue(true));
$response = $this->handler->render(
$this->request,
new \Exception('This error message should not be visible'),
)->getContent();
$this->assertTrue(false !== strpos($response, '"message": "Server Error"'));
$this->assertFalse(strpos($response, '<!DOCTYPE html>'));
$this->assertFalse(strpos($response, 'This error message should not be visible'));
$this->assertFalse(strpos($response, '"file":'));
$this->assertFalse(strpos($response, '"line":'));
$this->assertFalse(strpos($response, '"trace":'));
}
public function testReturnsJsonWithoutStackTraceWhenAjaxRequestAndDebugFalseAndHttpExceptionIsShown()
{
$this->config->expects($this->once())
->method('get')
->with('app.debug', $this->equalTo(null))
->will($this->returnValue(false));
$this->request->expects($this->once())->method('expectsJson')->will($this->returnValue(true));
$response = $this->handler->render(
$this->request,
new HttpException(
403,
'Custom error message',
),
)->getContent();
$this->assertTrue(false !== strpos($response, '"message": "Custom error message"'));
$this->assertFalse(strpos($response, '<!DOCTYPE html>'));
$this->assertFalse(strpos($response, '"message": "Server Error"'));
$this->assertFalse(strpos($response, '"file":'));
$this->assertFalse(strpos($response, '"line":'));
$this->assertFalse(strpos($response, '"trace":'));
}
public function testReturnsJsonWithoutStackTraceWhenAjaxRequestAndDebugFalseAndAccessDeniedHttpExceptionErrorIsShown()
{
$this->config->expects($this->once())
->method('get')
->with('app.debug', $this->equalTo(null))
->will($this->returnValue(false));
$this->request->expects($this->once())->method('expectsJson')->will($this->returnValue(true));
$response = $this->handler->render(
$this->request,
new AccessDeniedHttpException('Custom error message'),
)->getContent();
$this->assertTrue(false !== strpos($response, '"message": "Custom error message"'));
$this->assertFalse(strpos($response, '<!DOCTYPE html>'));
$this->assertFalse(strpos($response, '"message": "Server Error"'));
$this->assertFalse(strpos($response, '"file":'));
$this->assertFalse(strpos($response, '"line":'));
$this->assertFalse(strpos($response, '"trace":'));
}
public function testValidateFileMethod()
{
$argumentExpected = ['input' => 'My input value'];
$argumentActual = null;
$this->container->singleton('redirect', function () use (&$argumentActual) {
$redirector = $this->createMock(Redirector::class);
$redirector->expects($this->once())
->method('to')
->willReturn($responser = $this->createMock(RedirectResponse::class));
$responser->expects($this->once())
->method('withInput')
->with($this->callback(function ($argument) use (&$argumentActual) {
$argumentActual = $argument;
return true;
}))
->willReturn($responser);
$responser->expects($this->once())
->method('withErrors')
->willReturn($responser);
return $redirector;
});
$file = $this->createMock(UploadedFile::class);
$file->method('getPathname')->willReturn('photo.jpg');
$file->method('getClientOriginalName')->willReturn('photo.jpg');
$file->method('getClientMimeType')->willReturn('');
$file->method('getError')->willReturn(UPLOAD_ERR_OK);
$request = Request::create('/', 'POST', $argumentExpected, [], ['photo' => $file]);
$validator = $this->createMock(Validator::class);
$validator->method('errors')->willReturn(new MessageBag(['error' => 'My custom validation exception']));
$validationException = new ValidationException($validator);
$validationException->redirectTo = '/';
$this->handler->render($request, $validationException);
$this->assertEquals($argumentExpected, $argumentActual);
}
}
class CustomException extends \Exception implements Responsable
{
public function toResponse($request)
{
return response()->json(['response' => 'Custom exception response']);
}
}