scratch – Blame information for rev 115

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 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  
14 /**
15 * @covers Monolog\Formatter\LineFormatter
16 */
17 class LineFormatterTest extends \PHPUnit_Framework_TestCase
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  
33 public function testDefFormatWithArrayContext()
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  
80 public function testContextAndExtraOptionallyNotShownIfEmpty()
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 testContextAndExtraReplacement()
95 {
96 $formatter = new LineFormatter('%context.foo% => %extra.foo%');
97 $message = $formatter->format(array(
98 'level_name' => 'ERROR',
99 'channel' => 'meh',
100 'context' => array('foo' => 'bar'),
101 'datetime' => new \DateTime,
102 'extra' => array('foo' => 'xbar'),
103 'message' => 'log',
104 ));
105 $this->assertEquals('bar => xbar', $message);
106 }
107  
108 public function testDefFormatWithObject()
109 {
110 $formatter = new LineFormatter(null, 'Y-m-d');
111 $message = $formatter->format(array(
112 'level_name' => 'ERROR',
113 'channel' => 'meh',
114 'context' => array(),
115 'datetime' => new \DateTime,
116 'extra' => array('foo' => new TestFoo, 'bar' => new TestBar, 'baz' => array(), 'res' => fopen('php://memory', 'rb')),
117 'message' => 'foobar',
118 ));
119  
120 $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] (stream)"}'."\n", $message);
121 }
122  
123 public function testDefFormatWithException()
124 {
125 $formatter = new LineFormatter(null, 'Y-m-d');
126 $message = $formatter->format(array(
127 'level_name' => 'CRITICAL',
128 'channel' => 'core',
129 'context' => array('exception' => new \RuntimeException('Foo')),
130 'datetime' => new \DateTime,
131 'extra' => array(),
132 'message' => 'foobar',
133 ));
134  
135 $path = str_replace('\\/', '/', json_encode(__FILE__));
136  
137 $this->assertEquals('['.date('Y-m-d').'] core.CRITICAL: foobar {"exception":"[object] (RuntimeException(code: 0): Foo at '.substr($path, 1, -1).':'.(__LINE__ - 8).')"} []'."\n", $message);
138 }
139  
140 public function testDefFormatWithPreviousException()
141 {
142 $formatter = new LineFormatter(null, 'Y-m-d');
143 $previous = new \LogicException('Wut?');
144 $message = $formatter->format(array(
145 'level_name' => 'CRITICAL',
146 'channel' => 'core',
147 'context' => array('exception' => new \RuntimeException('Foo', 0, $previous)),
148 'datetime' => new \DateTime,
149 'extra' => array(),
150 'message' => 'foobar',
151 ));
152  
153 $path = str_replace('\\/', '/', json_encode(__FILE__));
154  
155 $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);
156 }
157  
158 public function testBatchFormat()
159 {
160 $formatter = new LineFormatter(null, 'Y-m-d');
161 $message = $formatter->formatBatch(array(
162 array(
163 'level_name' => 'CRITICAL',
164 'channel' => 'test',
165 'message' => 'bar',
166 'context' => array(),
167 'datetime' => new \DateTime,
168 'extra' => array(),
169 ),
170 array(
171 'level_name' => 'WARNING',
172 'channel' => 'log',
173 'message' => 'foo',
174 'context' => array(),
175 'datetime' => new \DateTime,
176 'extra' => array(),
177 ),
178 ));
179 $this->assertEquals('['.date('Y-m-d').'] test.CRITICAL: bar [] []'."\n".'['.date('Y-m-d').'] log.WARNING: foo [] []'."\n", $message);
180 }
181  
182 public function testFormatShouldStripInlineLineBreaks()
183 {
184 $formatter = new LineFormatter(null, 'Y-m-d');
185 $message = $formatter->format(
186 array(
187 'message' => "foo\nbar",
188 'context' => array(),
189 'extra' => array(),
190 )
191 );
192  
193 $this->assertRegExp('/foo bar/', $message);
194 }
195  
196 public function testFormatShouldNotStripInlineLineBreaksWhenFlagIsSet()
197 {
198 $formatter = new LineFormatter(null, 'Y-m-d', true);
199 $message = $formatter->format(
200 array(
201 'message' => "foo\nbar",
202 'context' => array(),
203 'extra' => array(),
204 )
205 );
206  
207 $this->assertRegExp('/foo\nbar/', $message);
208 }
209 }
210  
211 class TestFoo
212 {
213 public $foo = 'foo';
214 }
215  
216 class TestBar
217 {
218 public function __toString()
219 {
220 return 'bar';
221 }
222 }