ScalarFormatterTest.php 3.1 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 Monolog\DateTimeImmutable;
  12. class ScalarFormatterTest extends \PHPUnit\Framework\TestCase
  13. {
  14. private $formatter;
  15. public function setUp(): void
  16. {
  17. $this->formatter = new ScalarFormatter();
  18. }
  19. public function tearDown(): void
  20. {
  21. parent::tearDown();
  22. unset($this->formatter);
  23. }
  24. public function buildTrace(\Exception $e)
  25. {
  26. $data = [];
  27. $trace = $e->getTrace();
  28. foreach ($trace as $frame) {
  29. if (isset($frame['file'])) {
  30. $data[] = $frame['file'].':'.$frame['line'];
  31. }
  32. }
  33. return $data;
  34. }
  35. public function encodeJson($data)
  36. {
  37. return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  38. }
  39. public function testFormat()
  40. {
  41. $exception = new \Exception('foo');
  42. $formatted = $this->formatter->format([
  43. 'foo' => 'string',
  44. 'bar' => 1,
  45. 'baz' => false,
  46. 'bam' => [1, 2, 3],
  47. 'bat' => ['foo' => 'bar'],
  48. 'bap' => $dt = new DateTimeImmutable(true),
  49. 'ban' => $exception,
  50. ]);
  51. $this->assertSame([
  52. 'foo' => 'string',
  53. 'bar' => 1,
  54. 'baz' => false,
  55. 'bam' => $this->encodeJson([1, 2, 3]),
  56. 'bat' => $this->encodeJson(['foo' => 'bar']),
  57. 'bap' => (string) $dt,
  58. 'ban' => $this->encodeJson([
  59. 'class' => get_class($exception),
  60. 'message' => $exception->getMessage(),
  61. 'code' => $exception->getCode(),
  62. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  63. 'trace' => $this->buildTrace($exception),
  64. ]),
  65. ], $formatted);
  66. }
  67. public function testFormatWithErrorContext()
  68. {
  69. $context = ['file' => 'foo', 'line' => 1];
  70. $formatted = $this->formatter->format([
  71. 'context' => $context,
  72. ]);
  73. $this->assertSame([
  74. 'context' => $this->encodeJson($context),
  75. ], $formatted);
  76. }
  77. public function testFormatWithExceptionContext()
  78. {
  79. $exception = new \Exception('foo');
  80. $formatted = $this->formatter->format([
  81. 'context' => [
  82. 'exception' => $exception,
  83. ],
  84. ]);
  85. $this->assertSame([
  86. 'context' => $this->encodeJson([
  87. 'exception' => [
  88. 'class' => get_class($exception),
  89. 'message' => $exception->getMessage(),
  90. 'code' => $exception->getCode(),
  91. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  92. 'trace' => $this->buildTrace($exception),
  93. ],
  94. ]),
  95. ], $formatted);
  96. }
  97. }