Skip to content

Commit

Permalink
updating configuration to include enabled flag and dsn
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjohnson00 committed Oct 23, 2013
1 parent 6ffd700 commit bc97d46
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 30 deletions.
9 changes: 5 additions & 4 deletions DependencyInjection/Configuration.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public function getConfigTreeBuilder()
$rootNode = $treeBuilder->root('kunstmaan_sentry');
$rootNode
->children()
->arrayNode('environments')
->booleanNode('enabled')
->defaultValue(false)
->end()
->scalarNode('dsn')
->cannotBeEmpty()
->defaultValue(array('prod'))
->prototype('scalar')
->end()
->end()
->end();

// Here you should define the parameters that are allowed to
Expand Down
3 changes: 2 additions & 1 deletion DependencyInjection/KunstmaanSentryExtension.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$container->setParameter($this->getAlias(). '.environments', $config['environments']);
$container->setParameter($this->getAlias(). '.enabled', $config['enabled']);
$container->setParameter($this->getAlias(). '.dsn', $config['dsn']);

}
}
10 changes: 5 additions & 5 deletions EventListener/ExceptionListener.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ class ExceptionListener
protected $client;

/**
* @var array $environments
* @var boolean $enabled
*/
protected $environments;
protected $enabled;

/**
* @param Raven $client
*/
public function __construct(Raven $client, $environments)
public function __construct(Raven $client, $enabled)
{
$this->client = $client;
$this->environments = $environments;
$this->enabled = $enabled;
}

/**
Expand All @@ -44,7 +44,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
if ($event->getRequest()->attributes->has("_controller")) {
$culprit = $event->getRequest()->attributes->get("_controller");
}
if (!in_array($this->client->getEnvironment(), $this->environments)) {
if (!$this->enabled) {
return array($exception, $culprit, $this->client->getEnvironment());
} else {
$event_id = $this->client->getIdent($this->client->captureException($exception, $culprit, $this->client->getEnvironment()));
Expand Down
2 changes: 1 addition & 1 deletion KunstmaanSentryBundle.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class KunstmaanSentryBundle extends Bundle
*/
public function boot()
{
if (!in_array($this->container->getParameter('kernel.environment'), $this->container->getParameter('kunstmaan_sentry.environments'))) {
if (!$this->container->getParameter('kunstmaan_sentry.enabled')) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="sentry.dsn"></parameter>
<parameter key="sentry.dsn">%kunstmaan_sentry.dsn%</parameter>
<parameter key="sentry.client.class">Kunstmaan\SentryBundle\Raven\Raven</parameter>
<parameter key="sentry.exception_listener.class">Kunstmaan\SentryBundle\EventListener\ExceptionListener</parameter>
<parameter key="sentry.shutdown_listener.class">Kunstmaan\SentryBundle\EventListener\ShutdownListener</parameter>
Expand All @@ -20,7 +20,7 @@
<service id="sentry.exception_listener" class="%sentry.exception_listener.class%">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" />
<argument type="service" id="sentry.client" />
<argument>%kunstmaan_sentry.environments%</argument>
<argument>%kunstmaan_sentry.enabled%</argument>
</service>
</services>
</container>
23 changes: 6 additions & 17 deletions Tests/EventListener/ExceptionListenerTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,34 @@
class ExceptionListenerTest extends \PHPUnit_Framework_TestCase
{

private function getListnerResult($environments, $currentEnv = 'test')
private function getListnerResult($enabled, $currentEnv = 'test')
{
$kernel = new \TestKernel($currentEnv , false);
$raven = new Raven('http://public:[email protected]/1', $kernel->getEnvironment());
$listener = new ExceptionListener($raven, $environments);
$listener = new ExceptionListener($raven, $enabled);
$request = new Request();
$event = new GetResponseForExceptionEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, new Exception("Test"));
return $result = $listener->onKernelException($event);
}

/**
* @dataProvider unhandledEnvironmentsProvider
* @covers \Kunstmaan\SentryBundle\EventListener\ExceptionListener
*/
public function testExceptionListenerWithUnhandledEnv($environments)
public function testExceptionListenerWithUnhandledEnv()
{
$result = $this->getListnerResult($environments, 'test');
$result = $this->getListnerResult(false, 'test');
$this->assertTrue($result[0] instanceof Exception);
$this->assertEmpty($result[1]);
$this->assertEquals($result[2], 'test');
}

/**
* @dataProvider unhandledEnvironmentsProvider
* @covers \Kunstmaan\SentryBundle\EventListener\ExceptionListener
*/
public function testExceptionListenerWithHandledEnv($environments)
public function testExceptionListenerWithHandledEnv()
{
ini_set('error_log','/dev/null');
$result = $this->getListnerResult($environments, 'prod');
$result = $this->getListnerResult(true, 'prod');
$this->assertTrue($result);
}

public static function unhandledEnvironmentsProvider()
{
return array(
array(array('prod', 'dev')),
array(array('prod', 'dev', 'stage')),
array(array('prod', 'dev', 'my_env'))
);
}
}

0 comments on commit bc97d46

Please sign in to comment.