ScalarFormatterTest.php 3.0 KB

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