Parcourir la source

HandlerWrapper

Alexey Karapetov il y a 10 ans
Parent
commit
c688ff17eb

+ 2 - 0
doc/02-handlers-formatters-processors.md

@@ -105,6 +105,8 @@
 - _PsrHandler_: Can be used to forward log records to an existing PSR-3 logger
 - _TestHandler_: Used for testing, it records everything that is sent to it and
   has accessors to read out the information.
+- _HandlerWrapper_: A simple handler wrapper you can inherit from to create
+ your own wrappers easily.
 
 ## Formatters
 

+ 104 - 0
src/Monolog/Handler/HandlerWrapper.php

@@ -0,0 +1,104 @@
+<?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\Formatter\FormatterInterface;
+
+/**
+ * This simple wrapper class can be used to extend handlers functionality.
+ *
+ * Example: A filtering handle. Inherit from this class, override isHandling() like this
+ *
+ * public function isHandling(array $record)
+ * {
+ *      if ($record meets certain conditions) {
+ *          return false;
+ *      }
+ *      return $this->handler->isHandling($record);
+ * }
+ *
+ * @author Alexey Karapetov <alexey@karapetov.com>
+ */
+class HandlerWrapper implements HandlerInterface
+{
+    /**
+     * @var HandlerInterface
+     */
+    private $handler;
+
+    /**
+     * HandlerWrapper constructor.
+     * @param HandlerInterface $handler
+     */
+    public function __construct(HandlerInterface $handler)
+    {
+        $this->handler = $handler;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function isHandling(array $record)
+    {
+        return $this->handler->isHandling($record);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function handle(array $record)
+    {
+        return $this->handler->handle($record);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function handleBatch(array $records)
+    {
+        return $this->handler->handleBatch($records);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function pushProcessor($callback)
+    {
+        $this->handler->pushProcessor($callback);
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function popProcessor()
+    {
+        return $this->handler->popProcessor();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function setFormatter(FormatterInterface $formatter)
+    {
+        $this->handler->setFormatter($formatter);
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getFormatter()
+    {
+        return $this->handler->getFormatter();
+    }
+}

+ 130 - 0
tests/Monolog/Handler/HandlerWrapperTest.php

@@ -0,0 +1,130 @@
+<?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\TestCase;
+
+/**
+ * @author Alexey Karapetov <alexey@karapetov.com>
+ */
+class HandlerWrapperTest extends TestCase
+{
+    /**
+     * @var HandlerWrapper
+     */
+    private $wrapper;
+
+    private $handler;
+
+    public function setUp()
+    {
+        parent::setUp();
+        $this->handler = $this->getMock('Monolog\\Handler\\HandlerInterface');
+        $this->wrapper = new HandlerWrapper($this->handler);
+    }
+
+    /**
+     * @return array
+     */
+    public function trueFalseDataProvider()
+    {
+        return array(
+            array(true),
+            array(false),
+        );
+    }
+
+    /**
+     * @param $result
+     * @dataProvider trueFalseDataProvider
+     */
+    public function testIsHandling($result)
+    {
+        $record = $this->getRecord();
+        $this->handler->expects($this->once())
+            ->method('isHandling')
+            ->with($record)
+            ->willReturn($result);
+
+        $this->assertEquals($result, $this->wrapper->isHandling($record));
+    }
+
+    /**
+     * @param $result
+     * @dataProvider trueFalseDataProvider
+     */
+    public function testHandle($result)
+    {
+        $record = $this->getRecord();
+        $this->handler->expects($this->once())
+            ->method('handle')
+            ->with($record)
+            ->willReturn($result);
+
+        $this->assertEquals($result, $this->wrapper->handle($record));
+    }
+
+    /**
+     * @param $result
+     * @dataProvider trueFalseDataProvider
+     */
+    public function testHandleBatch($result)
+    {
+        $records = $this->getMultipleRecords();
+        $this->handler->expects($this->once())
+            ->method('handleBatch')
+            ->with($records)
+            ->willReturn($result);
+
+        $this->assertEquals($result, $this->wrapper->handleBatch($records));
+    }
+
+    public function testPushProcessor()
+    {
+        $processor = function () {};
+        $this->handler->expects($this->once())
+            ->method('pushProcessor')
+            ->with($processor);
+
+        $this->assertEquals($this->wrapper, $this->wrapper->pushProcessor($processor));
+    }
+
+    public function testPopProcessor()
+    {
+        $processor = function () {};
+        $this->handler->expects($this->once())
+            ->method('popProcessor')
+            ->willReturn($processor);
+
+        $this->assertEquals($processor, $this->wrapper->popProcessor());
+    }
+
+    public function testSetFormatter()
+    {
+        $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
+        $this->handler->expects($this->once())
+            ->method('setFormatter')
+            ->with($formatter);
+
+        $this->assertEquals($this->wrapper, $this->wrapper->setFormatter($formatter));
+    }
+
+    public function testGetFormatter()
+    {
+        $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
+        $this->handler->expects($this->once())
+            ->method('getFormatter')
+            ->willReturn($formatter);
+
+        $this->assertEquals($formatter, $this->wrapper->getFormatter());
+    }
+}