ConsoleLoggerTest.php 7.5 KB

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