2
0

ErrorLogHandlerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\Test\TestCase;
  12. use Monolog\Level;
  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(): void
  21. {
  22. $GLOBALS['error_log'] = [];
  23. }
  24. /**
  25. * @covers Monolog\Handler\ErrorLogHandler::__construct
  26. */
  27. public function testShouldNotAcceptAnInvalidTypeOnConstructor()
  28. {
  29. $this->expectException(\InvalidArgumentException::class);
  30. $this->expectExceptionMessage('The given message type "42" is not supported');
  31. new ErrorLogHandler(42);
  32. }
  33. /**
  34. * @covers Monolog\Handler\ErrorLogHandler::write
  35. */
  36. public function testShouldLogMessagesUsingErrorLogFunction()
  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(Level::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, Level::Debug, true, true);
  45. $handler->setFormatter(new LineFormatter(null, null, true));
  46. $handler->handle($this->getRecord(Level::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. }