Remove `@access` annotations
[phpmyadmin.git] / test / classes / ErrorHandlerTest.php
bloba6a656707b296e58d248de47bdba8853c4b92de7
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin\Tests;
7 use PhpMyAdmin\ErrorHandler;
9 use function array_keys;
10 use function count;
12 use const E_RECOVERABLE_ERROR;
13 use const E_USER_NOTICE;
14 use const E_USER_WARNING;
15 use const E_WARNING;
17 /**
18 * @covers \PhpMyAdmin\ErrorHandler
20 class ErrorHandlerTest extends AbstractTestCase
22 /** @var ErrorHandler */
23 protected $object;
25 /**
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
31 parent::setUp();
32 $this->object = new ErrorHandler();
33 $_SESSION['errors'] = [];
34 $GLOBALS['server'] = 0;
35 $GLOBALS['cfg']['environment'] = 'production';
36 $GLOBALS['cfg']['SendErrorReports'] = 'always';
39 /**
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
45 parent::tearDown();
46 unset($this->object);
49 /**
50 * Data provider for testHandleError
52 * @return array data for testHandleError
54 public function providerForTestHandleError(): array
56 return [
58 E_RECOVERABLE_ERROR,
59 'Compile Error',
60 'error.txt',
61 12,
62 'Compile Error',
63 '',
66 E_USER_NOTICE,
67 'User notice',
68 'error.txt',
69 12,
70 'User notice',
71 'User notice',
76 /**
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
84 * enabled
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(
91 int $errno,
92 string $errstr,
93 string $errfile,
94 int $errline,
95 string $output_show,
96 string $output_hide
97 ): void {
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);
107 } else {
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
121 * enabled
122 * @param string $output_hide expected output if showing of errors is
123 * disabled
125 * @dataProvider providerForTestHandleError
127 public function testGetDispErrorsForDisplayTrue(
128 int $errno,
129 string $errstr,
130 string $errfile,
131 int $errline,
132 string $output_show,
133 string $output_hide
134 ): void {
135 $this->object->handleError($errno, $errstr, $errfile, $errline);
137 $this->assertIsString($output_hide);// Useless check
138 $this->assertStringContainsString(
139 $output_show,
140 $this->object->getDispErrors()
145 * Test for checkSavedErrors
147 public function testCheckSavedErrors(): void
149 $this->callFunction(
150 $this->object,
151 ErrorHandler::class,
152 'checkSavedErrors',
155 $this->assertArrayNotHasKey('errors', $_SESSION);
159 * Test for countErrors
161 * @group medium
163 public function testCountErrors(): void
165 $this->object->addError('Compile Error', E_WARNING, 'error.txt', 15);
166 $this->assertEquals(
168 $this->object->countErrors()
173 * Test for sliceErrors
175 * @group medium
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);
181 $this->assertEquals(
183 $this->object->countErrors()
185 $this->assertEquals(
187 $this->object->sliceErrors(2)
189 $this->assertEquals(
191 $this->object->countErrors()
193 $this->assertCount(
195 $this->object->sliceErrors(1)
197 $this->assertEquals(
199 $this->object->countErrors()
204 * Test for sliceErrors with 10 elements as an example
206 * @group medium
208 public function testSliceErrorsOtherExample(): void
210 for ($i = 0; $i < 10; $i++) {
211 $this->object->addError('Compile Error', E_WARNING, 'error.txt', $i);
214 // 10 initial items
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
223 $this->assertEquals(
225 $firstKey => $elements[$firstKey],
227 $elements
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);
251 $this->assertEquals(
253 $this->object->countUserErrors()
255 $this->object->addError('Compile Error', E_USER_WARNING, 'error.txt', 15);
256 $this->assertEquals(
258 $this->object->countUserErrors()
263 * Test for hasUserErrors
265 public function testHasUserErrors(): void
267 $this->assertFalse($this->object->hasUserErrors());
271 * Test for hasErrors
273 public function testHasErrors(): void
275 $this->assertFalse($this->object->hasErrors());
279 * Test for countDisplayErrors
281 public function testCountDisplayErrorsForDisplayTrue(): void
283 $this->assertEquals(
285 $this->object->countDisplayErrors()
290 * Test for countDisplayErrors
292 public function testCountDisplayErrorsForDisplayFalse(): void
294 $this->assertEquals(
296 $this->object->countDisplayErrors()
301 * Test for hasDisplayErrors
303 public function testHasDisplayErrors(): void
305 $this->assertFalse($this->object->hasDisplayErrors());