Вход Регистрация
Файл: symfony-2.7/src/Symfony/Bridge/Monolog/Tests/Handler/ConsoleHandlerTest.php
Строк: 339
<?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 SymfonyBridgeMonologTestsHandler;

use 
MonologLogger;
use 
SymfonyBridgeMonologHandlerConsoleHandler;
use 
SymfonyComponentConsoleConsoleEvents;
use 
SymfonyComponentConsoleEventConsoleCommandEvent;
use 
SymfonyComponentConsoleEventConsoleTerminateEvent;
use 
SymfonyComponentConsoleOutputOutputInterface;
use 
SymfonyComponentConsoleOutputBufferedOutput;
use 
SymfonyComponentEventDispatcherEventDispatcher;
use 
SymfonyComponentConsoleCommandCommand;

/**
 * Tests the ConsoleHandler and also the ConsoleFormatter.
 *
 * @author Tobias Schultze <http://tobion.de>
 */
class ConsoleHandlerTest extends PHPUnit_Framework_TestCase
{
    public function 
testConstructor()
    {
        
$handler = new ConsoleHandler(nullfalse);
        
$this->assertFalse($handler->getBubble(), 'the bubble parameter gets propagated');
    }

    public function 
testIsHandling()
    {
        
$handler = new ConsoleHandler();
        
$this->assertFalse($handler->isHandling(array()), '->isHandling returns false when no output is set');
    }

    
/**
     * @dataProvider provideVerbosityMappingTests
     */
    
public function testVerbosityMapping($verbosity$level$isHandling, array $map = array())
    {
        
$output $this->getMock('SymfonyComponentConsoleOutputOutputInterface');
        
$output
            
->expects($this->atLeastOnce())
            ->
method('getVerbosity')
            ->
will($this->returnValue($verbosity))
        ;
        
$handler = new ConsoleHandler($outputtrue$map);
        
$this->assertSame($isHandling$handler->isHandling(array('level' => $level)),
            
'->isHandling returns correct value depending on console verbosity and log level'
        
);
    }

    public function 
provideVerbosityMappingTests()
    {
        return array(
            array(
OutputInterface::VERBOSITY_QUIETLogger::ERRORfalse),
            array(
OutputInterface::VERBOSITY_NORMALLogger::WARNINGtrue),
            array(
OutputInterface::VERBOSITY_NORMALLogger::NOTICEfalse),
            array(
OutputInterface::VERBOSITY_VERBOSELogger::NOTICEtrue),
            array(
OutputInterface::VERBOSITY_VERBOSELogger::INFOfalse),
            array(
OutputInterface::VERBOSITY_VERY_VERBOSELogger::INFOtrue),
            array(
OutputInterface::VERBOSITY_VERY_VERBOSELogger::DEBUGfalse),
            array(
OutputInterface::VERBOSITY_DEBUGLogger::DEBUGtrue),
            array(
OutputInterface::VERBOSITY_DEBUGLogger::EMERGENCYtrue),
            array(
OutputInterface::VERBOSITY_NORMALLogger::NOTICEtrue, array(
                
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
            )),
            array(
OutputInterface::VERBOSITY_DEBUGLogger::NOTICEtrue, array(
                
OutputInterface::VERBOSITY_NORMAL => Logger::NOTICE,
            )),
        );
    }

    public function 
testVerbosityChanged()
    {
        
$output $this->getMock('SymfonyComponentConsoleOutputOutputInterface');
        
$output
            
->expects($this->at(0))
            ->
method('getVerbosity')
            ->
will($this->returnValue(OutputInterface::VERBOSITY_QUIET))
        ;
        
$output
            
->expects($this->at(1))
            ->
method('getVerbosity')
            ->
will($this->returnValue(OutputInterface::VERBOSITY_DEBUG))
        ;
        
$handler = new ConsoleHandler($output);
        
$this->assertFalse($handler->isHandling(array('level' => Logger::NOTICE)),
            
'when verbosity is set to quiet, the handler does not handle the log'
        
);
        
$this->assertTrue($handler->isHandling(array('level' => Logger::NOTICE)),
            
'since the verbosity of the output increased externally, the handler is now handling the log'
        
);
    }

