From 447b7a549ae2a42719c387fe6319049fab8c39b0 Mon Sep 17 00:00:00 2001 From: Hennadiy Verkh Date: Mon, 17 Mar 2014 15:06:35 +0100 Subject: [PATCH 1/2] Added MinMaxHandler with unit tests. --- src/Monolog/Handler/MinMaxHandler.php | 83 ++++++++++++++++++ tests/Monolog/Handler/MinMaxHandlerTest.php | 96 +++++++++++++++++++++ 2 files changed, 179 insertions(+) create mode 100644 src/Monolog/Handler/MinMaxHandler.php create mode 100644 tests/Monolog/Handler/MinMaxHandlerTest.php diff --git a/src/Monolog/Handler/MinMaxHandler.php b/src/Monolog/Handler/MinMaxHandler.php new file mode 100644 index 00000000..31caf9f2 --- /dev/null +++ b/src/Monolog/Handler/MinMaxHandler.php @@ -0,0 +1,83 @@ +handler = $handler; + $this->minLevel = $minLevel; + $this->maxLevel = $maxLevel; + $this->bubble = $bubble; + } + + /** + * {@inheritdoc} + */ + public function isHandling(array $record) + { + return $record['level'] >= $this->minLevel && $record['level'] <= $this->maxLevel; + } + + /** + * {@inheritdoc} + */ + public function handle(array $record) + { + if (!$this->isHandling($record)) { + return false; + } + + if ($this->processors) { + foreach ($this->processors as $processor) { + $record = call_user_func($processor, $record); + } + } + + $this->handler->handle($record); + + return false === $this->bubble; + } +} diff --git a/tests/Monolog/Handler/MinMaxHandlerTest.php b/tests/Monolog/Handler/MinMaxHandlerTest.php new file mode 100644 index 00000000..bd18571c --- /dev/null +++ b/tests/Monolog/Handler/MinMaxHandlerTest.php @@ -0,0 +1,96 @@ +assertFalse($handler->isHandling($this->getRecord(Logger::DEBUG))); + $this->assertTrue($handler->isHandling($this->getRecord(Logger::INFO))); + $this->assertTrue($handler->isHandling($this->getRecord(Logger::NOTICE))); + $this->assertFalse($handler->isHandling($this->getRecord(Logger::WARNING))); + $this->assertFalse($handler->isHandling($this->getRecord(Logger::ERROR))); + $this->assertFalse($handler->isHandling($this->getRecord(Logger::CRITICAL))); + $this->assertFalse($handler->isHandling($this->getRecord(Logger::ALERT))); + $this->assertFalse($handler->isHandling($this->getRecord(Logger::EMERGENCY))); + } + + /** + * @covers Monolog\Handler\MinMaxHandler::handle + */ + public function testHandleProcessOnlyNeededLevels() + { + $test = new TestHandler(); + $handler = new MinMaxHandler($test, Logger::INFO, Logger::NOTICE); + + $handler->handle($this->getRecord(Logger::DEBUG)); + $this->assertFalse($test->hasDebugRecords()); + + $handler->handle($this->getRecord(Logger::INFO)); + $this->assertTrue($test->hasInfoRecords()); + $handler->handle($this->getRecord(Logger::NOTICE)); + $this->assertTrue($test->hasNoticeRecords()); + + $handler->handle($this->getRecord(Logger::WARNING)); + $this->assertFalse($test->hasWarningRecords()); + $handler->handle($this->getRecord(Logger::ERROR)); + $this->assertFalse($test->hasErrorRecords()); + $handler->handle($this->getRecord(Logger::CRITICAL)); + $this->assertFalse($test->hasCriticalRecords()); + $handler->handle($this->getRecord(Logger::ALERT)); + $this->assertFalse($test->hasAlertRecords()); + $handler->handle($this->getRecord(Logger::EMERGENCY)); + $this->assertFalse($test->hasEmergencyRecords()); + } + + /** + * @covers Monolog\Handler\MinMaxHandler::handle + */ + public function testHandleUsesProcessors() + { + $test = new TestHandler(); + $handler = new MinMaxHandler($test, Logger::DEBUG, Logger::EMERGENCY); + $handler->pushProcessor( + function ($record) { + $record['extra']['foo'] = true; + + return $record; + } + ); + $handler->handle($this->getRecord(Logger::WARNING)); + $this->assertTrue($test->hasWarningRecords()); + $records = $test->getRecords(); + $this->assertTrue($records[0]['extra']['foo']); + } + + /** + * @covers Monolog\Handler\MinMaxHandler::handle + */ + public function testHandleRespectsBubble() + { + $test = new TestHandler(); + + $handler = new MinMaxHandler($test, Logger::INFO, Logger::NOTICE, false); + $this->assertTrue($handler->handle($this->getRecord(Logger::INFO))); + $this->assertFalse($handler->handle($this->getRecord(Logger::WARNING))); + + $handler = new MinMaxHandler($test, Logger::INFO, Logger::NOTICE, true); + $this->assertFalse($handler->handle($this->getRecord(Logger::INFO))); + $this->assertFalse($handler->handle($this->getRecord(Logger::WARNING))); + } +} From 8e7f51f27f2ad15c0cdc919d3a23738ba1f76f6b Mon Sep 17 00:00:00 2001 From: Hennadiy Verkh Date: Mon, 17 Mar 2014 16:06:57 +0100 Subject: [PATCH 2/2] Fixed passing Closure as parameter to minMaxHandler. --- src/Monolog/Handler/MinMaxHandler.php | 24 +++++++++++---- tests/Monolog/Handler/MinMaxHandlerTest.php | 33 ++++++++++++++++++++- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/Monolog/Handler/MinMaxHandler.php b/src/Monolog/Handler/MinMaxHandler.php index 31caf9f2..f310555e 100644 --- a/src/Monolog/Handler/MinMaxHandler.php +++ b/src/Monolog/Handler/MinMaxHandler.php @@ -12,7 +12,7 @@ use Monolog\Logger; class MinMaxHandler extends AbstractHandler { /** - * Handler or factory callable($record, $fingersCrossedHandler) + * Handler or factory callable($record, $this) * * @var callable|\Monolog\Handler\HandlerInterface */ @@ -37,12 +37,10 @@ class MinMaxHandler extends AbstractHandler protected $bubble; /** - * @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler). - * @param int $minLevel - * @param int $maxLevel + * @param callable|HandlerInterface $handler Handler or factory callable($record, $this). + * @param int $minLevel Minimum level for logs that are passes to handler + * @param int $maxLevel Maximum level for logs that are passes to handler * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not - * - * @internal param \TtLibrary\Log\Handler\Maximum $int log level */ public function __construct($handler, $minLevel = Logger::DEBUG, $maxLevel = Logger::EMERGENCY, $bubble = true) { @@ -70,6 +68,20 @@ class MinMaxHandler extends AbstractHandler return false; } + // The same logic as in FingersCrossedHandler + if (!$this->handler instanceof HandlerInterface) { + if (!is_callable($this->handler)) { + throw new \RuntimeException( + "The given handler (" . json_encode($this->handler) + . ") is not a callable nor a Monolog\\Handler\\HandlerInterface object" + ); + } + $this->handler = call_user_func($this->handler, $record, $this); + if (!$this->handler instanceof HandlerInterface) { + throw new \RuntimeException("The factory callable should return a HandlerInterface"); + } + } + if ($this->processors) { foreach ($this->processors as $processor) { $record = call_user_func($processor, $record); diff --git a/tests/Monolog/Handler/MinMaxHandlerTest.php b/tests/Monolog/Handler/MinMaxHandlerTest.php index bd18571c..d7ce5c3d 100644 --- a/tests/Monolog/Handler/MinMaxHandlerTest.php +++ b/tests/Monolog/Handler/MinMaxHandlerTest.php @@ -93,4 +93,35 @@ class MinMaxHandlerTest extends TestCase $this->assertFalse($handler->handle($this->getRecord(Logger::INFO))); $this->assertFalse($handler->handle($this->getRecord(Logger::WARNING))); } -} + + /** + * @covers Monolog\Handler\MinMaxHandler::handle + */ + public function testHandleWithCallback() + { + $test = new TestHandler(); + $handler = new MinMaxHandler( + function ($record, $handler) use ($test) { + return $test; + }, Logger::INFO, Logger::NOTICE, false + ); + $handler->handle($this->getRecord(Logger::DEBUG)); + $handler->handle($this->getRecord(Logger::INFO)); + $this->assertFalse($test->hasDebugRecords()); + $this->assertTrue($test->hasInfoRecords()); + } + + /** + * @covers Monolog\Handler\MinMaxHandler::handle + * @expectedException \RuntimeException + */ + public function testHandleWithBadCallbackThrowsException() + { + $handler = new MinMaxHandler( + function ($record, $handler) { + return 'foo'; + } + ); + $handler->handle($this->getRecord(Logger::WARNING)); + } +} \ No newline at end of file