ScalarFormatterTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. class ScalarFormatterTest extends \Monolog\Test\MonologTestCase
  13. {
  14. private ScalarFormatter $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($this->getRecord(context: [
  43. 'foo' => 'string',
  44. 'bar' => 1,
  45. 'baz' => false,
  46. 'bam' => [1, 2, 3],
  47. 'bat' => ['foo' => 'bar'],
  48. 'bap' => $dt = new JsonSerializableDateTimeImmutable(true),
  49. 'ban' => $exception,
  50. ]));
  51. $this->assertSame($this->encodeJson([
  52. 'foo' => 'string',
  53. 'bar' => 1,
  54. 'baz' => false,
  55. 'bam' => [1, 2, 3],
  56. 'bat' => ['foo' => 'bar'],
  57. 'bap' => (string) $dt,
  58. 'ban' => [
  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['context']);
  66. }
  67. public function testFormatWithErrorContext()
  68. {
  69. $context = ['file' => 'foo', 'line' => 1];
  70. $formatted = $this->formatter->format($this->getRecord(
  71. context: $context,
  72. ));
  73. $this->assertSame($this->encodeJson($context), $formatted['context']);
  74. }
  75. public function testFormatWithExceptionContext()
  76. {
  77. $exception = new \Exception('foo');
  78. $formatted = $this->formatter->format($this->getRecord(context: [
  79. 'exception' => $exception,
  80. ]));
  81. $this->assertSame($this->encodeJson([
  82. 'exception' => [
  83. 'class' => \get_class($exception),
  84. 'message' => $exception->getMessage(),
  85. 'code' => $exception->getCode(),
  86. 'file' => $exception->getFile() . ':' . $exception->getLine(),
  87. 'trace' => $this->buildTrace($exception),
  88. ],
  89. ]), $formatted['context']);
  90. }
  91. }