ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
LoggerInterfaceTest.php
Go to the documentation of this file.
1<?php
2
3namespace Psr\Log\Test;
4
6
13{
17 abstract function getLogger();
18
26 abstract function getLogs();
27
28 public function testImplements()
29 {
30 $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
31 }
32
36 public function testLogsAtAllLevels($level, $message)
37 {
38 $logger = $this->getLogger();
39 $logger->{$level}($message, array('user' => 'Bob'));
40 $logger->log($level, $message, array('user' => 'Bob'));
41
42 $expected = array(
43 $level.' message of level '.$level.' with context: Bob',
44 $level.' message of level '.$level.' with context: Bob',
45 );
46 $this->assertEquals($expected, $this->getLogs());
47 }
48
49 public function provideLevelsAndMessages()
50 {
51 return array(
52 LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'),
53 LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'),
54 LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'),
55 LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'),
56 LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'),
57 LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'),
58 LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'),
59 LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'),
60 );
61 }
62
66 public function testThrowsOnInvalidLevel()
67 {
68 $logger = $this->getLogger();
69 $logger->log('invalid level', 'Foo');
70 }
71
72 public function testContextReplacement()
73 {
74 $logger = $this->getLogger();
75 $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
76
77 $expected = array('info {Message {nothing} Bob Bar a}');
78 $this->assertEquals($expected, $this->getLogs());
79 }
80
81 public function testObjectCastToString()
82 {
83 $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString'));
84 $dummy->expects($this->once())
85 ->method('__toString')
86 ->will($this->returnValue('DUMMY'));
87
88 $this->getLogger()->warning($dummy);
89 }
90
92 {
93 $context = array(
94 'bool' => true,
95 'null' => null,
96 'string' => 'Foo',
97 'int' => 0,
98 'float' => 0.5,
99 'nested' => array('with object' => new DummyTest),
100 'object' => new \DateTime,
101 'resource' => fopen('php://memory', 'r'),
102 );
103
104 $this->getLogger()->warning('Crazy context data', $context);
105 }
106
108 {
109 $this->getLogger()->warning('Random message', array('exception' => 'oops'));
110 $this->getLogger()->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
111 }
112}
113
115{
116}
Describes log levels.
Definition: LogLevel.php:9
Provides a base test class for ensuring compliance with the LoggerInterface.
getLogs()
This must return the log messages in order with a simple formatting: "<LOG LEVEL> <MESSAGE>".
testLogsAtAllLevels($level, $message)
@dataProvider provideLevelsAndMessages
testThrowsOnInvalidLevel()
@expectedException Psr\Log\InvalidArgumentException