1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 13:16:39 +02:00

Merge remote-tracking branch 'stof/tests'

This commit is contained in:
Jordi Boggiano
2011-09-17 11:45:25 +02:00
7 changed files with 295 additions and 18 deletions

View File

@@ -95,7 +95,8 @@ class Logger
/**
* @return string
*/
public function getName() {
public function getName()
{
return $this->name;
}

View File

@@ -63,6 +63,20 @@ class LineFormatterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] {"ip":"127.0.0.1"}'."\n", $message);
}
public function testFormatExtras()
{
$formatter = new LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra.file% %extra%\n", 'Y-m-d');
$message = $formatter->format(array(
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array(),
'datetime' => new \DateTime,
'extra' => array('ip' => '127.0.0.1', 'file' => 'test'),
'message' => 'log',
));
$this->assertEquals('['.date('Y-m-d').'] meh.ERROR: log [] test {"ip":"127.0.0.1"}'."\n", $message);
}
public function testDefFormatWithObject()
{
$formatter = new LineFormatter(null, 'Y-m-d');

View File

@@ -18,7 +18,7 @@ class WildfireFormatterTest extends \PHPUnit_Framework_TestCase
/**
* @covers Monolog\Formatter\WildfireFormatter::format
*/
public function testDefaultFormatIsLineFormatterWithoutNewLine()
public function testDefaultFormat()
{
$wildfire = new WildfireFormatter();
$record = array(
@@ -39,4 +39,73 @@ class WildfireFormatterTest extends \PHPUnit_Framework_TestCase
$message
);
}
/**
* @covers Monolog\Formatter\WildfireFormatter::format
*/
public function testFormatWithFileAndLine()
{
$wildfire = new WildfireFormatter();
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array('from' => 'logger'),
'datetime' => new \DateTime("@0"),
'extra' => array('ip' => '127.0.0.1', 'file' => 'test', 'line' => 14),
'message' => 'log',
);
$message = $wildfire->format($record);
$this->assertEquals(
'129|[{"Type":"ERROR","File":"test","Line":14,"Label":"meh"},'
.'{"message":"log","context":{"from":"logger"},"extra":{"ip":"127.0.0.1"}}]|',
$message
);
}
/**
* @covers Monolog\Formatter\WildfireFormatter::format
*/
public function testFormatWithoutContext()
{
$wildfire = new WildfireFormatter();
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array(),
'datetime' => new \DateTime("@0"),
'extra' => array(),
'message' => 'log',
);
$message = $wildfire->format($record);
$this->assertEquals(
'58|[{"Type":"ERROR","File":"","Line":"","Label":"meh"},"log"]|',
$message
);
}
/**
* @covers Monolog\Formatter\WildfireFormatter::formatBatch
* @expectedException BadMethodCallException
*/
public function testBatchFormatThrowException()
{
$wildfire = new WildfireFormatter();
$record = array(
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => array(),
'datetime' => new \DateTime("@0"),
'extra' => array(),
'message' => 'log',
);
$wildfire->formatBatch(array($record));
}
}

View File

@@ -29,7 +29,7 @@ class AbstractHandlerTest extends TestCase
*/
public function testConstructAndGetSet()
{
$handler = new TestHandler(Logger::WARNING, false);
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler', array(Logger::WARNING, false));
$this->assertEquals(Logger::WARNING, $handler->getLevel());
$this->assertEquals(false, $handler->getBubble());
@@ -38,7 +38,7 @@ class AbstractHandlerTest extends TestCase
$handler->setFormatter($formatter = new LineFormatter);
$this->assertEquals(Logger::ERROR, $handler->getLevel());
$this->assertEquals(true, $handler->getBubble());
$this->assertEquals($formatter, $handler->getFormatter());
$this->assertSame($formatter, $handler->getFormatter());
}
/**
@@ -46,9 +46,10 @@ class AbstractHandlerTest extends TestCase
*/
public function testHandleBatch()
{
$handler = new TestHandler();
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler');
$handler->expects($this->exactly(2))
->method('handle');
$handler->handleBatch(array($this->getRecord(), $this->getRecord()));
$this->assertEquals(2, count($handler->getRecords()));
}
/**
@@ -56,7 +57,7 @@ class AbstractHandlerTest extends TestCase
*/
public function testIsHandling()
{
$handler = new TestHandler(Logger::WARNING, false);
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler', array(Logger::WARNING, false));
$this->assertTrue($handler->isHandling($this->getRecord()));
$this->assertFalse($handler->isHandling($this->getRecord(Logger::DEBUG)));
}
@@ -67,7 +68,7 @@ class AbstractHandlerTest extends TestCase
*/
public function testGetFormatterInitializesDefault()
{
$handler = new TestHandler();
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler');
$this->assertInstanceOf('Monolog\Formatter\LineFormatter', $handler->getFormatter());
}
@@ -78,7 +79,7 @@ class AbstractHandlerTest extends TestCase
*/
public function testPushPopProcessor()
{
$logger = new TestHandler();
$logger = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler');
$processor1 = new WebProcessor;
$processor2 = new WebProcessor;
@@ -90,4 +91,14 @@ class AbstractHandlerTest extends TestCase
$logger->popProcessor();
}
/**
* @covers Monolog\Handler\AbstractHandler::pushProcessor
* @expectedException InvalidArgumentException
*/
public function testPushProcessorWithNonCallable()
{
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler');
$handler->pushProcessor(new \stdClass());
}
}

