LoggerVerbosityAndStyleTests.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Consolidation\Log;
  3. use Psr\Log\LogLevel;
  4. use Symfony\Component\Console\Output\BufferedOutput;
  5. use Symfony\Component\Console\Output\OutputInterface;
  6. use PHPUnit\Framework\TestCase;
  7. use Consolidation\TestUtils\TestDataPermuter;
  8. class LoggerVerbosityAndStyleTests extends TestCase
  9. {
  10. protected $output;
  11. protected $logger;
  12. function setup() {
  13. $this->output = new BufferedOutput();
  14. //$this->output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
  15. $this->logger = new Logger($this->output);
  16. }
  17. public static function logTestValues()
  18. {
  19. /**
  20. * Use TEST_ALL_LOG_LEVELS to ensure that output is the same
  21. * in instances where the output does not vary by log level.
  22. */
  23. $TEST_ALL_LOG_LEVELS = [
  24. OutputInterface::VERBOSITY_DEBUG,
  25. OutputInterface::VERBOSITY_VERY_VERBOSE,
  26. OutputInterface::VERBOSITY_VERBOSE,
  27. OutputInterface::VERBOSITY_NORMAL
  28. ];
  29. // Tests that return the same value for multiple inputs
  30. // may use the expandProviderDataArrays method, and list
  31. // repeated scalars as array values. All permutations of
  32. // all array items will be calculated, and one test will
  33. // be generated for each one.
  34. return TestDataPermuter::expandProviderDataArrays([
  35. [
  36. '\Consolidation\Log\UnstyledLogOutputStyler',
  37. $TEST_ALL_LOG_LEVELS,
  38. LogLevel::ERROR,
  39. 'Do not enter - wrong way.',
  40. ' [error] Do not enter - wrong way.',
  41. ],
  42. [
  43. '\Consolidation\Log\UnstyledLogOutputStyler',
  44. $TEST_ALL_LOG_LEVELS,
  45. LogLevel::WARNING,
  46. 'Steep grade.',
  47. ' [warning] Steep grade.',
  48. ],
  49. [
  50. '\Consolidation\Log\UnstyledLogOutputStyler',
  51. [
  52. OutputInterface::VERBOSITY_DEBUG,
  53. OutputInterface::VERBOSITY_VERY_VERBOSE,
  54. OutputInterface::VERBOSITY_VERBOSE,
  55. ],
  56. LogLevel::NOTICE,
  57. 'No loitering.',
  58. ' [notice] No loitering.',
  59. ],
  60. [
  61. '\Consolidation\Log\UnstyledLogOutputStyler',
  62. OutputInterface::VERBOSITY_NORMAL,
  63. LogLevel::NOTICE,
  64. 'No loitering.',
  65. '',
  66. ],
  67. [
  68. '\Consolidation\Log\UnstyledLogOutputStyler',
  69. OutputInterface::VERBOSITY_DEBUG,
  70. LogLevel::INFO,
  71. 'Scenic route.',
  72. ' [info] Scenic route.',
  73. ],
  74. [
  75. '\Consolidation\Log\UnstyledLogOutputStyler',
  76. OutputInterface::VERBOSITY_DEBUG,
  77. LogLevel::DEBUG,
  78. 'Counter incremented.',
  79. ' [debug] Counter incremented.',
  80. ],
  81. [
  82. '\Consolidation\Log\UnstyledLogOutputStyler',
  83. [
  84. OutputInterface::VERBOSITY_VERY_VERBOSE,
  85. OutputInterface::VERBOSITY_VERBOSE,
  86. OutputInterface::VERBOSITY_NORMAL
  87. ],
  88. LogLevel::DEBUG,
  89. 'Counter incremented.',
  90. '',
  91. ],
  92. [
  93. '\Consolidation\Log\UnstyledLogOutputStyler',
  94. $TEST_ALL_LOG_LEVELS,
  95. ConsoleLogLevel::SUCCESS,
  96. 'It worked!',
  97. ' [success] It worked!',
  98. ],
  99. [
  100. '\Consolidation\Log\LogOutputStyler',
  101. OutputInterface::VERBOSITY_NORMAL,
  102. ConsoleLogLevel::SUCCESS,
  103. 'It worked!',
  104. ' [success] It worked!',
  105. ],
  106. [
  107. '\Consolidation\Log\SymfonyLogOutputStyler',
  108. OutputInterface::VERBOSITY_DEBUG,
  109. LogLevel::WARNING,
  110. 'Steep grade.',
  111. "\n [WARNING] Steep grade.",
  112. ],
  113. [
  114. '\Consolidation\Log\SymfonyLogOutputStyler',
  115. OutputInterface::VERBOSITY_DEBUG,
  116. LogLevel::NOTICE,
  117. 'No loitering.',
  118. "\n ! [NOTE] No loitering.",
  119. ],
  120. [
  121. '\Consolidation\Log\SymfonyLogOutputStyler',
  122. OutputInterface::VERBOSITY_DEBUG,
  123. LogLevel::INFO,
  124. 'Scenic route.',
  125. "\n ! [NOTE] Scenic route.",
  126. ],
  127. [
  128. '\Consolidation\Log\SymfonyLogOutputStyler',
  129. OutputInterface::VERBOSITY_DEBUG,
  130. LogLevel::DEBUG,
  131. 'Counter incremented.',
  132. "\n ! [NOTE] Counter incremented.",
  133. ],
  134. [
  135. '\Consolidation\Log\SymfonyLogOutputStyler',
  136. OutputInterface::VERBOSITY_NORMAL,
  137. ConsoleLogLevel::SUCCESS,
  138. 'It worked!',
  139. "\n [OK] It worked!",
  140. ],
  141. ]);
  142. }
  143. /**
  144. * This is our only test method. It accepts all of the
  145. * permuted data from the data provider, and runs one
  146. * test on each one.
  147. *
  148. * @dataProvider logTestValues
  149. */
  150. function testLogging($styleClass, $verbocity, $level, $message, $expected) {
  151. $logStyler = new $styleClass;
  152. $this->logger->setLogOutputStyler($logStyler);
  153. $this->output->setVerbosity($verbocity);
  154. $this->logger->log($level, $message);
  155. $outputText = rtrim($this->output->fetch(), "\n\r\t ");
  156. $this->assertEquals($expected, $outputText);
  157. }
  158. }