ConsoleLoggerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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\OutputInterface;
  16. use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
  17. /**
  18. * Console logger test.
  19. *
  20. * @author Kévin Dunglas <dunglas@gmail.com>
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class ConsoleLoggerTest extends TestCase
  24. {
  25. /**
  26. * @var DummyOutput
  27. */
  28. protected $output;
  29. /**
  30. * @return LoggerInterface
  31. */
  32. public function getLogger()
  33. {
  34. $this->output = new DummyOutput(OutputInterface::VERBOSITY_VERBOSE);
  35. return new ConsoleLogger($this->output, array(
  36. LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
  37. LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
  38. LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
  39. LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
  40. LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
  41. LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
  42. LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
  43. LogLevel::DEBUG => OutputInterface::VERBOSITY_NORMAL,
  44. ));
  45. }
  46. /**
  47. * Return the log messages in order.
  48. *
  49. * @return string[]
  50. */
  51. public function getLogs()
  52. {
  53. return $this->output->getLogs();
  54. }
  55. public function testImplements()
  56. {
  57. $this->assertInstanceOf('Psr\Log\LoggerInterface', $this->getLogger());
  58. }
  59. /**
  60. * @dataProvider provideLevelsAndMessages
  61. */
  62. public function testLogsAtAllLevels($level, $message)
  63. {
  64. $logger = $this->getLogger();
  65. $logger->{$level}($message, array('user' => 'Bob'));
  66. $logger->log($level, $message, array('user' => 'Bob'));
  67. $expected = array(
  68. $level.' message of level '.$level.' with context: Bob',
  69. $level.' message of level '.$level.' with context: Bob',
  70. );
  71. $this->assertEquals($expected, $this->getLogs());
  72. }
  73. public function provideLevelsAndMessages()
  74. {
  75. return array(
  76. LogLevel::EMERGENCY => array(LogLevel::EMERGENCY, 'message of level emergency with context: {user}'),
  77. LogLevel::ALERT => array(LogLevel::ALERT, 'message of level alert with context: {user}'),
  78. LogLevel::CRITICAL => array(LogLevel::CRITICAL, 'message of level critical with context: {user}'),
  79. LogLevel::ERROR => array(LogLevel::ERROR, 'message of level error with context: {user}'),
  80. LogLevel::WARNING => array(LogLevel::WARNING, 'message of level warning with context: {user}'),
  81. LogLevel::NOTICE => array(LogLevel::NOTICE, 'message of level notice with context: {user}'),
  82. LogLevel::INFO => array(LogLevel::INFO, 'message of level info with context: {user}'),
  83. LogLevel::DEBUG => array(LogLevel::DEBUG, 'message of level debug with context: {user}'),
  84. );
  85. }
  86. /**
  87. * @expectedException \Psr\Log\InvalidArgumentException
  88. */
  89. public function testThrowsOnInvalidLevel()
  90. {
  91. $logger = $this->getLogger();
  92. $logger->log('invalid level', 'Foo');
  93. }
  94. public function testContextReplacement()
  95. {
  96. $logger = $this->getLogger();
  97. $logger->info('{Message {nothing} {user} {foo.bar} a}', array('user' => 'Bob', 'foo.bar' => 'Bar'));
  98. $expected = array('info {Message {nothing} Bob Bar a}');
  99. $this->assertEquals($expected, $this->getLogs());
  100. }
  101. public function testObjectCastToString()
  102. {
  103. if (method_exists($this, 'createPartialMock')) {
  104. $dummy = $this->createPartialMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
  105. } else {
  106. $dummy = $this->getMock('Symfony\Component\Console\Tests\Logger\DummyTest', array('__toString'));
  107. }
  108. $dummy->expects($this->once())
  109. ->method('__toString')
  110. ->will($this->returnValue('DUMMY'));
  111. $this->getLogger()->warning($dummy);
  112. $expected = array('warning DUMMY');
  113. $this->assertEquals($expected, $this->getLogs());
  114. }
  115. public function testContextCanContainAnything()
  116. {
  117. $context = array(
  118. 'bool' => true,
  119. 'null' => null,
  120. 'string' => 'Foo',
  121. 'int' => 0,
  122. 'float' => 0.5,
  123. 'nested' => array('with object' => new DummyTest()),
  124. 'object' => new \DateTime(),
  125. 'resource' => fopen('php://memory', 'r'),
  126. );
  127. $this->getLogger()->warning('Crazy context data', $context);
  128. $expected = array('warning Crazy context data');
  129. $this->assertEquals($expected, $this->getLogs());
  130. }
  131. public function testContextExceptionKeyCanBeExceptionOrOtherValues()
  132. {
  133. $logger = $this->getLogger();
  134. $logger->warning('Random message', array('exception' => 'oops'));
  135. $logger->critical('Uncaught Exception!', array('exception' => new \LogicException('Fail')));
  136. $expected = array(
  137. 'warning Random message',
  138. 'critical Uncaught Exception!',
  139. );
  140. $this->assertEquals($expected, $this->getLogs());
  141. }
  142. }
  143. class DummyTest
  144. {
  145. public function __toString()
  146. {
  147. }
  148. }