2
0

ErrorLogHandlerTest.php 2.1 KB

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