ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Monolog\Formatter\JsonFormatterTest Class Reference
+ Inheritance diagram for Monolog\Formatter\JsonFormatterTest:
+ Collaboration diagram for Monolog\Formatter\JsonFormatterTest:

Public Member Functions

 testConstruct ()
 Monolog::__construct Monolog::getBatchMode Monolog::isAppendingNewlines More...
 
 testFormat ()
 Monolog::format More...
 
 testFormatBatch ()
 Monolog::formatBatch Monolog::formatBatchJson More...
 
 testFormatBatchNewlines ()
 Monolog::formatBatch Monolog::formatBatchNewlines More...
 
 testDefFormatWithException ()
 
 testDefFormatWithPreviousException ()
 
 testDefFormatWithThrowable ()
 
 testNormalizeHandleLargeArraysWithExactly1000Items ()
 
 testNormalizeHandleLargeArrays ()
 

Private Member Functions

 assertContextContainsFormattedException ($expected, $actual)
 
 formatRecordWithExceptionInContext (JsonFormatter $formatter, $exception)
 
 formatExceptionFilePathWithLine ($exception)
 
 formatException ($exception, $previous=null)
 

Additional Inherited Members

- Protected Member Functions inherited from Monolog\TestCase
 getRecord ($level=Logger::WARNING, $message='test', $context=array())
 
 getMultipleRecords ()
 
 getIdentityFormatter ()
 

Detailed Description

Definition at line 17 of file JsonFormatterTest.php.

Member Function Documentation

◆ assertContextContainsFormattedException()

Monolog\Formatter\JsonFormatterTest::assertContextContainsFormattedException (   $expected,
  $actual 
)
private
Parameters
string$expected
string$actual

Definition at line 123 of file JsonFormatterTest.php.

Referenced by Monolog\Formatter\JsonFormatterTest\testDefFormatWithException(), Monolog\Formatter\JsonFormatterTest\testDefFormatWithPreviousException(), and Monolog\Formatter\JsonFormatterTest\testDefFormatWithThrowable().

124  {
125  $this->assertEquals(
126  '{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":[],"message":"foobar"}'."\n",
127  $actual
128  );
129  }
+ Here is the caller graph for this function:

◆ formatException()

Monolog\Formatter\JsonFormatterTest::formatException (   $exception,
  $previous = null 
)
private
Parameters
\Exception | \Throwable$exception
null | string$previous
Returns
string

Definition at line 172 of file JsonFormatterTest.php.

References Monolog\Formatter\JsonFormatterTest\formatExceptionFilePathWithLine().

Referenced by Monolog\Formatter\JsonFormatterTest\testDefFormatWithException(), Monolog\Formatter\JsonFormatterTest\testDefFormatWithPreviousException(), and Monolog\Formatter\JsonFormatterTest\testDefFormatWithThrowable().

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  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ formatExceptionFilePathWithLine()

Monolog\Formatter\JsonFormatterTest::formatExceptionFilePathWithLine (   $exception)
private
Parameters
\Exception | \Throwable$exception
Returns
string

Definition at line 155 of file JsonFormatterTest.php.

References PHPMailer\PHPMailer\$options, and $path.

Referenced by Monolog\Formatter\JsonFormatterTest\formatException().

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  }
$path
Definition: aliased.php:25
+ Here is the caller graph for this function:

◆ formatRecordWithExceptionInContext()

Monolog\Formatter\JsonFormatterTest::formatRecordWithExceptionInContext ( JsonFormatter  $formatter,
  $exception 
)
private
Parameters
JsonFormatter$formatter
\Exception | \Throwable$exception
Returns
string

Definition at line 137 of file JsonFormatterTest.php.

References $message, and Monolog\Formatter\JsonFormatter\format().

Referenced by Monolog\Formatter\JsonFormatterTest\testDefFormatWithException(), Monolog\Formatter\JsonFormatterTest\testDefFormatWithPreviousException(), and Monolog\Formatter\JsonFormatterTest\testDefFormatWithThrowable().

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  }
catch(Exception $e) $message
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ testConstruct()

Monolog\Formatter\JsonFormatterTest::testConstruct ( )

Monolog::__construct Monolog::getBatchMode Monolog::isAppendingNewlines

