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

Major refactoring to follow the Logbook model

This commit is contained in:
Jordi Boggiano
2011-02-20 20:52:52 +01:00
parent 860194e879
commit 3fa6e4b91f
24 changed files with 672 additions and 395 deletions

View File

@@ -0,0 +1,30 @@
<?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\Formatter;
use Monolog\Logger;
class JsonFormatterTest extends \PHPUnit_Framework_TestCase
{
public function testFormat()
{
$formatter = new JsonFormatter();
$message = $formatter->format(array(
'level_name' => 'WARNING',
'channel' => 'log',
'message' => array('foo'),
'datetime' => new \DateTime,
'extra' => array(),
));
$this->assertEquals(json_encode(array('foo')), $message['message']);
}
}

View File

@@ -0,0 +1,47 @@
<?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\Formatter;
use Monolog\Logger;
class LineFormatterTest extends \PHPUnit_Framework_TestCase
{
public function testDefFormatWithString()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format(array(
'level_name' => 'WARNING',
'channel' => 'log',
'message' => 'foo',
'datetime' => new \DateTime,
'extra' => array(),
));
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo'."\n", $message['message']);
}
public function testDefFormatWithArray()
{
$formatter = new LineFormatter(null, 'Y-m-d');
$message = $formatter->format(array(
'level_name' => 'ERROR',
'channel' => 'meh',
'datetime' => new \DateTime,
'extra' => array(),
'message' => array(
'channel' => 'log',
'level_name' => 'WARNING',
'message' => 'foo',
)
));
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo'."\n", $message['message']);
}
}

View File

@@ -1,38 +0,0 @@
<?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\Formatter;
use Monolog\Logger;
class SimpleFormatterTest extends \PHPUnit_Framework_TestCase
{
public function testDefFormatWithString()
{
$formatter = new SimpleFormatter(null, 'Y-m-d');
$message = $formatter->format('log', array('level' => Logger::WARNING, 'message' => 'foo'));
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo'."\n", $message);
}
public function testDefFormatWithArray()
{
$formatter = new SimpleFormatter(null, 'Y-m-d');
$message = $formatter->format('xx', array(
'level' => Logger::ERROR,
'message' => array(
'log' => 'log',
'level' => 'WARNING',
'message' => 'foo',
)
));
$this->assertEquals('['.date('Y-m-d').'] log.WARNING: foo'."\n", $message);
}
}

View File

@@ -0,0 +1,54 @@
<?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\Handler;
use Monolog\Logger;
class AbstractHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testHandle()
{
$handler = new TestHandler();
$this->assertTrue($handler->handle($this->getMessage()));
}
public function testHandleLowerLevelMessage()
{
$handler = new TestHandler();
$this->assertFalse($handler->handle($this->getMessage(Logger::DEBUG)));
}
public function testHandleBubbling()
{
$handler = new TestHandler(Logger::DEBUG, true);
$this->assertFalse($handler->handle($this->getMessage()));
}
protected function getMessage($level = Logger::WARNING)
{
return array(
'level' => $level,
'level_name' => 'WARNING',
'channel' => 'log',
'message' => 'foo',
'datetime' => new \DateTime,
'extra' => array(),
);
}
}
class TestHandler extends AbstractHandler
{
public function write($message)
{
}
}

View File

@@ -0,0 +1,51 @@
<?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\Handler;
use Monolog\Logger;
class NullHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testHandle()
{
$handler = new NullHandler();
$this->assertTrue($handler->handle($this->getMessage()));
}
public function testHandleLowerLevelMessage()
{
$handler = new NullHandler(Logger::WARNING);
$this->assertFalse($handler->handle($this->getMessage(Logger::DEBUG)));
}
public function testHandleBubbling()
{
$handler = new NullHandler(Logger::DEBUG, true);
$this->assertFalse($handler->handle($this->getMessage()));
}
/**
* No-op test for coverage
*/
public function testWrite()
{
$handler = new NullHandler();
$handler->write($this->getMessage());
}
protected function getMessage($level = Logger::WARNING)
{
return array(
'level' => $level,
);
}
}

View File

@@ -0,0 +1,61 @@
<?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\Handler;
use Monolog\Logger;
class StreamHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testWrite()
{
$handle = fopen('php://memory', 'a+');
$handler = new StreamHandler($handle);
$handler->write(array('message' => 'test'));
$handler->write(array('message' => 'test2'));
$handler->write(array('message' => 'test3'));
fseek($handle, 0);
$this->assertEquals('testtest2test3', fread($handle, 100));
}
public function testClose()
{
$handle = fopen('php://memory', 'a+');
$handler = new StreamHandler($handle);
$this->assertTrue(is_resource($handle));
$handler->close();
$this->assertFalse(is_resource($handle));
}
public function testWriteCreatesTheStreamResource()
{
$handler = new StreamHandler('php://memory');
$handler->write(array('message' => 'test'));
}
/**
* @expectedException LogicException
*/
public function testWriteMissingResource()
{
$handler = new StreamHandler(null);
$handler->write(array('message' => 'test'));
}
/**
* @expectedException UnexpectedValueException
*/
public function testWriteInvalidResource()
{
$handler = new StreamHandler('bogus://url');
@$handler->write(array('message' => 'test'));
}
}

