Passed
Push — master ( 4c1c1f...a4f8c1 )
by Maurício
08:40 queued 12s
created

ErrorHandlerTest::testHandleExceptionForProdEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Tests;
6
7
use Exception;
8
use PhpMyAdmin\Error;
9
use PhpMyAdmin\ErrorHandler;
10
11
use function array_keys;
12
use function array_pop;
13
use function count;
14
15
use const E_RECOVERABLE_ERROR;
16
use const E_USER_NOTICE;
17
use const E_USER_WARNING;
18
use const E_WARNING;
19
20
/**
21
 * @covers \PhpMyAdmin\ErrorHandler
22
 */
23
class ErrorHandlerTest extends AbstractTestCase
24
{
25
    /** @var ErrorHandler */
26
    protected $object;
27
28
    /**
29
     * Sets up the fixture, for example, opens a network connection.
30
     * This method is called before a test is executed.
31
     */
32
    protected function setUp(): void
33
    {
34
        parent::setUp();
35
        $this->object = new ErrorHandler();
36
        $_SESSION['errors'] = [];
37
        $GLOBALS['server'] = 0;
38
        $GLOBALS['cfg']['environment'] = 'production';
39
        $GLOBALS['cfg']['SendErrorReports'] = 'always';
40
    }
41
42
    /**
43
     * Tears down the fixture, for example, closes a network connection.
44
     * This method is called after a test is executed.
45
     */
46
    protected function tearDown(): void
47
    {
48
        parent::tearDown();
49
        unset($this->object);
50
    }
51
52
    /**
53
     * Data provider for testHandleError
54
     *
55
     * @return array data for testHandleError
56
     */
57
    public function providerForTestHandleError(): array
58
    {
59
        return [
60
            [
61
                E_RECOVERABLE_ERROR,
62
                'Compile Error',
63
                'error.txt',
64
                12,
65
                'Compile Error',
66
                '',
67
            ],
68
            [
69
                E_USER_NOTICE,
70
                'User notice',
71
                'error.txt',
72
                12,
73
                'User notice',
74
                'User notice',
75
            ],
76
        ];
77
    }
78
79
    /**
80
     * Test for getDispErrors when PHP errors are not shown
81
     *
82
     * @param int    $errno       error number
83
     * @param string $errstr      error string
84
     * @param string $errfile     error file
85
     * @param int    $errline     error line
86
     * @param string $output_show expected output if showing of errors is
87
     *                            enabled
88
     * @param string $output_hide expected output if showing of errors is
89
     *                            disabled and 'sendErrorReports' is set to 'never'
90
     *
91
     * @dataProvider providerForTestHandleError
92
     */
93
    public function testGetDispErrorsForDisplayFalse(
94
        int $errno,
95
        string $errstr,
96
        string $errfile,
97
        int $errline,
98
        string $output_show,
99
        string $output_hide
100
    ): void {
101
        // TODO: Add other test cases for all combination of 'sendErrorReports'
102
        $GLOBALS['cfg']['SendErrorReports'] = 'never';
103
104
        $this->object->handleError($errno, $errstr, $errfile, $errline);
105
106
        $output = $this->object->getDispErrors();
107
108
        if ($output_hide === '') {
109
            $this->assertEquals('', $output);
110
        } else {
111
            $this->assertNotEmpty($output_show);// Useless check
112
            $this->assertStringContainsString($output_hide, $output);
113
        }
114
    }
115
116
    /**
117
     * Test for getDispErrors when PHP errors are shown
118
     *
119
     * @param int    $errno       error number
120
     * @param string $errstr      error string
121
     * @param string $errfile     error file
122
     * @param int    $errline     error line
123
     * @param string $output_show expected output if showing of errors is
124
     *                            enabled
125
     * @param string $output_hide expected output if showing of errors is
126
     *                            disabled
127
     *
128
     * @dataProvider providerForTestHandleError
129
     */
130
    public function testGetDispErrorsForDisplayTrue(
131
        int $errno,
132
        string $errstr,
133
        string $errfile,
134
        int $errline,
135
        string $output_show,
136
        string $output_hide
137
    ): void {
138
        $this->object->handleError($errno, $errstr, $errfile, $errline);
139
140
        $this->assertIsString($output_hide);// Useless check
141
        $this->assertStringContainsString(
142
            $output_show,
143
            $this->object->getDispErrors()
144
        );
145
    }
146
147
    /**
148
     * Test for checkSavedErrors
149
     */
150
    public function testCheckSavedErrors(): void
151
    {
152
        $this->callFunction(
153
            $this->object,
154
            ErrorHandler::class,
155
            'checkSavedErrors',
156
            []
157
        );
158
        $this->assertArrayNotHasKey('errors', $_SESSION);
159
    }
160
161
    /**
162
     * Test for countErrors
163
     *
164
     * @group medium
165
     */
166
    public function testCountErrors(): void
167
    {
168
        $this->object->addError('Compile Error', E_WARNING, 'error.txt', 15);
169
        $this->assertEquals(
170
            1,
171
            $this->object->countErrors()
172
        );
173
    }
174
175
    /**
176
     * Test for sliceErrors
177
     *
178
     * @group medium
179
     */
180
    public function testSliceErrors(): void
181
    {
182
        $this->object->addError('Compile Error', E_WARNING, 'error.txt', 15);
183
        $this->object->addError('Compile Error', E_WARNING, 'error.txt', 16);
184
        $this->assertEquals(
185
            2,
186
            $this->object->countErrors()
187
        );
188
        $this->assertEquals(
189
            [],
190
            $this->object->sliceErrors(2)
191
        );
192
        $this->assertEquals(
193
            2,
194
            $this->object->countErrors()
195
        );
196
        $this->assertCount(
197
            1,
198
            $this->object->sliceErrors(1)
199
        );
200
        $this->assertEquals(
201
            1,
202
            $this->object->countErrors()
203
        );
204
    }
205
206
    /**
207
     * Test for sliceErrors with 10 elements as an example
208
     *
209
     * @group medium
210
     */
211
    public function testSliceErrorsOtherExample(): void
212
    {
213
        for ($i = 0; $i < 10; $i++) {
214
            $this->object->addError('Compile Error', E_WARNING, 'error.txt', $i);
215
        }
216
217
        // 10 initial items
218
        $this->assertEquals(10, $this->object->countErrors());
219
        $this->assertEquals(10, count($this->object->getCurrentErrors()));
220
221
        // slice 9 elements, returns one 10 - 9
222
        $elements = $this->object->sliceErrors(9);
223
        $firstKey = array_keys($elements)[0];
224
225
        // Gives the last element
226
        $this->assertEquals(
227
            [
228
                $firstKey => $elements[$firstKey],
229
            ],
230
            $elements
231
        );
232
        $this->assertEquals(9, count($this->object->getCurrentErrors()));
233
        $this->assertEquals(9, $this->object->countErrors());
234
235
        // Slice as much as there is (9), does nothing
236
        $elements = $this->object->sliceErrors(9);
237
        $this->assertEquals([], $elements);
238
        $this->assertEquals(9, count($this->object->getCurrentErrors()));
239
        $this->assertEquals(9, $this->object->countErrors());
240
241
        // Slice 0, removes everything
242
        $elements = $this->object->sliceErrors(0);
243
        $this->assertEquals(9, count($elements));
244
        $this->assertEquals(0, count($this->object->getCurrentErrors()));
245
        $this->assertEquals(0, $this->object->countErrors());
246
    }
247
248
    /**
249
     * Test for countUserErrors
250
     */
251
    public function testCountUserErrors(): void
252
    {
253
        $this->object->addError('Compile Error', E_WARNING, 'error.txt', 15);
254
        $this->assertEquals(
255
            0,
256
            $this->object->countUserErrors()
257
        );
258
        $this->object->addError('Compile Error', E_USER_WARNING, 'error.txt', 15);
259
        $this->assertEquals(
260
            1,
261
            $this->object->countUserErrors()
262
        );
263
    }
264
265
    /**
266
     * Test for hasUserErrors
267
     */
268
    public function testHasUserErrors(): void
269
    {
270
        $this->assertFalse($this->object->hasUserErrors());
271
    }
272
273
    /**
274
     * Test for hasErrors
275
     */
276
    public function testHasErrors(): void
277
    {
278
        $this->assertFalse($this->object->hasErrors());
279
    }
280
281
    /**
282
     * Test for countDisplayErrors
283
     */
284
    public function testCountDisplayErrorsForDisplayTrue(): void
285
    {
286
        $this->assertEquals(
287
            0,
288
            $this->object->countDisplayErrors()
289
        );
290
    }
291
292
    /**
293
     * Test for countDisplayErrors
294
     */
295
    public function testCountDisplayErrorsForDisplayFalse(): void
296
    {
297
        $this->assertEquals(
298
            0,
299
            $this->object->countDisplayErrors()
300
        );
301
    }
302
303
    /**
304
     * Test for hasDisplayErrors
305
     */
306
    public function testHasDisplayErrors(): void
307
    {
308
        $this->assertFalse($this->object->hasDisplayErrors());
309
    }
310
311
    public function testHandleExceptionForDevEnv(): void
312
    {
313
        $GLOBALS['config']->set('environment', 'development');
314
        $errorHandler = new ErrorHandler();
315
        $this->assertSame([], $errorHandler->getCurrentErrors());
316
        $errorHandler->handleException(new Exception('Exception message.'));
317
        $output = $this->getActualOutputForAssertion();
318
        $errors = $errorHandler->getCurrentErrors();
319
        $this->assertCount(1, $errors);
320
        $error = array_pop($errors);
321
        $this->assertInstanceOf(Error::class, $error);
322
        $this->assertSame('Exception: Exception message.', $error->getOnlyMessage());
323
        $this->assertStringContainsString($error->getDisplay(), $output);
324
        $this->assertStringContainsString('Internal error', $output);
325
        $this->assertStringContainsString('ErrorHandlerTest.php#' . $error->getLine(), $output);
326
        $this->assertStringContainsString('Exception: Exception message.', $output);
327
    }
328
329
    public function testHandleExceptionForProdEnv(): void
330
    {
331
        $GLOBALS['config']->set('environment', 'production');
332
        $errorHandler = new ErrorHandler();
333
        $this->assertSame([], $errorHandler->getCurrentErrors());
334
        $errorHandler->handleException(new Exception('Exception message.'));
335
        $output = $this->getActualOutputForAssertion();
336
        $errors = $errorHandler->getCurrentErrors();
337
        $this->assertCount(1, $errors);
338
        $error = array_pop($errors);
339
        $this->assertInstanceOf(Error::class, $error);
340
        $this->assertSame('Exception: Exception message.', $error->getOnlyMessage());
341
        $this->assertStringContainsString($error->getDisplay(), $output);
342
        $this->assertStringContainsString('Exception: Exception message.', $output);
343
        $this->assertStringNotContainsString('Internal error', $output);
344
        $this->assertStringNotContainsString('ErrorHandlerTest.php#' . $error->getLine(), $output);
345
    }
346
}
347