View File

@@ -22,7 +22,7 @@ class AbstractProcessingHandlerTest extends TestCase
*/
public function testHandleLowerLevelMessage()
{
$handler = new TestHandler(Logger::WARNING, true);
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', array(Logger::WARNING, true));
$this->assertFalse($handler->handle($this->getRecord(Logger::DEBUG)));
}
@@ -31,7 +31,7 @@ class AbstractProcessingHandlerTest extends TestCase
*/
public function testHandleBubbling()
{
$handler = new TestHandler(Logger::DEBUG, true);
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', array(Logger::DEBUG, true));
$this->assertFalse($handler->handle($this->getRecord()));
}
@@ -40,7 +40,7 @@ class AbstractProcessingHandlerTest extends TestCase
*/
public function testHandleNotBubbling()
{
$handler = new TestHandler(Logger::DEBUG, false);
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', array(Logger::DEBUG, false));
$this->assertTrue($handler->handle($this->getRecord()));
}
@@ -49,7 +49,7 @@ class AbstractProcessingHandlerTest extends TestCase
*/
public function testHandleIsFalseWhenNotHandled()
{
$handler = new TestHandler(Logger::WARNING, false);
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler', array(Logger::WARNING, false));
$this->assertTrue($handler->handle($this->getRecord()));
$this->assertFalse($handler->handle($this->getRecord(Logger::DEBUG)));
}
@@ -59,14 +59,20 @@ class AbstractProcessingHandlerTest extends TestCase
*/
public function testProcessRecord()
{
$handler = new TestHandler();
$handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractProcessingHandler');
$handler->pushProcessor(new WebProcessor(array(
'REQUEST_URI' => '',
'REQUEST_METHOD' => '',
'REMOTE_ADDR' => '',
)));
$handledRecord = null;
$handler->expects($this->once())
->method('write')
->will($this->returnCallback(function($record) use (&$handledRecord){
$handledRecord = $record;
}))
;
$handler->handle($this->getRecord());
list($record) = $handler->getRecords();
$this->assertEquals(3, count($record['extra']));
$this->assertEquals(3, count($handledRecord['extra']));
}
}

View File

@@ -16,9 +16,8 @@ use Monolog\Handler\TestHandler;
class LoggerTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Monolog\Logger::getName()
* @covers Monolog\Logger::getName
*/
public function testGetName()
{
@@ -107,6 +106,17 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger->popProcessor();
}
/**
* @covers Monolog\Logger::pushProcessor
* @expectedException InvalidArgumentException
*/
public function testPushProcessorWithNonCallable()
{
$logger = new Logger(__METHOD__);
$logger->pushProcessor(new \stdClass());
}
/**
* @covers Monolog\Logger::addRecord
*/
@@ -124,6 +134,161 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$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

View File

@@ -29,6 +29,17 @@ class WebProcessorTest extends TestCase
$this->assertEquals($server['REQUEST_METHOD'], $record['extra']['http_method']);
}
public function testProcessorDoNothingIfNoRequestUri()
{
$server = array(
'REMOTE_ADDR' => 'B',
'REQUEST_METHOD' => 'C',
);
$processor = new WebProcessor($server);
$record = $processor($this->getRecord());
$this->assertEmpty($record['extra']);
}
/**
* @expectedException UnexpectedValueException
*/