Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
phpbb_error_collector_test
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 setUp
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 test_collection
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 test_collection_with_mask
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14class phpbb_error_collector_test extends phpbb_test_case
15{
16    protected function setUp(): void
17    {
18        parent::setUp();
19
20        global $phpbb_filesystem;
21
22        $phpbb_filesystem = new \phpbb\filesystem\filesystem();
23    }
24
25    public function test_collection()
26    {
27        $collector = new \phpbb\error_collector(E_ALL | E_NOTICE); // php set_error_handler() default
28        $collector->install();
29
30        // Cause a warning
31        // Division by zero was promoted to fatal error and throws DivisionByZeroError exception in PHP 8+
32        version_compare(PHP_VERSION, '8', '>=') ? '1b'['0xFF'] : 1/0; $line = __LINE__;
33
34        $collector->uninstall();
35
36        list($errno, $msg_text, $errfile, $errline) = $collector->errors[0];
37        $error_contents = $collector->format_errors();
38
39        $this->assertEquals($errno, 2);
40
41        // Unfortunately $error_contents will contain the full path here,
42        // because the tests directory is outside of phpbb root path.
43        $this->assertStringStartsWith(version_compare(PHP_VERSION, '8', '>=') ? 'Errno 2: Illegal string offset "0xFF" at ' : 'Errno 2: Division by zero at ', $error_contents);
44        $this->assertStringEndsWith(" line $line", $error_contents);
45    }
46
47    public function test_collection_with_mask()
48    {
49        $collector = new \phpbb\error_collector(E_ALL & ~E_NOTICE); // not collecting notices
50        $collector->install();
51
52        // Cause a warning
53        // Division by zero was promoted to fatal error and throws DivisionByZeroError exception in PHP 8+
54        version_compare(PHP_VERSION, '8', '>=') ? '1b'['0xFF'] : 1/0; $line = __LINE__;
55
56        // Cause a "Notice: date_default_timezone_set(): Timezone ID 'ThisTimeZoneDoesNotExist' is invalid"
57        // https://github.com/php/php-src/blob/880faa39e8c648bdc3aad7aeca170755c6557831/ext/date/php_date.c#L5205
58        date_default_timezone_set('ThisTimeZoneDoesNotExist'); $line2 = __LINE__;
59
60        $collector->uninstall();
61
62        // The notice should not be collected
63        $this->assertFalse(isset($collector->errors[1]), 'Notice should not be added to errors');
64        $this->assertEquals(count($collector->errors), 1);
65
66        list($errno, $msg_text, $errfile, $errline) = $collector->errors[0];
67        $error_contents = $collector->format_errors();
68
69        $this->assertEquals($errno, 2);
70
71        // Unfortunately $error_contents will contain the full path here,
72        // because the tests directory is outside of phpbb root path.
73        $this->assertStringStartsWith(version_compare(PHP_VERSION, '8', '>=') ? 'Errno 2: Illegal string offset "0xFF" at ' : 'Errno 2: Division by zero at ', $error_contents);
74        $this->assertStringEndsWith(" line $line", $error_contents);
75    }
76}