ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
ScalarFormatterTest.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 
12 namespace Monolog\Formatter;
13 
15 {
16  private $formatter;
17 
18  public function setUp()
19  {
20  $this->formatter = new ScalarFormatter();
21  }
22 
23  public function buildTrace(\Exception $e)
24  {
25  $data = array();
26  $trace = $e->getTrace();
27  foreach ($trace as $frame) {
28  if (isset($frame['file'])) {
29  $data[] = $frame['file'].':'.$frame['line'];
30  } else {
31  $data[] = json_encode($frame);
32  }
33  }
34 
35  return $data;
36  }
37 
38  public function encodeJson($data)
39  {
40  if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
41  return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
42  }
43 
44  return json_encode($data);
45  }
46 
47  public function testFormat()
48  {
49  $exception = new \Exception('foo');
50  $formatted = $this->formatter->format(array(
51  'foo' => 'string',
52  'bar' => 1,
53  'baz' => false,
54  'bam' => array(1, 2, 3),
55  'bat' => array('foo' => 'bar'),
56  'bap' => \DateTime::createFromFormat(\DateTime::ISO8601, '1970-01-01T00:00:00+0000'),
57  'ban' => $exception,
58  ));
59 
60  $this->assertSame(array(
61  'foo' => 'string',
62  'bar' => 1,
63  'baz' => false,
64  'bam' => $this->encodeJson(array(1, 2, 3)),
65  'bat' => $this->encodeJson(array('foo' => 'bar')),
66  'bap' => '1970-01-01 00:00:00',
67  'ban' => $this->encodeJson(array(
68  'class' => get_class($exception),
69  'message' => $exception->getMessage(),
70  'code' => $exception->getCode(),
71  'file' => $exception->getFile() . ':' . $exception->getLine(),
72  'trace' => $this->buildTrace($exception),
73  )),
74  ), $formatted);
75  }
76 
77  public function testFormatWithErrorContext()
78  {
79  $context = array('file' => 'foo', 'line' => 1);
80  $formatted = $this->formatter->format(array(
81  'context' => $context,
82  ));
83 
84  $this->assertSame(array(
85  'context' => $this->encodeJson($context),
86  ), $formatted);
87  }
88 
90  {
91  $exception = new \Exception('foo');
92  $formatted = $this->formatter->format(array(
93  'context' => array(
94  'exception' => $exception,
95  ),
96  ));
97 
98  $this->assertSame(array(
99  'context' => $this->encodeJson(array(
100  'exception' => array(
101  'class' => get_class($exception),
102  'message' => $exception->getMessage(),
103  'code' => $exception->getCode(),
104  'file' => $exception->getFile() . ':' . $exception->getLine(),
105  'trace' => $this->buildTrace($exception),
106  ),
107  )),
108  ), $formatted);
109  }
110 }
Create styles array
The data for the language used.
Formats data into an associative array of scalar values.