TYPO3  7.6
LoggerInterfaceTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Psr\Log\Test;
4 
7 
14 abstract class LoggerInterfaceTest extends \PHPUnit_Framework_TestCase
15 {
19  abstract public function getLogger();
20 
30  abstract public function getLogs();
31 
32  public function testImplements()
33  {
34  $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
35  }
36 
40  public function testLogsAtAllLevels($level, $message)
41  {
42  $logger = $this->getLogger();
43  $logger->{$level}($message, array('user' => 'Bob'));
44  $logger->log($level, $message, array('user' => 'Bob'));
45 
46  $expected = array(
47  $level.' message of level '.$level.' with context: Bob',
48  $level.' message of level '.$level.' with context: Bob',
49  );
50  $this->assertEquals($expected, $this->getLogs());
51  }
52 
53  public function provideLevelsAndMessages()
54  {
55  return array(
56  LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'),
57  LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'),
58  LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'),
59  LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'),
60  LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'),
61  LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'),
62  LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'),
63  LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'),
64  );
65  }
66 
70  public function testThrowsOnInvalidLevel()
71  {
72  $logger = $this->getLogger();
73  $logger->log('invalid level', 'Foo');
74  }
75 
76  public function testContextReplacement()
77  {
78  $logger = $this->getLogger();
79  $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
80 
81  $expected = array('info {Message {nothing} Bob Bar a}');
82  $this->assertEquals($expected, $this->getLogs());
83  }
84 
85  public function testObjectCastToString()
86  {
87  $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString'));
88  $dummy->expects($this->once())
89  ->method('__toString')
90  ->will($this->returnValue('DUMMY'));
91 
92  $this->getLogger()->warning($dummy);
93 
94  $expected = array('warning DUMMY');
95  $this->assertEquals($expected, $this->getLogs());
96  }
97 
98  public function testContextCanContainAnything()
99  {
100  $context = array(
101  'bool' => true,
102  'null' => null,
103  'string' => 'Foo',
104  'int' => 0,
105  'float' => 0.5,
106  'nested' => array('with object' => new DummyTest),
107  'object' => new \DateTime,
108  'resource' => fopen('php://memory', 'r'),
109  );
110 
111  $this->getLogger()->warning('Crazy context data', $context);
112 
113  $expected = array('warning Crazy context data');
114  $this->assertEquals($expected, $this->getLogs());
115  }
116 
117  public function testContextExceptionKeyCanBeExceptionOrOtherValues()
118  {
119  $logger = $this->getLogger();
120  $logger->warning('Random message', array('exception' => 'oops'));
121  $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
122 
123  $expected = array(
124  'warning Random message',
125  'critical Uncaught Exception!'
126  );
127  $this->assertEquals($expected, $this->getLogs());
128  }
129 }
130 
132 {
133 }