Skip to content

Commit 1e38928

Browse files
committed
add test cases
1 parent 5229f18 commit 1e38928

File tree

4 files changed

+110
-6
lines changed

4 files changed

+110
-6
lines changed

src/DependencyInjection/Compiler/SentryBufferFlushPass.php renamed to src/DependencyInjection/Compiler/BufferFlushPass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
use Monolog\Handler\BufferHandler;
88
use Sentry\Monolog\Handler as SentryHandler;
9-
use Sentry\SentryBundle\EventListener\SentryBufferFlusher;
9+
use Sentry\SentryBundle\EventListener\BufferFlusher;
1010
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1111
use Symfony\Component\DependencyInjection\ContainerBuilder;
1212
use Symfony\Component\DependencyInjection\Definition;
1313
use Symfony\Component\DependencyInjection\Reference;
1414

15-
class SentryBufferFlushPass implements CompilerPassInterface
15+
class BufferFlushPass implements CompilerPassInterface
1616
{
1717
public function process(ContainerBuilder $container): void
1818
{
@@ -22,7 +22,7 @@ public function process(ContainerBuilder $container): void
2222
return;
2323
}
2424

25-
$flusherDefinition = new Definition(SentryBufferFlusher::class);
25+
$flusherDefinition = new Definition(BufferFlusher::class);
2626
$flusherDefinition->setArguments([$sentryBufferHandlers]);
2727
$flusherDefinition->addTag('kernel.event_subscriber');
2828

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Symfony\Component\HttpKernel\Event\TerminateEvent;
1212
use Symfony\Component\HttpKernel\KernelEvents;
1313

14-
class SentryBufferFlusher implements EventSubscriberInterface
14+
class BufferFlusher implements EventSubscriberInterface
1515
{
1616
/**
1717
* @var BufferHandler[]

src/SentryBundle.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Sentry\SentryBundle\DependencyInjection\Compiler\CacheTracingPass;
99
use Sentry\SentryBundle\DependencyInjection\Compiler\DbalTracingPass;
1010
use Sentry\SentryBundle\DependencyInjection\Compiler\HttpClientTracingPass;
11-
use Sentry\SentryBundle\DependencyInjection\Compiler\SentryBufferFlushPass;
11+
use Sentry\SentryBundle\DependencyInjection\Compiler\BufferFlushPass;
1212
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1313
use Symfony\Component\DependencyInjection\ContainerBuilder;
1414
use Symfony\Component\HttpKernel\Bundle\Bundle;
@@ -27,6 +27,6 @@ public function build(ContainerBuilder $container): void
2727
$container->addCompilerPass(new CacheTracingPass());
2828
$container->addCompilerPass(new HttpClientTracingPass());
2929
$container->addCompilerPass(new AddLoginListenerTagPass());
30-
$container->addCompilerPass(new SentryBufferFlushPass());
30+
$container->addCompilerPass(new BufferFlushPass());
3131
}
3232
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\DependencyInjection\Compiler;
6+
7+
use Monolog\Handler\BufferHandler;
8+
use Monolog\Handler\NullHandler;
9+
use PHPUnit\Framework\TestCase;
10+
use Sentry\Monolog\Handler as SentryHandler;
11+
use Sentry\SentryBundle\DependencyInjection\Compiler\BufferFlushPass;
12+
use Symfony\Component\DependencyInjection\ContainerBuilder;
13+
use Symfony\Component\DependencyInjection\Definition;
14+
use Symfony\Component\DependencyInjection\Reference;
15+
16+
class BufferFlushPassTest extends TestCase
17+
{
18+
/**
19+
* @param Reference[] $services
20+
* @return string[]
21+
*/
22+
private function servicesToName(array $services): array
23+
{
24+
return array_map(function ($item) {
25+
return (string) $item;
26+
}, $services);
27+
}
28+
29+
/**
30+
* Tests that the flusher will only container references to handler that wrap sentry.
31+
*
32+
* @return void
33+
*/
34+
public function testProcessWithMultipleHandlers()
35+
{
36+
$container = new ContainerBuilder();
37+
$container->setDefinition('sentry.handler', new Definition(SentryHandler::class));
38+
$container->setDefinition('null.handler', new Definition(NullHandler::class));
39+
$container->setDefinition('sentry.test.handler', new Definition(BufferHandler::class, [new Reference('sentry.handler')]));
40+
$container->setDefinition('other.test.handler', new Definition(BufferHandler::class, [new Reference('null.handler')]));
41+
42+
(new BufferFlushPass())->process($container);
43+
$definition = $container->getDefinition('sentry.buffer_flusher');
44+
$serviceIds = $this->servicesToName($definition->getArgument(0));
45+
$this->assertEquals(['sentry.test.handler'], $serviceIds);
46+
}
47+
48+
/**
49+
* Tests that if no sentry handlers exist, there is also no flusher.
50+
*
51+
* @return void
52+
*/
53+
public function testProcessWithoutSentryHandler()
54+
{
55+
$container = new ContainerBuilder();
56+
$container->setDefinition('null.handler', new Definition(NullHandler::class));
57+
$container->setDefinition('other.test.handler', new Definition(BufferHandler::class, [new Reference('null.handler')]));
58+
59+
(new BufferFlushPass())->process($container);
60+
$this->assertFalse($container->hasDefinition('sentry.buffer_flusher'));
61+
}
62+
63+
/**
64+
* Tests that even if there are multiple sentry handler (for some reason), it will only
65+
* collect them and no others.
66+
*
67+
* @return void
68+
*/
69+
public function testProcessWithMultipleSentryHandlers()
70+
{
71+
$container = new ContainerBuilder();
72+
$container->setDefinition('sentry.handler', new Definition(SentryHandler::class));
73+
$container->setDefinition('sentry.other.handler', new Definition(SentryHandler::class));
74+
$container->setDefinition('null.handler', new Definition(NullHandler::class));
75+
$container->setDefinition('sentry.test.handler', new Definition(BufferHandler::class, [new Reference('sentry.handler')]));
76+
$container->setDefinition('sentry.other.test.handler', new Definition(BufferHandler::class, [new Reference('sentry.other.handler')]));
77+
$container->setDefinition('other.test.handler', new Definition(BufferHandler::class, [new Reference('null.handler')]));
78+
79+
(new BufferFlushPass())->process($container);
80+
$definition = $container->getDefinition('sentry.buffer_flusher');
81+
$serviceIds = $this->servicesToName($definition->getArgument(0));
82+
$this->assertEquals(['sentry.test.handler', 'sentry.other.test.handler'], $serviceIds);
83+
}
84+
85+
/**
86+
* Tests that handlers that are named sentry will not be flushed because the matching happens by class
87+
* name and not by service id.
88+
*
89+
* @return void
90+
*/
91+
public function testProcessWithFakeSentryHandlers()
92+
{
93+
$container = new ContainerBuilder();
94+
$container->setDefinition('sentry.handler', new Definition(SentryHandler::class));
95+
$container->setDefinition('sentry.fake.handler', new Definition(NullHandler::class));
96+
$container->setDefinition('sentry.test.handler', new Definition(BufferHandler::class, [new Reference('sentry.handler')]));
97+
$container->setDefinition('sentry.fake.test.handler', new Definition(BufferHandler::class, [new Reference('null.handler')]));
98+
99+
(new BufferFlushPass())->process($container);
100+
$definition = $container->getDefinition('sentry.buffer_flusher');
101+
$serviceIds = $this->servicesToName($definition->getArgument(0));
102+
$this->assertEquals(['sentry.test.handler'], $serviceIds);
103+
}
104+
}

0 commit comments

Comments
 (0)