ConsoleLoggerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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\Output\OutputInterface;
  17. use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
  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, [
  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 = [])
  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 = [LogLevel::EMERGENCY => OutputInterface::VERBOSITY_QUIET];
  70. return [
  71. [LogLevel::EMERGENCY, OutputInterface::VERBOSITY_NORMAL, true],
  72. [LogLevel::WARNING, OutputInterface::VERBOSITY_NORMAL, true],
  73. [LogLevel::INFO, OutputInterface::VERBOSITY_NORMAL, false],
  74. [LogLevel::DEBUG, OutputInterface::VERBOSITY_NORMAL, false],
  75. [LogLevel::INFO, OutputInterface::VERBOSITY_VERBOSE, false],
  76. [LogLevel::INFO, OutputInterface::VERBOSITY_VERY_VERBOSE, true],
  77. [LogLevel::DEBUG, OutputInterface::VERBOSITY_VERY_VERBOSE, false],
  78. [LogLevel::DEBUG, OutputInterface::VERBOSITY_DEBUG, true],
  79. [LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false],
  80. [LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, false],
  81. [LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false, $quietMap],
  82. [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, ['user' => 'Bob']);
  105. $logger->log($level, $message, ['user' => 'Bob']);
  106. $expected = [
  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 [
  115. LogLevel::EMERGENCY => [LogLevel::EMERGENCY, 'message of level emergency with context: {user}'],
  116. LogLevel::ALERT => [LogLevel::ALERT, 'message of level alert with context: {user}'],
  117. LogLevel::CRITICAL => [LogLevel::CRITICAL, 'message of level critical with context: {user}'],
  118. LogLevel::ERROR => [LogLevel::ERROR, 'message of level error with context: {user}'],
  119. LogLevel::WARNING => [LogLevel::WARNING, 'message of level warning with context: {user}'],
  120. LogLevel::NOTICE => [LogLevel::NOTICE, 'message of level notice with context: {user}'],
  121. LogLevel::INFO => [LogLevel::INFO, 'message of level info with context: {user}'],
  122. LogLevel::DEBUG => [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}', ['user' => 'Bob', 'foo.bar' => 'Bar']);
  137. $expected = ['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', ['__toString']);
  144. } else {
  145. $dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', ['__toString']);
  146. }
  147. $dummy->method('__toString')->will($this->returnValue('DUMMY'));
  148. $this->getLogger()->warning($dummy);
  149. $expected = ['warning DUMMY'];
  150. $this->assertEquals($expected, $this->getLogs());
  151. }
  152. public function testContextCanContainAnything()
  153. {
  154. $context = [
  155. 'bool' => true,
  156. 'null' => null,
  157. 'string' => 'Foo',
  158. 'int' => 0,
  159. 'float' => 0.5,
  160. 'nested' => ['with object' => new DummyTest()],
  161. 'object' => new \DateTime(),
  162. 'resource' => fopen('php://memory', 'r'),
  163. ];
  164. $this->getLogger()->warning('Crazy context data', $context);
  165. $expected = ['warning Crazy context data'];
  166. $this->assertEquals($expected, $this->getLogs());
  167. }
  168. public function testContextExceptionKeyCanBeExceptionOrOtherValues()
  169. {
  170. $logger = $this->getLogger();
  171. $logger->warning('Random message', ['exception' => 'oops']);
  172. $logger->critical('Uncaught Exception!', ['exception' => new \LogicException('Fail')]);
  173. $expected = [
  174. 'warning Random message',
  175. 'critical Uncaught Exception!',
  176. ];
  177. $this->assertEquals($expected, $this->getLogs());
  178. }
  179. }
  180. class DummyTest
  181. {
  182. public function __toString()
  183. {
  184. }
  185. }