ErrorHandlerTest.php 5.5 KB

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