Repository navigation
Expand file tree
/
Copy pathHandlerTest.php
More file actions
204 lines (153 loc) · 6.65 KB
/
Copy pathHandlerTest.php
File metadata and controls
204 lines (153 loc) · 6.65 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
<?php
namespace Dingo\Api\Tests\Exception;
use Mockery as m;
use RuntimeException;
use Illuminate\Http\Response;
use PHPUnit\Framework\TestCase;
use Dingo\Api\Exception\Handler;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Dingo\Api\Http\Request as ApiRequest;
use Dingo\Api\Exception\ResourceException;
use Symfony\Component\HttpKernel\Exception\HttpException;
class HandlerTest extends TestCase
{
protected $parentHandler;
protected $exceptionHandler;
public function setUp()
{
$this->parentHandler = m::mock('Illuminate\Contracts\Debug\ExceptionHandler');
$this->exceptionHandler = new Handler($this->parentHandler, [
'message' => ':message',
'errors' => ':errors',
'code' => ':code',
'status_code' => ':status_code',
'debug' => ':debug',
], false);
}
public function tearDown()
{
m::close();
}
public function testRegisterExceptionHandler()
{
$this->exceptionHandler->register(function (HttpException $e) {
//
});
$this->assertArrayHasKey(\Symfony\Component\HttpKernel\Exception\HttpException::class, $this->exceptionHandler->getHandlers());
}
public function testExceptionHandlerHandlesException()
{
$this->exceptionHandler->register(function (HttpException $e) {
return new Response('foo', 404);
});
$exception = new HttpException(404, 'bar');
$response = $this->exceptionHandler->handle($exception);
$this->assertSame('foo', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
public function testExceptionHandlerHandlesExceptionAndCreatesNewResponse()
{
$this->exceptionHandler->register(function (HttpException $e) {
return 'foo';
});
$exception = new HttpException(404, 'bar');
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('foo', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
public function testExceptionHandlerHandlesExceptionWithRedirectResponse()
{
$this->exceptionHandler->register(function (HttpException $e) {
return new RedirectResponse('foo');
});
$exception = new HttpException(404, 'bar');
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('foo', $response->getTargetUrl());
$this->assertSame(302, $response->getStatusCode());
}
public function testExceptionHandlerHandlesExceptionWithJsonResponse()
{
$this->exceptionHandler->register(function (HttpException $e) {
return new JsonResponse(['foo' => 'bar'], 404);
});
$exception = new HttpException(404, 'bar');
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf(JsonResponse::class, $response);
$this->assertSame('{"foo":"bar"}', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
public function testExceptionHandlerReturnsGenericWhenNoMatchingHandler()
{
$exception = new HttpException(404, 'bar');
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('{"message":"bar","status_code":404}', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
public function testUsingMultidimensionalArrayForGenericResponse()
{
$this->exceptionHandler->setErrorFormat([
'error' => [
'message' => ':message',
'errors' => ':errors',
'code' => ':code',
'status_code' => ':status_code',
'debug' => ':debug',
],
]);
$exception = new HttpException(404, 'bar');
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('{"error":{"message":"bar","status_code":404}}', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
public function testRegularExceptionsAreHandledByGenericHandler()
{
$exception = new RuntimeException('Uh oh');
$response = $this->exceptionHandler->handle($exception);
$this->assertSame('{"message":"Uh oh","status_code":500}', $response->getContent());
$this->assertSame(500, $response->getStatusCode());
}
public function testResourceExceptionErrorsAreIncludedInResponse()
{
$exception = new ResourceException('bar', ['foo' => 'bar'], null, [], 10);
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('{"message":"bar","errors":{"foo":["bar"]},"code":10,"status_code":422}', $response->getContent());
$this->assertSame(422, $response->getStatusCode());
}
public function testExceptionTraceIncludedInResponse()
{
$this->exceptionHandler->setDebug(true);
$exception = new HttpException(404, 'bar');
$response = $this->exceptionHandler->handle($exception);
$object = json_decode($response->getContent());
$this->assertObjectHasAttribute('debug', $object);
}
public function testHttpExceptionsWithNoMessageUseStatusCodeMessage()
{
$exception = new HttpException(404);
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf(Response::class, $response);
$this->assertSame('{"message":"404 Not Found","status_code":404}', $response->getContent());
$this->assertSame(404, $response->getStatusCode());
}
public function testExceptionsHandledByRenderAreReroutedThroughHandler()
{
$request = ApiRequest::create('foo', 'GET');
$exception = new HttpException(404);
$response = $this->exceptionHandler->render($request, $exception);
$this->assertSame('{"message":"404 Not Found","status_code":404}', $response->getContent());
}
public function testSettingUserDefinedReplacements()
{
$this->exceptionHandler->setReplacements([':foo' => 'bar']);
$this->exceptionHandler->setErrorFormat(['bing' => ':foo']);
$exception = new HttpException(404);
$response = $this->exceptionHandler->handle($exception);
$this->assertSame('{"bing":"bar"}', $response->getContent());
}
}