ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
LineFormatterTest.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 
18 {
19  public function testDefFormatWithString()
20  {
21  $formatter = new LineFormatter(null, 'Y-m-d');
22  $message = $formatter->format(array(
23  'level_name' => 'WARNING',
24  'channel' => 'log',
25  'context' => array(),
26  'message' => 'foo',
27  'datetime' => new \DateTime,
28  'extra' => array(),
29  ));
30  $this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
31  }
32 
34  {
35  $formatter = new LineFormatter(null, 'Y-m-d');
36  $message = $formatter->format(array(
37  'level_name' => 'ERROR',
38  'channel' => 'meh',
39  'message' => 'foo',
40  'datetime' => new \DateTime,
41  'extra' => array(),
42  'context' => array(
43  'foo' => 'bar',
44  'baz' => 'qux',
45  'bool' => false,
46  'null' => null,
47  )
48  ));
49  $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foo {"foo":"bar","baz":"qux","bool":false,"null":null} []'."\n", $message);
50  }
51 
52  public function testDefFormatExtras()
53  {
54  $formatter = new LineFormatter(null, 'Y-m-d');
55  $message = $formatter->format(array(
56  'level_name' => 'ERROR',
57  'channel' => 'meh',
58  'context' => array(),
59  'datetime' => new \DateTime,
60  'extra' => array('ip' => '127.0.0.1'),
61  'message' => 'log',
62  ));
63  $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
64  }
65 
66  public function testFormatExtras()
67  {
68  $formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
69  $message = $formatter->format(array(
70  'level_name' => 'ERROR',
71  'channel' => 'meh',
72  'context' => array(),
73  'datetime' => new \DateTime,
74  'extra' => array('ip' => '127.0.0.1', 'file' => 'test'),
75  'message' => 'log',
76  ));
77  $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
78  }
79 
81  {
82  $formatter = new LineFormatter(null, 'Y-m-d', false, true);
83  $message = $formatter->format(array(
84  'level_name' => 'ERROR',
85  'channel' => 'meh',
86  'context' => array(),
87  'datetime' => new \DateTime,
88  'extra' => array(),
89  'message' => 'log',
90  ));
91  $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log '."\n", $message);
92  }
93 
94  public function testDefFormatWithObject()
95  {
96  $formatter = new LineFormatter(null, 'Y-m-d');
97  $message = $formatter->format(array(
98  'level_name' => 'ERROR',
99  'channel' => 'meh',
100  'context' => array(),
101  'datetime' => new \DateTime,
102  'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
103  'message' => 'foobar',
104  ));
105 
106  $this->assertEquals('['.date('Y-m-d').'] meh.ERROR: foobar [] {"foo":"[object] (Monolog\\\\Formatter\\\\TestFoo: {\\"foo\\":\\"foo\\"})","bar":"[object] (Monolog\\\\Formatter\\\\TestBar: bar)","baz":[],"res":"[resource]"}'."\n", $message);
107  }
108 
109  public function testDefFormatWithException()
110  {
111  $formatter = new LineFormatter(null, 'Y-m-d');
112  $message = $formatter->format(array(
113  'level_name' => 'CRITICAL',
114  'channel' => 'core',
115  'context' => array('exception' => new \RuntimeException('Foo')),
116  'datetime' => new \DateTime,
117  'extra' => array(),
118  'message' => 'foobar',
119  ));
120 
121  $path = str_replace('\\/', '/', json_encode(__FILE__));
122 
123  $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__-8).')"} []'."\n", $message);
124  }
125 
127  {
128  $formatter = new LineFormatter(null, 'Y-m-d');
129  $previous = new \LogicException('Wut?');
130  $message = $formatter->format(array(
131  'level_name' => 'CRITICAL',
132  'channel' => 'core',
133  'context' => array('exception' => new \RuntimeException('Foo', 0, $previous)),
134  'datetime' => new \DateTime,
135  'extra' => array(),
136  'message' => 'foobar',
137  ));
138 
139  $path = str_replace('\\/', '/', json_encode(__FILE__));
140 
141  $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__-8).', LogicException(code: 0): Wut? at '.substr($path, 1, -1).':'.(__LINE__-12).')"} []'."\n", $message);
142  }
143 
144  public function testBatchFormat()
145  {
146  $formatter = new LineFormatter(null, 'Y-m-d');
147  $message = $formatter->formatBatch(array(
148  array(
149  'level_name' => 'CRITICAL',
150  'channel' => 'test',
151  'message' => 'bar',
152  'context' => array(),
153  'datetime' => new \DateTime,
154  'extra' => array(),
155  ),
156  array(
157  'level_name' => 'WARNING',
158  'channel' => 'log',
159  'message' => 'foo',
160  'context' => array(),
161  'datetime' => new \DateTime,
162  'extra' => array(),
163  ),
164  ));
165  $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
166  }
167 
169  {
170  $formatter = new LineFormatter(null, 'Y-m-d');
171  $message = $formatter->format(
172  array(
173  'message' => "foo\nbar",
174  'context' => array(),
175  'extra' => array(),
176  )
177  );
178 
179  $this->assertRegExp('/foo bar/', $message);
180  }
181 
183  {
184  $formatter = new LineFormatter(null, 'Y-m-d', true);
185  $message = $formatter->format(
186  array(
187  'message' => "foo\nbar",
188  'context' => array(),
189  'extra' => array(),
190  )
191  );
192 
193  $this->assertRegExp('/foo\nbar/', $message);
194  }
195 }
196 
197 class TestFoo
198 {
199  public $foo = 'foo';
200 }
201 
202 class TestBar
203 {
204  public function __toString()
205  {
206  return 'bar';
207  }
208 }
$path
Definition: index.php:22
Formats incoming records into a one-line string.