File: ScalarFormatterTest.php

package info (click to toggle)
php-monolog 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,456 kB
  • sloc: php: 16,836; makefile: 20
file content (107 lines) | stat: -rw-r--r-- 3,102 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php declare(strict_types=1);

/*
 * This file is part of the Monolog package.
 *
 * (c) Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Monolog\Formatter;

use Monolog\JsonSerializableDateTimeImmutable;

class ScalarFormatterTest extends \Monolog\Test\MonologTestCase
{
    private ScalarFormatter $formatter;

    public function setUp(): void
    {
        $this->formatter = new ScalarFormatter();
    }

    public function tearDown(): void
    {
        parent::tearDown();

        unset($this->formatter);
    }

    public function buildTrace(\Exception $e)
    {
        $data = [];
        $trace = $e->getTrace();
        foreach ($trace as $frame) {
            if (isset($frame['file'])) {
                $data[] = $frame['file'].':'.$frame['line'];
            }
        }

        return $data;
    }

    public function encodeJson($data)
    {
        return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
    }

    public function testFormat()
    {
        $exception = new \Exception('foo');
        $formatted = $this->formatter->format($this->getRecord(context: [
            'foo' => 'string',
            'bar' => 1,
            'baz' => false,
            'bam' => [1, 2, 3],
            'bat' => ['foo' => 'bar'],
            'bap' => $dt = new JsonSerializableDateTimeImmutable(true),
            'ban' => $exception,
        ]));

        $this->assertSame($this->encodeJson([
            'foo' => 'string',
            'bar' => 1,
            'baz' => false,
            'bam' => [1, 2, 3],
            'bat' => ['foo' => 'bar'],
            'bap' => (string) $dt,
            'ban' => [
                'class'   => \get_class($exception),
                'message' => $exception->getMessage(),
                'code'    => $exception->getCode(),
                'file'    => $exception->getFile() . ':' . $exception->getLine(),
                'trace'   => $this->buildTrace($exception),
            ],
        ]), $formatted['context']);
    }

    public function testFormatWithErrorContext()
    {
        $context = ['file' => 'foo', 'line' => 1];
        $formatted = $this->formatter->format($this->getRecord(
            context: $context,
        ));

        $this->assertSame($this->encodeJson($context), $formatted['context']);
    }

    public function testFormatWithExceptionContext()
    {
        $exception = new \Exception('foo');
        $formatted = $this->formatter->format($this->getRecord(context: [
            'exception' => $exception,
        ]));

        $this->assertSame($this->encodeJson([
            'exception' => [
                'class'   => \get_class($exception),
                'message' => $exception->getMessage(),
                'code'    => $exception->getCode(),
                'file'    => $exception->getFile() . ':' . $exception->getLine(),
                'trace'   => $this->buildTrace($exception),
            ],
        ]), $formatted['context']);
    }
}