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