ErrorHandlerTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testHandleError()
  15. {
  16. $logger = new Logger('test', [$handler = new TestHandler]);
  17. $errHandler = new ErrorHandler($logger);
  18. $errHandler->registerErrorHandler([E_USER_NOTICE => Logger::EMERGENCY], false);
  19. trigger_error('Foo', E_USER_ERROR);
  20. $this->assertCount(1, $handler->getRecords());
  21. $this->assertTrue($handler->hasErrorRecords());
  22. trigger_error('Foo', E_USER_NOTICE);
  23. $this->assertCount(2, $handler->getRecords());
  24. $this->assertTrue($handler->hasEmergencyRecords());
  25. }
  26. public function testHandleException()
  27. {
  28. $logger = new Logger('test', [$handler = new TestHandler]);
  29. $errHandler = new ErrorHandler($logger);
  30. $errHandler->registerExceptionHandler(['Monolog\CustomTestException' => Logger::ALERT, 'Throwable' => Logger::WARNING], false);
  31. try {
  32. throw new CustomCustomException();
  33. $this->assertCount(1, $handler->getRecords());
  34. $this->assertTrue($handler->hasAlertRecords());
  35. } catch (\Throwable $e) {
  36. }
  37. try {
  38. throw new CustomTestException();
  39. $this->assertCount(2, $handler->getRecords());
  40. $this->assertTrue($handler->hasAlertRecords());
  41. } catch (\Throwable $e) {
  42. }
  43. try {
  44. throw new RuntimeException();
  45. $this->assertCount(3, $handler->getRecords());
  46. $this->assertTrue($handler->hasWarningRecords());
  47. } catch (\Throwable $e) {
  48. }
  49. }
  50. }
  51. class CustomTestException extends \Exception
  52. {
  53. }
  54. class CustomCustomException extends CustomTestException
  55. {
  56. }