JsonFormatterTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Formatter;
  11. use Monolog\Logger;
  12. use Monolog\Test\TestCase;
  13. class JsonFormatterTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Formatter\JsonFormatter::__construct
  17. * @covers Monolog\Formatter\JsonFormatter::getBatchMode
  18. * @covers Monolog\Formatter\JsonFormatter::isAppendingNewlines
  19. */
  20. public function testConstruct()
  21. {
  22. $formatter = new JsonFormatter();
  23. $this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode());
  24. $this->assertEquals(true, $formatter->isAppendingNewlines());
  25. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES, false);
  26. $this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode());
  27. $this->assertEquals(false, $formatter->isAppendingNewlines());
  28. }
  29. /**
  30. * @covers Monolog\Formatter\JsonFormatter::format
  31. */
  32. public function testFormat()
  33. {
  34. $formatter = new JsonFormatter();
  35. $record = $this->getRecord();
  36. $this->assertEquals(json_encode($record)."\n", $formatter->format($record));
  37. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
  38. $record = $this->getRecord();
  39. $this->assertEquals('{"message":"test","context":[],"level":300,"level_name":"WARNING","channel":"test","datetime":"'.$record['datetime']->format('Y-m-d\TH:i:s.uP').'","extra":[]}', $formatter->format($record));
  40. }
  41. /**
  42. * @covers Monolog\Formatter\JsonFormatter::formatBatch
  43. * @covers Monolog\Formatter\JsonFormatter::formatBatchJson
  44. */
  45. public function testFormatBatch()
  46. {
  47. $formatter = new JsonFormatter();
  48. $records = [
  49. $this->getRecord(Logger::WARNING),
  50. $this->getRecord(Logger::DEBUG),
  51. ];
  52. $this->assertEquals(json_encode($records), $formatter->formatBatch($records));
  53. }
  54. /**
  55. * @covers Monolog\Formatter\JsonFormatter::formatBatch
  56. * @covers Monolog\Formatter\JsonFormatter::formatBatchNewlines
  57. */
  58. public function testFormatBatchNewlines()
  59. {
  60. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES);
  61. $records = $expected = [
  62. $this->getRecord(Logger::WARNING),
  63. $this->getRecord(Logger::DEBUG),
  64. ];
  65. array_walk($expected, function (&$value, $key) {
  66. $value = json_encode($value);
  67. });
  68. $this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
  69. }
  70. public function testDefFormatWithException()
  71. {
  72. $formatter = new JsonFormatter();
  73. $exception = new \RuntimeException('Foo');
  74. $message = $formatter->format([
  75. 'level_name' => 'CRITICAL',
  76. 'channel' => 'core',
  77. 'context' => ['exception' => $exception],
  78. 'datetime' => new \DateTimeImmutable(),
  79. 'extra' => [],
  80. 'message' => 'foobar',
  81. ]);
  82. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  83. $path = substr(json_encode($exception->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1);
  84. } else {
  85. $path = substr(json_encode($exception->getFile()), 1, -1);
  86. }
  87. $this->assertEquals('{"level_name":"CRITICAL","channel":"core","context":{"exception":{"class":"RuntimeException","message":"'.$exception->getMessage().'","code":'.$exception->getCode().',"file":"'.$path.':'.$exception->getLine().'"}},"datetime":'.json_encode(new \DateTimeImmutable()).',"extra":[],"message":"foobar"}'."\n", $message);
  88. }
  89. public function testDefFormatWithPreviousException()
  90. {
  91. $formatter = new JsonFormatter();
  92. $exception = new \RuntimeException('Foo', 0, new \LogicException('Wut?'));
  93. $message = $formatter->format([
  94. 'level_name' => 'CRITICAL',
  95. 'channel' => 'core',
  96. 'context' => ['exception' => $exception],
  97. 'datetime' => new \DateTimeImmutable(),
  98. 'extra' => [],
  99. 'message' => 'foobar',
  100. ]);
  101. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  102. $pathPrevious = substr(json_encode($exception->getPrevious()->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1);
  103. $pathException = substr(json_encode($exception->getFile(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), 1, -1);
  104. } else {
  105. $pathPrevious = substr(json_encode($exception->getPrevious()->getFile()), 1, -1);
  106. $pathException = substr(json_encode($exception->getFile()), 1, -1);
  107. }
  108. $this->assertEquals('{"level_name":"CRITICAL","channel":"core","context":{"exception":{"class":"RuntimeException","message":"'.$exception->getMessage().'","code":'.$exception->getCode().',"file":"'.$pathException.':'.$exception->getLine().'","previous":{"class":"LogicException","message":"'.$exception->getPrevious()->getMessage().'","code":'.$exception->getPrevious()->getCode().',"file":"'.$pathPrevious.':'.$exception->getPrevious()->getLine().'"}}},"datetime":'.json_encode(new \DateTimeImmutable()).',"extra":[],"message":"foobar"}'."\n", $message);
  109. }
  110. }