2
0

ErrorHandlerTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog;
  11. use Monolog\Handler\TestHandler;
  12. use PHPUnit\Framework\Attributes\DataProvider;
  13. use PHPUnit\Framework\Attributes\WithoutErrorHandler;
  14. use Psr\Log\LogLevel;
  15. class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
  16. {
  17. public function testRegister()
  18. {
  19. $logger = new Logger('test', [$handler = new TestHandler]);
  20. $this->assertInstanceOf(ErrorHandler::class, ErrorHandler::register($logger, false, false, false));
  21. }
  22. #[WithoutErrorHandler]
  23. public function testHandleError()
  24. {
  25. $logger = new Logger('test', [$handler = new TestHandler]);
  26. $errHandler = new ErrorHandler($logger);
  27. $phpunitHandler = set_error_handler($prevHandler = function () {
  28. });
  29. try {
  30. $errHandler->registerErrorHandler([], true);
  31. $prop = $this->getPrivatePropertyValue($errHandler, 'previousErrorHandler');
  32. $this->assertTrue(\is_callable($prop));
  33. $this->assertSame($prevHandler, $prop);
  34. $resHandler = $errHandler->registerErrorHandler([E_USER_NOTICE => LogLevel::EMERGENCY], false);
  35. $this->assertSame($errHandler, $resHandler);
  36. trigger_error('Foo', E_USER_ERROR);
  37. $this->assertCount(1, $handler->getRecords());
  38. $this->assertTrue($handler->hasErrorRecords());
  39. trigger_error('Foo', E_USER_NOTICE);
  40. $this->assertCount(2, $handler->getRecords());
  41. // check that the remapping of notice to emergency above worked
  42. $this->assertTrue($handler->hasEmergencyRecords());
  43. $this->assertFalse($handler->hasNoticeRecords());
  44. } finally {
  45. // restore previous handler
  46. set_error_handler($phpunitHandler);
  47. }
  48. }
  49. public static function fatalHandlerProvider()
  50. {
  51. return [
  52. [null, 10, str_repeat(' ', 1024 * 10), LogLevel::ALERT],
  53. [LogLevel::DEBUG, 15, str_repeat(' ', 1024 * 15), LogLevel::DEBUG],
  54. ];
  55. }
  56. protected function getPrivatePropertyValue($instance, $property)
  57. {
  58. $ref = new \ReflectionClass(\get_class($instance));
  59. $prop = $ref->getProperty($property);
  60. return $prop->getValue($instance);
  61. }
  62. #[DataProvider('fatalHandlerProvider')]
  63. #[WithoutErrorHandler]
  64. public function testFatalHandler(
  65. $level,
  66. $reservedMemorySize,
  67. $expectedReservedMemory,
  68. $expectedFatalLevel
  69. ) {
  70. $logger = new Logger('test', [$handler = new TestHandler]);
  71. $errHandler = new ErrorHandler($logger);
  72. $res = $errHandler->registerFatalHandler($level, $reservedMemorySize);
  73. $this->assertSame($res, $errHandler);
  74. $this->assertTrue($this->getPrivatePropertyValue($errHandler, 'hasFatalErrorHandler'));
  75. $this->assertEquals($expectedReservedMemory, $this->getPrivatePropertyValue($errHandler, 'reservedMemory'));
  76. $this->assertEquals($expectedFatalLevel, $this->getPrivatePropertyValue($errHandler, 'fatalLevel'));
  77. }
  78. #[WithoutErrorHandler]
  79. public function testHandleException()
  80. {
  81. $logger = new Logger('test', [$handler = new TestHandler]);
  82. $errHandler = new ErrorHandler($logger);
  83. $resHandler = $errHandler->registerExceptionHandler($map = ['Monolog\CustomTestException' => LogLevel::DEBUG, 'TypeError' => LogLevel::NOTICE, 'Throwable' => LogLevel::WARNING], false);
  84. $this->assertSame($errHandler, $resHandler);
  85. $map['ParseError'] = LogLevel::CRITICAL;
  86. $prop = $this->getPrivatePropertyValue($errHandler, 'uncaughtExceptionLevelMap');
  87. $this->assertSame($map, $prop);
  88. $errHandler->registerExceptionHandler([], true);
  89. $prop = $this->getPrivatePropertyValue($errHandler, 'previousExceptionHandler');
  90. $this->assertTrue(\is_callable($prop));
  91. restore_exception_handler();
  92. restore_exception_handler();
  93. }
  94. public function testCodeToString()
  95. {
  96. $method = new \ReflectionMethod(ErrorHandler::class, 'codeToString');
  97. $this->assertEquals('E_ERROR', $method->invokeArgs(null, [E_ERROR]));
  98. $this->assertEquals('E_WARNING', $method->invokeArgs(null, [E_WARNING]));
  99. $this->assertEquals('E_PARSE', $method->invokeArgs(null, [E_PARSE]));
  100. $this->assertEquals('E_NOTICE', $method->invokeArgs(null, [E_NOTICE]));
  101. $this->assertEquals('E_CORE_ERROR', $method->invokeArgs(null, [E_CORE_ERROR]));
  102. $this->assertEquals('E_CORE_WARNING', $method->invokeArgs(null, [E_CORE_WARNING]));
  103. $this->assertEquals('E_COMPILE_ERROR', $method->invokeArgs(null, [E_COMPILE_ERROR]));
  104. $this->assertEquals('E_COMPILE_WARNING', $method->invokeArgs(null, [E_COMPILE_WARNING]));
  105. $this->assertEquals('E_USER_ERROR', $method->invokeArgs(null, [E_USER_ERROR]));
  106. $this->assertEquals('E_USER_WARNING', $method->invokeArgs(null, [E_USER_WARNING]));
  107. $this->assertEquals('E_USER_NOTICE', $method->invokeArgs(null, [E_USER_NOTICE]));
  108. $this->assertEquals('E_STRICT', $method->invokeArgs(null, [2048]));
  109. $this->assertEquals('E_RECOVERABLE_ERROR', $method->invokeArgs(null, [E_RECOVERABLE_ERROR]));
  110. $this->assertEquals('E_DEPRECATED', $method->invokeArgs(null, [E_DEPRECATED]));
  111. $this->assertEquals('E_USER_DEPRECATED', $method->invokeArgs(null, [E_USER_DEPRECATED]));
  112. $this->assertEquals('Unknown PHP error', $method->invokeArgs(null, [E_ALL]));
  113. }
  114. }
  115. class CustomTestException extends \Exception
  116. {
  117. }
  118. class CustomCustomException extends CustomTestException
  119. {
  120. }