Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Tests / Logger / ConsoleLoggerTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Tests\Logger;
13
14 use PHPUnit\Framework\TestCase;
15 use Psr\Log\LoggerInterface;
16 use Psr\Log\LogLevel;
17 use Symfony\Component\Console\Logger\ConsoleLogger;
18 use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
19 use Symfony\Component\Console\Output\OutputInterface;
20
21 /**
22  * Console logger test.
23  *
24  * @author Kévin Dunglas <dunglas@gmail.com>
25  * @author Jordi Boggiano <j.boggiano@seld.be>
26  */
27 class ConsoleLoggerTest extends TestCase
28 {
29     /**
30      * @var DummyOutput
31      */
32     protected $output;
33
34     /**
35      * @return LoggerInterface
36      */
37     public function getLogger()
38     {
39         $this->output = new DummyOutput(OutputInterface::VERBOSITY_VERBOSE);
40
41         return new ConsoleLogger($this->output, array(
42             LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
43             LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
44             LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
45             LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
46             LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
47             LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
48             LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
49             LogLevel::DEBUG => OutputInterface::VERBOSITY_NORMAL,
50         ));
51     }
52
53     /**
54      * Return the log messages in order.
55      *
56      * @return string[]
57      */
58     public function getLogs()
59     {
60         return $this->output->getLogs();
61     }
62
63     public function testImplements()
64     {
65         $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
66     }
67
68     /**
69      * @dataProvider provideLevelsAndMessages
70      */
71     public function testLogsAtAllLevels($level, $message)
72     {
73         $logger = $this->getLogger();
74         $logger->{$level}($message, array('user' => 'Bob'));
75         $logger->log($level, $message, array('user' => 'Bob'));
76
77         $expected = array(
78             $level.' message of level '.$level.' with context: Bob',
79             $level.' message of level '.$level.' with context: Bob',
80         );
81         $this->assertEquals($expected, $this->getLogs());
82     }
83
84     public function provideLevelsAndMessages()
85     {
86         return array(
87             LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'),
88             LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'),
89             LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'),
90             LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'),
91             LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'),
92             LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'),
93             LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'),
94             LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'),
95         );
96     }
97
98     /**
99      * @expectedException \Psr\Log\InvalidArgumentException
100      */
101     public function testThrowsOnInvalidLevel()
102     {
103         $logger = $this->getLogger();
104         $logger->log('invalid level', 'Foo');
105     }
106
107     public function testContextReplacement()
108     {
109         $logger = $this->getLogger();
110         $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
111
112         $expected = array('info {Message {nothing} Bob Bar a}');
113         $this->assertEquals($expected, $this->getLogs());
114     }
115
116     public function testObjectCastToString()
117     {
118         if (method_exists($this, 'createPartialMock')) {
119             $dummy = $this->createPartialMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
120         } else {
121             $dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
122         }
123         $dummy->expects($this->once())
124             ->method('__toString')
125             ->will($this->returnValue('DUMMY'));
126
127         $this->getLogger()->warning($dummy);
128
129         $expected = array('warning DUMMY');
130         $this->assertEquals($expected, $this->getLogs());
131     }
132
133     public function testContextCanContainAnything()
134     {
135         $context = array(
136             'bool' => true,
137             'null' => null,
138             'string' => 'Foo',
139             'int' => 0,
140             'float' => 0.5,
141             'nested' => array('with object' => new DummyTest()),
142             'object' => new \DateTime(),
143             'resource' => fopen('php://memory', 'r'),
144         );
145
146         $this->getLogger()->warning('Crazy context data', $context);
147
148         $expected = array('warning Crazy context data');
149         $this->assertEquals($expected, $this->getLogs());
150     }
151
152     public function testContextExceptionKeyCanBeExceptionOrOtherValues()
153     {
154         $logger = $this->getLogger();
155         $logger->warning('Random message', array('exception' => 'oops'));
156         $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
157
158         $expected = array(
159             'warning Random message',
160             'critical Uncaught Exception!',
161         );
162         $this->assertEquals($expected, $this->getLogs());
163     }
164 }
165
166 class DummyTest
167 {
168     public function __toString()
169     {
170     }
171 }