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