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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Monolog\Tests\Handler;
use Monolog\Level;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Monolog\Formatter\ConsoleFormatter;
use Symfony\Bridge\Monolog\Handler\ConsoleHandler;
use Symfony\Bridge\Monolog\Tests\RecordFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
/**
* Tests the ConsoleHandler and also the ConsoleFormatter.
*
* @author Tobias Schultze <http://tobion.de>
*/
class ConsoleHandlerTest extends TestCase
{
public function testConstructor()
{
$handler = new ConsoleHandler(null, false);
$this->assertFalse($handler->getBubble(), 'the bubble parameter gets propagated');
}
public function testIsHandling()
{
$handler = new ConsoleHandler();
$this->assertFalse($handler->isHandling(RecordFactory::create()), '->isHandling returns false when no output is set');
}
#[\PHPUnit\Framework\Attributes\DataProvider('provideVerbosityMappingTests')]
public function testVerbosityMapping($verbosity, $level, $isHandling, array $map = [])
{
$output = $this->createMock(OutputInterface::class);
$output
->expects($this->atLeastOnce())
->method('getVerbosity')
->willReturn($verbosity)
;
$handler = new ConsoleHandler($output, true, $map);
$this->assertSame($isHandling, $handler->isHandling(RecordFactory::create($level)),
'->isHandling returns correct value depending on console verbosity and log level'
);
// check that the handler actually outputs the record if it handles it
$levelName = Logger::getLevelName($level);
$levelName = \sprintf('%-9s', $levelName);
$realOutput = $this->getMockBuilder(Output::class)->onlyMethods(['doWrite'])->getMock();
$realOutput->setVerbosity($verbosity);
$log = "16:21:54 $levelName [app] My info message\n";
$realOutput
->expects($isHandling ? $this->once() : $this->never())
->method('doWrite')
->with($log, false);
$handler = new ConsoleHandler($realOutput, true, $map);
$infoRecord = RecordFactory::create($level, 'My info message', 'app', datetime: new \DateTimeImmutable('2013-05-29 16:21:54'));
$this->assertFalse($handler->handle($infoRecord), 'The handler finished handling the log.');
}
public static function provideVerbosityMappingTests(): array
{
return [
[OutputInterface::VERBOSITY_QUIET, Level::Error, true],
[OutputInterface::VERBOSITY_QUIET, Level::Warning, false],
[OutputInterface::VERBOSITY_NORMAL, Level::Warning, true],
[OutputInterface::VERBOSITY_NORMAL, Level::Notice, false],
[OutputInterface::VERBOSITY_VERBOSE, Level::Notice, true],
[OutputInterface::VERBOSITY_VERBOSE, Level::Info, false],
[OutputInterface::VERBOSITY_VERY_VERBOSE, Level::Info, true],
[OutputInterface::VERBOSITY_VERY_VERBOSE, Level::Debug, false],
[OutputInterface::VERBOSITY_DEBUG, Level::Debug, true],
[OutputInterface::VERBOSITY_DEBUG, Level::Emergency, true],
[OutputInterface::VERBOSITY_NORMAL, Level::Notice, true, [
OutputInterface::VERBOSITY_NORMAL => Level::Notice,
]],
[OutputInterface::VERBOSITY_DEBUG, Level::Notice, true, [
OutputInterface::VERBOSITY_NORMAL => Level::Notice,
]],
];
}
public function testVerbosityChanged()
{
$output = $this->createMock(OutputInterface::class);
$output
->expects($this->exactly(2))
->method('getVerbosity')
->willReturn(OutputInterface::VERBOSITY_QUIET, OutputInterface::VERBOSITY_DEBUG)
;
$handler = new ConsoleHandler($output);
$this->assertFalse($handler->isHandling(RecordFactory::create(Level::Notice)),
'when verbosity is set to quiet, the handler does not handle the log'
);
$this->assertTrue($handler->isHandling(RecordFactory::create(Level::Notice)),
'since the verbosity of the output increased externally, the handler is now handling the log'
);
}
public function testGetFormatter()
{
$handler = new ConsoleHandler();
$this->assertInstanceOf(
ConsoleFormatter::class, $handler->getFormatter(),
'->getFormatter returns ConsoleFormatter by default'
);
}
public function testWritingAndFormatting()
{
$output = $this->createMock(OutputInterface::class);
$output
->expects($this->any())
->method('getVerbosity')
->willReturn(OutputInterface::VERBOSITY_DEBUG)
;
$output
->expects($this->once())
->method('write')
->with("16:21:54 <fg=green>INFO </> <comment>[app]</> My info message\n")
;
$handler = new ConsoleHandler(null, false);
$handler->setOutput($output);
$infoRecord = RecordFactory::create(Level::Info, 'My info message', 'app', datetime: new \DateTimeImmutable('2013-05-29 16:21:54'));
$this->assertTrue($handler->handle($infoRecord), 'The handler finished handling the log as bubble is false.');
}
public function testLogsFromListeners()
{
$output = new BufferedOutput();
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
$handler = new ConsoleHandler(null, false);
$logger = new Logger('app');
$logger->pushHandler($handler);
$dispatcher = new EventDispatcher();
$dispatcher->addListener(ConsoleEvents::COMMAND, function () use ($logger) {
$logger->info('Before command message.');
});
$dispatcher->addListener(ConsoleEvents::TERMINATE, function () use ($logger) {
$logger->info('Before terminate message.');
});
$dispatcher->addSubscriber($handler);
$dispatcher->addListener(ConsoleEvents::COMMAND, function () use ($logger) {
$logger->info('After command message.');
});
$dispatcher->addListener(ConsoleEvents::TERMINATE, function () use ($logger) {
$logger->info('After terminate message.');
});
$event = new ConsoleCommandEvent(new Command('foo'), $this->createMock(InputInterface::class), $output);
$dispatcher->dispatch($event, ConsoleEvents::COMMAND);
$this->assertStringContainsString('Before command message.', $out = $output->fetch());
$this->assertStringContainsString('After command message.', $out);
$event = new ConsoleTerminateEvent(new Command('foo'), $this->createMock(InputInterface::class), $output, 0);
$dispatcher->dispatch($event, ConsoleEvents::TERMINATE);
$this->assertStringContainsString('Before terminate message.', $out = $output->fetch());
$this->assertStringContainsString('After terminate message.', $out);
}
}
|