2
0

ErrorHandlerTest.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  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', array($handler = new TestHandler));
  17. $errHandler = new ErrorHandler($logger);
  18. $errHandler->registerErrorHandler(array(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', array($handler = new TestHandler));
  29. $errHandler = new ErrorHandler($logger);
  30. $errHandler->registerExceptionHandler(array('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. try {
  37. throw new CustomTestException();
  38. $this->assertCount(2, $handler->getRecords());
  39. $this->assertTrue($handler->hasAlertRecords());
  40. } catch (\Throwable $e) {}
  41. try {
  42. throw new RuntimeException();
  43. $this->assertCount(3, $handler->getRecords());
  44. $this->assertTrue($handler->hasWarningRecords());
  45. } catch (\Throwable $e) {}
  46. }
  47. }
  48. class CustomTestException extends \Exception {}
  49. class CustomCustomException extends CustomTestException {}