ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
40  {
41  $this->assertEquals(Logger::toMonologLevel('debug'), 100);
42  $this->assertEquals(Logger::toMonologLevel('info'), 200);
43  $this->assertEquals(Logger::toMonologLevel('notice'), 250);
44  $this->assertEquals(Logger::toMonologLevel('warning'), 300);
45  $this->assertEquals(Logger::toMonologLevel('error'), 400);
46  $this->assertEquals(Logger::toMonologLevel('critical'), 500);
47  $this->assertEquals(Logger::toMonologLevel('alert'), 550);
48  $this->assertEquals(Logger::toMonologLevel('emergency'), 600);
49  }
50 
55  public function testGetLevelNameThrows()
56  {
57  Logger::getLevelName(5);
58  }
59 
63  public function testChannel()
64  {
65  $logger = new Logger('foo');
66  $handler = new TestHandler;
67  $logger->pushHandler($handler);
68  $logger->addWarning('test');
69  list($record) = $handler->getRecords();
70  $this->assertEquals('foo', $record['channel']);
71  }
72 
76  public function testLog()
77  {
78  $logger = new Logger(__METHOD__);
79 
80  $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
81  $handler->expects($this->once())
82  ->method('handle');
83  $logger->pushHandler($handler);
84 
85  $this->assertTrue($logger->addWarning('test'));
86  }
87 
91  public function testLogNotHandled()
92  {
93  $logger = new Logger(__METHOD__);
94 
95  $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'), array(Logger::ERROR));
96  $handler->expects($this->never())
97  ->method('handle');
98  $logger->pushHandler($handler);
99 
100  $this->assertFalse($logger->addWarning('test'));
101  }
102 
103  public function testHandlersInCtor()
104  {
105  $handler1 = new TestHandler;
106  $handler2 = new TestHandler;
107  $logger = new Logger(__METHOD__, array($handler1, $handler2));
108 
109  $this->assertEquals($handler1, $logger->popHandler());
110  $this->assertEquals($handler2, $logger->popHandler());
111  }
112 
113  public function testProcessorsInCtor()
114  {
115  $processor1 = new WebProcessor;
116  $processor2 = new WebProcessor;
117  $logger = new Logger(__METHOD__, array(), array($processor1, $processor2));
118 
119  $this->assertEquals($processor1, $logger->popProcessor());
120  $this->assertEquals($processor2, $logger->popProcessor());
121  }
122 
128  public function testPushPopHandler()
129  {
130  $logger = new Logger(__METHOD__);
131  $handler1 = new TestHandler;
132  $handler2 = new TestHandler;
133 
134  $logger->pushHandler($handler1);
135  $logger->pushHandler($handler2);
136 
137  $this->assertEquals($handler2, $logger->popHandler());
138  $this->assertEquals($handler1, $logger->popHandler());
139  $logger->popHandler();
140  }
141 
147  public function testPushPopProcessor()
148  {
149  $logger = new Logger(__METHOD__);
150  $processor1 = new WebProcessor;
151  $processor2 = new WebProcessor;
152 
153  $logger->pushProcessor($processor1);
154  $logger->pushProcessor($processor2);
155 
156  $this->assertEquals($processor2, $logger->popProcessor());
157  $this->assertEquals($processor1, $logger->popProcessor());
158  $logger->popProcessor();
159  }
160 
166  {
167  $logger = new Logger(__METHOD__);
168 
169  $logger->pushProcessor(new \stdClass());
170  }
171 
175  public function testProcessorsAreExecuted()
176  {
177  $logger = new Logger(__METHOD__);
178  $handler = new TestHandler;
179  $logger->pushHandler($handler);
180  $logger->pushProcessor(function ($record) {
181  $record['extra']['win'] = true;
182 
183  return $record;
184  });
185  $logger->addError('test');
186  list($record) = $handler->getRecords();
187  $this->assertTrue($record['extra']['win']);
188  }
189 
194  {
195  $logger = new Logger(__METHOD__);
196  $handler = $this->getMock('Monolog\Handler\HandlerInterface');
197  $handler->expects($this->any())
198  ->method('isHandling')
199  ->will($this->returnValue(true))
200  ;
201  $handler->expects($this->any())
202  ->method('handle')
203  ->will($this->returnValue(true))
204  ;
205  $logger->pushHandler($handler);
206 
207  $processor = $this->getMockBuilder('Monolog\Processor\WebProcessor')
208  ->disableOriginalConstructor()
209  ->setMethods(array('__invoke'))
210  ->getMock()
211  ;
212  $processor->expects($this->once())
213  ->method('__invoke')
214  ->will($this->returnArgument(0))
215  ;
216  $logger->pushProcessor($processor);
217 
218  $logger->addError('test');
219  }
220 
225  {
226  $logger = new Logger(__METHOD__);
227  $handler = $this->getMock('Monolog\Handler\HandlerInterface');
228  $handler->expects($this->once())
229  ->method('isHandling')
230  ->will($this->returnValue(false))
231  ;
232  $logger->pushHandler($handler);
233  $that = $this;
234  $logger->pushProcessor(function ($record) use ($that) {
235  $that->fail('The processor should not be called');
236  });
237  $logger->addAlert('test');
238  }
239 
244  {
245  $logger = new Logger(__METHOD__);
246 
247  $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
248  $handler1->expects($this->never())
249  ->method('isHandling')
250  ->will($this->returnValue(false))
251  ;
252  $handler1->expects($this->once())
253  ->method('handle')
254  ->will($this->returnValue(false))
255  ;
256  $logger->pushHandler($handler1);
257 
258  $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
259  $handler2->expects($this->once())
260  ->method('isHandling')
261  ->will($this->returnValue(true))
262  ;
263  $handler2->expects($this->once())
264  ->method('handle')
265  ->will($this->returnValue(false))
266  ;
267  $logger->pushHandler($handler2);
268 
269  $handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
270  $handler3->expects($this->once())
271  ->method('isHandling')
272  ->will($this->returnValue(false))
273  ;
274  $handler3->expects($this->never())
275  ->method('handle')
276  ;
277  $logger->pushHandler($handler3);
278 
279  $logger->debug('test');
280  }
281 
286  {
287  $logger = new Logger(__METHOD__);
288 
289  $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
290  $handler1->expects($this->any())
291  ->method('isHandling')
292  ->will($this->returnValue(true))
293  ;
294  $handler1->expects($this->once())
295  ->method('handle')
296  ->will($this->returnValue(false))
297  ;
298  $logger->pushHandler($handler1);
299 
300  $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
301  $handler2->expects($this->any())
302  ->method('isHandling')
303  ->will($this->returnValue(true))
304  ;
305  $handler2->expects($this->once())
306  ->method('handle')
307  ->will($this->returnValue(false))
308  ;
309  $logger->pushHandler($handler2);
310 
311  $logger->debug('test');
312  }
313 
318  {
319  $logger = new Logger(__METHOD__);
320 
321  $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
322  $handler1->expects($this->any())
323  ->method('isHandling')
324  ->will($this->returnValue(true))
325  ;
326  $handler1->expects($this->never())
327  ->method('handle')
328  ;
329  $logger->pushHandler($handler1);
330 
331  $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
332  $handler2->expects($this->any())
333  ->method('isHandling')
334  ->will($this->returnValue(true))
335  ;
336  $handler2->expects($this->once())
337  ->method('handle')
338  ->will($this->returnValue(true))
339  ;
340  $logger->pushHandler($handler2);
341 
342  $logger->debug('test');
343  }
344 
348  public function testIsHandling()
349  {
350  $logger = new Logger(__METHOD__);
351 
352  $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
353  $handler1->expects($this->any())
354  ->method('isHandling')
355  ->will($this->returnValue(false))
356  ;
357 
358  $logger->pushHandler($handler1);
359  $this->assertFalse($logger->isHandling(Logger::DEBUG));
360 
361  $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
362  $handler2->expects($this->any())
363  ->method('isHandling')
364  ->will($this->returnValue(true))
365  ;
366 
367  $logger->pushHandler($handler2);
368  $this->assertTrue($logger->isHandling(Logger::DEBUG));
369  }
370 
390  public function testLogMethods($method, $expectedLevel)
391  {
392  $logger = new Logger('foo');
393  $handler = new TestHandler;
394  $logger->pushHandler($handler);
395  $logger->{$method}('test');
396  list($record) = $handler->getRecords();
397  $this->assertEquals($expectedLevel, $record['level']);
398  }
399 
400  public function logMethodProvider()
401  {
402  return array(
403  // monolog methods
404  array('addDebug', Logger::DEBUG),
405  array('addInfo', Logger::INFO),
406  array('addNotice', Logger::NOTICE),
407  array('addWarning', Logger::WARNING),
408  array('addError', Logger::ERROR),
409  array('addCritical', Logger::CRITICAL),
410  array('addAlert', Logger::ALERT),
411  array('addEmergency', Logger::EMERGENCY),
412 
413  // ZF/Sf2 compat methods
414  array('debug', Logger::DEBUG),
415  array('info', Logger::INFO),
416  array('notice', Logger::NOTICE),
417  array('warn', Logger::WARNING),
418  array('err', Logger::ERROR),
419  array('crit', Logger::CRITICAL),
420  array('alert', Logger::ALERT),
421  array('emerg', Logger::EMERGENCY),
422  );
423  }
424 
429  public function testSetTimezone($tz)
430  {
431  Logger::setTimezone($tz);
432  $logger = new Logger('foo');
433  $handler = new TestHandler;
434  $logger->pushHandler($handler);
435  $logger->info('test');
436  list($record) = $handler->getRecords();
437  $this->assertEquals($tz, $record['datetime']->getTimezone());
438  }
439 
440  public function setTimezoneProvider()
441  {
442  return array_map(
443  function ($tz) { return array(new \DateTimeZone($tz)); },
444  \DateTimeZone::listIdentifiers()
445  );
446  }
447 }
testLogNotHandled()
Monolog::addRecord
Definition: LoggerTest.php:91
testProcessorsAreExecuted()
Monolog::addRecord
Definition: LoggerTest.php:175
Monolog log channel.
Definition: Logger.php:27
Injects url/method and remote IP of the current web request in all records.
testBubblingWhenTheHandlerReturnsFalse()
Monolog::addRecord
Definition: LoggerTest.php:285
testConvertPSR3ToMonologLevel()
Monolog::toMonologLevel
Definition: LoggerTest.php:39
testIsHandling()
Monolog::isHandling
Definition: LoggerTest.php:348
testChannel()
Monolog::__construct
Definition: LoggerTest.php:63
testSetTimezone($tz)
setTimezoneProvider Monolog::setTimezone
Definition: LoggerTest.php:429
testGetName()
Monolog::getName
Definition: LoggerTest.php:22
const DEBUG
testLog()
Monolog::addRecord
Definition: LoggerTest.php:76
testProcessorsNotCalledWhenNotHandled()
Monolog::addRecord
Definition: LoggerTest.php:224
testGetLevelNameThrows()
Monolog::getLevelName InvalidArgumentException
Definition: LoggerTest.php:55
testHandlersNotCalledBeforeFirstHandling()
Monolog::addRecord
Definition: LoggerTest.php:243
testProcessorsAreCalledOnlyOnce()
Monolog::addRecord
Definition: LoggerTest.php:193
testPushProcessorWithNonCallable()
Monolog::pushProcessor InvalidArgumentException
Definition: LoggerTest.php:165
testLogMethods($method, $expectedLevel)
logMethodProvider Monolog::addDebug Monolog::addInfo Monolog::addNotice Monolog::addWarning Mono...
Definition: LoggerTest.php:390
Used for testing purposes.
Definition: TestHandler.php:68
testPushPopHandler()
Monolog::pushHandler Monolog::popHandler LogicException
Definition: LoggerTest.php:128
testGetLevelName()
Monolog::getLevelName
Definition: LoggerTest.php:31
testNotBubblingWhenTheHandlerReturnsTrue()
Monolog::addRecord
Definition: LoggerTest.php:317
pushProcessor($callback)
{Adds a processor in the stack.self}
testPushPopProcessor()
Monolog::pushProcessor Monolog::popProcessor LogicException
Definition: LoggerTest.php:147