From 10e5ecd4090152ddabfa42367a798c6d26ae4e52 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Mon, 29 Aug 2011 15:00:28 +0200 Subject: [PATCH 1/3] Added some tests --- src/Monolog/Logger.php | 3 +- tests/Monolog/Formatter/LineFormatterTest.php | 14 ++++ .../Formatter/WildfireFormatterTest.php | 71 ++++++++++++++++++- tests/Monolog/LoggerTest.php | 14 +++- tests/Monolog/Processor/WebProcessorTest.php | 11 +++ 5 files changed, 109 insertions(+), 4 deletions(-) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 860cab7c..618c9523 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -95,7 +95,8 @@ class Logger /** * @return string */ - public function getName() { + public function getName() + { return $this->name; } diff --git a/tests/Monolog/Formatter/LineFormatterTest.php b/tests/Monolog/Formatter/LineFormatterTest.php index 4f53978f..f2adc199 100644 --- a/tests/Monolog/Formatter/LineFormatterTest.php +++ b/tests/Monolog/Formatter/LineFormatterTest.php @@ -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'); diff --git a/tests/Monolog/Formatter/WildfireFormatterTest.php b/tests/Monolog/Formatter/WildfireFormatterTest.php index a3553091..0b07e330 100644 --- a/tests/Monolog/Formatter/WildfireFormatterTest.php +++ b/tests/Monolog/Formatter/WildfireFormatterTest.php @@ -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)); + } } diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index e76bb7f7..310e74fd 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -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 */ diff --git a/tests/Monolog/Processor/WebProcessorTest.php b/tests/Monolog/Processor/WebProcessorTest.php index 28a04c95..060cbb35 100644 --- a/tests/Monolog/Processor/WebProcessorTest.php +++ b/tests/Monolog/Processor/WebProcessorTest.php @@ -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 */ From 8c094aa3b349715ad0133c40b55460b6d9f55d27 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Mon, 29 Aug 2011 15:00:42 +0200 Subject: [PATCH 2/3] Changed the tests for the abstract handlers to use a mock instead of the TestHandler --- tests/Monolog/Handler/AbstractHandlerTest.php | 25 +++++++++++++------ .../Handler/AbstractProcessingHandlerTest.php | 20 +++++++++------ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/tests/Monolog/Handler/AbstractHandlerTest.php b/tests/Monolog/Handler/AbstractHandlerTest.php index 4c4c700b..01d522fa 100644 --- a/tests/Monolog/Handler/AbstractHandlerTest.php +++ b/tests/Monolog/Handler/AbstractHandlerTest.php @@ -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()); + } } diff --git a/tests/Monolog/Handler/AbstractProcessingHandlerTest.php b/tests/Monolog/Handler/AbstractProcessingHandlerTest.php index 31a45381..b9cd3502 100644 --- a/tests/Monolog/Handler/AbstractProcessingHandlerTest.php +++ b/tests/Monolog/Handler/AbstractProcessingHandlerTest.php @@ -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'])); } } From 64009a58808afe2ac96af3a9b1318a8a28805e43 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Tue, 6 Sep 2011 23:37:29 +0200 Subject: [PATCH 3/3] Added more tests for the logger --- tests/Monolog/LoggerTest.php | 155 +++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index 310e74fd..6e687dc7 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -134,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