Repository navigation
Expand file tree
/
Copy pathHandlerTest.php
More file actions
169 lines (127 loc) · 5.55 KB
/
Copy pathHandlerTest.php
File metadata and controls
169 lines (127 loc) · 5.55 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
<?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 Dingo\Api\Http\Request as ApiRequest;
use Dingo\Api\Exception\ResourceException;
use Symfony\Component\HttpKernel\Exception\HttpException;
class HandlerTest extends PHPUnit_Framework_TestCase
{
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', $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->assertEquals('foo', $response->getContent());
$this->assertEquals(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('Illuminate\Http\Response', $response);
$this->assertEquals('foo', $response->getContent());
$this->assertEquals(404, $response->getStatusCode());
}
public function testExceptionHandlerReturnsGenericWhenNoMatchingHandler()
{
$exception = new HttpException(404, 'bar');
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf('Illuminate\Http\Response', $response);
$this->assertEquals('{"message":"bar","status_code":404}', $response->getContent());
$this->assertEquals(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('Illuminate\Http\Response', $response);
$this->assertEquals('{"error":{"message":"bar","status_code":404}}', $response->getContent());
$this->assertEquals(404, $response->getStatusCode());
}
public function testRegularExceptionsAreHandledByGenericHandler()
{
$exception = new RuntimeException('Uh oh');
$response = $this->exceptionHandler->handle($exception);
$this->assertEquals('{"message":"Uh oh","status_code":500}', $response->getContent());
$this->assertEquals(500, $response->getStatusCode());
}
public function testResourceExceptionErrorsAreIncludedInResponse()
{
$exception = new ResourceException('bar', ['foo' => 'bar'], null, [], 10);
$response = $this->exceptionHandler->handle($exception);
$this->assertInstanceOf('Illuminate\Http\Response', $response);
$this->assertEquals('{"message":"bar","errors":{"foo":["bar"]},"code":10,"status_code":422}', $response->getContent());
$this->assertEquals(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('Illuminate\Http\Response', $response);
$this->assertEquals('{"message":"404 Not Found","status_code":404}', $response->getContent());
$this->assertEquals(404, $response->getStatusCode());
}
public function testExceptionsHandledByRenderAreReroutedThroughHandler()
{
$request = ApiRequest::create('foo', 'GET');
$exception = new HttpException(404);
$response = $this->exceptionHandler->render($request, $exception);
$this->assertEquals('{"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->assertEquals('{"bing":"bar"}', $response->getContent());
}
}