Added some tests.

This commit is contained in:
Adirelle 2015-05-03 14:33:28 +02:00
parent 3af507e7b7
commit 125ed061e5
2 changed files with 86 additions and 3 deletions

View file

@ -39,13 +39,14 @@ class OutputLogHandler extends AbstractProcessingHandler
/**
* Map log levels to colors.
*
* @var string[]
* @var array
*/
static protected $colors = array(
Logger::ERROR => 'red',
Logger::WARNING => 'yellow',
Logger::NOTICE => 'green',
Logger::INFO => 'white'
// No color markup below NOTICE
Logger::INFO => false
);
/**
@ -97,6 +98,8 @@ class OutputLogHandler extends AbstractProcessingHandler
*
* @param array $record
* @return array
*
* @internal Used as a Processor.
*/
public function addConsoleColor($record)
{
@ -106,7 +109,10 @@ class OutputLogHandler extends AbstractProcessingHandler
}
}
$record['message'] = sprintf('<fg=%s>%s</fg=%s>', $color, rtrim($record['message']), $color);
if ($color !== false) {
$record['message'] = sprintf('<fg=%s>%s</fg=%s>', $color, rtrim($record['message']), $color);
}
return $record;
}
}

View file

@ -0,0 +1,77 @@
<?php
namespace Tests\PHPCI\Logging;
use Monolog\Logger;
use PHPCI\Logging\OutputLogHandler;
use PHPUnit_Framework_TestCase;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Generated by PHPUnit_SkeletonGenerator on 2015-05-03 at 14:16:42.
*/
class OutputLogHandlerTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider getVerbosityLevelMap
*/
public function testLevelMatchVerbosity($verbosity, $level)
{
$output = $this->prophesize('\Symfony\Component\Console\Output\OutputInterface');
$output->getVerbosity()->willReturn($verbosity);
$handler = new OutputLogHandler($output->reveal());
$this->assertEquals($level, $handler->getLevel());
}
public function getVerbosityLevelMap()
{
return array(
array(OutputInterface::VERBOSITY_QUIET, Logger::ERROR),
array(OutputInterface::VERBOSITY_NORMAL, Logger::WARNING),
array(OutputInterface::VERBOSITY_VERBOSE, Logger::NOTICE),
array(OutputInterface::VERBOSITY_VERY_VERBOSE, Logger::INFO),
array(OutputInterface::VERBOSITY_DEBUG, Logger::DEBUG),
);
}
public function testSendInfoToOutput()
{
$record = array('message' => 'FOO', 'level' => Logger::INFO);
$output = $this->prophesize('\Symfony\Component\Console\Output\ConsoleOutputInterface');
$output->getVerbosity()->willReturn(OutputInterface::VERBOSITY_DEBUG);
$output->getErrorOutput()->shouldNotBeCalled();
$output->write('BAR')->shouldBeCalled();
$formatter = $this->prophesize('\Monolog\Formatter\FormatterInterface');
$formatter->format($record)->willReturn('BAR');
$handler = new OutputLogHandler($output->reveal());
$handler->setFormatter($formatter->reveal());
$handler->handle($record);
}
public function testSendErrorsToStderr()
{
$record = array('message' => 'FOO', 'level' => Logger::ERROR);
$error = $this->prophesize('\Symfony\Component\Console\Output\OutputInterface');
$error->write('BAR')->shouldBeCalled();
$output = $this->prophesize('\Symfony\Component\Console\Output\ConsoleOutputInterface');
$output->getVerbosity()->willReturn(OutputInterface::VERBOSITY_DEBUG);
$output->getErrorOutput()->willReturn($error);
$formatter = $this->prophesize('\Monolog\Formatter\FormatterInterface');
$formatter->format(array('message' => '<fg=red>FOO</fg=red>', 'level' => Logger::ERROR))->willReturn('BAR');
$handler = new OutputLogHandler($output->reveal());
$handler->setFormatter($formatter->reveal());
$handler->handle($record);
}
}