ErrorHandlerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 Psr\Log\LogLevel;
  13. class ErrorHandlerTest extends \PHPUnit\Framework\TestCase
  14. {
  15. public function testRegister()
  16. {
  17. $logger = new Logger('test', [$handler = new TestHandler]);
  18. $this->assertInstanceOf(ErrorHandler::class, ErrorHandler::register($logger, false, false, false));
  19. }
  20. public function testHandleError()
  21. {
  22. $logger = new Logger('test', [$handler = new TestHandler]);
  23. $errHandler = new ErrorHandler($logger);
  24. $phpunitHandler = set_error_handler($prevHandler = function () {
  25. });
  26. try {
  27. $errHandler->registerErrorHandler([], true);
  28. $prop = $this->getPrivatePropertyValue($errHandler, 'previousErrorHandler');
  29. $this->assertTrue(is_callable($prop));
  30. $this->assertSame($prevHandler, $prop);
  31. $resHandler = $errHandler->registerErrorHandler([E_USER_NOTICE => Logger::EMERGENCY], false);
  32. $this->assertSame($errHandler, $resHandler);
  33. trigger_error('Foo', E_USER_ERROR);
  34. $this->assertCount(1, $handler->getRecords());
  35. $this->assertTrue($handler->hasErrorRecords());
  36. trigger_error('Foo', E_USER_NOTICE);
  37. $this->assertCount(2, $handler->getRecords());
  38. $this->assertTrue($handler->hasEmergencyRecords());
  39. } finally {
  40. // restore previous handler
  41. set_error_handler($phpunitHandler);
  42. }
  43. }
  44. public function fatalHandlerProvider()
  45. {
  46. return [
  47. [null, 10, str_repeat(' ', 1024 * 10), LogLevel::ALERT],
  48. [LogLevel::DEBUG, 15, str_repeat(' ', 1024 * 15), LogLevel::DEBUG],
  49. ];
  50. }
  51. protected function getPrivatePropertyValue($instance, $property)
  52. {
  53. $ref = new \ReflectionClass(get_class($instance));
  54. $prop = $ref->getProperty($property);
  55. $prop->setAccessible(true);
  56. return $prop->getValue($instance);
  57. }
  58. /**
  59. * @dataProvider fatalHandlerProvider
  60. */
  61. public function testFatalHandler(
  62. $level,
  63. $reservedMemorySize,
  64. $expectedReservedMemory,
  65. $expectedFatalLevel
  66. ) {
  67. $logger = new Logger('test', [$handler = new TestHandler]);
  68. $errHandler = new ErrorHandler($logger);
  69. $res = $errHandler->registerFatalHandler($level, $reservedMemorySize);
  70. $this->assertSame($res, $errHandler);
  71. $this->assertTrue($this->getPrivatePropertyValue($errHandler, 'hasFatalErrorHandler'));
  72. $this->assertEquals($expectedReservedMemory, $this->getPrivatePropertyValue($errHandler, 'reservedMemory'));
  73. $this->assertEquals($expectedFatalLevel, $this->getPrivatePropertyValue($errHandler, 'fatalLevel'));
  74. }
  75. public function testHandleException()
  76. {
  77. $logger = new Logger('test', [$handler = new TestHandler]);
  78. $errHandler = new ErrorHandler($logger);
  79. $resHandler = $errHandler->registerExceptionHandler($map = ['Monolog\CustomTestException' => LogLevel::DEBUG, 'TypeError' => LogLevel::NOTICE, 'Throwable' => LogLevel::WARNING], false);
  80. $this->assertSame($errHandler, $resHandler);
  81. $map['ParseError'] = LogLevel::CRITICAL;
  82. $prop = $this->getPrivatePropertyValue($errHandler, 'uncaughtExceptionLevelMap');
  83. $this->assertSame($map, $prop);
  84. $errHandler->registerExceptionHandler([], true);
  85. $prop = $this->getPrivatePropertyValue($errHandler, 'previousExceptionHandler');
  86. $this->assertTrue(is_callable($prop));
  87. }
  88. public function testCodeToString()
  89. {
  90. $method = new \ReflectionMethod(ErrorHandler::class, 'codeToString');
  91. $method->setAccessible(true);
  92. $this->assertEquals('E_ERROR', $method->invokeArgs(null, [E_ERROR]));
  93. $this->assertEquals('E_WARNING', $method->invokeArgs(null, [E_WARNING]));
  94. $this->assertEquals('E_PARSE', $method->invokeArgs(null, [E_PARSE]));
  95. $this->assertEquals('E_NOTICE', $method->invokeArgs(null, [E_NOTICE]));
  96. $this->assertEquals('E_CORE_ERROR', $method->invokeArgs(null, [E_CORE_ERROR]));
  97. $this->assertEquals('E_CORE_WARNING', $method->invokeArgs(null, [E_CORE_WARNING]));
  98. $this->assertEquals('E_COMPILE_ERROR', $method->invokeArgs(null, [E_COMPILE_ERROR]));
  99. $this->assertEquals('E_COMPILE_WARNING', $method->invokeArgs(null, [E_COMPILE_WARNING]));
  100. $this->assertEquals('E_USER_ERROR', $method->invokeArgs(null, [E_USER_ERROR]));
  101. $this->assertEquals('E_USER_WARNING', $method->invokeArgs(null, [E_USER_WARNING]));
  102. $this->assertEquals('E_USER_NOTICE', $method->invokeArgs(null, [E_USER_NOTICE]));
  103. $this->assertEquals('E_STRICT', $method->invokeArgs(null, [2048]));
  104. $this->assertEquals('E_RECOVERABLE_ERROR', $method->invokeArgs(null, [E_RECOVERABLE_ERROR]));
  105. $this->assertEquals('E_DEPRECATED', $method->invokeArgs(null, [E_DEPRECATED]));
  106. $this->assertEquals('E_USER_DEPRECATED', $method->invokeArgs(null, [E_USER_DEPRECATED]));
  107. $this->assertEquals('Unknown PHP error', $method->invokeArgs(null, ['RANDOM_TEXT']));
  108. $this->assertEquals('Unknown PHP error', $method->invokeArgs(null, [E_ALL]));
  109. }
  110. }
  111. class CustomTestException extends \Exception
  112. {
  113. }
  114. class CustomCustomException extends CustomTestException
  115. {
  116. }