Brak opisu

ConsoleLoggerTest.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 Psr\Log\Test\LoggerInterfaceTest;
  12. use Psr\Log\LogLevel;
  13. use Symfony\Component\Console\Logger\ConsoleLogger;
  14. use Symfony\Component\Console\Output\BufferedOutput;
  15. use Symfony\Component\Console\Tests\Fixtures\DummyOutput;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * Console logger test.
  19. *
  20. * @author Kévin Dunglas <dunglas@gmail.com>
  21. */
  22. class ConsoleLoggerTest extends LoggerInterfaceTest
  23. {
  24. /**
  25. * @var DummyOutput
  26. */
  27. protected $output;
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getLogger()
  32. {
  33. $this->output = new DummyOutput(OutputInterface::VERBOSITY_VERBOSE);
  34. return new ConsoleLogger($this->output, array(
  35. LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
  36. LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
  37. LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
  38. LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
  39. LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
  40. LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
  41. LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
  42. LogLevel::DEBUG => OutputInterface::VERBOSITY_NORMAL,
  43. ));
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getLogs()
  49. {
  50. return $this->output->getLogs();
  51. }
  52. /**
  53. * @dataProvider provideOutputMappingParams
  54. */
  55. public function testOutputMapping($logLevel, $outputVerbosity, $isOutput, $addVerbosityLevelMap = array())
  56. {
  57. $out = new BufferedOutput($outputVerbosity);
  58. $logger = new ConsoleLogger($out, $addVerbosityLevelMap);
  59. $logger->log($logLevel, 'foo bar');
  60. $logs = $out->fetch();
  61. $this->assertEquals($isOutput ? "[$logLevel] foo bar\n" : '', $logs);
  62. }
  63. public function provideOutputMappingParams()
  64. {
  65. $quietMap = array(LogLevel::EMERGENCY => OutputInterface::VERBOSITY_QUIET);
  66. return array(
  67. array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_NORMAL, true),
  68. array(LogLevel::WARNING, OutputInterface::VERBOSITY_NORMAL, true),
  69. array(LogLevel::INFO, OutputInterface::VERBOSITY_NORMAL, false),
  70. array(LogLevel::DEBUG, OutputInterface::VERBOSITY_NORMAL, false),
  71. array(LogLevel::INFO, OutputInterface::VERBOSITY_VERBOSE, false),
  72. array(LogLevel::INFO, OutputInterface::VERBOSITY_VERY_VERBOSE, true),
  73. array(LogLevel::DEBUG, OutputInterface::VERBOSITY_VERY_VERBOSE, false),
  74. array(LogLevel::DEBUG, OutputInterface::VERBOSITY_DEBUG, true),
  75. array(LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false),
  76. array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, false),
  77. array(LogLevel::ALERT, OutputInterface::VERBOSITY_QUIET, false, $quietMap),
  78. array(LogLevel::EMERGENCY, OutputInterface::VERBOSITY_QUIET, true, $quietMap),
  79. );
  80. }
  81. public function testHasErrored()
  82. {
  83. $logger = new ConsoleLogger(new BufferedOutput());
  84. $this->assertFalse($logger->hasErrored());
  85. $logger->warning('foo');
  86. $this->assertFalse($logger->hasErrored());
  87. $logger->error('bar');
  88. $this->assertTrue($logger->hasErrored());
  89. }
  90. }