SyslogFormatterTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\Formatter;
  11. use DateTimeImmutable;
  12. use Monolog\Level;
  13. use Monolog\LogRecord;
  14. use PHPUnit\Framework\TestCase;
  15. class SyslogFormatterTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider formatDataProvider
  19. *
  20. * @param string $expected
  21. * @param DateTimeImmutable $dateTime
  22. * @param string $channel
  23. * @param Level $level
  24. * @param string $message
  25. * @param string|null $appName
  26. * @param mixed[] $context
  27. * @param mixed[] $extra
  28. * @return void
  29. */
  30. public function testFormat(
  31. string $expected,
  32. DateTimeImmutable $dateTime,
  33. string $channel,
  34. Level $level,
  35. string $message,
  36. string $appName = null,
  37. array $context = [],
  38. array $extra = []
  39. ): void {
  40. if ($appName !== null) {
  41. $formatter = new SyslogFormatter($appName);
  42. } else {
  43. $formatter = new SyslogFormatter();
  44. }
  45. $record = new LogRecord(
  46. datetime: $dateTime,
  47. channel: $channel,
  48. level: $level,
  49. message: $message,
  50. context: $context,
  51. extra: $extra
  52. );
  53. $message = $formatter->format($record);
  54. $this->assertEquals($expected, $message);
  55. }
  56. /**
  57. * @return mixed[]
  58. */
  59. public function formatDataProvider(): array
  60. {
  61. return [
  62. 'error' => [
  63. 'expected' => "<11>1 1970-01-01T00:00:00.000000+00:00 " . gethostname() . " - " . getmypid() ." meh - ERROR: log \n",
  64. 'dateTime' => new DateTimeImmutable("@0"),
  65. 'channel' => 'meh',
  66. 'level' => Level::Error,
  67. 'message' => 'log',
  68. ],
  69. 'info' => [
  70. 'expected' => "<11>1 1970-01-01T00:00:00.000000+00:00 " . gethostname() . " - " . getmypid() ." meh - ERROR: log \n",
  71. 'dateTime' => new DateTimeImmutable("@0"),
  72. 'channel' => 'meh',
  73. 'level' => Level::Error,
  74. 'message' => 'log',
  75. ],
  76. 'with app name' => [
  77. 'expected' => "<11>1 1970-01-01T00:00:00.000000+00:00 " . gethostname() . " my-app " . getmypid() ." meh - ERROR: log \n",
  78. 'dateTime' => new DateTimeImmutable("@0"),
  79. 'channel' => 'meh',
  80. 'level' => Level::Error,
  81. 'message' => 'log',
  82. 'appName' => 'my-app',
  83. ],
  84. 'with context' => [
  85. 'expected' => "<11>1 1970-01-01T00:00:00.000000+00:00 " . gethostname() . " - " . getmypid() ." meh - ERROR: log {\"additional-context\":\"test\"} \n",
  86. 'dateTime' => new DateTimeImmutable("@0"),
  87. 'channel' => 'meh',
  88. 'level' => Level::Error,
  89. 'message' => 'log',
  90. 'appName' => null,
  91. 'context' => ['additional-context' => 'test'],
  92. ],
  93. 'with extra' => [
  94. 'expected' => "<11>1 1970-01-01T00:00:00.000000+00:00 " . gethostname() . " - " . getmypid() ." meh - ERROR: log {\"userId\":1}\n",
  95. 'dateTime' => new DateTimeImmutable("@0"),
  96. 'channel' => 'meh',
  97. 'level' => Level::Error,
  98. 'message' => 'log',
  99. 'appName' => null,
  100. 'context' => [],
  101. 'extra' => ['userId' => 1],
  102. ],
  103. ];
  104. }
  105. }