JsonFormatterTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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\TestCase;
  13. class JsonFormatterTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Formatter\JsonFormatter::__construct
  17. * @covers Monolog\Formatter\JsonFormatter::getBatchMode
  18. */
  19. public function testConstruct()
  20. {
  21. $formatter = new JsonFormatter();
  22. $this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode());
  23. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES);
  24. $this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode());
  25. }
  26. /**
  27. * @covers Monolog\Formatter\JsonFormatter::format
  28. */
  29. public function testFormat()
  30. {
  31. $formatter = new JsonFormatter();
  32. $record = $this->getRecord();
  33. $this->assertEquals(json_encode($record), $formatter->format($record));
  34. }
  35. /**
  36. * @covers Monolog\Formatter\JsonFormatter::formatBatch
  37. * @covers Monolog\Formatter\JsonFormatter::formatBatchJson
  38. */
  39. public function testFormatBatch()
  40. {
  41. $formatter = new JsonFormatter();
  42. $records = array(
  43. $this->getRecord(Logger::WARNING),
  44. $this->getRecord(Logger::DEBUG),
  45. );
  46. $this->assertEquals(json_encode($records), $formatter->formatBatch($records));
  47. }
  48. /**
  49. * @covers Monolog\Formatter\JsonFormatter::formatBatch
  50. * @covers Monolog\Formatter\JsonFormatter::formatBatchNewlines
  51. */
  52. public function testFormatBatchNewlines()
  53. {
  54. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES);
  55. $records = $expected = array(
  56. $this->getRecord(Logger::WARNING),
  57. $this->getRecord(Logger::DEBUG),
  58. );
  59. array_walk($expected, function(&$value, $key) {
  60. $value = json_encode($value);
  61. });
  62. $this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
  63. }
  64. }