ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
JsonFormatterTest.php
Go to the documentation of this file.
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
12namespace Monolog\Formatter;
13
16
18{
24 public function testConstruct()
25 {
26 $formatter = new JsonFormatter();
27 $this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode());
28 $this->assertEquals(true, $formatter->isAppendingNewlines());
30 $this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode());
31 $this->assertEquals(false, $formatter->isAppendingNewlines());
32 }
33
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
52 public function testFormatBatch()
53 {
54 $formatter = new JsonFormatter();
55 $records = array(
58 );
59 $this->assertEquals(json_encode($records), $formatter->formatBatch($records));
60 }
61
66 public function testFormatBatchNewlines()
67 {
69 $records = $expected = array(
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
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
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
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
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
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
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
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
185 {
186 $formatter = new NormalizerFormatter();
187 $largeArray = range(1, 1000);
188
189 $res = $formatter->format(array(
190 'level_name' => 'CRITICAL',
191 'channel' => 'test',
192 'message' => 'bar',
193 'context' => array($largeArray),
194 'datetime' => new \DateTime,
195 'extra' => array(),
196 ));
197
198 $this->assertCount(1000, $res['context'][0]);
199 $this->assertArrayNotHasKey('...', $res['context'][0]);
200 }
201
203 {
204 $formatter = new NormalizerFormatter();
205 $largeArray = range(1, 2000);
206
207 $res = $formatter->format(array(
208 'level_name' => 'CRITICAL',
209 'channel' => 'test',
210 'message' => 'bar',
211 'context' => array($largeArray),
212 'datetime' => new \DateTime,
213 'extra' => array(),
214 ));
215
216 $this->assertCount(1001, $res['context'][0]);
217 $this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']);
218 }
219}
$path
Definition: aliased.php:25
An exception for terminatinating execution or to throw for unit testing.
testFormatBatch()
@covers Monolog\Formatter\JsonFormatter::formatBatch @covers Monolog\Formatter\JsonFormatter::formatB...
formatException($exception, $previous=null)
testFormat()
@covers Monolog\Formatter\JsonFormatter::format
assertContextContainsFormattedException($expected, $actual)
testConstruct()
@covers Monolog\Formatter\JsonFormatter::__construct @covers Monolog\Formatter\JsonFormatter::getBatc...
formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
testFormatBatchNewlines()
@covers Monolog\Formatter\JsonFormatter::formatBatch @covers Monolog\Formatter\JsonFormatter::formatB...
Encodes whatever record data is passed to it as json.
format(array $record)
{{Formats a log record.mixed The formatted record}}
Normalizes incoming records to remove objects/resources so it's easier to dump to various targets.
Monolog log channel.
Definition: Logger.php:29
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:53
const DEBUG
Detailed debug information.
Definition: Logger.php:33
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
$key
Definition: croninfo.php:18
catch(Exception $e) $message
foreach($_POST as $key=> $value) $res
$records
Definition: simple_test.php:22