Updated to Drupal 8.5. Core Media not yet in use.
[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\Output\BufferedOutput;
19 use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
20 use Symfony\Component\Console\Output\OutputInterface;
21
22 /**
23  * Console logger test.
24  *
25  * @author Kévin Dunglas <dunglas@gmail.com>
26  * @author Jordi Boggiano <j.boggiano@seld.be>
27  */
28 class ConsoleLoggerTest extends TestCase
29 {
30     /**
31      * @var DummyOutput
32      */
33     protected $output;
34
35     /**
36      * @return LoggerInterface
37      */
38     public function getLogger()
39     {
40         $this->output = new DummyOutput(OutputInterface::VERBOSITY_VERBOSE);
41
42         return new ConsoleLogger($this->output, array(
43             LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
44             LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
45             LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
46             LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
47             LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
48             LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
49             LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
50             LogLevel::DEBUG => OutputInterface::VERBOSITY_NORMAL,
51         ));
52     }
53
54     /**
55      * Return the log messages in order.
56      *
57      * @return string[]
58      */
59     public function getLogs()
60     {
61         return $this->output->getLogs();
62     }
63
64     /**
65      * @dataProvider provideOutputMappingParams
66      */
67     public function testOutputMapping($logLevel, $outputVerbosity, $isOutput, $addVerbosityLevelMap = array())
68     {
69         $out = new BufferedOutput($outputVerbosity);
70         $logger = new ConsoleLogger($out, $addVerbosityLevelMap);
71         $logger->log($logLevel, 'foo bar');
72         $logs = $out->fetch();
73         $this->assertEquals($isOutput ? "[$logLevel] foo bar".PHP_EOL : '', $logs);
74     }
75
76     public function provideOutputMappingParams()
77     {
78         $quietMap = array(LogLevel::EMERGENCY => OutputInterface::VERBOSITY_QUIET);
79
80         return array(
81             array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_NORMAL, true),
82             array(LogLevel::WARNING, OutputInterface::VERBOSITY_NORMAL, true),
83             array(LogLevel::INFO, OutputInterface::VERBOSITY_NORMAL, false),
84             array(LogLevel::DEBUG, OutputInterface::VERBOSITY_NORMAL, false),
85             array(LogLevel::INFO, OutputInterface::VERBOSITY_VERBOSE, false),
86             array(LogLevel::INFO, OutputInterface::VERBOSITY_VERY_VERBOSE, true),
87             array(LogLevel::DEBUG, OutputInterface::VERBOSITY_VERY_VERBOSE, false),
88             array(LogLevel::DEBUG, OutputInterface::VERBOSITY_DEBUG, true),
89             array(LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false),
90             array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, false),
91             array(LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false, $quietMap),
92             array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, true, $quietMap),
93         );
94     }
95
96     public function testHasErrored()
97     {
98         $logger = new ConsoleLogger(new BufferedOutput());
99
100         $this->assertFalse($logger->hasErrored());
101
102         $logger->warning('foo');
103         $this->assertFalse($logger->hasErrored());
104
105         $logger->error('bar');
106         $this->assertTrue($logger->hasErrored());
107     }
108
109     public function testImplements()
110     {
111         $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
112     }
113
114     /**
115      * @dataProvider provideLevelsAndMessages
116      */
117     public function testLogsAtAllLevels($level, $message)
118     {
119         $logger = $this->getLogger();
120         $logger->{$level}($message, array('user' => 'Bob'));
121         $logger->log($level, $message, array('user' => 'Bob'));
122
123         $expected = array(
124             $level.' message of level '.$level.' with context: Bob',
125             $level.' message of level '.$level.' with context: Bob',
126         );
127         $this->assertEquals($expected, $this->getLogs());
128     }
129
130     public function provideLevelsAndMessages()
131     {
132         return array(
133             LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'),
134             LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'),
135             LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'),
136             LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'),
137             LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'),
138             LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'),
139             LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'),
140             LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'),
141         );
142     }
143
144     /**
145      * @expectedException \Psr\Log\InvalidArgumentException
146      */
147     public function testThrowsOnInvalidLevel()
148     {
149         $logger = $this->getLogger();
150         $logger->log('invalid level', 'Foo');
151     }
152
153     public function testContextReplacement()
154     {
155         $logger = $this->getLogger();
156         $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
157
158         $expected = array('info {Message {nothing} Bob Bar a}');
159         $this->assertEquals($expected, $this->getLogs());
160     }
161
162     public function testObjectCastToString()
163     {
164         if (method_exists($this, 'createPartialMock')) {
165             $dummy = $this->createPartialMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
166         } else {
167             $dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
168         }
169         $dummy->method('__toString')->will($this->returnValue('DUMMY'));
170
171         $this->getLogger()->warning($dummy);
172
173         $expected = array('warning DUMMY');
174         $this->assertEquals($expected, $this->getLogs());
175     }
176
177     public function testContextCanContainAnything()
178     {
179         $context = array(
180             'bool' => true,
181             'null' => null,
182             'string' => 'Foo',
183             'int' => 0,
184             'float' => 0.5,
185             'nested' => array('with object' => new DummyTest()),
186             'object' => new \DateTime(),
187             'resource' => fopen('php://memory', 'r'),
188         );
189
190         $this->getLogger()->warning('Crazy context data', $context);
191
192         $expected = array('warning Crazy context data');
193         $this->assertEquals($expected, $this->getLogs());
194     }
195
196     public function testContextExceptionKeyCanBeExceptionOrOtherValues()
197     {
198         $logger = $this->getLogger();
199         $logger->warning('Random message', array('exception' => 'oops'));
200         $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
201
202         $expected = array(
203             'warning Random message',
204             'critical Uncaught Exception!',
205         );
206         $this->assertEquals($expected, $this->getLogs());
207     }
208 }
209
210 class DummyTest
211 {
212     public function __toString()
213     {
214     }
215 }