1<?php
2
3namespace Psr\Log\Test;
4
5use Psr\Log\LoggerInterface;
6use Psr\Log\LogLevel;
7
8/**
9 * Provides a base test class for ensuring compliance with the LoggerInterface.
10 *
11 * Implementors can extend the class and implement abstract methods to run this
12 * as part of their test suite.
13 */
14abstract class LoggerInterfaceTest extends \PHPUnit_Framework_TestCase
15{
16    /**
17     * @return LoggerInterface
18     */
19    abstract public function getLogger();
20
21    /**
22     * This must return the log messages in order.
23     *
24     * The simple formatting of the messages is: "<LOG LEVEL> <MESSAGE>".
25     *
26     * Example ->error('Foo') would yield "error Foo".
27     *
28     * @return string[]
29     */
30    abstract public function getLogs();
31
32    public function testImplements()
33    {
34        $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
35    }
36
37    /**
38     * @dataProvider provideLevelsAndMessages
39     */
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
67    /**
68     * @expectedException \Psr\Log\InvalidArgumentException
69     */
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        if (method_exists($this, 'createPartialMock')) {
88            $dummy = $this->createPartialMock('Psr\Log\Test\DummyTest', array('__toString'));
89        } else {
90            $dummy = $this->getMock('Psr\Log\Test\DummyTest', array('__toString'));
91        }
92        $dummy->expects($this->once())
93            ->method('__toString')
94            ->will($this->returnValue('DUMMY'));
95
96        $this->getLogger()->warning($dummy);
97
98        $expected = array('warning DUMMY');
99        $this->assertEquals($expected, $this->getLogs());
100    }
101
102    public function testContextCanContainAnything()
103    {
104        $closed = fopen('php://memory', 'r');
105        fclose($closed);
106
107        $context = array(
108            'bool' => true,
109            'null' => null,
110            'string' => 'Foo',
111            'int' => 0,
112            'float' => 0.5,
113            'nested' => array('with object' => new DummyTest),
114            'object' => new \DateTime,
115            'resource' => fopen('php://memory', 'r'),
116            'closed' => $closed,
117        );
118
119        $this->getLogger()->warning('Crazy context data', $context);
120
121        $expected = array('warning Crazy context data');
122        $this->assertEquals($expected, $this->getLogs());
123    }
124
125    public function testContextExceptionKeyCanBeExceptionOrOtherValues()
126    {
127        $logger = $this->getLogger();
128        $logger->warning('Random message', array('exception' => 'oops'));
129        $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
130
131        $expected = array(
132            'warning Random message',
133            'critical Uncaught Exception!'
134        );
135        $this->assertEquals($expected, $this->getLogs());
136    }
137}
138
139class DummyTest
140{
141    public function __toString()
142    {
143    }
144}
145