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