Skip to content

Commit 1b8c381

Browse files
committed
Add requirement that exception listener implement interface
Per suggestions from others in the PR, and with their help, I've added in a check that will validate that the exception listener implements the required interface. In addition, I've updated the tests and added a new test to verify that this works as expected.
1 parent 812007b commit 1b8c381

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/Sentry/SentryBundle/DependencyInjection/Configuration.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function getConfigTreeBuilder()
4444
->end()
4545
->scalarNode('exception_listener')
4646
->defaultValue('Sentry\SentryBundle\EventListener\ExceptionListener')
47+
->validate()
48+
->ifTrue($this->getExceptionListenerInvalidationClosure())
49+
->thenInvalid('The "sentry.exception_listener" parameter should be a FQCN of a class implementing the SentryExceptionListenerInterface interface')
50+
->end()
4751
->end()
4852
->arrayNode('skip_capture')
4953
->treatNullLike(array())
@@ -72,4 +76,18 @@ public function getConfigTreeBuilder()
7276

7377
return $treeBuilder;
7478
}
79+
80+
/**
81+
* @return \Closure
82+
*/
83+
private function getExceptionListenerInvalidationClosure()
84+
{
85+
return function ($value) {
86+
$implements = class_implements($value);
87+
if ($implements === false) {
88+
return true;
89+
}
90+
return !in_array('Sentry\SentryBundle\EventListener\SentryExceptionListenerInterface', $implements, true);
91+
};
92+
}
7593
}

test/Sentry/SentryBundle/Test/DependencyInjection/ExtensionTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ public function test_that_it_uses_options_value()
133133
);
134134
}
135135

136+
/**
137+
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
138+
*/
139+
public function test_that_it_is_invalid_if_exception_listener_fails_to_implement_required_interface()
140+
{
141+
$class = 'Sentry\SentryBundle\Test\Fixtures\InvalidExceptionListener';
142+
$container = $this->getContainer(array(
143+
static::CONFIG_ROOT => array(
144+
'exception_listener' => $class,
145+
),
146+
));
147+
}
148+
136149
public function test_that_it_uses_defined_class_as_exception_listener_class_by_default()
137150
{
138151
$container = $this->getContainer();
@@ -145,14 +158,15 @@ public function test_that_it_uses_defined_class_as_exception_listener_class_by_d
145158

146159
public function test_that_it_uses_exception_listener_value()
147160
{
161+
$class = 'Sentry\SentryBundle\Test\Fixtures\CustomExceptionListener';
148162
$container = $this->getContainer(array(
149163
static::CONFIG_ROOT => array(
150-
'exception_listener' => 'exceptionListenerClass',
164+
'exception_listener' => $class,
151165
),
152166
));
153167

154168
$this->assertSame(
155-
'exceptionListenerClass',
169+
$class,
156170
$container->getParameter('sentry.exception_listener')
157171
);
158172
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Test\Fixtures;
4+
5+
use Sentry\SentryBundle\EventListener\ExceptionListener;
6+
7+
/**
8+
* @package Sentry\SentryBundle\Tests\Fixtures
9+
*/
10+
class CustomExceptionListener extends ExceptionListener
11+
{
12+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Test\Fixtures;
4+
5+
/**
6+
* An invalid <code>ExceptionListener</code> for testing.
7+
*
8+
* This exception listener does not implement, or extend a class that
9+
* implements, the appropriate interface that the dependency injection
10+
* container requires.
11+
*
12+
* @package Sentry\SentryBundle\Tests\Fixtures
13+
*/
14+
class InvalidExceptionListener
15+
{
16+
}

0 commit comments

Comments
 (0)