From 3cb3dbdc8fdf72a22044c529b6f45994d42c4785 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 6 Apr 2011 13:22:06 +0200 Subject: [PATCH] Made the write method protected Tests are not a good reason to make it public. --- src/Monolog/Handler/AbstractHandler.php | 17 ++++---- src/Monolog/Handler/BufferHandler.php | 2 +- src/Monolog/Handler/FingersCrossedHandler.php | 2 +- src/Monolog/Handler/NullHandler.php | 2 +- src/Monolog/Handler/RotatingFileHandler.php | 26 ++++++------ src/Monolog/Handler/StreamHandler.php | 36 ++++++++--------- src/Monolog/Handler/SyslogHandler.php | 8 ++-- src/Monolog/Handler/TestHandler.php | 2 +- tests/Monolog/Handler/AbstractHandlerTest.php | 27 ++++--------- tests/Monolog/Handler/BufferHandlerTest.php | 15 +------ .../Handler/FingersCrossedHandlerTest.php | 15 +------ tests/Monolog/Handler/NullHandlerTest.php | 19 +-------- .../Handler/RotatingFileHandlerTest.php | 12 ++++-- tests/Monolog/Handler/StreamHandlerTest.php | 24 ++++++----- tests/Monolog/TestCase.php | 40 +++++++++++++++++++ tests/bootstrap.php | 2 + 16 files changed, 125 insertions(+), 124 deletions(-) create mode 100644 tests/Monolog/TestCase.php diff --git a/src/Monolog/Handler/AbstractHandler.php b/src/Monolog/Handler/AbstractHandler.php index d257e689..d52699d6 100644 --- a/src/Monolog/Handler/AbstractHandler.php +++ b/src/Monolog/Handler/AbstractHandler.php @@ -69,6 +69,7 @@ abstract class AbstractHandler implements HandlerInterface $record = $this->formatter->format($record); $this->write($record); + return false === $this->bubble; } @@ -82,14 +83,6 @@ abstract class AbstractHandler implements HandlerInterface } } - /** - * Writes the record down to the log of the implementing handler - * - * @param array $record - * @return void - */ - abstract public function write(array $record); - /** * Closes the handler. * @@ -178,6 +171,14 @@ abstract class AbstractHandler implements HandlerInterface $this->close(); } + /** + * Writes the record down to the log of the implementing handler + * + * @param array $record + * @return void + */ + abstract protected function write(array $record); + /** * Gets the default formatter. * diff --git a/src/Monolog/Handler/BufferHandler.php b/src/Monolog/Handler/BufferHandler.php index 64af35e4..07738d6b 100644 --- a/src/Monolog/Handler/BufferHandler.php +++ b/src/Monolog/Handler/BufferHandler.php @@ -63,7 +63,7 @@ class BufferHandler extends AbstractHandler /** * Implemented to comply with the AbstractHandler requirements. Can not be called. */ - public function write(array $record) + protected function write(array $record) { throw new \BadMethodCallException('This method should not be called directly on the FingersCrossedHandler.'); } diff --git a/src/Monolog/Handler/FingersCrossedHandler.php b/src/Monolog/Handler/FingersCrossedHandler.php index f8c0c513..8a98cb40 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -85,7 +85,7 @@ class FingersCrossedHandler extends AbstractHandler /** * Implemented to comply with the AbstractHandler requirements. Can not be called. */ - public function write(array $record) + protected function write(array $record) { throw new \BadMethodCallException('This method should not be called directly on the FingersCrossedHandler.'); } diff --git a/src/Monolog/Handler/NullHandler.php b/src/Monolog/Handler/NullHandler.php index d014c363..f3b7606a 100644 --- a/src/Monolog/Handler/NullHandler.php +++ b/src/Monolog/Handler/NullHandler.php @@ -38,7 +38,7 @@ class NullHandler extends AbstractHandler /** * {@inheritdoc} */ - public function write(array $record) + protected function write(array $record) { } } \ No newline at end of file diff --git a/src/Monolog/Handler/RotatingFileHandler.php b/src/Monolog/Handler/RotatingFileHandler.php index 68ec2654..ebecd569 100644 --- a/src/Monolog/Handler/RotatingFileHandler.php +++ b/src/Monolog/Handler/RotatingFileHandler.php @@ -52,19 +52,6 @@ class RotatingFileHandler extends StreamHandler parent::__construct($timedFilename, $level, $bubble); } - /** - * {@inheritdoc} - */ - public function write(array $record) - { - // on the first record written, if the log is new, we should rotate (once per day) - if (null === $this->mustRotate) { - $this->mustRotate = !file_exists($this->url); - } - - parent::write($record); - } - /** * {@inheritdoc} */ @@ -77,6 +64,19 @@ class RotatingFileHandler extends StreamHandler } } + /** + * {@inheritdoc} + */ + protected function write(array $record) + { + // on the first record written, if the log is new, we should rotate (once per day) + if (null === $this->mustRotate) { + $this->mustRotate = !file_exists($this->url); + } + + parent::write($record); + } + /** * Rotates the files. */ diff --git a/src/Monolog/Handler/StreamHandler.php b/src/Monolog/Handler/StreamHandler.php index 0cc9ed6e..c454c0c8 100644 --- a/src/Monolog/Handler/StreamHandler.php +++ b/src/Monolog/Handler/StreamHandler.php @@ -41,24 +41,6 @@ class StreamHandler extends AbstractHandler } } - /** - * {@inheritdoc} - */ - public function write(array $record) - { - if (null === $this->stream) { - if (!$this->url) { - throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().'); - } - $this->stream = fopen($this->url, 'a'); - if (!is_resource($this->stream)) { - $this->stream = null; - throw new \UnexpectedValueException('The stream could not be opened, "'.$this->url.'" may be an invalid url.'); - } - } - fwrite($this->stream, (string) $record['message']); - } - /** * {@inheritdoc} */ @@ -69,4 +51,22 @@ class StreamHandler extends AbstractHandler $this->stream = null; } } + + /** + * {@inheritdoc} + */ + protected function write(array $record) + { + if (null === $this->stream) { + if (!$this->url) { + throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().'); + } + $this->stream = @fopen($this->url, 'a'); + if (!is_resource($this->stream)) { + $this->stream = null; + throw new \UnexpectedValueException('The stream could not be opened, "'.$this->url.'" may be an invalid url.'); + } + } + fwrite($this->stream, (string) $record['message']); + } } \ No newline at end of file diff --git a/src/Monolog/Handler/SyslogHandler.php b/src/Monolog/Handler/SyslogHandler.php index 820930bb..fd7252b2 100644 --- a/src/Monolog/Handler/SyslogHandler.php +++ b/src/Monolog/Handler/SyslogHandler.php @@ -93,16 +93,16 @@ class SyslogHandler extends AbstractHandler /** * {@inheritdoc} */ - public function write(array $record) + public function close() { - syslog($this->logLevels[$record['level']], (string) $record['message']); + closelog(); } /** * {@inheritdoc} */ - public function close() + protected function write(array $record) { - closelog(); + syslog($this->logLevels[$record['level']], (string) $record['message']); } } diff --git a/src/Monolog/Handler/TestHandler.php b/src/Monolog/Handler/TestHandler.php index ea70e196..dd7e8d94 100644 --- a/src/Monolog/Handler/TestHandler.php +++ b/src/Monolog/Handler/TestHandler.php @@ -89,7 +89,7 @@ class TestHandler extends AbstractHandler /** * {@inheritdoc} */ - public function write(array $record) + protected function write(array $record) { $this->recordsByLevel[$record['level']][] = $record; $this->records[] = $record; diff --git a/tests/Monolog/Handler/AbstractHandlerTest.php b/tests/Monolog/Handler/AbstractHandlerTest.php index c51cd896..4cf5e307 100644 --- a/tests/Monolog/Handler/AbstractHandlerTest.php +++ b/tests/Monolog/Handler/AbstractHandlerTest.php @@ -11,50 +11,39 @@ namespace Monolog\Handler; +use Monolog\TestCase; use Monolog\Logger; -class AbstractHandlerTest extends \PHPUnit_Framework_TestCase +class AbstractHandlerTest extends TestCase { public function testHandle() { $handler = new TestHandler(); - $this->assertTrue($handler->handle($this->getMessage())); + $this->assertTrue($handler->handle($this->getRecord())); } public function testHandleLowerLevelMessage() { $handler = new TestHandler(Logger::WARNING); - $this->assertFalse($handler->handle($this->getMessage(Logger::DEBUG))); + $this->assertFalse($handler->handle($this->getRecord(Logger::DEBUG))); } public function testHandleBubbling() { $handler = new TestHandler(Logger::DEBUG, true); - $this->assertFalse($handler->handle($this->getMessage())); + $this->assertFalse($handler->handle($this->getRecord())); } public function testHandleNotBubbling() { $handler = new TestHandler(Logger::DEBUG); - $this->assertTrue($handler->handle($this->getMessage())); + $this->assertTrue($handler->handle($this->getRecord())); } public function testIsHandling() { $handler = new TestHandler(Logger::WARNING); - $this->assertTrue($handler->handle($this->getMessage())); - $this->assertFalse($handler->handle($this->getMessage(Logger::DEBUG))); - } - - protected function getMessage($level = Logger::WARNING) - { - return array( - 'level' => $level, - 'level_name' => Logger::getLevelName($level), - 'channel' => 'log', - 'message' => 'foo', - 'datetime' => new \DateTime, - 'extra' => array(), - ); + $this->assertTrue($handler->handle($this->getRecord())); + $this->assertFalse($handler->handle($this->getRecord(Logger::DEBUG))); } } diff --git a/tests/Monolog/Handler/BufferHandlerTest.php b/tests/Monolog/Handler/BufferHandlerTest.php index 369d1e7d..a9be1a5b 100644 --- a/tests/Monolog/Handler/BufferHandlerTest.php +++ b/tests/Monolog/Handler/BufferHandlerTest.php @@ -11,9 +11,10 @@ namespace Monolog\Handler; +use Monolog\TestCase; use Monolog\Logger; -class BufferHandlerTest extends \PHPUnit_Framework_TestCase +class BufferHandlerTest extends TestCase { public function testHandleBuffers() { @@ -52,16 +53,4 @@ class BufferHandlerTest extends \PHPUnit_Framework_TestCase $this->assertTrue($test->hasInfoRecords()); $this->assertFalse($test->hasDebugRecords()); } - - protected function getRecord($level = Logger::WARNING) - { - return array( - 'level' => $level, - 'level_name' => Logger::getLevelName($level), - 'channel' => 'log', - 'Record' => 'foo', - 'datetime' => new \DateTime, - 'extra' => array(), - ); - } } diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index 6a7b5da9..2871cb6f 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -11,9 +11,10 @@ namespace Monolog\Handler; +use Monolog\TestCase; use Monolog\Logger; -class FingersCrossedHandlerTest extends \PHPUnit_Framework_TestCase +class FingersCrossedHandlerTest extends TestCase { public function testHandleBuffers() { @@ -76,16 +77,4 @@ class FingersCrossedHandlerTest extends \PHPUnit_Framework_TestCase }); $handler->handle($this->getRecord(Logger::WARNING)); } - - protected function getRecord($level = Logger::WARNING) - { - return array( - 'level' => $level, - 'level_name' => Logger::getLevelName($level), - 'channel' => 'log', - 'Record' => 'foo', - 'datetime' => new \DateTime, - 'extra' => array(), - ); - } } diff --git a/tests/Monolog/Handler/NullHandlerTest.php b/tests/Monolog/Handler/NullHandlerTest.php index 3beb36e9..b105cfef 100644 --- a/tests/Monolog/Handler/NullHandlerTest.php +++ b/tests/Monolog/Handler/NullHandlerTest.php @@ -11,9 +11,10 @@ namespace Monolog\Handler; +use Monolog\TestCase; use Monolog\Logger; -class NullHandlerTest extends \PHPUnit_Framework_TestCase +class NullHandlerTest extends TestCase { public function testHandle() { @@ -26,20 +27,4 @@ class NullHandlerTest extends \PHPUnit_Framework_TestCase $handler = new NullHandler(Logger::WARNING); $this->assertFalse($handler->handle($this->getRecord(Logger::DEBUG))); } - - /** - * No-op test for coverage - */ - public function testWrite() - { - $handler = new NullHandler(); - $handler->write($this->getRecord()); - } - - protected function getRecord($level = Logger::WARNING) - { - return array( - 'level' => $level, - ); - } } \ No newline at end of file diff --git a/tests/Monolog/Handler/RotatingFileHandlerTest.php b/tests/Monolog/Handler/RotatingFileHandlerTest.php index b783b62c..fc9819e9 100644 --- a/tests/Monolog/Handler/RotatingFileHandlerTest.php +++ b/tests/Monolog/Handler/RotatingFileHandlerTest.php @@ -11,9 +11,10 @@ namespace Monolog\Handler; +use Monolog\TestCase; use Monolog\Logger; -class RotatingFileHandlerTest extends \PHPUnit_Framework_TestCase +class RotatingFileHandlerTest extends TestCase { public function setUp() { @@ -29,7 +30,8 @@ class RotatingFileHandlerTest extends \PHPUnit_Framework_TestCase touch(__DIR__.'/Fixtures/foo-'.date('Y-m-d', time() - 86400).'.rot'); $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot'); - $handler->write(array('message' => 'test')); + $handler->setFormatter($this->getIdentityFormatter()); + $handler->handle($this->getRecord()); $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot'; $this->assertTrue(file_exists($log)); @@ -53,7 +55,8 @@ class RotatingFileHandlerTest extends \PHPUnit_Framework_TestCase } $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot', 2); - $handler->write(array('message' => 'test')); + $handler->setFormatter($this->getIdentityFormatter()); + $handler->handle($this->getRecord()); $handler->close(); @@ -80,7 +83,8 @@ class RotatingFileHandlerTest extends \PHPUnit_Framework_TestCase $log = __DIR__.'/Fixtures/foo-'.date('Y-m-d').'.rot'; file_put_contents($log, "foo"); $handler = new RotatingFileHandler(__DIR__.'/Fixtures/foo.rot'); - $handler->write(array('message' => 'test')); + $handler->setFormatter($this->getIdentityFormatter()); + $handler->handle($this->getRecord()); $this->assertEquals('footest', file_get_contents($log)); } diff --git a/tests/Monolog/Handler/StreamHandlerTest.php b/tests/Monolog/Handler/StreamHandlerTest.php index 58ff92e5..296d8cdf 100644 --- a/tests/Monolog/Handler/StreamHandlerTest.php +++ b/tests/Monolog/Handler/StreamHandlerTest.php @@ -11,17 +11,19 @@ namespace Monolog\Handler; +use Monolog\TestCase; use Monolog\Logger; -class StreamHandlerTest extends \PHPUnit_Framework_TestCase +class StreamHandlerTest extends TestCase { - public function testWrite() + public function testHandle() { $handle = fopen('php://memory', 'a+'); $handler = new StreamHandler($handle); - $handler->write(array('message' => 'test')); - $handler->write(array('message' => 'test2')); - $handler->write(array('message' => 'test3')); + $handler->setFormatter($this->getIdentityFormatter()); + $handler->handle($this->getRecord(Logger::WARNING, 'test')); + $handler->handle($this->getRecord(Logger::WARNING, 'test2')); + $handler->handle($this->getRecord(Logger::WARNING, 'test3')); fseek($handle, 0); $this->assertEquals('testtest2test3', fread($handle, 100)); } @@ -35,27 +37,27 @@ class StreamHandlerTest extends \PHPUnit_Framework_TestCase $this->assertFalse(is_resource($handle)); } - public function testWriteCreatesTheStreamResource() + public function testHandleCreatesTheStreamResource() { $handler = new StreamHandler('php://memory'); - $handler->write(array('message' => 'test')); + $handler->handle($this->getRecord()); } /** * @expectedException LogicException */ - public function testWriteMissingResource() + public function testHandleMissingResource() { $handler = new StreamHandler(null); - $handler->write(array('message' => 'test')); + $handler->handle($this->getRecord()); } /** * @expectedException UnexpectedValueException */ - public function testWriteInvalidResource() + public function testHandleInvalidResource() { $handler = new StreamHandler('bogus://url'); - @$handler->write(array('message' => 'test')); + $handler->handle($this->getRecord()); } } diff --git a/tests/Monolog/TestCase.php b/tests/Monolog/TestCase.php new file mode 100644 index 00000000..00ebae1e --- /dev/null +++ b/tests/Monolog/TestCase.php @@ -0,0 +1,40 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog; + +class TestCase extends \PHPUnit_Framework_TestCase +{ + protected function getRecord($level = Logger::WARNING, $message = 'test') + { + return array( + 'message' => $message, + 'level' => $level, + 'level_name' => Logger::getLevelName($level), + 'channel' => 'test', + 'datetime' => new \DateTime(), + 'extra' => array(), + ); + } + + /** + * @return Monolog\Formatter\FormatterInterface + */ + protected function getIdentityFormatter() + { + $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface'); + $formatter->expects($this->any()) + ->method('format') + ->will($this->returnArgument(0)); + + return $formatter; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d6eaa6b4..e3610ebc 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -9,6 +9,8 @@ * file that was distributed with this source code. */ +require_once __DIR__.'/Monolog/TestCase.php'; + spl_autoload_register(function($class) { $file = __DIR__.'/../src/'.strtr($class, '\\', '/').'.php';