ErrorHandlerTest.php 5.2 KB

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