ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
LoggerTest.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;
13 
16 
18 {
22  public function testGetName()
23  {
24  $logger = new Logger('foo');
25  $this->assertEquals('foo', $logger->getName());
26  }
27 
31  public function testGetLevelName()
32  {
33  $this->assertEquals('ERROR', Logger::getLevelName(Logger::ERROR));
34  }
35 
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 
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 
68  public function testGetLevelNameThrows()
69  {
70  Logger::getLevelName(5);
71  }
72 
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 
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 
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 
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 
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 
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 
203  {
204  $logger = new Logger(__METHOD__);
205 
206  $logger->pushProcessor(new \stdClass());
207  }
208 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
552  public function testSetExceptionHandler()
553  {
554  $logger = new Logger(__METHOD__);
555  $this->assertNull($logger->getExceptionHandler());
556  $callback = function ($ex) {
557  };
558  $logger->setExceptionHandler($callback);
559  $this->assertEquals($callback, $logger->getExceptionHandler());
560  }
561 
566  public function testBadExceptionHandlerType()
567  {
568  $logger = new Logger(__METHOD__);
569  $logger->setExceptionHandler(false);
570  }
571 
576  public function testDefaultHandleException()
577  {
578  $logger = new Logger(__METHOD__);
579  $handler = $this->getMock('Monolog\Handler\HandlerInterface');
580  $handler->expects($this->any())
581  ->method('isHandling')
582  ->will($this->returnValue(true))
583  ;
584  $handler->expects($this->any())
585  ->method('handle')
586  ->will($this->throwException(new \Exception('Some handler exception')))
587  ;
588  $logger->pushHandler($handler);
589  $logger->info('test');
590  }
591 
596  public function testCustomHandleException()
597  {
598  $logger = new Logger(__METHOD__);
599  $that = $this;
600  $logger->setExceptionHandler(function ($e, $record) use ($that) {
601  $that->assertEquals($e->getMessage(), 'Some handler exception');
602  $that->assertTrue(is_array($record));
603  $that->assertEquals($record['message'], 'test');
604  });
605  $handler = $this->getMock('Monolog\Handler\HandlerInterface');
606  $handler->expects($this->any())
607  ->method('isHandling')
608  ->will($this->returnValue(true))
609  ;
610  $handler->expects($this->any())
611  ->method('handle')
612  ->will($this->throwException(new \Exception('Some handler exception')))
613  ;
614  $logger->pushHandler($handler);
615  $logger->info('test');
616  }
617 
618  public function testReset()
619  {
620  $logger = new Logger('app');
621 
622  $testHandler = new Handler\TestHandler();
623  $bufferHandler = new Handler\BufferHandler($testHandler);
624  $groupHandler = new Handler\GroupHandler(array($bufferHandler));
625  $fingersCrossedHandler = new Handler\FingersCrossedHandler($groupHandler);
626 
627  $logger->pushHandler($fingersCrossedHandler);
628 
629  $processorUid1 = new Processor\UidProcessor(10);
630  $uid1 = $processorUid1->getUid();
631  $groupHandler->pushProcessor($processorUid1);
632 
633  $processorUid2 = new Processor\UidProcessor(5);
634  $uid2 = $processorUid2->getUid();
635  $logger->pushProcessor($processorUid2);
636 
637  $getProperty = function ($object, $property) {
638  $reflectionProperty = new \ReflectionProperty(get_class($object), $property);
639  $reflectionProperty->setAccessible(true);
640 
641  return $reflectionProperty->getValue($object);
642  };
643  $that = $this;
644  $assertBufferOfBufferHandlerEmpty = function () use ($getProperty, $bufferHandler, $that) {
645  $that->assertEmpty($getProperty($bufferHandler, 'buffer'));
646  };
647  $assertBuffersEmpty = function() use ($assertBufferOfBufferHandlerEmpty, $getProperty, $fingersCrossedHandler, $that) {
648  $assertBufferOfBufferHandlerEmpty();
649  $that->assertEmpty($getProperty($fingersCrossedHandler, 'buffer'));
650  };
651 
652  $logger->debug('debug');
653  $logger->reset();
654  $assertBuffersEmpty();
655  $this->assertFalse($testHandler->hasDebugRecords());
656  $this->assertFalse($testHandler->hasErrorRecords());
657  $this->assertNotSame($uid1, $uid1 = $processorUid1->getUid());
658  $this->assertNotSame($uid2, $uid2 = $processorUid2->getUid());
659 
660  $logger->debug('debug');
661  $logger->error('error');
662  $logger->reset();
663  $assertBuffersEmpty();
664  $this->assertTrue($testHandler->hasDebugRecords());
665  $this->assertTrue($testHandler->hasErrorRecords());
666  $this->assertNotSame($uid1, $uid1 = $processorUid1->getUid());
667  $this->assertNotSame($uid2, $uid2 = $processorUid2->getUid());
668 
669  $logger->info('info');
670  $this->assertNotEmpty($getProperty($fingersCrossedHandler, 'buffer'));
671  $assertBufferOfBufferHandlerEmpty();
672  $this->assertFalse($testHandler->hasInfoRecords());
673 
674  $logger->reset();
675  $assertBuffersEmpty();
676  $this->assertFalse($testHandler->hasInfoRecords());
677  $this->assertNotSame($uid1, $uid1 = $processorUid1->getUid());
678  $this->assertNotSame($uid2, $uid2 = $processorUid2->getUid());
679 
680  $logger->notice('notice');
681  $logger->emergency('emergency');
682  $logger->reset();
683  $assertBuffersEmpty();
684  $this->assertFalse($testHandler->hasInfoRecords());
685  $this->assertTrue($testHandler->hasNoticeRecords());
686  $this->assertTrue($testHandler->hasEmergencyRecords());
687  $this->assertNotSame($uid1, $processorUid1->getUid());
688  $this->assertNotSame($uid2, $processorUid2->getUid());
689  }
690 }
testLogNotHandled()
Monolog::addRecord
Definition: LoggerTest.php:104
testProcessorsAreExecuted()
Monolog::addRecord
Definition: LoggerTest.php:212
testSetHandlers()
Monolog::setHandlers
Definition: LoggerTest.php:158
Buffers all records until a certain level is reached.
Monolog log channel.
Definition: Logger.php:28
Injects url/method and remote IP of the current web request in all records.
testBubblingWhenTheHandlerReturnsFalse()
Monolog::addRecord
Definition: LoggerTest.php:361
testConvertPSR3ToMonologLevel()
Monolog::toMonologLevel
Definition: LoggerTest.php:52
testIsHandling()
Monolog::isHandling
Definition: LoggerTest.php:424
testSetExceptionHandler()
Monolog::setExceptionHandler
Definition: LoggerTest.php:552
testChannel()
Monolog::__construct
Definition: LoggerTest.php:76
testBadExceptionHandlerType()
Monolog::setExceptionHandler InvalidArgumentException
Definition: LoggerTest.php:566
Buffers all records until closing the handler and then pass them as batch.
testSetTimezone($tz)
setTimezoneProvider Monolog::setTimezone
Definition: LoggerTest.php:505
testDefaultHandleException()
Monolog::handleException Exception
Definition: LoggerTest.php:576
testGetName()
Monolog::getName
Definition: LoggerTest.php:22
const DEBUG
testLog()
Monolog::addRecord
Definition: LoggerTest.php:89
Forwards records to multiple handlers.
testProcessorsNotCalledWhenNotHandled()
Monolog::addRecord
Definition: LoggerTest.php:261
testHandlersNotCalledBeforeFirstHandlingWithAssocArray()
Monolog::addRecord
Definition: LoggerTest.php:322
testCustomHandleException()
Monolog::handleException Monolog::addRecord
Definition: LoggerTest.php:596
once($eventName, callable $callBack, $priority=100)
Subscribe to an event exactly once.
Adds a unique identifier into records.
testGetLevelNameThrows()
Monolog::getLevelName InvalidArgumentException
Definition: LoggerTest.php:68
testHandlersNotCalledBeforeFirstHandling()
Monolog::addRecord
Definition: LoggerTest.php:280
testProcessorsAreCalledOnlyOnce()
Monolog::addRecord
Definition: LoggerTest.php:230
testPushProcessorWithNonCallable()
Monolog::pushProcessor InvalidArgumentException
Definition: LoggerTest.php:202
testLogMethods($method, $expectedLevel)
logMethodProvider Monolog::addDebug Monolog::addInfo Monolog::addNotice Monolog::addWarning Mono...
Definition: LoggerTest.php:466
testWithName()
Monolog::withName
Definition: LoggerTest.php:39
Used for testing purposes.
Definition: TestHandler.php:66
useMicrosecondTimestampsProvider()
Definition: LoggerTest.php:540
testUseMicrosecondTimestamps($micro, $assert)
useMicrosecondTimestampsProvider Monolog::useMicrosecondTimestamps Monolog::addRecord ...
Definition: LoggerTest.php:529
testPushPopHandler()
Monolog::pushHandler Monolog::popHandler LogicException
Definition: LoggerTest.php:141
$handler
testGetLevelName()
Monolog::getLevelName
Definition: LoggerTest.php:31
testNotBubblingWhenTheHandlerReturnsTrue()
Monolog::addRecord
Definition: LoggerTest.php:393
pushProcessor($callback)
{Adds a processor in the stack.self}
testPushPopProcessor()
Monolog::pushProcessor Monolog::popProcessor LogicException
Definition: LoggerTest.php:184