LoggerVerbosityAndStyleTests.php 4.71 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
<?php
namespace Consolidation\Log;

use Psr\Log\LogLevel;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;
use PHPUnit\Framework\TestCase;

use Consolidation\TestUtils\TestDataPermuter;

class LoggerVerbosityAndStyleTests extends TestCase
{
  protected $output;
  protected $logger;

  function setup() {
    $this->output = new BufferedOutput();
    //$this->output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
    $this->logger = new Logger($this->output);
  }

  public static function logTestValues()
  {
    /**
     * Use TEST_ALL_LOG_LEVELS to ensure that output is the same
     * in instances where the output does not vary by log level.
     */
    $TEST_ALL_LOG_LEVELS = [
      OutputInterface::VERBOSITY_DEBUG,
      OutputInterface::VERBOSITY_VERY_VERBOSE,
      OutputInterface::VERBOSITY_VERBOSE,
      OutputInterface::VERBOSITY_NORMAL
    ];

    // Tests that return the same value for multiple inputs
    // may use the expandProviderDataArrays method, and list
    // repeated scalars as array values.  All permutations of
    // all array items will be calculated, and one test will
    // be generated for each one.
    return TestDataPermuter::expandProviderDataArrays([
      [
        '\Consolidation\Log\UnstyledLogOutputStyler',
        $TEST_ALL_LOG_LEVELS,
        LogLevel::ERROR,
        'Do not enter - wrong way.',
        ' [error] Do not enter - wrong way.',
      ],
      [
        '\Consolidation\Log\UnstyledLogOutputStyler',
        $TEST_ALL_LOG_LEVELS,
        LogLevel::WARNING,
        'Steep grade.',
        ' [warning] Steep grade.',
      ],
      [
        '\Consolidation\Log\UnstyledLogOutputStyler',
        [
          OutputInterface::VERBOSITY_DEBUG,
          OutputInterface::VERBOSITY_VERY_VERBOSE,
          OutputInterface::VERBOSITY_VERBOSE,
        ],
        LogLevel::NOTICE,
        'No loitering.',
        ' [notice] No loitering.',
      ],
      [
        '\Consolidation\Log\UnstyledLogOutputStyler',
        OutputInterface::VERBOSITY_NORMAL,
        LogLevel::NOTICE,
        'No loitering.',
        '',
      ],
      [
        '\Consolidation\Log\UnstyledLogOutputStyler',
        OutputInterface::VERBOSITY_DEBUG,
        LogLevel::INFO,
        'Scenic route.',
        ' [info] Scenic route.',
      ],
      [
        '\Consolidation\Log\UnstyledLogOutputStyler',
        OutputInterface::VERBOSITY_DEBUG,
        LogLevel::DEBUG,
        'Counter incremented.',
        ' [debug] Counter incremented.',
      ],
      [
        '\Consolidation\Log\UnstyledLogOutputStyler',
        [
          OutputInterface::VERBOSITY_VERY_VERBOSE,
          OutputInterface::VERBOSITY_VERBOSE,
          OutputInterface::VERBOSITY_NORMAL
        ],
        LogLevel::DEBUG,
        'Counter incremented.',
        '',
      ],
      [
        '\Consolidation\Log\UnstyledLogOutputStyler',
        $TEST_ALL_LOG_LEVELS,
        ConsoleLogLevel::SUCCESS,
        'It worked!',
        ' [success] It worked!',
      ],
      [
        '\Consolidation\Log\LogOutputStyler',
        OutputInterface::VERBOSITY_NORMAL,
        ConsoleLogLevel::SUCCESS,
        'It worked!',
        ' [success] It worked!',
      ],
      [
        '\Consolidation\Log\SymfonyLogOutputStyler',
        OutputInterface::VERBOSITY_DEBUG,
        LogLevel::WARNING,
        'Steep grade.',
        "\n [WARNING] Steep grade.",
      ],
      [
        '\Consolidation\Log\SymfonyLogOutputStyler',
        OutputInterface::VERBOSITY_DEBUG,
        LogLevel::NOTICE,
        'No loitering.',
        "\n ! [NOTE] No loitering.",
      ],
      [
        '\Consolidation\Log\SymfonyLogOutputStyler',
        OutputInterface::VERBOSITY_DEBUG,
        LogLevel::INFO,
        'Scenic route.',
        "\n ! [NOTE] Scenic route.",
      ],
      [
        '\Consolidation\Log\SymfonyLogOutputStyler',
        OutputInterface::VERBOSITY_DEBUG,
        LogLevel::DEBUG,
        'Counter incremented.',
        "\n ! [NOTE] Counter incremented.",
      ],
      [
        '\Consolidation\Log\SymfonyLogOutputStyler',
        OutputInterface::VERBOSITY_NORMAL,
        ConsoleLogLevel::SUCCESS,
        'It worked!',
        "\n [OK] It worked!",
      ],
    ]);
  }

  /**
   * This is our only test method. It accepts all of the
   * permuted data from the data provider, and runs one
   * test on each one.
   *
   * @dataProvider logTestValues
   */
  function testLogging($styleClass, $verbocity, $level, $message, $expected) {
    $logStyler = new $styleClass;
    $this->logger->setLogOutputStyler($logStyler);
    $this->output->setVerbosity($verbocity);
    $this->logger->log($level, $message);
    $outputText = rtrim($this->output->fetch(), "\n\r\t ");
    $this->assertEquals($expected, $outputText);
  }
}