scratch – Blame information for rev

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;
13  
14 use Monolog\Processor\WebProcessor;
15 use Monolog\Handler\TestHandler;
16  
17 class LoggerTest extends \PHPUnit_Framework_TestCase
18 {
19 /**
20 * @covers Monolog\Logger::getName
21 */
22 public function testGetName()
23 {
24 $logger = new Logger('foo');
25 $this->assertEquals('foo', $logger->getName());
26 }
27  
28 /**
29 * @covers Monolog\Logger::getLevelName
30 */
31 public function testGetLevelName()
32 {
33 $this->assertEquals('ERROR', Logger::getLevelName(Logger::ERROR));
34 }
35  
36 /**
37 * @covers Monolog\Logger::withName
38 */
39 public function testWithName()
40 {
41 $first = new Logger('first', array($handler = new TestHandler()));
42 $second = $first->withName('second');
43  
44 $this->assertSame('first', $first->getName());
45 $this->assertSame('second', $second->getName());
46 $this->assertSame($handler, $second->popHandler());
47 }
48  
49 /**
50 * @covers Monolog\Logger::toMonologLevel
51 */
52 public function testConvertPSR3ToMonologLevel()
53 {
54 $this->assertEquals(Logger::toMonologLevel('debug'), 100);
55 $this->assertEquals(Logger::toMonologLevel('info'), 200);
56 $this->assertEquals(Logger::toMonologLevel('notice'), 250);
57 $this->assertEquals(Logger::toMonologLevel('warning'), 300);
58 $this->assertEquals(Logger::toMonologLevel('error'), 400);
59 $this->assertEquals(Logger::toMonologLevel('critical'), 500);
60 $this->assertEquals(Logger::toMonologLevel('alert'), 550);
61 $this->assertEquals(Logger::toMonologLevel('emergency'), 600);
62 }
63  
64 /**
65 * @covers Monolog\Logger::getLevelName
66 * @expectedException InvalidArgumentException
67 */
68 public function testGetLevelNameThrows()
69 {
70 Logger::getLevelName(5);
71 }
72  
73 /**
74 * @covers Monolog\Logger::__construct
75 */
76 public function testChannel()
77 {
78 $logger = new Logger('foo');
79 $handler = new TestHandler;
80 $logger->pushHandler($handler);
81 $logger->addWarning('test');
82 list($record) = $handler->getRecords();
83 $this->assertEquals('foo', $record['channel']);
84 }
85  
86 /**
87 * @covers Monolog\Logger::addRecord
88 */
89 public function testLog()
90 {
91 $logger = new Logger(__METHOD__);
92  
93 $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
94 $handler->expects($this->once())
95 ->method('handle');
96 $logger->pushHandler($handler);
97  
98 $this->assertTrue($logger->addWarning('test'));
99 }
100  
101 /**
102 * @covers Monolog\Logger::addRecord
103 */
104 public function testLogNotHandled()
105 {
106 $logger = new Logger(__METHOD__);
107  
108 $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'), array(Logger::ERROR));
109 $handler->expects($this->never())
110 ->method('handle');
111 $logger->pushHandler($handler);
112  
113 $this->assertFalse($logger->addWarning('test'));
114 }
115  
116 public function testHandlersInCtor()
117 {
118 $handler1 = new TestHandler;
119 $handler2 = new TestHandler;
120 $logger = new Logger(__METHOD__, array($handler1, $handler2));
121  
122 $this->assertEquals($handler1, $logger->popHandler());
123 $this->assertEquals($handler2, $logger->popHandler());
124 }
125  
126 public function testProcessorsInCtor()
127 {
128 $processor1 = new WebProcessor;
129 $processor2 = new WebProcessor;
130 $logger = new Logger(__METHOD__, array(), array($processor1, $processor2));
131  
132 $this->assertEquals($processor1, $logger->popProcessor());
133 $this->assertEquals($processor2, $logger->popProcessor());
134 }
135  
136 /**
137 * @covers Monolog\Logger::pushHandler
138 * @covers Monolog\Logger::popHandler
139 * @expectedException LogicException
140 */
141 public function testPushPopHandler()
142 {
143 $logger = new Logger(__METHOD__);
144 $handler1 = new TestHandler;
145 $handler2 = new TestHandler;
146  
147 $logger->pushHandler($handler1);
148 $logger->pushHandler($handler2);
149  
150 $this->assertEquals($handler2, $logger->popHandler());
151 $this->assertEquals($handler1, $logger->popHandler());
152 $logger->popHandler();
153 }
154  
155 /**
156 * @covers Monolog\Logger::setHandlers
157 */
158 public function testSetHandlers()
159 {
160 $logger = new Logger(__METHOD__);
161 $handler1 = new TestHandler;
162 $handler2 = new TestHandler;
163  
164 $logger->pushHandler($handler1);
165 $logger->setHandlers(array($handler2));
166  
167 // handler1 has been removed
168 $this->assertEquals(array($handler2), $logger->getHandlers());
169  
170 $logger->setHandlers(array(
171 "AMapKey" => $handler1,
172 "Woop" => $handler2,
173 ));
174  
175 // Keys have been scrubbed
176 $this->assertEquals(array($handler1, $handler2), $logger->getHandlers());
177 }
178  
179 /**
180 * @covers Monolog\Logger::pushProcessor
181 * @covers Monolog\Logger::popProcessor
182 * @expectedException LogicException
183 */
184 public function testPushPopProcessor()
185 {
186 $logger = new Logger(__METHOD__);
187 $processor1 = new WebProcessor;
188 $processor2 = new WebProcessor;
189  
190 $logger->pushProcessor($processor1);
191 $logger->pushProcessor($processor2);
192  
193 $this->assertEquals($processor2, $logger->popProcessor());
194 $this->assertEquals($processor1, $logger->popProcessor());
195 $logger->popProcessor();
196 }
197  
198 /**
199 * @covers Monolog\Logger::pushProcessor
200 * @expectedException InvalidArgumentException
201 */
202 public function testPushProcessorWithNonCallable()
203 {
204 $logger = new Logger(__METHOD__);
205  
206 $logger->pushProcessor(new \stdClass());
207 }
208  
209 /**
210 * @covers Monolog\Logger::addRecord
211 */
212 public function testProcessorsAreExecuted()
213 {
214 $logger = new Logger(__METHOD__);
215 $handler = new TestHandler;
216 $logger->pushHandler($handler);
217 $logger->pushProcessor(function ($record) {
218 $record['extra']['win'] = true;
219  
220 return $record;
221 });
222 $logger->addError('test');
223 list($record) = $handler->getRecords();
224 $this->assertTrue($record['extra']['win']);
225 }
226  
227 /**
228 * @covers Monolog\Logger::addRecord
229 */
230 public function testProcessorsAreCalledOnlyOnce()
231 {
232 $logger = new Logger(__METHOD__);
233 $handler = $this->getMock('Monolog\Handler\HandlerInterface');
234 $handler->expects($this->any())
235 ->method('isHandling')
236 ->will($this->returnValue(true))
237 ;
238 $handler->expects($this->any())
239 ->method('handle')
240 ->will($this->returnValue(true))
241 ;
242 $logger->pushHandler($handler);
243  
244 $processor = $this->getMockBuilder('Monolog\Processor\WebProcessor')
245 ->disableOriginalConstructor()
246 ->setMethods(array('__invoke'))
247 ->getMock()
248 ;
249 $processor->expects($this->once())
250 ->method('__invoke')
251 ->will($this->returnArgument(0))
252 ;
253 $logger->pushProcessor($processor);
254  
255 $logger->addError('test');
256 }
257  
258 /**
259 * @covers Monolog\Logger::addRecord
260 */
261 public function testProcessorsNotCalledWhenNotHandled()
262 {
263 $logger = new Logger(__METHOD__);
264 $handler = $this->getMock('Monolog\Handler\HandlerInterface');
265 $handler->expects($this->once())
266 ->method('isHandling')
267 ->will($this->returnValue(false))
268 ;
269 $logger->pushHandler($handler);
270 $that = $this;
271 $logger->pushProcessor(function ($record) use ($that) {
272 $that->fail('The processor should not be called');
273 });
274 $logger->addAlert('test');
275 }
276  
277 /**
278 * @covers Monolog\Logger::addRecord
279 */
280 public function testHandlersNotCalledBeforeFirstHandling()
281 {
282 $logger = new Logger(__METHOD__);
283  
284 $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
285 $handler1->expects($this->never())
286 ->method('isHandling')
287 ->will($this->returnValue(false))
288 ;
289 $handler1->expects($this->once())
290 ->method('handle')
291 ->will($this->returnValue(false))
292 ;
293 $logger->pushHandler($handler1);
294  
295 $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
296 $handler2->expects($this->once())
297 ->method('isHandling')
298 ->will($this->returnValue(true))
299 ;
300 $handler2->expects($this->once())
301 ->method('handle')
302 ->will($this->returnValue(false))
303 ;
304 $logger->pushHandler($handler2);
305  
306 $handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
307 $handler3->expects($this->once())
308 ->method('isHandling')
309 ->will($this->returnValue(false))
310 ;
311 $handler3->expects($this->never())
312 ->method('handle')
313 ;
314 $logger->pushHandler($handler3);
315  
316 $logger->debug('test');
317 }
318  
319 /**
320 * @covers Monolog\Logger::addRecord
321 */
322 public function testHandlersNotCalledBeforeFirstHandlingWithAssocArray()
323 {
324 $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
325 $handler1->expects($this->never())
326 ->method('isHandling')
327 ->will($this->returnValue(false))
328 ;
329 $handler1->expects($this->once())
330 ->method('handle')
331 ->will($this->returnValue(false))
332 ;
333  
334 $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
335 $handler2->expects($this->once())
336 ->method('isHandling')
337 ->will($this->returnValue(true))
338 ;
339 $handler2->expects($this->once())
340 ->method('handle')
341 ->will($this->returnValue(false))
342 ;
343  
344 $handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
345 $handler3->expects($this->once())
346 ->method('isHandling')
347 ->will($this->returnValue(false))
348 ;
349 $handler3->expects($this->never())
350 ->method('handle')
351 ;
352  
353 $logger = new Logger(__METHOD__, array('last' => $handler3, 'second' => $handler2, 'first' => $handler1));
354  
355 $logger->debug('test');
356 }
357  
358 /**
359 * @covers Monolog\Logger::addRecord
360 */
361 public function testBubblingWhenTheHandlerReturnsFalse()
362 {
363 $logger = new Logger(__METHOD__);
364  
365 $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
366 $handler1->expects($this->any())
367 ->method('isHandling')
368 ->will($this->returnValue(true))
369 ;
370 $handler1->expects($this->once())
371 ->method('handle')
372 ->will($this->returnValue(false))
373 ;
374 $logger->pushHandler($handler1);
375  
376 $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
377 $handler2->expects($this->any())
378 ->method('isHandling')
379 ->will($this->returnValue(true))
380 ;
381 $handler2->expects($this->once())
382 ->method('handle')
383 ->will($this->returnValue(false))
384 ;
385 $logger->pushHandler($handler2);
386  
387 $logger->debug('test');
388 }
389  
390 /**
391 * @covers Monolog\Logger::addRecord
392 */
393 public function testNotBubblingWhenTheHandlerReturnsTrue()
394 {
395 $logger = new Logger(__METHOD__);
396  
397 $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
398 $handler1->expects($this->any())
399 ->method('isHandling')
400 ->will($this->returnValue(true))
401 ;
402 $handler1->expects($this->never())
403 ->method('handle')
404 ;
405 $logger->pushHandler($handler1);
406  
407 $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
408 $handler2->expects($this->any())
409 ->method('isHandling')
410 ->will($this->returnValue(true))
411 ;
412 $handler2->expects($this->once())
413 ->method('handle')
414 ->will($this->returnValue(true))
415 ;
416 $logger->pushHandler($handler2);
417  
418 $logger->debug('test');
419 }
420  
421 /**
422 * @covers Monolog\Logger::isHandling
423 */
424 public function testIsHandling()
425 {
426 $logger = new Logger(__METHOD__);
427  
428 $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
429 $handler1->expects($this->any())
430 ->method('isHandling')
431 ->will($this->returnValue(false))
432 ;
433  
434 $logger->pushHandler($handler1);
435 $this->assertFalse($logger->isHandling(Logger::DEBUG));
436  
437 $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
438 $handler2->expects($this->any())
439 ->method('isHandling')
440 ->will($this->returnValue(true))
441 ;
442  
443 $logger->pushHandler($handler2);
444 $this->assertTrue($logger->isHandling(Logger::DEBUG));
445 }
446  
447 /**
448 * @dataProvider logMethodProvider
449 * @covers Monolog\Logger::addDebug
450 * @covers Monolog\Logger::addInfo
451 * @covers Monolog\Logger::addNotice
452 * @covers Monolog\Logger::addWarning
453 * @covers Monolog\Logger::addError
454 * @covers Monolog\Logger::addCritical
455 * @covers Monolog\Logger::addAlert
456 * @covers Monolog\Logger::addEmergency
457 * @covers Monolog\Logger::debug
458 * @covers Monolog\Logger::info
459 * @covers Monolog\Logger::notice
460 * @covers Monolog\Logger::warn
461 * @covers Monolog\Logger::err
462 * @covers Monolog\Logger::crit
463 * @covers Monolog\Logger::alert
464 * @covers Monolog\Logger::emerg
465 */
466 public function testLogMethods($method, $expectedLevel)
467 {
468 $logger = new Logger('foo');
469 $handler = new TestHandler;
470 $logger->pushHandler($handler);
471 $logger->{$method}('test');
472 list($record) = $handler->getRecords();
473 $this->assertEquals($expectedLevel, $record['level']);
474 }
475  
476 public function logMethodProvider()
477 {
478 return array(
479 // monolog methods
480 array('addDebug', Logger::DEBUG),
481 array('addInfo', Logger::INFO),
482 array('addNotice', Logger::NOTICE),
483 array('addWarning', Logger::WARNING),
484 array('addError', Logger::ERROR),
485 array('addCritical', Logger::CRITICAL),
486 array('addAlert', Logger::ALERT),
487 array('addEmergency', Logger::EMERGENCY),
488  
489 // ZF/Sf2 compat methods
490 array('debug', Logger::DEBUG),
491 array('info', Logger::INFO),
492 array('notice', Logger::NOTICE),
493 array('warn', Logger::WARNING),
494 array('err', Logger::ERROR),
495 array('crit', Logger::CRITICAL),
496 array('alert', Logger::ALERT),
497 array('emerg', Logger::EMERGENCY),
498 );
499 }
500  
501 /**
502 * @dataProvider setTimezoneProvider
503 * @covers Monolog\Logger::setTimezone
504 */
505 public function testSetTimezone($tz)
506 {
507 Logger::setTimezone($tz);
508 $logger = new Logger('foo');
509 $handler = new TestHandler;
510 $logger->pushHandler($handler);
511 $logger->info('test');
512 list($record) = $handler->getRecords();
513 $this->assertEquals($tz, $record['datetime']->getTimezone());
514 }
515  
516 public function setTimezoneProvider()
517 {
518 return array_map(
519 function ($tz) { return array(new \DateTimeZone($tz)); },
520 \DateTimeZone::listIdentifiers()
521 );
522 }
523  
524 /**
525 * @dataProvider useMicrosecondTimestampsProvider
526 * @covers Monolog\Logger::useMicrosecondTimestamps
527 * @covers Monolog\Logger::addRecord
528 */
529 public function testUseMicrosecondTimestamps($micro, $assert)
530 {
531 $logger = new Logger('foo');
532 $logger->useMicrosecondTimestamps($micro);
533 $handler = new TestHandler;
534 $logger->pushHandler($handler);
535 $logger->info('test');
536 list($record) = $handler->getRecords();
537 $this->{$assert}('000000', $record['datetime']->format('u'));
538 }
539  
540 public function useMicrosecondTimestampsProvider()
541 {
542 return array(
543 // this has a very small chance of a false negative (1/10^6)
544 'with microseconds' => array(true, 'assertNotSame'),
545 'without microseconds' => array(false, PHP_VERSION_ID >= 70100 ? 'assertNotSame' : 'assertSame'),
546 );
547 }
548 }