ErrorLogHandlerTest.php 2.1 KB

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