From e32886e26f5afab758e9e90931ccfb04e2858932 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Tue, 5 Apr 2011 10:29:08 +0200 Subject: [PATCH] Fixed callback factory for the FingersCrossedHandler --- src/Monolog/Handler/FingersCrossedHandler.php | 7 +++-- .../Handler/FingersCrossedHandlerTest.php | 26 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Handler/FingersCrossedHandler.php b/src/Monolog/Handler/FingersCrossedHandler.php index c6da1d05..6e82cb89 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -62,8 +62,11 @@ class FingersCrossedHandler extends AbstractHandler } if ($record['level'] >= $this->actionLevel) { $this->buffering = false; - if (!$this->handler instanceof AbstractHandler) { - $this->handler = $this->handler($record, $this); + if (!$this->handler instanceof HandlerInterface) { + $this->handler = call_user_func_array($this->handler, array($record, $this)); + } + if (!$this->handler instanceof HandlerInterface) { + throw new \RuntimeException("The factory callback should return an HandlerInterface"); } foreach ($this->buffer as $record) { $this->handler->handle($record); diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index 838d59eb..6a7b5da9 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -51,6 +51,32 @@ class FingersCrossedHandlerTest extends \PHPUnit_Framework_TestCase $this->assertFalse($test->hasDebugRecords()); } + public function testHandleWithCallback() + { + $test = new TestHandler(); + $handler = new FingersCrossedHandler(function($record, $handler) use ($test) { + return $test; + }); + $handler->handle($this->getRecord(Logger::DEBUG)); + $handler->handle($this->getRecord(Logger::INFO)); + $this->assertFalse($test->hasDebugRecords()); + $this->assertFalse($test->hasInfoRecords()); + $handler->handle($this->getRecord(Logger::WARNING)); + $this->assertTrue($test->hasInfoRecords()); + $this->assertTrue(count($test->getRecords()) === 3); + } + + /** + * @expectedException RuntimeException + */ + public function testHandleWithBadCallbackThrowsException() + { + $handler = new FingersCrossedHandler(function($record, $handler) { + return 'foo'; + }); + $handler->handle($this->getRecord(Logger::WARNING)); + } + protected function getRecord($level = Logger::WARNING) { return array(