Skip to content

Commit 936fe1b

Browse files
committed
fix configuration, extend tests
1 parent fb1962c commit 936fe1b

File tree

11 files changed

+178
-23
lines changed

11 files changed

+178
-23
lines changed

DependencyInjection/Configuration.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ public function getConfigTreeBuilder()
5555
$rootNode
5656
->children()
5757
->scalarNode('enable')->defaultTrue()->end()
58-
->scalarNode('exclude')->defaultValue(static::$exclude)->end()
58+
->arrayNode('exclude')
59+
->treatNullLike([])
60+
->prototype('scalar')->end()
61+
->defaultValue(static::$exclude)
62+
->end()
5963
->arrayNode('rollbar')->children()
6064
->scalarNode('access_token')->defaultValue('')->end()
6165
->scalarNode('agent_log_location')->defaultValue('%kernel.logs_dir%/rollbar.log')->end()
@@ -68,9 +72,21 @@ public function getConfigTreeBuilder()
6872
->scalarNode('code_version')->defaultValue('')->end()
6973
->scalarNode('enable_utf8_sanitization')->defaultTrue()->end()
7074
->scalarNode('environment')->defaultValue(static::ENVIRONMENT)->end()
71-
->scalarNode('custom')->defaultValue([])->end()
72-
->scalarNode('error_sample_rates')->defaultValue([])->end()
73-
->scalarNode('exception_sample_rates')->defaultValue([])->end()
75+
->arrayNode('custom')
76+
->treatNullLike([])
77+
->prototype('scalar')->end()
78+
->defaultValue([])
79+
->end()
80+
->arrayNode('error_sample_rates')
81+
->treatNullLike([])
82+
->prototype('scalar')->end()
83+
->defaultValue([])
84+
->end()
85+
->arrayNode('exception_sample_rates')
86+
->treatNullLike([])
87+
->prototype('scalar')->end()
88+
->defaultValue([])
89+
->end()
7490
->scalarNode('fluent_host')->defaultValue(static::FLUENT_HOST)->end()
7591
->scalarNode('fluent_port')->defaultValue(static::FLUENT_PORT)->end()
7692
->scalarNode('fluent_tag')->defaultValue(static::FLUENT_TAG)->end()
@@ -80,10 +96,18 @@ public function getConfigTreeBuilder()
8096
->scalarNode('include_exception_code_context')->defaultFalse()->end()
8197
->scalarNode('included_errno')->defaultValue($defaultErrorMask)->end()
8298
->scalarNode('logger')->defaultNull()->end()
83-
->scalarNode('person')->defaultValue([])->end()
99+
->arrayNode('person')
100+
->treatNullLike([])
101+
->prototype('scalar')->end()
102+
->defaultValue([])
103+
->end()
84104
->scalarNode('person_fn')->defaultNull()->end()
85105
->scalarNode('root')->defaultValue('%kernel.root_dir%')->end()
86-
->scalarNode('scrub_fields')->defaultValue(static::$scrubFieldsDefault)->end()
106+
->arrayNode('scrub_fields')
107+
->treatNullLike([])
108+
->prototype('scalar')->end()
109+
->defaultValue(static::$scrubFieldsDefault)
110+
->end()
87111
->scalarNode('scrub_whitelist')->defaultNull()->end()
88112
->scalarNode('shift_function')->defaultTrue()->end()
89113
->scalarNode('timeout')->defaultValue(static::TIMEOUT)->end()

EventListener/AbstractListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ abstract class AbstractListener implements EventSubscriberInterface
2525
*/
2626
protected $container;
2727

28+
/**
29+
* @var \SymfonyRollbarBundle\Provider\RollbarHandler
30+
*/
31+
protected $handler;
32+
2833
/**
2934
* @var array
3035
*/
@@ -47,6 +52,7 @@ public function __construct(ContainerInterface $container)
4752
$config = $this->getContainer()->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
4853
$this->exclude = empty($config['exclude']) ? [] : $config['exclude'];
4954

55+
$this->handler = $rbHandler;
5056
$this->getLogger()->pushHandler($rbHandler);
5157
}
5258

