diff options
author | Christophe Coevoet <stof@notk.org> | 2011-08-29 15:00:28 +0200 |
---|---|---|
committer | Christophe Coevoet <stof@notk.org> | 2011-08-29 15:00:28 +0200 |
commit | 10e5ecd4090152ddabfa42367a798c6d26ae4e52 (patch) | |
tree | 630e7d6a63d097c99cefd26a53fb12d75aed1da8 | |
parent | 5267b03b1e4861c4657ede17a88f13ef479db482 (diff) | |
download | monolog-10e5ecd4090152ddabfa42367a798c6d26ae4e52.zip monolog-10e5ecd4090152ddabfa42367a798c6d26ae4e52.tar.gz monolog-10e5ecd4090152ddabfa42367a798c6d26ae4e52.tar.bz2 |
Added some tests
-rw-r--r-- | src/Monolog/Logger.php | 3 | ||||
-rw-r--r-- | tests/Monolog/Formatter/LineFormatterTest.php | 14 | ||||
-rw-r--r-- | tests/Monolog/Formatter/WildfireFormatterTest.php | 71 | ||||
-rw-r--r-- | tests/Monolog/LoggerTest.php | 14 | ||||
-rw-r--r-- | 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 860cab7..618c952 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 4f53978..f2adc19 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 a355309..0b07e33 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 e76bb7f..310e74f 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() { @@ -108,6 +107,17 @@ class LoggerTest extends \PHPUnit_Framework_TestCase } /** + * @covers Monolog\Logger::pushProcessor + * @expectedException InvalidArgumentException + */ + public function testPushProcessorWithNonCallable() + { + $logger = new Logger(__METHOD__); + + $logger->pushProcessor(new \stdClass()); + } + + /** * @covers Monolog\Logger::addRecord */ public function testProcessorsAreExecuted() diff --git a/tests/Monolog/Processor/WebProcessorTest.php b/tests/Monolog/Processor/WebProcessorTest.php index 28a04c9..060cbb3 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 */ |