ErrorLogHandlerTest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Monolog\Handler;
  3. use Monolog\TestCase;
  4. use Monolog\Logger;
  5. use Monolog\Formatter\LineFormatter;
  6. function error_log()
  7. {
  8. $GLOBALS['error_log'][] = func_get_args();
  9. }
  10. class ErrorLogHandlerTest extends TestCase
  11. {
  12. protected function setUp()
  13. {
  14. $GLOBALS['error_log'] = array();
  15. }
  16. /**
  17. * @covers Monolog\Handler\ErrorLogHandler::__construct
  18. * @expectedException InvalidArgumentException
  19. * @expectedExceptionMessage The given message type "42" is not supported
  20. */
  21. public function testShouldNotAcceptAnInvalidTypeOnContructor()
  22. {
  23. new ErrorLogHandler(42);
  24. }
  25. /**
  26. * @covers Monolog\Handler\ErrorLogHandler::write
  27. */
  28. public function testShouldLogMessagesUsingErrorLogFuncion()
  29. {
  30. $type = ErrorLogHandler::OPERATING_SYSTEM;
  31. $handler = new ErrorLogHandler($type);
  32. $handler->setFormatter(new LineFormatter('%channel%.%level_name%: %message% %context% %extra%', null, true));
  33. $handler->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
  34. $this->assertSame("test.ERROR: Foo\nBar\r\n\r\nBaz [] []", $GLOBALS['error_log'][0][0]);
  35. $this->assertSame($GLOBALS['error_log'][0][1], $type);
  36. $handler = new ErrorLogHandler($type, Logger::DEBUG, true, true);
  37. $handler->setFormatter(new LineFormatter(null, null, true));
  38. $handler->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
  39. $this->assertStringMatchesFormat('[%s] test.ERROR: Foo', $GLOBALS['error_log'][1][0]);
  40. $this->assertSame($GLOBALS['error_log'][1][1], $type);
  41. $this->assertStringMatchesFormat('Bar', $GLOBALS['error_log'][2][0]);
  42. $this->assertSame($GLOBALS['error_log'][2][1], $type);
  43. $this->assertStringMatchesFormat('Baz [] []', $GLOBALS['error_log'][3][0]);
  44. $this->assertSame($GLOBALS['error_log'][3][1], $type);
  45. }
  46. }