EventListener/ExceptionListener.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,10 @@ public function onKernelException(GetResponseForExceptionEvent $event)
4242
*/
4343
public function handleException($exception)
4444
{
45-
if ($exception instanceof \Exception) {
46-
// check exception
47-
foreach ($this->exclude as $instance) {
48-
if (class_exists($instance) && $exception instanceof $instance) {
49-
return;
50-
}
51-
}
45+
if ($exception instanceof \Exception && $this->handler->shouldSkip($exception)) {
46+
return;
5247
}
5348

54-
5549
$payload = [];
5650
// @link http://php.net/manual/en/reserved.constants.php
5751
// @link http://php.net/manual/en/language.errors.php7.php

Tests/Fixtures/ErrorHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
namespace Tests\Fixtures;
3+
namespace SymfonyRollbarBundle\Tests\Fixtures;
44

55
use Monolog\Handler\AbstractProcessingHandler;
66
use Monolog\Logger;
77

88
class ErrorHandler extends AbstractProcessingHandler
99
{
1010
/**
11-
* @var \Tests\Fixtures\ErrorHandler
11+
* @var \SymfonyRollbarBundle\Tests\Fixtures\ErrorHandler
1212
*/
1313
protected static $instance;
1414

@@ -18,7 +18,7 @@ class ErrorHandler extends AbstractProcessingHandler
1818
protected $assert;
1919

2020
/**
21-
* @return \Tests\Fixtures\ErrorHandler
21+
* @return \SymfonyRollbarBundle\Tests\Fixtures\ErrorHandler
2222
*/
2323
public static function getInstance()
2424
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace SymfonyRollbarBundle\Tests\Fixtures;
4+
5+
/**
6+
* Class MyAwesomeException
7+
* @package SymfonyRollbarBundle\Tests\Fixtures
8+
*/
9+
class MyAwesomeException extends \Exception
10+
{
11+
12+
}

Tests/Fixtures/app/config/config_test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ imports:
44

55
symfony_rollbar:
66
enable: true
7+
exclude:
8+
- \Symfony\Component\Debug\Exception\FatalErrorException
9+
- \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException
710
rollbar:
811
access_token: '%env(ROLLBAR_ACCESS_TOKEN)%'
912
environment: '%kernel.environment%'

Tests/SymfonyRollbarBundle/DependencyInjection/ConfigurationTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ public function testParameters()
2020
$config = $container->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
2121
$defaultErrorMask = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
2222

23+
$exclude = Configuration::$exclude;
24+
$exclude[] = '\SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException';
25+
2326
$default = [
2427
'enable' => true,
25-
'exclude' => Configuration::$exclude,
28+
'exclude' => $exclude,
2629
'rollbar' => [
2730
'access_token' => getenv('ROLLBAR_ACCESS_TOKEN'),
2831
'agent_log_location' => static::$kernel->getLogDir() . '/rollbar.log',

Tests/SymfonyRollbarBundle/DependencyInjection/SymfonyRollbarExtensionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ public function testConfigEnabledVars($var, $value)
4141

4242
public function generatorConfigVars()
4343
{
44+
$exclude = Configuration::$exclude;
45+
4446
return [
4547
['symfony_rollbar.event_listener.exception_listener.class', ExceptionListener::class],
4648
['symfony_rollbar.event_listener.error_listener.class', ErrorListener::class],
4749
['symfony_rollbar.provider.rollbar_handler.class', RollbarHandler::class],
48-
['symfony_rollbar.config', ['enable' => true, 'exclude' => Configuration::$exclude]],
50+
['symfony_rollbar.config', ['enable' => true, 'exclude' => $exclude]],
4951
];
5052
}
5153

Tests/SymfonyRollbarBundle/EventListener/ErrorListenerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use SymfonyRollbarBundle\EventListener\AbstractListener;
99
use SymfonyRollbarBundle\EventListener\ErrorListener;
1010
use SymfonyRollbarBundle\Provider\RollbarHandler;
11+
use SymfonyRollbarBundle\Tests\Fixtures\ErrorHandler;
1112

1213
/**
1314
* Class ErrorListenerTest
@@ -33,7 +34,7 @@ public function testUserError()
3334
*/
3435
$eventDispatcher = $container->get('event_dispatcher');
3536
$listeners = $eventDispatcher->getListeners('kernel.exception');
36-
$handler = \Tests\Fixtures\ErrorHandler::getInstance();
37+
$handler = ErrorHandler::getInstance();
3738

3839
$handler->setAssert(function (array $record) use ($message) {
3940
$this->assertNotEmpty($record);
@@ -73,7 +74,7 @@ public function testFatalError()
7374
*/
7475
$eventDispatcher = $container->get('event_dispatcher');
7576
$listeners = $eventDispatcher->getListeners('kernel.exception');
76-
$handler = \Tests\Fixtures\ErrorHandler::getInstance();
77+
$handler = ErrorHandler::getInstance();
7778
$rbHandler = new RollbarHandler($container);
7879

7980
$handler->setAssert(function (array $record) use ($rbHandler) {

Tests/SymfonyRollbarBundle/EventListener/ExceptionListenerTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
use \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
99
use Symfony\Component\HttpKernel\HttpKernelInterface;
1010
use SymfonyRollbarBundle\EventListener\AbstractListener;
11+
use SymfonyRollbarBundle\EventListener\ExceptionListener;
1112
use SymfonyRollbarBundle\Provider\RollbarHandler;
12-
use Tests\Fixtures\ErrorHandler;
13+
use SymfonyRollbarBundle\Tests\Fixtures\ErrorHandler;
14+
use SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException;
1315

1416
/**
1517
* Class ExceptionListenerTest
@@ -105,7 +107,7 @@ public function testInvalidHandleParams($data)
105107
$this->assertInstanceOf(\Exception::class, $exception);
106108
});
107109

108-
$listener = new \SymfonyRollbarBundle\EventListener\ExceptionListener($container);
110+
$listener = new ExceptionListener($container);
109111
$listener->getLogger()->setHandlers([$handler]);
110112

111113
$listener->handleException($data);
@@ -123,4 +125,40 @@ public function generatorRandomParams()
123125
[(object)['a' => 'b']],
124126
];
125127
}
128+
129+
public function testSkipException()
130+
{
131+
$container = static::$kernel->getContainer();
132+
133+
/**
134+
* @var TraceableEventDispatcher $eventDispatcher
135+
*/
136+
$eventDispatcher = $container->get('event_dispatcher');
137+
$listeners = $eventDispatcher->getListeners('kernel.exception');
138+
$event = new GetResponseForExceptionEvent(
139+
static::$kernel,
140+
new Request(),
141+
HttpKernelInterface::MASTER_REQUEST,
142+
new MyAwesomeException("Hello!")
143+
);
144+
145+
$handler = new ErrorHandler();
146+
$handler->setAssert(function ($record) {
147+
$this->fail("This should be newer called!");
148+
});
149+
150+
foreach ($listeners as $listener) {
151+
/**
152+
* @var AbstractListener $listener
153+
*/
154+
if (!$listener[0] instanceof AbstractListener) {
155+
continue;
156+
}
157+
158+
$listener[0]->getLogger()->setHandlers([$handler]);
159+
}
160+
161+
$eventDispatcher->dispatch('kernel.exception', $event);
162+
restore_error_handler();
163+
}
126164
}

0 commit comments

Comments
 (0)