3 declare(strict_types
=1);
5 namespace PhpMyAdmin\Tests
;
7 use PhpMyAdmin\ErrorHandler
;
9 use function array_keys
;
12 use const E_RECOVERABLE_ERROR
;
13 use const E_USER_NOTICE
;
14 use const E_USER_WARNING
;
18 * @covers \PhpMyAdmin\ErrorHandler
20 class ErrorHandlerTest
extends AbstractTestCase
22 /** @var ErrorHandler */
26 * Sets up the fixture, for example, opens a network connection.
27 * This method is called before a test is executed.
29 protected function setUp(): void
32 $this->object = new ErrorHandler();
33 $_SESSION['errors'] = [];
34 $GLOBALS['server'] = 0;
35 $GLOBALS['cfg']['environment'] = 'production';
36 $GLOBALS['cfg']['SendErrorReports'] = 'always';
40 * Tears down the fixture, for example, closes a network connection.
41 * This method is called after a test is executed.
43 protected function tearDown(): void
50 * Data provider for testHandleError
52 * @return array data for testHandleError
54 public function providerForTestHandleError(): array
77 * Test for getDispErrors when PHP errors are not shown
79 * @param int $errno error number
80 * @param string $errstr error string
81 * @param string $errfile error file
82 * @param int $errline error line
83 * @param string $output_show expected output if showing of errors is
85 * @param string $output_hide expected output if showing of errors is
86 * disabled and 'sendErrorReports' is set to 'never'
88 * @dataProvider providerForTestHandleError
90 public function testGetDispErrorsForDisplayFalse(
98 // TODO: Add other test cases for all combination of 'sendErrorReports'
99 $GLOBALS['cfg']['SendErrorReports'] = 'never';
101 $this->object->handleError($errno, $errstr, $errfile, $errline);
103 $output = $this->object->getDispErrors();
105 if ($output_hide === '') {
106 $this->assertEquals('', $output);
108 $this->assertNotEmpty($output_show);// Useless check
109 $this->assertStringContainsString($output_hide, $output);
114 * Test for getDispErrors when PHP errors are shown
116 * @param int $errno error number
117 * @param string $errstr error string
118 * @param string $errfile error file
119 * @param int $errline error line
120 * @param string $output_show expected output if showing of errors is
122 * @param string $output_hide expected output if showing of errors is
125 * @dataProvider providerForTestHandleError
127 public function testGetDispErrorsForDisplayTrue(
135 $this->object->handleError($errno, $errstr, $errfile, $errline);
137 $this->assertIsString($output_hide);// Useless check
138 $this->assertStringContainsString(
140 $this->object->getDispErrors()
145 * Test for checkSavedErrors
147 public function testCheckSavedErrors(): void
155 $this->assertArrayNotHasKey('errors', $_SESSION);
159 * Test for countErrors
163 public function testCountErrors(): void
165 $this->object->addError('Compile Error', E_WARNING
, 'error.txt', 15);
168 $this->object->countErrors()
173 * Test for sliceErrors
177 public function testSliceErrors(): void
179 $this->object->addError('Compile Error', E_WARNING
, 'error.txt', 15);
180 $this->object->addError('Compile Error', E_WARNING
, 'error.txt', 16);
183 $this->object->countErrors()
187 $this->object->sliceErrors(2)
191 $this->object->countErrors()
195 $this->object->sliceErrors(1)
199 $this->object->countErrors()
204 * Test for sliceErrors with 10 elements as an example
208 public function testSliceErrorsOtherExample(): void
210 for ($i = 0; $i < 10; $i++
) {
211 $this->object->addError('Compile Error', E_WARNING
, 'error.txt', $i);
215 $this->assertEquals(10, $this->object->countErrors());
216 $this->assertEquals(10, count($this->object->getCurrentErrors()));
218 // slice 9 elements, returns one 10 - 9
219 $elements = $this->object->sliceErrors(9);
220 $firstKey = array_keys($elements)[0];
222 // Gives the last element
225 $firstKey => $elements[$firstKey],
229 $this->assertEquals(9, count($this->object->getCurrentErrors()));
230 $this->assertEquals(9, $this->object->countErrors());
232 // Slice as much as there is (9), does nothing
233 $elements = $this->object->sliceErrors(9);
234 $this->assertEquals([], $elements);
235 $this->assertEquals(9, count($this->object->getCurrentErrors()));
236 $this->assertEquals(9, $this->object->countErrors());
238 // Slice 0, removes everything
239 $elements = $this->object->sliceErrors(0);
240 $this->assertEquals(9, count($elements));
241 $this->assertEquals(0, count($this->object->getCurrentErrors()));
242 $this->assertEquals(0, $this->object->countErrors());
246 * Test for countUserErrors
248 public function testCountUserErrors(): void
250 $this->object->addError('Compile Error', E_WARNING
, 'error.txt', 15);
253 $this->object->countUserErrors()
255 $this->object->addError('Compile Error', E_USER_WARNING
, 'error.txt', 15);
258 $this->object->countUserErrors()
263 * Test for hasUserErrors
265 public function testHasUserErrors(): void
267 $this->assertFalse($this->object->hasUserErrors());
273 public function testHasErrors(): void
275 $this->assertFalse($this->object->hasErrors());
279 * Test for countDisplayErrors
281 public function testCountDisplayErrorsForDisplayTrue(): void
285 $this->object->countDisplayErrors()
290 * Test for countDisplayErrors
292 public function testCountDisplayErrorsForDisplayFalse(): void
296 $this->object->countDisplayErrors()
301 * Test for hasDisplayErrors
303 public function testHasDisplayErrors(): void
305 $this->assertFalse($this->object->hasDisplayErrors());