| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php declare(strict_types=1);
- /*
- * 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 Monolog\Handler;
- use Monolog\Level;
- use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
- use Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy;
- use Psr\Log\LogLevel;
- class FingersCrossedHandlerTest extends \Monolog\Test\MonologTestCase
- {
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::__construct
- * @covers Monolog\Handler\FingersCrossedHandler::handle
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- */
- public function testHandleBuffers()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test);
- $handler->handle($this->getRecord(Level::Debug));
- $handler->handle($this->getRecord(Level::Info));
- $this->assertFalse($test->hasDebugRecords());
- $this->assertFalse($test->hasInfoRecords());
- $handler->handle($this->getRecord(Level::Warning));
- $handler->close();
- $this->assertTrue($test->hasInfoRecords());
- $this->assertCount(3, $test->getRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::handle
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- */
- public function testHandleStopsBufferingAfterTrigger()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test);
- $handler->handle($this->getRecord(Level::Warning));
- $handler->handle($this->getRecord(Level::Debug));
- $handler->close();
- $this->assertTrue($test->hasWarningRecords());
- $this->assertTrue($test->hasDebugRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::handle
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- * @covers Monolog\Handler\FingersCrossedHandler::reset
- */
- public function testHandleResetBufferingAfterReset()
- {
- $test = new TestHandler();
- $test->setSkipReset(true);
- $handler = new FingersCrossedHandler($test);
- $handler->handle($this->getRecord(Level::Warning));
- $handler->handle($this->getRecord(Level::Debug));
- $handler->reset();
- $handler->handle($this->getRecord(Level::Info));
- $handler->close();
- $this->assertTrue($test->hasWarningRecords());
- $this->assertTrue($test->hasDebugRecords());
- $this->assertFalse($test->hasInfoRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::handle
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- */
- public function testHandleResetBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, Level::Warning, 0, false, false);
- $handler->handle($this->getRecord(Level::Debug));
- $handler->handle($this->getRecord(Level::Warning));
- $handler->handle($this->getRecord(Level::Info));
- $handler->close();
- $this->assertTrue($test->hasWarningRecords());
- $this->assertTrue($test->hasDebugRecords());
- $this->assertFalse($test->hasInfoRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::handle
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- */
- public function testHandleBufferLimit()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, Level::Warning, 2);
- $handler->handle($this->getRecord(Level::Debug));
- $handler->handle($this->getRecord(Level::Debug));
- $handler->handle($this->getRecord(Level::Info));
- $handler->handle($this->getRecord(Level::Warning));
- $this->assertTrue($test->hasWarningRecords());
- $this->assertTrue($test->hasInfoRecords());
- $this->assertFalse($test->hasDebugRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::handle
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- */
- public function testHandleWithCallback()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler(function ($record, $handler) use ($test) {
- return $test;
- });
- $handler->handle($this->getRecord(Level::Debug));
- $handler->handle($this->getRecord(Level::Info));
- $this->assertFalse($test->hasDebugRecords());
- $this->assertFalse($test->hasInfoRecords());
- $handler->handle($this->getRecord(Level::Warning));
- $this->assertTrue($test->hasInfoRecords());
- $this->assertCount(3, $test->getRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::handle
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- */
- public function testHandleWithBadCallbackThrowsException()
- {
- $handler = new FingersCrossedHandler(function ($record, $handler) {
- return 'foo';
- });
- $this->expectException(\RuntimeException::class);
- $handler->handle($this->getRecord(Level::Warning));
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::isHandling
- */
- public function testIsHandlingAlways()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, Level::Error);
- $this->assertTrue($handler->isHandling($this->getRecord(Level::Debug)));
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::__construct
- * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct
- * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated
- */
- public function testErrorLevelActivationStrategy()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Level::Warning));
- $handler->handle($this->getRecord(Level::Debug));
- $this->assertFalse($test->hasDebugRecords());
- $handler->handle($this->getRecord(Level::Warning));
- $this->assertTrue($test->hasDebugRecords());
- $this->assertTrue($test->hasWarningRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::__construct
- * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct
- * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated
- */
- public function testErrorLevelActivationStrategyWithPsrLevel()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning'));
- $handler->handle($this->getRecord(Level::Debug));
- $this->assertFalse($test->hasDebugRecords());
- $handler->handle($this->getRecord(Level::Warning));
- $this->assertTrue($test->hasDebugRecords());
- $this->assertTrue($test->hasWarningRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::__construct
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- */
- public function testOverrideActivationStrategy()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning'));
- $handler->handle($this->getRecord(Level::Debug));
- $this->assertFalse($test->hasDebugRecords());
- $handler->activate();
- $this->assertTrue($test->hasDebugRecords());
- $handler->handle($this->getRecord(Level::Info));
- $this->assertTrue($test->hasInfoRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
- * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
- */
- public function testChannelLevelActivationStrategy()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Level::Error, ['othertest' => Level::Debug]));
- $handler->handle($this->getRecord(Level::Warning));
- $this->assertFalse($test->hasWarningRecords());
- $record = $this->getRecord(Level::Debug, channel: 'othertest');
- $handler->handle($record);
- $this->assertTrue($test->hasDebugRecords());
- $this->assertTrue($test->hasWarningRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
- * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
- */
- public function testChannelLevelActivationStrategyWithPsrLevels()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy('error', ['othertest' => 'debug']));
- $handler->handle($this->getRecord(Level::Warning));
- $this->assertFalse($test->hasWarningRecords());
- $record = $this->getRecord(Level::Debug, channel: 'othertest');
- $handler->handle($record);
- $this->assertTrue($test->hasDebugRecords());
- $this->assertTrue($test->hasWarningRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::handle
- * @covers Monolog\Handler\FingersCrossedHandler::activate
- */
- public function testHandleUsesProcessors()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, Level::Info);
- $handler->pushProcessor(function ($record) {
- $record->extra['foo'] = true;
- return $record;
- });
- $handler->handle($this->getRecord(Level::Warning));
- $this->assertTrue($test->hasWarningRecords());
- $records = $test->getRecords();
- $this->assertTrue($records[0]['extra']['foo']);
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::close
- */
- public function testPassthruOnClose()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Level::Warning), 0, true, true, Level::Info);
- $handler->handle($this->getRecord(Level::Debug));
- $handler->handle($this->getRecord(Level::Info));
- $handler->handle($this->getRecord(Level::Notice));
- $handler->close();
- $this->assertFalse($test->hasDebugRecords());
- $this->assertTrue($test->hasInfoRecords());
- $this->assertTrue($test->hasNoticeRecords());
- }
- /**
- * @covers Monolog\Handler\FingersCrossedHandler::close
- */
- public function testPsrLevelPassthruOnClose()
- {
- $test = new TestHandler();
- $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Level::Warning), 0, true, true, LogLevel::INFO);
- $handler->handle($this->getRecord(Level::Debug));
- $handler->handle($this->getRecord(Level::Info));
- $handler->handle($this->getRecord(Level::Notice));
- $handler->close();
- $this->assertFalse($test->hasDebugRecords());
- $this->assertTrue($test->hasInfoRecords());
- $this->assertTrue($test->hasNoticeRecords());
- }
- }
|