-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
AbstractErrorRendererTest.php
263 lines (201 loc) · 9.25 KB
/
AbstractErrorRendererTest.php
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim\Tests\Error;
use Exception;
use ReflectionClass;
use RuntimeException;
use Slim\Error\Renderers\HtmlErrorRenderer;
use Slim\Error\Renderers\JsonErrorRenderer;
use Slim\Error\Renderers\PlainTextErrorRenderer;
use Slim\Error\Renderers\XmlErrorRenderer;
use Slim\Exception\HttpException;
use Slim\Tests\TestCase;
use stdClass;
use function json_decode;
use function json_encode;
use function simplexml_load_string;
class AbstractErrorRendererTest extends TestCase
{
public function testHTMLErrorRendererDisplaysErrorDetails()
{
$exception = new RuntimeException('Oops..');
$renderer = new HtmlErrorRenderer();
$output = $renderer->__invoke($exception, true);
$this->assertMatchesRegularExpression(
'/.*The application could not run because of the following error:.*/',
$output
);
$this->assertStringContainsString('Oops..', $output);
}
public function testHTMLErrorRendererNoErrorDetails()
{
$exception = new RuntimeException('Oops..');
$renderer = new HtmlErrorRenderer();
$output = $renderer->__invoke($exception, false);
$this->assertMatchesRegularExpression(
'/.*A website error has occurred. Sorry for the temporary inconvenience.*/',
$output
);
$this->assertStringNotContainsString('Oops..', $output);
}
public function testHTMLErrorRendererRenderFragmentMethod()
{
$exception = new Exception('Oops..', 500);
$renderer = new HtmlErrorRenderer();
$reflectionRenderer = new ReflectionClass(HtmlErrorRenderer::class);
$method = $reflectionRenderer->getMethod('renderExceptionFragment');
$method->setAccessible(true);
$output = $method->invoke($renderer, $exception);
$this->assertMatchesRegularExpression('/.*Type:*/', $output);
$this->assertMatchesRegularExpression('/.*Code:*/', $output);
$this->assertMatchesRegularExpression('/.*Message*/', $output);
$this->assertMatchesRegularExpression('/.*File*/', $output);
$this->assertMatchesRegularExpression('/.*Line*/', $output);
}
public function testHTMLErrorRendererRenderHttpException()
{
$exceptionTitle = 'title';
$exceptionDescription = 'description';
$httpExceptionProphecy = $this->prophesize(HttpException::class);
$httpExceptionProphecy
->getTitle()
->willReturn($exceptionTitle)
->shouldBeCalledOnce();
$httpExceptionProphecy
->getDescription()
->willReturn($exceptionDescription)
->shouldBeCalledOnce();
$renderer = new HtmlErrorRenderer();
$output = $renderer->__invoke($httpExceptionProphecy->reveal(), false);
$this->assertStringContainsString($exceptionTitle, $output, 'Should contain http exception title');
$this->assertStringContainsString($exceptionDescription, $output, 'Should contain http exception description');
}
public function testJSONErrorRendererDisplaysErrorDetails()
{
$exception = new Exception('Oops..');
$renderer = new JsonErrorRenderer();
$reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class);
$method = $reflectionRenderer->getMethod('formatExceptionFragment');
$method->setAccessible(true);
$fragment = $method->invoke($renderer, $exception);
$output = json_encode(json_decode($renderer->__invoke($exception, true)));
$expectedString = json_encode(['message' => 'Slim Application Error', 'exception' => [$fragment]]);
$this->assertSame($output, $expectedString);
}
public function testJSONErrorRendererDoesNotDisplayErrorDetails()
{
$exception = new Exception('Oops..');
$renderer = new JsonErrorRenderer();
$output = json_encode(json_decode($renderer->__invoke($exception, false)));
$this->assertSame($output, json_encode(['message' => 'Slim Application Error']));
}
public function testJSONErrorRendererDisplaysPreviousError()
{
$previousException = new Exception('Oh no!');
$exception = new Exception('Oops..', 0, $previousException);
$renderer = new JsonErrorRenderer();
$reflectionRenderer = new ReflectionClass(JsonErrorRenderer::class);
$method = $reflectionRenderer->getMethod('formatExceptionFragment');
$method->setAccessible(true);
$output = json_encode(json_decode($renderer->__invoke($exception, true)));
$fragments = [
$method->invoke($renderer, $exception),
$method->invoke($renderer, $previousException),
];
$expectedString = json_encode(['message' => 'Slim Application Error', 'exception' => $fragments]);
$this->assertSame($output, $expectedString);
}
public function testJSONErrorRendererRenderHttpException()
{
$exceptionTitle = 'title';
$httpExceptionProphecy = $this->prophesize(HttpException::class);
$httpExceptionProphecy
->getTitle()
->willReturn($exceptionTitle)
->shouldBeCalledOnce();
$renderer = new JsonErrorRenderer();
$output = json_encode(json_decode($renderer->__invoke($httpExceptionProphecy->reveal(), false)));
$this->assertSame(
$output,
json_encode(['message' => $exceptionTitle]),
'Should contain http exception title'
);
}
public function testXMLErrorRendererDisplaysErrorDetails()
{
$previousException = new RuntimeException('Oops..');
$exception = new Exception('Ooops...', 0, $previousException);
$renderer = new XmlErrorRenderer();
/** @var stdClass $output */
$output = simplexml_load_string($renderer->__invoke($exception, true));
$this->assertSame((string) $output->message[0], 'Slim Application Error');
$this->assertSame((string) $output->exception[0]->type, 'Exception');
$this->assertSame((string) $output->exception[0]->message, 'Ooops...');
$this->assertSame((string) $output->exception[1]->type, 'RuntimeException');
$this->assertSame((string) $output->exception[1]->message, 'Oops..');
}
public function testXMLErrorRendererRenderHttpException()
{
$exceptionTitle = 'title';
$httpExceptionProphecy = $this->prophesize(HttpException::class);
$httpExceptionProphecy
->getTitle()
->willReturn($exceptionTitle)
->shouldBeCalledOnce();
$renderer = new XmlErrorRenderer();
/** @var stdClass $output */
$output = simplexml_load_string($renderer->__invoke($httpExceptionProphecy->reveal(), true));
$this->assertSame((string) $output->message[0], $exceptionTitle, 'Should contain http exception title');
}
public function testPlainTextErrorRendererFormatFragmentMethod()
{
$message = 'Oops.. <br>';
$exception = new Exception($message, 500);
$renderer = new PlainTextErrorRenderer();
$reflectionRenderer = new ReflectionClass(PlainTextErrorRenderer::class);
$method = $reflectionRenderer->getMethod('formatExceptionFragment');
$method->setAccessible(true);
$output = $method->invoke($renderer, $exception);
$this->assertIsString($output);
$this->assertMatchesRegularExpression('/.*Type:*/', $output);
$this->assertMatchesRegularExpression('/.*Code:*/', $output);
$this->assertMatchesRegularExpression('/.*Message*/', $output);
$this->assertMatchesRegularExpression('/.*File*/', $output);
$this->assertMatchesRegularExpression('/.*Line*/', $output);
// ensure the renderer doesn't reformat the message
$this->assertMatchesRegularExpression("/.*$message/", $output);
}
public function testPlainTextErrorRendererDisplaysErrorDetails()
{
$previousException = new RuntimeException('Oops..');
$exception = new Exception('Ooops...', 0, $previousException);
$renderer = new PlainTextErrorRenderer();
$output = $renderer->__invoke($exception, true);
$this->assertMatchesRegularExpression('/Ooops.../', $output);
}
public function testPlainTextErrorRendererNotDisplaysErrorDetails()
{
$previousException = new RuntimeException('Oops..');
$exception = new Exception('Ooops...', 0, $previousException);
$renderer = new PlainTextErrorRenderer();
$output = $renderer->__invoke($exception, false);
$this->assertSame("Slim Application Error\n", $output, 'Should show only one string');
}
public function testPlainTextErrorRendererRenderHttpException()
{
$exceptionTitle = 'title';
$httpExceptionProphecy = $this->prophesize(HttpException::class);
$httpExceptionProphecy
->getTitle()
->willReturn($exceptionTitle)
->shouldBeCalledOnce();
$renderer = new PlainTextErrorRenderer();
$output = $renderer->__invoke($httpExceptionProphecy->reveal(), true);
$this->assertStringContainsString($exceptionTitle, $output, 'Should contain http exception title');
}
}