View File

@@ -1,44 +0,0 @@
<?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;
class LogTest extends \PHPUnit_Framework_TestCase
{
public function testLog()
{
$logger = new Log('bob');
$writer1 = $this->getMock('Monolog\Writer\NullWriter', array('write'));
$writer1->expects($this->once())
->method('write')
->with('bob', Logger::WARNING, 'test');
$writer2 = $this->getMock('Monolog\Writer\NullWriter', array('write'));
$writer2->expects($this->once())
->method('write')
->with('bob', Logger::WARNING, 'test');
$logger->addWriter($writer1);
$logger->addWriter($writer2);
$logger->addMessage(Logger::WARNING, 'test');
}
public function testLogLowLevel()
{
$logger = new Log('bob');
$logger->setLevel(Logger::ERROR);
$this->assertEquals(Logger::ERROR, $logger->getLevel());
$writer1 = $this->getMock('Monolog\Writer\NullWriter', array('write'));
$writer1->expects($this->never())
->method('write');
$logger->addWriter($writer1);
$logger->addMessage(Logger::WARNING, 'test');
}
}

View File

@@ -13,33 +13,57 @@ namespace Monolog;
class LoggerTest extends \PHPUnit_Framework_TestCase
{
public function testLogAll()
public function testLog()
{
$logger = new Logger();
$log1 = $this->getMock('Monolog\Log', array('log'), array('a'));
$log1->expects($this->once())
->method('log');
$log2 = $this->getMock('Monolog\Log', array('log'), array('b'));
$log2->expects($this->once())
->method('log');
$logger->addLog($log1);
$logger->addLog($log2);
$logger->warn('test');
$logger = new Logger(__METHOD__);
$handler = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
$handler->expects($this->once())
->method('handle');
$logger->pushHandler($handler);
$logger->addWarning('test');
}
public function testLogFiltered()
/**
* @dataProvider logValues
*/
public function testLogUntilHandled($bubble)
{
$logger = new Logger();
$log1 = $this->getMock('Monolog\Log', array('log'), array('a'));
$log1->expects($this->exactly(2))
->method('log');
$log2 = $this->getMock('Monolog\Log', array('log'), array('b'));
$log2->expects($this->never())
->method('log');
$logger->addLog($log1);
$logger->addLog($log2);
$logger = new Logger(__METHOD__);
$logger->warn('test', 'a');
$logger->warn('test', array('a'));
$bottomHandler = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
$bottomHandler->expects($bubble ? $this->once() : $this->never())
->method('handle');
$logger->pushHandler($bottomHandler);
$topHandler = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
$topHandler->expects($this->once())
->method('handle')
->will($this->returnValue(!$bubble));
$logger->pushHandler($topHandler);
$logger->addWarning('test');
}
public function logValues()
{
return array(array(true), array(false));
}
public function testPushPopHandler()
{
$logger = new Logger(__METHOD__);
$handler1 = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
$handler2 = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
$handler3 = $this->getMock('Monolog\Handler\NullHandler', array('handle'));
$logger->pushHandler($handler1);
$logger->pushHandler($handler2);
$logger->pushHandler($handler3);
$this->assertEquals($handler3, $logger->popHandler());
$this->assertEquals($handler2, $logger->popHandler());
$this->assertEquals($handler1, $logger->popHandler());
}
}

View File

@@ -1,37 +0,0 @@
<?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\Writer;
use Monolog\Logger;
class StreamWritterTest extends \PHPUnit_Framework_TestCase
{
public function testWrite()
{
$handle = fopen('php://memory', 'a+');
$writer = new StreamWriter($handle);
$writer->write('log', array('level' => Logger::WARNING, 'message' => 'test'));
$writer->write('log', array('level' => Logger::WARNING, 'message' => 'test2'));
$writer->write('log', array('level' => Logger::WARNING, 'message' => 'test3'));
fseek($handle, 0);
$this->assertEquals('testtest2test3', fread($handle, 100));
}
public function testClose()
{
$handle = fopen('php://memory', 'a+');
$writer = new StreamWriter($handle);
$this->assertTrue(is_resource($handle));
$writer->close();
$this->assertFalse(is_resource($handle));
}
}