Вход Регистрация
Файл: gapps/vendor/monolog/monolog/tests/Monolog/Handler/FingersCrossedHandlerTest.php
Строк: 496
<?php

/*
 * This file is part of the Monolog package.
 *
 * (c) Jordi Boggiano <j.boggiano@seld.be>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace MonologHandler;

use 
MonologTestCase;
use 
MonologLogger;
use 
MonologHandlerFingersCrossedErrorLevelActivationStrategy;
use 
MonologHandlerFingersCrossedChannelLevelActivationStrategy;
use 
PsrLogLogLevel;

class 
FingersCrossedHandlerTest extends TestCase
{
    
/**
     * @covers MonologHandlerFingersCrossedHandler::__construct
     * @covers MonologHandlerFingersCrossedHandler::handle
     * @covers MonologHandlerFingersCrossedHandler::activate
     */
    
public function testHandleBuffers()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test);
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->handle($this->getRecord(Logger::INFO));
        
$this->assertFalse($test->hasDebugRecords());
        
$this->assertFalse($test->hasInfoRecords());
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$handler->close();
        
$this->assertTrue($test->hasInfoRecords());
        
$this->assertTrue(count($test->getRecords()) === 3);
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::handle
     * @covers MonologHandlerFingersCrossedHandler::activate
     */
    
public function testHandleStopsBufferingAfterTrigger()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test);
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->close();
        
$this->assertTrue($test->hasWarningRecords());
        
$this->assertTrue($test->hasDebugRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::handle
     * @covers MonologHandlerFingersCrossedHandler::activate
     * @covers MonologHandlerFingersCrossedHandler::reset
     */
    
public function testHandleRestartBufferingAfterReset()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test);
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->reset();
        
$handler->handle($this->getRecord(Logger::INFO));
        
$handler->close();
        
$this->assertTrue($test->hasWarningRecords());
        
$this->assertTrue($test->hasDebugRecords());
        
$this->assertFalse($test->hasInfoRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::handle
     * @covers MonologHandlerFingersCrossedHandler::activate
     */
    
public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($testLogger::WARNING0falsefalse);
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$handler->handle($this->getRecord(Logger::INFO));
        
$handler->close();
        
$this->assertTrue($test->hasWarningRecords());
        
$this->assertTrue($test->hasDebugRecords());
        
$this->assertFalse($test->hasInfoRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::handle
     * @covers MonologHandlerFingersCrossedHandler::activate
     */
    
public function testHandleBufferLimit()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($testLogger::WARNING2);
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->handle($this->getRecord(Logger::INFO));
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$this->assertTrue($test->hasWarningRecords());
        
$this->assertTrue($test->hasInfoRecords());
        
$this->assertFalse($test->hasDebugRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::handle
     * @covers MonologHandlerFingersCrossedHandler::activate
     */
    
public function testHandleWithCallback()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler(function ($record$handler) use ($test) {
                    return 
$test;
                });
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->handle($this->getRecord(Logger::INFO));
        
$this->assertFalse($test->hasDebugRecords());
        
$this->assertFalse($test->hasInfoRecords());
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$this->assertTrue($test->hasInfoRecords());
        
$this->assertTrue(count($test->getRecords()) === 3);
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::handle
     * @covers MonologHandlerFingersCrossedHandler::activate
     * @expectedException RuntimeException
     */
    
public function testHandleWithBadCallbackThrowsException()
    {
        
$handler = new FingersCrossedHandler(function ($record$handler) {
                    return 
'foo';
                });
        
$handler->handle($this->getRecord(Logger::WARNING));
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::isHandling
     */
    
public function testIsHandlingAlways()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($testLogger::ERROR);
        
$this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG)));
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::__construct
     * @covers MonologHandlerFingersCrossedErrorLevelActivationStrategy::__construct
     * @covers MonologHandlerFingersCrossedErrorLevelActivationStrategy::isHandlerActivated
     */
    
public function testErrorLevelActivationStrategy()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING));
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$this->assertFalse($test->hasDebugRecords());
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$this->assertTrue($test->hasDebugRecords());
        
$this->assertTrue($test->hasWarningRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::__construct
     * @covers MonologHandlerFingersCrossedErrorLevelActivationStrategy::__construct
     * @covers MonologHandlerFingersCrossedErrorLevelActivationStrategy::isHandlerActivated
     */
    
public function testErrorLevelActivationStrategyWithPsrLevel()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning'));
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$this->assertFalse($test->hasDebugRecords());
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$this->assertTrue($test->hasDebugRecords());
        
$this->assertTrue($test->hasWarningRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::__construct
     * @covers MonologHandlerFingersCrossedHandler::activate
     */
    
public function testOverrideActivationStrategy()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning'));
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$this->assertFalse($test->hasDebugRecords());
        
$handler->activate();
        
$this->assertTrue($test->hasDebugRecords());
        
$handler->handle($this->getRecord(Logger::INFO));
        
$this->assertTrue($test->hasInfoRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedChannelLevelActivationStrategy::__construct
     * @covers MonologHandlerFingersCrossedChannelLevelActivationStrategy::isHandlerActivated
     */
    
public function testChannelLevelActivationStrategy()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG)));
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$this->assertFalse($test->hasWarningRecords());
        
$record $this->getRecord(Logger::DEBUG);
        
$record['channel'] = 'othertest';
        
$handler->handle($record);
        
$this->assertTrue($test->hasDebugRecords());
        
$this->assertTrue($test->hasWarningRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedChannelLevelActivationStrategy::__construct
     * @covers MonologHandlerFingersCrossedChannelLevelActivationStrategy::isHandlerActivated
     */
    
public function testChannelLevelActivationStrategyWithPsrLevels()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy('error', array('othertest' => 'debug')));
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$this->assertFalse($test->hasWarningRecords());
        
$record $this->getRecord(Logger::DEBUG);
        
$record['channel'] = 'othertest';
        
$handler->handle($record);
        
$this->assertTrue($test->hasDebugRecords());
        
$this->assertTrue($test->hasWarningRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::handle
     * @covers MonologHandlerFingersCrossedHandler::activate
     */
    
public function testHandleUsesProcessors()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($testLogger::INFO);
        
$handler->pushProcessor(function ($record) {
            
$record['extra']['foo'] = true;

            return 
$record;
        });
        
$handler->handle($this->getRecord(Logger::WARNING));
        
$this->assertTrue($test->hasWarningRecords());
        
$records $test->getRecords();
        
$this->assertTrue($records[0]['extra']['foo']);
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::close
     */
    
public function testPassthruOnClose()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0truetrueLogger::INFO);
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->handle($this->getRecord(Logger::INFO));
        
$handler->close();
        
$this->assertFalse($test->hasDebugRecords());
        
$this->assertTrue($test->hasInfoRecords());
    }

    
/**
     * @covers MonologHandlerFingersCrossedHandler::close
     */
    
public function testPsrLevelPassthruOnClose()
    {
        
$test = new TestHandler();
        
$handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0truetrueLogLevel::INFO);
        
$handler->handle($this->getRecord(Logger::DEBUG));
        
$handler->handle($this->getRecord(Logger::INFO));
        
$handler->close();
        
$this->assertFalse($test->hasDebugRecords());
        
$this->assertTrue($test->hasInfoRecords());
    }
}
Онлайн: 1
Реклама