ScalarFormatterTest.php 3.0 KB

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