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\Tests\Fixtures\DummyOutput;
19 use Symfony\Component\Console\Output\OutputInterface;
22 * Console logger test.
24 * @author Kévin Dunglas <dunglas@gmail.com>
25 * @author Jordi Boggiano <j.boggiano@seld.be>
27 class ConsoleLoggerTest extends TestCase
35 * @return LoggerInterface
37 public function getLogger()
39 $this->output = new DummyOutput(OutputInterface::VERBOSITY_VERBOSE);
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,
54 * Return the log messages in order.
58 public function getLogs()
60 return $this->output->getLogs();
63 public function testImplements()
65 $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
69 * @dataProvider provideLevelsAndMessages
71 public function testLogsAtAllLevels($level, $message)
73 $logger = $this->getLogger();
74 $logger->{$level}($message, array('user' => 'Bob'));
75 $logger->log($level, $message, array('user' => 'Bob'));
78 $level.' message of level '.$level.' with context: Bob',
79 $level.' message of level '.$level.' with context: Bob',
81 $this->assertEquals($expected, $this->getLogs());
84 public function provideLevelsAndMessages()
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}'),
99 * @expectedException \Psr\Log\InvalidArgumentException
101 public function testThrowsOnInvalidLevel()
103 $logger = $this->getLogger();
104 $logger->log('invalid level', 'Foo');
107 public function testContextReplacement()
109 $logger = $this->getLogger();
110 $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
112 $expected = array('info {Message {nothing} Bob Bar a}');
113 $this->assertEquals($expected, $this->getLogs());
116 public function testObjectCastToString()
118 if (method_exists($this, 'createPartialMock')) {
119 $dummy = $this->createPartialMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
121 $dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
123 $dummy->expects($this->once())
124 ->method('__toString')
125 ->will($this->returnValue('DUMMY'));
127 $this->getLogger()->warning($dummy);
129 $expected = array('warning DUMMY');
130 $this->assertEquals($expected, $this->getLogs());
133 public function testContextCanContainAnything()
141 'nested' => array('with object' => new DummyTest()),
142 'object' => new \DateTime(),
143 'resource' => fopen('php://memory', 'r'),
146 $this->getLogger()->warning('Crazy context data', $context);
148 $expected = array('warning Crazy context data');
149 $this->assertEquals($expected, $this->getLogs());
152 public function testContextExceptionKeyCanBeExceptionOrOtherValues()
154 $logger = $this->getLogger();
155 $logger->warning('Random message', array('exception' => 'oops'));
156 $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
159 'warning Random message',
160 'critical Uncaught Exception!',
162 $this->assertEquals($expected, $this->getLogs());
168 public function __toString()