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