| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?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);
- }
- }
|