Definition at line 24 of file JsonFormatterTest.php.

References Monolog\Formatter\JsonFormatter\BATCH_MODE_JSON, and Monolog\Formatter\JsonFormatter\BATCH_MODE_NEWLINES.

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  }

◆ testDefFormatWithException()

Monolog\Formatter\JsonFormatterTest::testDefFormatWithException ( )

Definition at line 79 of file JsonFormatterTest.php.

References $message, Monolog\Formatter\JsonFormatterTest\assertContextContainsFormattedException(), Monolog\Formatter\JsonFormatterTest\formatException(), and Monolog\Formatter\JsonFormatterTest\formatRecordWithExceptionInContext().

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  }
assertContextContainsFormattedException($expected, $actual)
catch(Exception $e) $message
formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
formatException($exception, $previous=null)
+ Here is the call graph for this function:

◆ testDefFormatWithPreviousException()

Monolog\Formatter\JsonFormatterTest::testDefFormatWithPreviousException ( )

Definition at line 90 of file JsonFormatterTest.php.

References $message, Monolog\Formatter\JsonFormatterTest\assertContextContainsFormattedException(), Monolog\Formatter\JsonFormatterTest\formatException(), and Monolog\Formatter\JsonFormatterTest\formatRecordWithExceptionInContext().

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  }
assertContextContainsFormattedException($expected, $actual)
catch(Exception $e) $message
formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
formatException($exception, $previous=null)
+ Here is the call graph for this function:

◆ testDefFormatWithThrowable()

Monolog\Formatter\JsonFormatterTest::testDefFormatWithThrowable ( )

Definition at line 102 of file JsonFormatterTest.php.

References $message, Monolog\Formatter\JsonFormatterTest\assertContextContainsFormattedException(), Monolog\Formatter\JsonFormatterTest\formatException(), and Monolog\Formatter\JsonFormatterTest\formatRecordWithExceptionInContext().

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  }
assertContextContainsFormattedException($expected, $actual)
catch(Exception $e) $message
formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
formatException($exception, $previous=null)
+ Here is the call graph for this function:

◆ testFormat()

Monolog\Formatter\JsonFormatterTest::testFormat ( )

Monolog::format

Definition at line 37 of file JsonFormatterTest.php.

References Monolog\Formatter\JsonFormatter\BATCH_MODE_JSON, and Monolog\TestCase\getRecord().

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  }
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
+ Here is the call graph for this function:

◆ testFormatBatch()

Monolog\Formatter\JsonFormatterTest::testFormatBatch ( )

Monolog::formatBatch Monolog::formatBatchJson

Definition at line 52 of file JsonFormatterTest.php.

References $records, Monolog\Logger\DEBUG, Monolog\TestCase\getRecord(), and Monolog\Logger\WARNING.

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  }
const DEBUG
Detailed debug information.
Definition: Logger.php:33
$records
Definition: simple_test.php:22
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:53
+ Here is the call graph for this function:

◆ testFormatBatchNewlines()

Monolog\Formatter\JsonFormatterTest::testFormatBatchNewlines ( )

Monolog::formatBatch Monolog::formatBatchNewlines

Definition at line 66 of file JsonFormatterTest.php.

References $key, $records, Monolog\Formatter\JsonFormatter\BATCH_MODE_NEWLINES, Monolog\Logger\DEBUG, Monolog\TestCase\getRecord(), and Monolog\Logger\WARNING.

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  }
const DEBUG
Detailed debug information.
Definition: Logger.php:33
$records
Definition: simple_test.php:22
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:53
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:

◆ testNormalizeHandleLargeArrays()

Monolog\Formatter\JsonFormatterTest::testNormalizeHandleLargeArrays ( )

Definition at line 202 of file JsonFormatterTest.php.

References $res.

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  }
foreach($_POST as $key=> $value) $res

◆ testNormalizeHandleLargeArraysWithExactly1000Items()

Monolog\Formatter\JsonFormatterTest::testNormalizeHandleLargeArraysWithExactly1000Items ( )

Definition at line 184 of file JsonFormatterTest.php.

References $res.

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  }
foreach($_POST as $key=> $value) $res

The documentation for this class was generated from the following file: