123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?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 Monolog;
-
- use Monolog\Processor\WebProcessor;
- use Monolog\Handler\TestHandler;
-
- class LoggerTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @covers Monolog\Logger::getName
- */
- public function testGetName()
- {
- $logger = new Logger('foo');
- $this->assertEquals('foo', $logger->getName());
- }
-
- /**
- * @covers Monolog\Logger::__construct
- */
- public function testChannel()
- {
- $logger = new Logger('foo');
- $handler = new TestHandler;
- $logger->pushHandler($handler);
- $logger->addWarning('test');
- list($record) = $handler->getRecords();
- $this->assertEquals('foo', $record['channel']);
- }
-
- /**
- * @covers Monolog\Logger::addRecord
- */
- public function testLog()
- {
- $logger = new Logger(__METHOD__);
-
- $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
- $handler->expects($this->once())
- ->method('handle');
- $logger->pushHandler($handler);
-
- $this->assertTrue($logger->addWarning('test'));
- }
-
- /**
- * @covers Monolog\Logger::addRecord
- */
- public function testLogNotHandled()
- {
- $logger = new Logger(__METHOD__);
-
- $handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'), array(Logger::ERROR));
- $handler->expects($this->never())
- ->method('handle');
- $logger->pushHandler($handler);
-
- $this->assertFalse($logger->addWarning('test'));
- }
-
- /**
- * @covers Monolog\Logger::pushHandler
- * @covers Monolog\Logger::popHandler
- * @expectedException LogicException
- */
- public function testPushPopHandler()
- {
- $logger = new Logger(__METHOD__);
- $handler1 = new TestHandler;
- $handler2 = new TestHandler;
-
- $logger->pushHandler($handler1);
- $logger->pushHandler($handler2);
-
- $this->assertEquals($handler2, $logger->popHandler());
- $this->assertEquals($handler1, $logger->popHandler());
- $logger->popHandler();
- }
-
- /**
- * @covers Monolog\Logger::pushProcessor
- * @covers Monolog\Logger::popProcessor
- * @expectedException LogicException
- */
- public function testPushPopProcessor()
- {
- $logger = new Logger(__METHOD__);
- $processor1 = new WebProcessor;
- $processor2 = new WebProcessor;
-
- $logger->pushProcessor($processor1);
- $logger->pushProcessor($processor2);
-
- $this->assertEquals($processor2, $logger->popProcessor());
- $this->assertEquals($processor1, $logger->popProcessor());
- $logger->popProcessor();
- }
-
- /**
- * @covers Monolog\Logger::pushProcessor
- * @expectedException InvalidArgumentException
- */
- public function testPushProcessorWithNonCallable()
- {
- $logger = new Logger(__METHOD__);
-
- $logger->pushProcessor(new \stdClass());
- }
-
- /**
- * @covers Monolog\Logger::addRecord
- */
- public function testProcessorsAreExecuted()
- {
- $logger = new Logger(__METHOD__);
- $handler = new TestHandler;
- $logger->pushHandler($handler);
- $logger->pushProcessor(function($record) {
- $record['extra']['win'] = true;
- return $record;
- });
- $logger->addError('test');
- list($record) = $handler->getRecords();
- $this->assertTrue($record['extra']['win']);
- }
-
- /**
- * @covers Monolog\Logger::addRecord
- */
- public function testProcessorsAreCalledOnlyOnce()
- {
- $logger = new Logger(__METHOD__);
- $handler = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler->expects($this->any())
- ->method('isHandling')
- ->will($this->returnValue(true))
- ;
- $handler->expects($this->any())
- ->method('handle')
- ->will($this->returnValue(true))
- ;
- $logger->pushHandler($handler);
-
- $processor = $this->getMockBuilder('Monolog\Processor\WebProcessor')
- ->disableOriginalConstructor()
- ->setMethods(array('__invoke'))
- ->getMock()
- ;
- $processor->expects($this->once())
- ->method('__invoke')
- ->will($this->returnArgument(0))
- ;
- $logger->pushProcessor($processor);
-
- $logger->addError('test');
- }
-
- /**
- * @covers Monolog\Logger::addRecord
- */
- public function testProcessorsNotCalledWhenNotHandled()
- {
- $logger = new Logger(__METHOD__);
- $handler = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler->expects($this->once())
- ->method('isHandling')
- ->will($this->returnValue(false))
- ;
- $logger->pushHandler($handler);
- $that = $this;
- $logger->pushProcessor(function($record) use ($that){
- $that->fail('The processor should not be called');
- });
- $logger->addAlert('test');
- }
-
- /**
- * @covers Monolog\Logger::addRecord
- */
- public function testHandlersNotCalledBeforeFirstHandling()
- {
- $logger = new Logger(__METHOD__);
-
- $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler1->expects($this->never())
- ->method('isHandling')
- ->will($this->returnValue(false))
- ;
- $handler1->expects($this->once())
- ->method('handle')
- ->will($this->returnValue(false))
- ;
- $logger->pushHandler($handler1);
-
- $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler2->expects($this->once())
- ->method('isHandling')
- ->will($this->returnValue(true))
- ;
- $handler2->expects($this->once())
- ->method('handle')
- ->will($this->returnValue(false))
- ;
- $logger->pushHandler($handler2);
-
- $handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler3->expects($this->once())
- ->method('isHandling')
- ->will($this->returnValue(false))
- ;
- $handler3->expects($this->never())
- ->method('handle')
- ;
- $logger->pushHandler($handler3);
-
- $logger->debug('test');
- }
-
- /**
- * @covers Monolog\Logger::addRecord
- */
- public function testBubblingWhenTheHandlerReturnsFalse()
- {
- $logger = new Logger(__METHOD__);
-
- $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler1->expects($this->any())
- ->method('isHandling')
- ->will($this->returnValue(true))
- ;
- $handler1->expects($this->once())
- ->method('handle')
- ->will($this->returnValue(false))
- ;
- $logger->pushHandler($handler1);
-
- $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler2->expects($this->any())
- ->method('isHandling')
- ->will($this->returnValue(true))
- ;
- $handler2->expects($this->once())
- ->method('handle')
- ->will($this->returnValue(false))
- ;
- $logger->pushHandler($handler2);
-
- $logger->debug('test');
- }
-
- /**
- * @covers Monolog\Logger::addRecord
- */
- public function testNotBubblingWhenTheHandlerReturnsTrue()
- {
- $logger = new Logger(__METHOD__);
-
- $handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler1->expects($this->any())
- ->method('isHandling')
- ->will($this->returnValue(true))
- ;
- $handler1->expects($this->never())
- ->method('handle')
- ;
- $logger->pushHandler($handler1);
-
- $handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
- $handler2->expects($this->any())
- ->method('isHandling')
- ->will($this->returnValue(true))
- ;
- $handler2->expects($this->once())
- ->method('handle')
- ->will($this->returnValue(true))
- ;
- $logger->pushHandler($handler2);
-
- $logger->debug('test');
- }
-
- /**
- * @dataProvider logMethodProvider
- * @covers Monolog\Logger::addDebug
- * @covers Monolog\Logger::addInfo
- * @covers Monolog\Logger::addWarning
- * @covers Monolog\Logger::addError
- * @covers Monolog\Logger::addCritical
- * @covers Monolog\Logger::addAlert
- * @covers Monolog\Logger::debug
- * @covers Monolog\Logger::info
- * @covers Monolog\Logger::notice
- * @covers Monolog\Logger::warn
- * @covers Monolog\Logger::err
- * @covers Monolog\Logger::crit
- * @covers Monolog\Logger::alert
- * @covers Monolog\Logger::emerg
- */
- public function testLogMethods($method, $expectedLevel)
- {
- $logger = new Logger('foo');
- $handler = new TestHandler;
- $logger->pushHandler($handler);
- $logger->{$method}('test');
- list($record) = $handler->getRecords();
- $this->assertEquals($expectedLevel, $record['level']);
- }
-
- public function logMethodProvider()
- {
- return array(
- // monolog methods
- array('addDebug', Logger::DEBUG),
- array('addInfo', Logger::INFO),
- array('addWarning', Logger::WARNING),
- array('addError', Logger::ERROR),
- array('addCritical', Logger::CRITICAL),
- array('addAlert', Logger::ALERT),
-
- // ZF/Sf2 compat methods
- array('debug', Logger::DEBUG),
- array('info', Logger::INFO),
- array('notice', Logger::INFO),
- array('warn', Logger::WARNING),
- array('err', Logger::ERROR),
- array('crit', Logger::CRITICAL),
- array('alert', Logger::ALERT),
- array('emerg', Logger::ALERT),
- );
- }
- }
|