4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Tests\Logger;
14 use PHPUnit\Framework\TestCase;
15 use Psr\Log\LoggerInterface;
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;
23 * Console logger test.
25 * @author Kévin Dunglas <dunglas@gmail.com>
26 * @author Jordi Boggiano <j.boggiano@seld.be>
28 class ConsoleLoggerTest extends TestCase
36 * @return LoggerInterface
38 public function getLogger()
40 $this->output = new DummyOutput(OutputInterface::VERBOSITY_VERBOSE);
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,
55 * Return the log messages in order.
59 public function getLogs()
61 return $this->output->getLogs();
65 * @dataProvider provideOutputMappingParams
67 public function testOutputMapping($logLevel, $outputVerbosity, $isOutput, $addVerbosityLevelMap = array())
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);
76 public function provideOutputMappingParams()
78 $quietMap = array(LogLevel::EMERGENCY => OutputInterface::VERBOSITY_QUIET);
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),
96 public function testHasErrored()
98 $logger = new ConsoleLogger(new BufferedOutput());
100 $this->assertFalse($logger->hasErrored());
102 $logger->warning('foo');
103 $this->assertFalse($logger->hasErrored());
105 $logger->error('bar');
106 $this->assertTrue($logger->hasErrored());
109 public function testImplements()
111 $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
115 * @dataProvider provideLevelsAndMessages
117 public function testLogsAtAllLevels($level, $message)
119 $logger = $this->getLogger();
120 $logger->{$level}($message, array('user' => 'Bob'));
121 $logger->log($level, $message, array('user' => 'Bob'));
124 $level.' message of level '.$level.' with context: Bob',
125 $level.' message of level '.$level.' with context: Bob',
127 $this->assertEquals($expected, $this->getLogs());
130 public function provideLevelsAndMessages()
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}'),
145 * @expectedException \Psr\Log\InvalidArgumentException
147 public function testThrowsOnInvalidLevel()
149 $logger = $this->getLogger();
150 $logger->log('invalid level', 'Foo');
153 public function testContextReplacement()
155 $logger = $this->getLogger();
156 $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
158 $expected = array('info {Message {nothing} Bob Bar a}');
159 $this->assertEquals($expected, $this->getLogs());
162 public function testObjectCastToString()
164 if (method_exists($this, 'createPartialMock')) {
165 $dummy = $this->createPartialMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
167 $dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
169 $dummy->method('__toString')->will($this->returnValue('DUMMY'));
171 $this->getLogger()->warning($dummy);
173 $expected = array('warning DUMMY');
174 $this->assertEquals($expected, $this->getLogs());
177 public function testContextCanContainAnything()
185 'nested' => array('with object' => new DummyTest()),
186 'object' => new \DateTime(),
187 'resource' => fopen('php://memory', 'r'),
190 $this->getLogger()->warning('Crazy context data', $context);
192 $expected = array('warning Crazy context data');
193 $this->assertEquals($expected, $this->getLogs());
196 public function testContextExceptionKeyCanBeExceptionOrOtherValues()
198 $logger = $this->getLogger();
199 $logger->warning('Random message', array('exception' => 'oops'));
200 $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
203 'warning Random message',
204 'critical Uncaught Exception!',
206 $this->assertEquals($expected, $this->getLogs());
212 public function __toString()