File: ErrorHandlerTest.php

package info (click to toggle)
php-monolog 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,456 kB
  • sloc: php: 16,836; makefile: 20
file content (143 lines) | stat: -rw-r--r-- 5,848 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php declare(strict_types=1);

/*
 * This file is part of the Monolog package.
 *
 * (c) Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Monolog;

use Monolog\Handler\TestHandler;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use Psr\Log\LogLevel;

class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
{
    public function testRegister()
    {
        $logger = new Logger('test', [$handler = new TestHandler]);

        $this->assertInstanceOf(ErrorHandler::class, ErrorHandler::register($logger, false, false, false));
    }

    #[WithoutErrorHandler]
    public function testHandleError()
    {
        $logger = new Logger('test', [$handler = new TestHandler]);
        $errHandler = new ErrorHandler($logger);

        $phpunitHandler = set_error_handler($prevHandler = function () {
        });

        try {
            $errHandler->registerErrorHandler([], true);
            $prop = $this->getPrivatePropertyValue($errHandler, 'previousErrorHandler');
            $this->assertTrue(\is_callable($prop));
            $this->assertSame($prevHandler, $prop);

            $resHandler = $errHandler->registerErrorHandler([E_USER_NOTICE => LogLevel::EMERGENCY], false);
            $this->assertSame($errHandler, $resHandler);
            trigger_error('Foo', E_USER_ERROR);
            $this->assertCount(1, $handler->getRecords());
            $this->assertTrue($handler->hasErrorRecords());
            trigger_error('Foo', E_USER_NOTICE);
            $this->assertCount(2, $handler->getRecords());
            // check that the remapping of notice to emergency above worked
            $this->assertTrue($handler->hasEmergencyRecords());
            $this->assertFalse($handler->hasNoticeRecords());
        } finally {
            // restore previous handler
            set_error_handler($phpunitHandler);
        }
    }

    public static function fatalHandlerProvider()
    {
        return [
            [null, 10, str_repeat(' ', 1024 * 10), LogLevel::ALERT],
            [LogLevel::DEBUG, 15, str_repeat(' ', 1024 * 15), LogLevel::DEBUG],
        ];
    }

    protected function getPrivatePropertyValue($instance, $property)
    {
        $ref = new \ReflectionClass(\get_class($instance));
        $prop = $ref->getProperty($property);

        return $prop->getValue($instance);
    }

    #[DataProvider('fatalHandlerProvider')]
    #[WithoutErrorHandler]
    public function testFatalHandler(
        $level,
        $reservedMemorySize,
        $expectedReservedMemory,
        $expectedFatalLevel
    ) {
        $logger = new Logger('test', [$handler = new TestHandler]);
        $errHandler = new ErrorHandler($logger);
        $res = $errHandler->registerFatalHandler($level, $reservedMemorySize);

        $this->assertSame($res, $errHandler);
        $this->assertTrue($this->getPrivatePropertyValue($errHandler, 'hasFatalErrorHandler'));
        $this->assertEquals($expectedReservedMemory, $this->getPrivatePropertyValue($errHandler, 'reservedMemory'));
        $this->assertEquals($expectedFatalLevel, $this->getPrivatePropertyValue($errHandler, 'fatalLevel'));
    }

    #[WithoutErrorHandler]
    public function testHandleException()
    {
        $logger = new Logger('test', [$handler = new TestHandler]);
        $errHandler = new ErrorHandler($logger);

        $resHandler = $errHandler->registerExceptionHandler($map = ['Monolog\CustomTestException' => LogLevel::DEBUG, 'TypeError' => LogLevel::NOTICE, 'Throwable' => LogLevel::WARNING], false);
        $this->assertSame($errHandler, $resHandler);

        $map['ParseError'] = LogLevel::CRITICAL;
        $prop = $this->getPrivatePropertyValue($errHandler, 'uncaughtExceptionLevelMap');
        $this->assertSame($map, $prop);

        $errHandler->registerExceptionHandler([], true);
        $prop = $this->getPrivatePropertyValue($errHandler, 'previousExceptionHandler');
        $this->assertTrue(\is_callable($prop));

        restore_exception_handler();
        restore_exception_handler();
    }

    public function testCodeToString()
    {
        $method = new \ReflectionMethod(ErrorHandler::class, 'codeToString');

        $this->assertEquals('E_ERROR', $method->invokeArgs(null, [E_ERROR]));
        $this->assertEquals('E_WARNING', $method->invokeArgs(null, [E_WARNING]));
        $this->assertEquals('E_PARSE', $method->invokeArgs(null, [E_PARSE]));
        $this->assertEquals('E_NOTICE', $method->invokeArgs(null, [E_NOTICE]));
        $this->assertEquals('E_CORE_ERROR', $method->invokeArgs(null, [E_CORE_ERROR]));
        $this->assertEquals('E_CORE_WARNING', $method->invokeArgs(null, [E_CORE_WARNING]));
        $this->assertEquals('E_COMPILE_ERROR', $method->invokeArgs(null, [E_COMPILE_ERROR]));
        $this->assertEquals('E_COMPILE_WARNING', $method->invokeArgs(null, [E_COMPILE_WARNING]));
        $this->assertEquals('E_USER_ERROR', $method->invokeArgs(null, [E_USER_ERROR]));
        $this->assertEquals('E_USER_WARNING', $method->invokeArgs(null, [E_USER_WARNING]));
        $this->assertEquals('E_USER_NOTICE', $method->invokeArgs(null, [E_USER_NOTICE]));
        $this->assertEquals('E_STRICT', $method->invokeArgs(null, [2048]));
        $this->assertEquals('E_RECOVERABLE_ERROR', $method->invokeArgs(null, [E_RECOVERABLE_ERROR]));
        $this->assertEquals('E_DEPRECATED', $method->invokeArgs(null, [E_DEPRECATED]));
        $this->assertEquals('E_USER_DEPRECATED', $method->invokeArgs(null, [E_USER_DEPRECATED]));

        $this->assertEquals('Unknown PHP error', $method->invokeArgs(null, [E_ALL]));
    }
}

class CustomTestException extends \Exception
{
}
class CustomCustomException extends CustomTestException
{
}