Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Psr\Log\Test;
|
Chris@0
|
4
|
Chris@0
|
5 use Psr\Log\LoggerInterface;
|
Chris@0
|
6 use Psr\Log\LogLevel;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides a base test class for ensuring compliance with the LoggerInterface.
|
Chris@0
|
10 *
|
Chris@0
|
11 * Implementors can extend the class and implement abstract methods to run this
|
Chris@0
|
12 * as part of their test suite.
|
Chris@0
|
13 */
|
Chris@0
|
14 abstract class LoggerInterfaceTest extends \PHPUnit_Framework_TestCase
|
Chris@0
|
15 {
|
Chris@0
|
16 /**
|
Chris@0
|
17 * @return LoggerInterface
|
Chris@0
|
18 */
|
Chris@0
|
19 abstract public function getLogger();
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * This must return the log messages in order.
|
Chris@0
|
23 *
|
Chris@0
|
24 * The simple formatting of the messages is: "<LOG LEVEL> <MESSAGE>".
|
Chris@0
|
25 *
|
Chris@0
|
26 * Example ->error('Foo') would yield "error Foo".
|
Chris@0
|
27 *
|
Chris@0
|
28 * @return string[]
|
Chris@0
|
29 */
|
Chris@0
|
30 abstract public function getLogs();
|
Chris@0
|
31
|
Chris@0
|
32 public function testImplements()
|
Chris@0
|
33 {
|
Chris@0
|
34 $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
|
Chris@0
|
35 }
|
Chris@0
|
36
|
Chris@0
|
37 /**
|
Chris@0
|
38 * @dataProvider provideLevelsAndMessages
|
Chris@0
|
39 */
|
Chris@0
|
40 public function testLogsAtAllLevels($level, $message)
|
Chris@0
|
41 {
|
Chris@0
|
42 $logger = $this->getLogger();
|
Chris@0
|
43 $logger->{$level}($message, array('user' => 'Bob'));
|
Chris@0
|
44 $logger->log($level, $message, array('user' => 'Bob'));
|
Chris@0
|
45
|
Chris@0
|
46 $expected = array(
|
Chris@0
|
47 $level.' message of level '.$level.' with context: Bob',
|
Chris@0
|
48 $level.' message of level '.$level.' with context: Bob',
|
Chris@0
|
49 );
|
Chris@0
|
50 $this->assertEquals($expected, $this->getLogs());
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 public function provideLevelsAndMessages()
|
Chris@0
|
54 {
|
Chris@0
|
55 return array(
|
Chris@0
|
56 LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'),
|
Chris@0
|
57 LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'),
|
Chris@0
|
58 LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'),
|
Chris@0
|
59 LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'),
|
Chris@0
|
60 LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'),
|
Chris@0
|
61 LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'),
|
Chris@0
|
62 LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'),
|
Chris@0
|
63 LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'),
|
Chris@0
|
64 );
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 /**
|
Chris@0
|
68 * @expectedException \Psr\Log\InvalidArgumentException
|
Chris@0
|
69 */
|
Chris@0
|
70 public function testThrowsOnInvalidLevel()
|
Chris@0
|
71 {
|
Chris@0
|
72 $logger = $this->getLogger();
|
Chris@0
|
73 $logger->log('invalid level', 'Foo');
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 public function testContextReplacement()
|
Chris@0
|
77 {
|
Chris@0
|
78 $logger = $this->getLogger();
|
Chris@0
|
79 $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
|
Chris@0
|
80
|
Chris@0
|
81 $expected = array('info {Message {nothing} Bob Bar a}');
|
Chris@0
|
82 $this->assertEquals($expected, $this->getLogs());
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 public function testObjectCastToString()
|
Chris@0
|
86 {
|
Chris@0
|
87 if (method_exists($this, 'createPartialMock')) {
|
Chris@0
|
88 $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString'));
|
Chris@0
|
89 } else {
|
Chris@0
|
90 $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString'));
|
Chris@0
|
91 }
|
Chris@0
|
92 $dummy->expects($this->once())
|
Chris@0
|
93 ->method('__toString')
|
Chris@0
|
94 ->will($this->returnValue('DUMMY'));
|
Chris@0
|
95
|
Chris@0
|
96 $this->getLogger()->warning($dummy);
|
Chris@0
|
97
|
Chris@0
|
98 $expected = array('warning DUMMY');
|
Chris@0
|
99 $this->assertEquals($expected, $this->getLogs());
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 public function testContextCanContainAnything()
|
Chris@0
|
103 {
|
Chris@17
|
104 $closed = fopen('php://memory', 'r');
|
Chris@17
|
105 fclose($closed);
|
Chris@17
|
106
|
Chris@0
|
107 $context = array(
|
Chris@0
|
108 'bool' => true,
|
Chris@0
|
109 'null' => null,
|
Chris@0
|
110 'string' => 'Foo',
|
Chris@0
|
111 'int' => 0,
|
Chris@0
|
112 'float' => 0.5,
|
Chris@0
|
113 'nested' => array('with object' => new DummyTest),
|
Chris@0
|
114 'object' => new \DateTime,
|
Chris@0
|
115 'resource' => fopen('php://memory', 'r'),
|
Chris@17
|
116 'closed' => $closed,
|
Chris@0
|
117 );
|
Chris@0
|
118
|
Chris@0
|
119 $this->getLogger()->warning('Crazy context data', $context);
|
Chris@0
|
120
|
Chris@0
|
121 $expected = array('warning Crazy context data');
|
Chris@0
|
122 $this->assertEquals($expected, $this->getLogs());
|
Chris@0
|
123 }
|
Chris@0
|
124
|
Chris@0
|
125 public function testContextExceptionKeyCanBeExceptionOrOtherValues()
|
Chris@0
|
126 {
|
Chris@0
|
127 $logger = $this->getLogger();
|
Chris@0
|
128 $logger->warning('Random message', array('exception' => 'oops'));
|
Chris@0
|
129 $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
|
Chris@0
|
130
|
Chris@0
|
131 $expected = array(
|
Chris@0
|
132 'warning Random message',
|
Chris@0
|
133 'critical Uncaught Exception!'
|
Chris@0
|
134 );
|
Chris@0
|
135 $this->assertEquals($expected, $this->getLogs());
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 class DummyTest
|
Chris@0
|
140 {
|
Chris@0
|
141 public function __toString()
|
Chris@0
|
142 {
|
Chris@0
|
143 }
|
Chris@0
|
144 }
|