diff --git a/EventListener/AbstractListener.php b/EventListener/AbstractListener.php deleted file mode 100755 index acfe41a..0000000 --- a/EventListener/AbstractListener.php +++ /dev/null @@ -1,101 +0,0 @@ -container = $container; - $this->logger = $logger; - $this->generator = $generator; - } - - /** - * Get logger. - * - * @return Logger - */ - public function getLogger() - { - return $this->logger; - } - - /** - * {@inheritdoc} - */ - public static function getSubscribedEvents() - { - return [ - KernelEvents::EXCEPTION => ['onKernelException', 1], - ]; - } - - /** - * On kernel exception event handler. - * - * @param GetResponseForExceptionEvent $event - */ - public function onKernelException(GetResponseForExceptionEvent $event) - { - // dummy - } - - /** - * Get container. - * - * @return ContainerInterface - */ - public function getContainer() - { - return $this->container; - } - - /** - * Get generator. - * - * @return Generator - */ - public function getGenerator() - { - return $this->generator; - } -} diff --git a/EventListener/ErrorListener.php b/EventListener/ErrorListener.php deleted file mode 100755 index 2d3b925..0000000 --- a/EventListener/ErrorListener.php +++ /dev/null @@ -1,110 +0,0 @@ -isReportable($code)) { - return; - } - - list($message, $payload) = $this->getGenerator()->getErrorPayload($code, $message, $file, $line); - - $this->getLogger()->error($message, [ - 'payload' => $payload, - ]); - } - - /** - * Process fatal errors - */ - public function handleFatalError() - { - $error = $this->getLastError(); - if (empty($error)) { - return; - } - - // due to PHP docs we allways will have such structure for errors - $code = $error['type']; - $message = $error['message']; - $file = $error['file']; - $line = $error['line']; - - $this->handleError($code, $message, $file, $line); - } - - /** - * Wrap php error_get_last() to get more testable code - * - * @link: http://php.net/manual/en/function.error-get-last.php - * - * @return array|null - * - * @codeCoverageIgnore - */ - protected function getLastError() - { - return error_get_last(); - } - - /** - * Check do we need to report error or skip - * - * @param mixed $code - * - * @return int - */ - protected function isReportable($code) - { - $code = (int) $code; - $config = $this->getContainer()->getParameter(RollbarExtension::ALIAS . '.config'); - - return true - && $config['enabled'] - && !(error_reporting() === 0 && $config['report_suppressed']) - && !(($config['use_error_reporting'] && (error_reporting() & $code) === 0)) - && !($config['included_errno'] != -1 && ($code & $config['included_errno']) != $code); - } -} diff --git a/EventListener/ExceptionListener.php b/EventListener/ExceptionListener.php deleted file mode 100755 index ec62d88..0000000 --- a/EventListener/ExceptionListener.php +++ /dev/null @@ -1,45 +0,0 @@ -getException(); - - if ($exception instanceof \Exception - || (version_compare(PHP_VERSION, '7.0.0') >= 0 && $exception instanceof \Error) - ) { - $this->handleException($exception); - } - } - - /** - * Handle provided exception - * - * @param mixed $exception - */ - public function handleException($exception) - { - // generate payload and log data - list($message, $payload) = $this->getGenerator()->getExceptionPayload($exception); - - $this->getLogger()->error($message, [ - 'payload' => $payload, - 'exception' => $exception, - ]); - } -} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 1e3a2d4..4b8e19a 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,19 +1,4 @@ services: - Rollbar\Symfony\RollbarBundle\EventListener\ExceptionListener: - arguments: - - '@service_container' - - '@logger' - - '@Rollbar\Symfony\RollbarBundle\Payload\Generator' - tags: - - { name: kernel.event_listener, event: kernel.exception, priority: -100 } - - Rollbar\Symfony\RollbarBundle\EventListener\ErrorListener: - arguments: - - '@service_container' - - '@logger' - - '@Rollbar\Symfony\RollbarBundle\Payload\Generator' - tags: - - { name: kernel.event_listener, event: kernel.exception } Rollbar\Symfony\RollbarBundle\Factories\RollbarHandlerFactory: arguments: diff --git a/Tests/EventListener/ErrorListenerTest.php b/Tests/EventListener/ErrorListenerTest.php deleted file mode 100755 index 60cc24a..0000000 --- a/Tests/EventListener/ErrorListenerTest.php +++ /dev/null @@ -1,171 +0,0 @@ -getContainer(); - $eventDispatcher = $container->get('event_dispatcher'); - $listeners = $eventDispatcher->getListeners('kernel.exception'); - $handler = ErrorHandler::getInstance(); - - $handler->setAssert(function (array $record) use ($message) { - $this->assertNotEmpty($record); - - $this->assertEquals($message, $record['message']); - $this->assertEquals(Logger::ERROR, $record['level']); - $this->assertTrue(!empty($record['context']['payload']['body']['trace']['exception']['class'])); - - $trace = $record['context']['payload']['body']['trace']; - $this->assertEquals('E_USER_ERROR', $trace['exception']['class']); - $this->assertNotEmpty($trace['frames']); - }); - - foreach ($listeners as $listener) { - if (!$listener[0] instanceof AbstractListener) { - continue; - } - - $listener[0]->getLogger()->setHandlers([$handler]); - } - - trigger_error($message, E_USER_ERROR); - - $handler->setAssert(null); - } - - /** - * Test fatal error parser. - * - * @dataProvider generateFatalError - * - * @param array $error - * @param bool $called - */ - public function testFatalErrorParser($error, $called) - { - $mock = $this->getMockBuilder(ErrorListener::class) - ->setMethods(['getLastError', 'handleError']) - ->disableOriginalConstructor() - ->getMock(); - - $mock->method('getLastError') - ->willReturn($error); - - $mock->expects($called ? $this->once() : $this->never()) - ->method('handleError') - ->with( - $this->equalTo($error['type']), - $this->stringContains($error['message']), - $this->stringContains($error['file']), - $this->equalTo($error['line']) - ); - - /** @var ErrorListener $mock */ - $mock->handleFatalError(); - } - - /** - * Data provider generate fatal error. - * - * @return array - */ - public function generateFatalError() - { - return [ - [['type' => E_ERROR, 'message' => 'Error message', 'file' => __DIR__, 'line' => rand(10, 100)], true], - [null, false] - ]; - } - - /** - * Test isReportable. - * - * @dataProvider generateIsReportable - * - * @param bool $called - */ - public function testIsReportable($called) - { - $container = $this->getContainer(); - $generator = $container->get('test.' . Generator::class); - - $logger = $this->getMockBuilder(Logger::class) - ->setMethods(['error']) - ->setConstructorArgs(['test-alias']) - ->getMock(); - - $logger->method('error') - ->willReturn(true); - - $mock = $this->getMockBuilder(ErrorListener::class) - ->setMethods(['isReportable', 'getGenerator', 'getLogger']) - ->disableOriginalConstructor() - ->getMock(); - - $mock->method('isReportable') - ->willReturn($called); - - $mock->method('getGenerator') - ->willReturn($generator); - - $mock->method('getLogger') - ->willReturn($logger); - - /** @var ErrorListener $mock */ - $mock->handleError(E_ERROR, 'Message', __FILE__, rand(1, 10)); - } - - /** - * Data provider for testIsReportable. - * - * @return array - */ - public function generateIsReportable() - { - return [ - [true], - [false] - ]; - } - - /** - * Get container. - * - * @return ContainerInterface - */ - private function getContainer() - { - return isset(static::$container) ? static::$container : static::$kernel->getContainer(); - } -} diff --git a/Tests/EventListener/ExceptionListenerTest.php b/Tests/EventListener/ExceptionListenerTest.php deleted file mode 100755 index 55d3853..0000000 --- a/Tests/EventListener/ExceptionListenerTest.php +++ /dev/null @@ -1,55 +0,0 @@ -getContainer(); - $eventDispatcher = $container->get('event_dispatcher'); - $exception = new \Exception('This is new exception'); - $event = new GetResponseForExceptionEvent( - static::$kernel, - new Request(), - HttpKernelInterface::MASTER_REQUEST, - $exception - ); - - $eventDispatcher->dispatch('kernel.exception', $event); - } - - /** - * Get container. - * - * @return ContainerInterface - */ - private function getContainer() - { - return isset(static::$container) ? static::$container : static::$kernel->getContainer(); - } -} diff --git a/Tests/RollbarBundleTest.php b/Tests/RollbarBundleTest.php deleted file mode 100755 index 619639b..0000000 --- a/Tests/RollbarBundleTest.php +++ /dev/null @@ -1,54 +0,0 @@ -getContainer(); - $eventDispatcher = $container->get('event_dispatcher'); - $listeners = $eventDispatcher->getListeners('kernel.exception'); - $listeners = array_merge( - $listeners, - $eventDispatcher->getListeners('kernel.controller') - ); - - $expectedListeners = [ - ErrorListener::class, - ExceptionListener::class, - ]; - - foreach ($listeners as $listener) { - foreach ($expectedListeners as $key => $expectedListener) { - if ($listener[0] instanceof $expectedListener) { - unset($expectedListeners[$key]); - } - } - } - - $this->assertEmpty($expectedListeners, 'Listeners were not registered'); - } -} diff --git a/composer.json b/composer.json index 6a98504..8f0d7d6 100755 --- a/composer.json +++ b/composer.json @@ -43,9 +43,9 @@ "scripts": { "test": [ "composer install", - "./vendor/bin/phpcs --standard=psr2 DependencyInjection/ EventListener/ Factories/ Payload/ Tests/DependencyInjection/ Tests/EventListener/ Tests/Payload/ Tests/RollbarBundleTest.php", + "./vendor/bin/phpcs --standard=psr2 DependencyInjection/ Factories/ Payload/ Tests/DependencyInjection/ Tests/Payload/", "./vendor/bin/phpunit -c ." ], - "fix": "phpcbf --standard=PSR1,PSR2 DependencyInjection/ EventListener/ Factories/ Payload/ Tests/DependencyInjection/ Tests/EventListener/ Tests/Payload/ Tests/RollbarBundleTest.php" + "fix": "phpcbf --standard=PSR1,PSR2 DependencyInjection/ Factories/ Payload/ Tests/DependencyInjection/ Tests/Payload/" } }