Przeglądaj źródła

Made the write method protected

Tests are not a good reason to make it public.
Christophe Coevoet 14 lat temu
rodzic
commit
3cb3dbdc8f

+ 9 - 8
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.
      *

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

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

+ 1 - 1
src/Monolog/Handler/NullHandler.php

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

+ 11 - 11
src/Monolog/Handler/RotatingFileHandler.php

@@ -55,26 +55,26 @@ class RotatingFileHandler extends StreamHandler
     /**
      * {@inheritdoc}
      */
-    public function write(array $record)
+    public function close()
     {
-        // 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::close();
 
-        parent::write($record);
+        if (true === $this->mustRotate) {
+            $this->rotate();
+        }
     }
 
     /**
      * {@inheritdoc}
      */
-    public function close()
+    protected function write(array $record)
     {
-        parent::close();
-
-        if (true === $this->mustRotate) {
-            $this->rotate();
+        // 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);
     }
 
     /**

+ 13 - 13
src/Monolog/Handler/StreamHandler.php

@@ -44,13 +44,24 @@ class StreamHandler extends AbstractHandler
     /**
      * {@inheritdoc}
      */
-    public function write(array $record)
+    public function close()
+    {
+        if (null !== $this->stream) {
+            fclose($this->stream);
+            $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');
+            $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.');
@@ -58,15 +69,4 @@ class StreamHandler extends AbstractHandler
         }
         fwrite($this->stream, (string) $record['message']);
     }
-
-    /**
-     * {@inheritdoc}
-     */
-    public function close()
-    {
-        if (null !== $this->stream) {
-            fclose($this->stream);
-            $this->stream = null;
-        }
-    }
 }

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

+ 1 - 1
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;

+ 8 - 19
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)));
     }
 }

+ 2 - 13
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(),
-        );
-    }
 }

+ 2 - 13
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(),
-        );
-    }
 }

+ 2 - 17
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,
-        );
-    }
 }

+ 8 - 4
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));
     }
 

+ 13 - 11
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());
     }
 }

+ 40 - 0
tests/Monolog/TestCase.php

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

+ 2 - 0
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';