scratch – Blame information for rev 120
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
120 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of the Monolog package. |
||
5 | * |
||
6 | * (c) Jordi Boggiano <j.boggiano@seld.be> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Monolog\Formatter; |
||
13 | |||
14 | use Monolog\Logger; |
||
15 | use Monolog\TestCase; |
||
16 | |||
17 | class JsonFormatterTest extends TestCase |
||
18 | { |
||
19 | /** |
||
20 | * @covers Monolog\Formatter\JsonFormatter::__construct |
||
21 | * @covers Monolog\Formatter\JsonFormatter::getBatchMode |
||
22 | * @covers Monolog\Formatter\JsonFormatter::isAppendingNewlines |
||
23 | */ |
||
24 | public function testConstruct() |
||
25 | { |
||
26 | $formatter = new JsonFormatter(); |
||
27 | $this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode()); |
||
28 | $this->assertEquals(true, $formatter->isAppendingNewlines()); |
||
29 | $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES, false); |
||
30 | $this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode()); |
||
31 | $this->assertEquals(false, $formatter->isAppendingNewlines()); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @covers Monolog\Formatter\JsonFormatter::format |
||
36 | */ |
||
37 | public function testFormat() |
||
38 | { |
||
39 | $formatter = new JsonFormatter(); |
||
40 | $record = $this->getRecord(); |
||
41 | $this->assertEquals(json_encode($record)."\n", $formatter->format($record)); |
||
42 | |||
43 | $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false); |
||
44 | $record = $this->getRecord(); |
||
45 | $this->assertEquals(json_encode($record), $formatter->format($record)); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @covers Monolog\Formatter\JsonFormatter::formatBatch |
||
50 | * @covers Monolog\Formatter\JsonFormatter::formatBatchJson |
||
51 | */ |
||
52 | public function testFormatBatch() |
||
53 | { |
||
54 | $formatter = new JsonFormatter(); |
||
55 | $records = array( |
||
56 | $this->getRecord(Logger::WARNING), |
||
57 | $this->getRecord(Logger::DEBUG), |
||
58 | ); |
||
59 | $this->assertEquals(json_encode($records), $formatter->formatBatch($records)); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @covers Monolog\Formatter\JsonFormatter::formatBatch |
||
64 | * @covers Monolog\Formatter\JsonFormatter::formatBatchNewlines |
||
65 | */ |
||
66 | public function testFormatBatchNewlines() |
||
67 | { |
||
68 | $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES); |
||
69 | $records = $expected = array( |
||
70 | $this->getRecord(Logger::WARNING), |
||
71 | $this->getRecord(Logger::DEBUG), |
||
72 | ); |
||
73 | array_walk($expected, function (&$value, $key) { |
||
74 | $value = json_encode($value); |
||
75 | }); |
||
76 | $this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records)); |
||
77 | } |
||
78 | |||
79 | public function testDefFormatWithException() |
||
80 | { |
||
81 | $formatter = new JsonFormatter(); |
||
82 | $exception = new \RuntimeException('Foo'); |
||
83 | $formattedException = $this->formatException($exception); |
||
84 | |||
85 | $message = $this->formatRecordWithExceptionInContext($formatter, $exception); |
||
86 | |||
87 | $this->assertContextContainsFormattedException($formattedException, $message); |
||
88 | } |
||
89 | |||
90 | public function testDefFormatWithPreviousException() |
||
91 | { |
||
92 | $formatter = new JsonFormatter(); |
||
93 | $exception = new \RuntimeException('Foo', 0, new \LogicException('Wut?')); |
||
94 | $formattedPrevException = $this->formatException($exception->getPrevious()); |
||
95 | $formattedException = $this->formatException($exception, $formattedPrevException); |
||
96 | |||
97 | $message = $this->formatRecordWithExceptionInContext($formatter, $exception); |
||
98 | |||
99 | $this->assertContextContainsFormattedException($formattedException, $message); |
||
100 | } |
||
101 | |||
102 | public function testDefFormatWithThrowable() |
||
103 | { |
||
104 | if (!class_exists('Error') || !is_subclass_of('Error', 'Throwable')) { |
||
105 | $this->markTestSkipped('Requires PHP >=7'); |
||
106 | } |
||
107 | |||
108 | $formatter = new JsonFormatter(); |
||
109 | $throwable = new \Error('Foo'); |
||
110 | $formattedThrowable = $this->formatException($throwable); |
||
111 | |||
112 | $message = $this->formatRecordWithExceptionInContext($formatter, $throwable); |
||
113 | |||
114 | $this->assertContextContainsFormattedException($formattedThrowable, $message); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $expected |
||
119 | * @param string $actual |
||
120 | * |
||
121 | * @internal param string $exception |
||
122 | */ |
||
123 | private function assertContextContainsFormattedException($expected, $actual) |
||
124 | { |
||
125 | $this->assertEquals( |
||
126 | '{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":[],"message":"foobar"}'."\n", |
||
127 | $actual |
||
128 | ); |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @param JsonFormatter $formatter |
||
133 | * @param \Exception|\Throwable $exception |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | private function formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception) |
||
138 | { |
||
139 | $message = $formatter->format(array( |
||
140 | 'level_name' => 'CRITICAL', |
||
141 | 'channel' => 'core', |
||
142 | 'context' => array('exception' => $exception), |
||
143 | 'datetime' => null, |
||
144 | 'extra' => array(), |
||
145 | 'message' => 'foobar', |
||
146 | )); |
||
147 | return $message; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * @param \Exception|\Throwable $exception |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | private function formatExceptionFilePathWithLine($exception) |
||
156 | { |
||
157 | $options = 0; |
||
158 | if (version_compare(PHP_VERSION, '5.4.0', '>=')) { |
||
159 | $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; |
||
160 | } |
||
161 | $path = substr(json_encode($exception->getFile(), $options), 1, -1); |
||
162 | return $path . ':' . $exception->getLine(); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param \Exception|\Throwable $exception |
||
167 | * |
||
168 | * @param null|string $previous |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | private function formatException($exception, $previous = null) |
||
173 | { |
||
174 | $formattedException = |
||
175 | '{"class":"' . get_class($exception) . |
||
176 | '","message":"' . $exception->getMessage() . |
||
177 | '","code":' . $exception->getCode() . |
||
178 | ',"file":"' . $this->formatExceptionFilePathWithLine($exception) . |
||
179 | ($previous ? '","previous":' . $previous : '"') . |
||
180 | '}'; |
||
181 | return $formattedException; |
||
182 | } |
||
183 | } |