    public function 
testGetFormatter()
    {
        
$handler = new ConsoleHandler();
        
$this->assertInstanceOf('SymfonyBridgeMonologFormatterConsoleFormatter'$handler->getFormatter(),
            
'-getFormatter returns ConsoleFormatter by default'
        
);
    }

    public function 
testWritingAndFormatting()
    {
        
$output $this->getMock('SymfonyComponentConsoleOutputConsoleOutputInterface');
        
$output
            
->expects($this->any())
            ->
method('getVerbosity')
            ->
will($this->returnValue(OutputInterface::VERBOSITY_DEBUG))
        ;
        
$output
            
->expects($this->once())
            ->
method('write')
            ->
with('<info>[2013-05-29 16:21:54] app.INFO:</info> My info message  '."n")
        ;

        
$errorOutput $this->getMock('SymfonyComponentConsoleOutputOutputInterface');
        
$errorOutput
            
->expects($this->once())
            ->
method('write')
            ->
with('<error>[2013-05-29 16:21:54] app.ERROR:</error> My error message  '."n")
        ;

        
$output
            
->expects($this->any())
            ->
method('getErrorOutput')
            ->
will($this->returnValue($errorOutput))
        ;

        
$handler = new ConsoleHandler(nullfalse);
        
$handler->setOutput($output);

        
$infoRecord = array(
            
'message' => 'My info message',
            
'context' => array(),
            
'level' => Logger::INFO,
            
'level_name' => Logger::getLevelName(Logger::INFO),
            
'channel' => 'app',
            
'datetime' => new DateTime('2013-05-29 16:21:54'),
            
'extra' => array(),
        );

        
$this->assertTrue($handler->handle($infoRecord), 'The handler finished handling the log as bubble is false.');

        
$errorRecord = array(
            
'message' => 'My error message',
            
'context' => array(),
            
'level' => Logger::ERROR,
            
'level_name' => Logger::getLevelName(Logger::ERROR),
            
'channel' => 'app',
            
'datetime' => new DateTime('2013-05-29 16:21:54'),
            
'extra' => array(),
        );

        
$this->assertTrue($handler->handle($errorRecord), 'The handler finished handling the log as bubble is false.');
    }

    public function 
testLogsFromListeners()
    {
        
$output = new BufferedOutput();
        
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);

        
$handler = new ConsoleHandler(nullfalse);

        
$logger = new Logger('app');
        
$logger->pushHandler($handler);

        
$dispatcher = new EventDispatcher();
        
$dispatcher->addListener(ConsoleEvents::COMMAND, function () use ($logger) {
            
$logger->addInfo('Before command message.');
        });
        
$dispatcher->addListener(ConsoleEvents::TERMINATE, function () use ($logger) {
            
$logger->addInfo('Before terminate message.');
        });

        
$dispatcher->addSubscriber($handler);

        
$dispatcher->addListener(ConsoleEvents::COMMAND, function () use ($logger) {
            
$logger->addInfo('After command message.');
        });
        
$dispatcher->addListener(ConsoleEvents::TERMINATE, function () use ($logger) {
            
$logger->addInfo('After terminate message.');
        });

        
$event = new ConsoleCommandEvent(new Command('foo'), $this->getMock('SymfonyComponentConsoleInputInputInterface'), $output);
        
$dispatcher->dispatch(ConsoleEvents::COMMAND$event);
        
$this->assertContains('Before command message.'$out $output->fetch());
        
$this->assertContains('After command message.'$out);

        
$event = new ConsoleTerminateEvent(new Command('foo'), $this->getMock('SymfonyComponentConsoleInputInputInterface'), $output0);
        
$dispatcher->dispatch(ConsoleEvents::TERMINATE$event);
        
$this->assertContains('Before terminate message.'$out $output->fetch());
        
$this->assertContains('After terminate message.'$out);
    }
}
Онлайн: 1
Реклама