Passed
Push — master ( 3eb4db...99d5ff )
by Kirill
03:12
created

HandlerTest   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 262
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 151
c 1
b 0
f 0
dl 0
loc 262
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A testPlainHandlerStacktrace() 0 13 2
A testGetMessage() 0 30 1
A testConsoleHandlerWithColorsDebug() 0 15 1
A testConsoleHandlerErrorBasic() 0 9 1
A testHtmlHandlerInvertedBasic() 0 14 1
A testHtmlHandlerDefaultDebug() 0 14 1
A testJsonHandler() 0 13 2
A testConsoleHandlerErrorVerbose() 0 9 1
A testHtmlHandlerInvertedDebug() 0 14 1
A testHtmlHandlerDefaultBasic() 0 14 1
A testHtmlHandlerInvertedStacktrace() 0 14 2
A testConsoleHandlerWithColorsBasic() 0 15 1
A testConsoleHandlerWithoutColorsBasic() 0 15 1
A testHtmlHandlerStacktrace() 0 14 2
A testConsoleHandlerStacktrace() 0 14 2
A makeException() 0 10 2
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\Debug;
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
        $handler = new HtmlHandler(HtmlHandler::DEFAULT);
166
167
        $result = $handler->renderException(new Error(
168
            'message',
169
            100,
170
            __FILE__,
171
            __LINE__
172
        ), HandlerInterface::VERBOSITY_DEBUG);
173
174
        $this->assertStringContainsString('Error', $result);
175
        $this->assertStringContainsString('message', $result);
176
        $this->assertStringContainsString(__FILE__, $result);
177
    }
178
179
    public function testHtmlHandlerInvertedDebug(): void
180
    {
181
        $handler = new HtmlHandler(HtmlHandler::INVERTED);
182
183
        $result = $handler->renderException(new Error(
184
            'message',
185
            100,
186
            __FILE__,
187
            __LINE__
188
        ), HandlerInterface::VERBOSITY_DEBUG);
189
190
        $this->assertStringContainsString('Error', $result);
191
        $this->assertStringContainsString('message', $result);
192
        $this->assertStringContainsString(__FILE__, $result);
193
    }
194
195
    public function testConsoleHandlerStacktrace(): void
196
    {
197
        $handler = new ConsoleHandler();
198
        $handler->setColorsSupport(true);
199
200
        try {
201
            $this->makeException();
202
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
203
        }
204
205
        $result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
206
207
        $this->assertStringContainsString('LogicException', $result);
208
        $this->assertStringContainsString('makeException', $result);
209
    }
210
211
212
    public function testPlainHandlerStacktrace(): void
213
    {
214
        $handler = new PlainHandler();
215
216
        try {
217
            $this->makeException();
218
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
219
        }
220
221
        $result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
222
223
        $this->assertStringContainsString('LogicException', $result);
224
        $this->assertStringContainsString('makeException', $result);
225
    }
226
227
    public function testJsonHandler(): void
228
    {
229
        $handler = new JsonHandler();
230
231
        try {
232
            $this->makeException();
233
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
234
        }
235
236
        $result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
237
238
        $this->assertStringContainsString('LogicException', $result);
239
        $this->assertStringContainsString('makeException', $result);
240
    }
241
242
    public function testHtmlHandlerStacktrace(): void
243
    {
244
        $handler = new HtmlHandler(HtmlHandler::DEFAULT);
245
246
        try {
247
            $this->makeException();
248
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
249
        }
250
251
        $result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
252
253
        $this->assertStringContainsString('RuntimeException', $result);
254
        $this->assertStringContainsString('LogicException', $result);
255
        $this->assertStringContainsString('makeException', $result);
256
    }
257
258
    public function testHtmlHandlerInvertedStacktrace(): void
259
    {
260
        $handler = new HtmlHandler(HtmlHandler::INVERTED);
261
262
        try {
263
            $this->makeException();
264
        } catch (\Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
265
        }
266
267
        $result = $handler->renderException($e, HandlerInterface::VERBOSITY_DEBUG);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $e does not seem to be defined for all execution paths leading up to this point.
Loading history...
268
269
        $this->assertStringContainsString('RuntimeException', $result);
270
        $this->assertStringContainsString('LogicException', $result);
271
        $this->assertStringContainsString('makeException', $result);
272
    }
273
274
    public function makeException(): void
275
    {
276
        try {
277
            $f = function (): void {
278
                throw new \RuntimeException('error');
279
            };
280
281
            $f();
282
        } catch (\Throwable $e) {
283
            throw new \LogicException('error', 0, $e);
284
        }
285
    }
286
}
287