1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Tests\Exceptions; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\Error\Error; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Spiral\Exceptions\ConsoleHandler; |
17
|
|
|
use Spiral\Exceptions\HandlerInterface; |
18
|
|
|
use Spiral\Exceptions\HtmlHandler; |
19
|
|
|
use Spiral\Exceptions\JsonHandler; |
20
|
|
|
use Spiral\Exceptions\PlainHandler; |
21
|
|
|
|
22
|
|
|
class HandlerTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function testGetMessage(): void |
25
|
|
|
{ |
26
|
|
|
$handler = new ConsoleHandler(); |
27
|
|
|
|
28
|
|
|
$this->assertStringContainsString('Error', $handler->getMessage(new Error( |
29
|
|
|
'message', |
30
|
|
|
100, |
31
|
|
|
__FILE__, |
32
|
|
|
__LINE__ |
33
|
|
|
))); |
34
|
|
|
|
35
|
|
|
$this->assertStringContainsString('message', $handler->getMessage(new Error( |
36
|
|
|
'message', |
37
|
|
|
100, |
38
|
|
|
__FILE__, |
39
|
|
|
__LINE__ |
40
|
|
|
))); |
41
|
|
|
|
42
|
|
|
$this->assertStringContainsString(__FILE__, $handler->getMessage(new Error( |
43
|
|
|
'message', |
44
|
|
|
100, |
45
|
|
|
__FILE__, |
46
|
|
|
__LINE__ |
47
|
|
|
))); |
48
|
|
|
|
49
|
|
|
$this->assertStringContainsString('100', $handler->getMessage(new Error( |
50
|
|
|
'message', |
51
|
|
|
100, |
52
|
|
|
__FILE__, |
53
|
|
|
100 |
54
|
|
|
))); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testConsoleHandlerWithoutColorsBasic(): void |
58
|
|
|
{ |
59
|
|
|
$handler = new ConsoleHandler(); |
60
|
|
|
$handler->setColorsSupport(false); |
61
|
|
|
|
62
|
|
|
$result = $handler->renderException(new Error( |
63
|
|
|
'message', |
64
|
|
|
100, |
65
|
|
|
__FILE__, |
66
|
|
|
__LINE__ |
67
|
|
|
), HandlerInterface::VERBOSITY_BASIC); |
68
|
|
|
|
69
|
|
|
$this->assertStringContainsString('Error', $result); |
70
|
|
|
$this->assertStringContainsString('message', $result); |
71
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testConsoleHandlerErrorBasic(): void |
75
|
|
|
{ |
76
|
|
|
$handler = new ConsoleHandler(); |
77
|
|
|
$handler->setColorsSupport(true); |
78
|
|
|
$result = $handler->renderException(new \Error('message', 100), HandlerInterface::VERBOSITY_BASIC); |
79
|
|
|
|
80
|
|
|
$this->assertStringContainsString('Error', $result); |
81
|
|
|
$this->assertStringContainsString('message', $result); |
82
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testConsoleHandlerErrorVerbose(): void |
86
|
|
|
{ |
87
|
|
|
$handler = new ConsoleHandler(); |
88
|
|
|
$handler->setColorsSupport(true); |
89
|
|
|
$result = $handler->renderException(new \Error('message', 100), HandlerInterface::VERBOSITY_VERBOSE); |
90
|
|
|
|
91
|
|
|
$this->assertStringContainsString('Error', $result); |
92
|
|
|
$this->assertStringContainsString('message', $result); |
93
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
public function testConsoleHandlerWithColorsBasic(): void |
98
|
|
|
{ |
99
|
|
|
$handler = new ConsoleHandler(); |
100
|
|
|
$handler->setColorsSupport(true); |
101
|
|
|
|
102
|
|
|
$result = $handler->renderException(new Error( |
103
|
|
|
'message', |
104
|
|
|
100, |
105
|
|
|
__FILE__, |
106
|
|
|
__LINE__ |
107
|
|
|
), HandlerInterface::VERBOSITY_BASIC); |
108
|
|
|
|
109
|
|
|
$this->assertStringContainsString('Error', $result); |
110
|
|
|
$this->assertStringContainsString('message', $result); |
111
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testHtmlHandlerDefaultBasic(): void |
115
|
|
|
{ |
116
|
|
|
$handler = new HtmlHandler(HtmlHandler::DEFAULT); |
117
|
|
|
|
118
|
|
|
$result = $handler->renderException(new Error( |
119
|
|
|
'message', |
120
|
|
|
100, |
121
|
|
|
__FILE__, |
122
|
|
|
__LINE__ |
123
|
|
|
), HandlerInterface::VERBOSITY_BASIC); |
124
|
|
|
|
125
|
|
|
$this->assertStringContainsString('Error', $result); |
126
|
|
|
$this->assertStringContainsString('message', $result); |
127
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testHtmlHandlerInvertedBasic(): void |
131
|
|
|
{ |
132
|
|
|
$handler = new HtmlHandler(HtmlHandler::INVERTED); |
133
|
|
|
|
134
|
|
|
$result = $handler->renderException(new Error( |
135
|
|
|
'message', |
136
|
|
|
100, |
137
|
|
|
__FILE__, |
138
|
|
|
__LINE__ |
139
|
|
|
), HandlerInterface::VERBOSITY_BASIC); |
140
|
|
|
|
141
|
|
|
$this->assertStringContainsString('Error', $result); |
142
|
|
|
$this->assertStringContainsString('message', $result); |
143
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function testConsoleHandlerWithColorsDebug(): void |
147
|
|
|
{ |
148
|
|
|
$handler = new ConsoleHandler(); |
149
|
|
|
$handler->setColorsSupport(true); |
150
|
|
|
|
151
|
|
|
$result = $handler->renderException(new Error( |
152
|
|
|
'message', |
153
|
|
|
100, |
154
|
|
|
__FILE__, |
155
|
|
|
__LINE__ |
156
|
|
|
), HandlerInterface::VERBOSITY_DEBUG); |
157
|
|
|
|
158
|
|
|
$this->assertStringContainsString('Error', $result); |
159
|
|
|
$this->assertStringContainsString('message', $result); |
160
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testHtmlHandlerDefaultDebug(): void |
164
|
|
|
{ |
165
|
|
|
$this->markTestSkipped('FIXME: Very long execution time'); |
166
|
|
|
|
167
|
|
|
$handler = new HtmlHandler(HtmlHandler::DEFAULT); |
168
|
|
|
|
169
|
|
|
$result = $handler->renderException(new Error( |
170
|
|
|
'message', |
171
|
|
|
100, |
172
|
|
|
__FILE__, |
173
|
|
|
__LINE__ |
174
|
|
|
), HandlerInterface::VERBOSITY_DEBUG); |
175
|
|
|
|
176
|
|
|
$this->assertStringContainsString('Error', $result); |
177
|
|
|
$this->assertStringContainsString('message', $result); |
178
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testHtmlHandlerInvertedDebug(): void |
182
|
|
|
{ |
183
|
|
|
$this->markTestSkipped('FIXME: Very long execution time'); |
184
|
|
|
|
185
|
|
|
$handler = new HtmlHandler(HtmlHandler::INVERTED); |
186
|
|
|
|
187
|
|
|
$result = $handler->renderException(new Error( |
188
|
|
|
'message', |
189
|
|
|
100, |
190
|
|
|
__FILE__, |
191
|
|
|
__LINE__ |
192
|
|
|
), HandlerInterface::VERBOSITY_DEBUG); |
193
|
|
|
|
194
|
|
|
$this->assertStringContainsString('Error', $result); |
195
|
|
|
$this->assertStringContainsString('message', $result); |
196
|
|
|
$this->assertStringContainsString(__FILE__, $result); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testConsoleHandlerStacktrace(): void |
200
|
|
|
{ |
201
|
|
|
$handler = new ConsoleHandler(); |
202
|
|
|
$handler->setColorsSupport(true); |
203
|
|
|
|
204
|
|
|
try { |
205
|
|
|
$this->makeException(); |
206
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG); |
|
|
|
|
210
|
|
|
|
211
|
|
|
$this->assertStringContainsString('LogicException', $result); |
212
|
|
|
$this->assertStringContainsString('makeException', $result); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
public function testPlainHandlerStacktrace(): void |
217
|
|
|
{ |
218
|
|
|
$handler = new PlainHandler(); |
219
|
|
|
|
220
|
|
|
try { |
221
|
|
|
$this->makeException(); |
222
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
$result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG); |
|
|
|
|
226
|
|
|
|
227
|
|
|
$this->assertStringContainsString('LogicException', $result); |
228
|
|
|
$this->assertStringContainsString('makeException', $result); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function testJsonHandler(): void |
232
|
|
|
{ |
233
|
|
|
$handler = new JsonHandler(); |
234
|
|
|
|
235
|
|
|
try { |
236
|
|
|
$this->makeException(); |
237
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
$result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG); |
|
|
|
|
241
|
|
|
|
242
|
|
|
$this->assertStringContainsString('LogicException', $result); |
243
|
|
|
$this->assertStringContainsString('makeException', $result); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function testHtmlHandlerStacktrace(): void |
247
|
|
|
{ |
248
|
|
|
$this->markTestSkipped('FIXME: Very long execution time'); |
249
|
|
|
|
250
|
|
|
$handler = new HtmlHandler(HtmlHandler::DEFAULT); |
251
|
|
|
|
252
|
|
|
try { |
253
|
|
|
$this->makeException(); |
254
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG); |
|
|
|
|
258
|
|
|
|
259
|
|
|
$this->assertStringContainsString('RuntimeException', $result); |
260
|
|
|
$this->assertStringContainsString('LogicException', $result); |
261
|
|
|
$this->assertStringContainsString('makeException', $result); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function testHtmlHandlerInvertedStacktrace(): void |
265
|
|
|
{ |
266
|
|
|
$this->markTestSkipped('FIXME: Very long execution time'); |
267
|
|
|
|
268
|
|
|
$handler = new HtmlHandler(HtmlHandler::INVERTED); |
269
|
|
|
|
270
|
|
|
try { |
271
|
|
|
$this->makeException(); |
272
|
|
|
} catch (\Throwable $e) { |
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
$result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG); |
|
|
|
|
276
|
|
|
|
277
|
|
|
$this->assertStringContainsString('RuntimeException', $result); |
278
|
|
|
$this->assertStringContainsString('LogicException', $result); |
279
|
|
|
$this->assertStringContainsString('makeException', $result); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function makeException(): void |
283
|
|
|
{ |
284
|
|
|
try { |
285
|
|
|
$f = function (): void { |
286
|
|
|
throw new \RuntimeException('error'); |
287
|
|
|
}; |
288
|
|
|
|
289
|
|
|
$f(); |
290
|
|
|
} catch (\Throwable $e) { |
291
|
|
|
throw new \LogicException('error', 0, $e); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|