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

Made the write method protected

Tests are not a good reason to make it public.
This commit is contained in:
Christophe Coevoet
2011-04-06 13:22:06 +02:00
parent 87332a3e4e
commit 3cb3dbdc8f
16 changed files with 125 additions and 124 deletions

View File

@@ -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.
*

View File

@@ -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.');
}

View File

@@ -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.');
}

View File

@@ -38,7 +38,7 @@ class NullHandler extends AbstractHandler
/**
* {@inheritdoc}
*/
public function write(array $record)
protected function write(array $record)
{
}
}

View File

@@ -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.
*/

View File

@@ -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']);
}
}

View File

@@ -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']);
}
}

View File

@@ -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;

View File

@@ -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)));
}
}

View File

@@ -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(),
);
}
}

View File

@@ -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(),
);
}
}

View File

@@ -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,
);
}
}

View File

@@ -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));
}

View File

@@ -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());
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
}
}

View File

@@ -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';