From 81e8bf618972264042a8deea14fab7bfacce5697 Mon Sep 17 00:00:00 2001 From: Erik Johansson Date: Sun, 19 Mar 2017 00:49:00 +0100 Subject: [PATCH 1/2] Add level parameter to payload when reporting Rollbar exceptions (#852) --- src/Monolog/Handler/RollbarHandler.php | 1 + tests/Monolog/Handler/RollbarHandlerTest.php | 82 ++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 tests/Monolog/Handler/RollbarHandlerTest.php diff --git a/src/Monolog/Handler/RollbarHandler.php b/src/Monolog/Handler/RollbarHandler.php index 0d9de1a2..6c8a3e3e 100644 --- a/src/Monolog/Handler/RollbarHandler.php +++ b/src/Monolog/Handler/RollbarHandler.php @@ -97,6 +97,7 @@ class RollbarHandler extends AbstractProcessingHandler )); if (isset($context['exception']) && $context['exception'] instanceof Exception) { + $payload['level'] = $context['level']; $exception = $context['exception']; unset($context['exception']); diff --git a/tests/Monolog/Handler/RollbarHandlerTest.php b/tests/Monolog/Handler/RollbarHandlerTest.php new file mode 100644 index 00000000..e691b1e7 --- /dev/null +++ b/tests/Monolog/Handler/RollbarHandlerTest.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Exception; +use Monolog\Test\TestCase; +use Monolog\Logger; +use PHPUnit_Framework_MockObject_MockObject as MockObject; + +/** + * @author Erik Johansson + * @see https://rollbar.com/docs/notifier/rollbar-php/ + * + * @coversDefaultClass Monolog\Handler\RollbarHandler + */ +class RollbarHandlerTest extends TestCase +{ + /** + * @var MockObject + */ + private $rollbarNotifier; + + /** + * @var array + */ + private $reportedExceptionArguments = null; + + protected function setUp() + { + parent::setUp(); + + $this->setupRollbarNotifierMock(); + } + + /** + * When reporting exceptions to Rollbar the + * level has to be set in the payload data + */ + public function testExceptionLogLevel() + { + $handler = $this->createHandler(); + + $handler->handle($this->createExceptionRecord(Logger::DEBUG)); + + $this->assertEquals('debug', $this->reportedExceptionArguments['payload']['level']); + } + + private function setupRollbarNotifierMock() + { + $this->rollbarNotifier = $this->getMockBuilder('RollbarNotifier') + ->setMethods(array('report_message', 'report_exception', 'flush')) + ->getMock(); + + $this->rollbarNotifier + ->expects($this->any()) + ->method('report_exception') + ->willReturnCallback(function ($exception, $context, $payload) { + $this->reportedExceptionArguments = compact('exception', 'context', 'payload'); + }); + } + + private function createHandler(): RollbarHandler + { + return new RollbarHandler($this->rollbarNotifier, Logger::DEBUG); + } + + private function createExceptionRecord($level = Logger::DEBUG, $message = 'test', $exception = null): array + { + return $this->getRecord($level, $message, [ + 'exception' => $exception ?? new Exception() + ]); + } +} From 3b1f98df2aa2ed6b71caba7d2087359dbb8325d1 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 20 Mar 2017 10:07:14 +0100 Subject: [PATCH 2/2] Fix 5.3 support --- tests/Monolog/Handler/RollbarHandlerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Monolog/Handler/RollbarHandlerTest.php b/tests/Monolog/Handler/RollbarHandlerTest.php index e691b1e7..f12062e3 100644 --- a/tests/Monolog/Handler/RollbarHandlerTest.php +++ b/tests/Monolog/Handler/RollbarHandlerTest.php @@ -75,8 +75,8 @@ class RollbarHandlerTest extends TestCase private function createExceptionRecord($level = Logger::DEBUG, $message = 'test', $exception = null): array { - return $this->getRecord($level, $message, [ - 'exception' => $exception ?? new Exception() - ]); + return $this->getRecord($level, $message, array( + 'exception' => $exception ?: new Exception() + )); } }