Skip to content

Commit e89d26c

Browse files
committed
Add PHPUnit tests
1 parent 99e8ee3 commit e89d26c

File tree

8 files changed

+889
-10
lines changed

8 files changed

+889
-10
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
examples/ export-ignore
2+
test/ export-ignore

.php_cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
$finder = Symfony\CS\Finder\DefaultFinder::create()
44
->in(__DIR__ . '/src')
5+
->in(__DIR__ . '/test')
56
;
67

78
return Symfony\CS\Config\Config::create()

phpunit.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="test/bootstrap.php">
3+
<php>
4+
</php>
5+
<testsuites>
6+
<testsuite name="SentryBundle test suite">
7+
<directory suffix="Test.php">./test</directory>
8+
</testsuite>
9+
</testsuites>
10+
</phpunit>

src/Sentry/SentryBundle/EventListener/ExceptionListener.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\Component\HttpKernel\HttpKernelInterface;
1010
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1111
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
12+
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
1213
use Symfony\Component\Security\Core\User\UserInterface;
1314
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
1415

@@ -77,7 +78,8 @@ public function onKernelRequest(GetResponseEvent $event)
7778
}
7879

7980
$token = $this->tokenStorage->getToken();
80-
if (null !== $token && $this->authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
81+
82+
if (null !== $token && $this->authorizationChecker->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED)) {
8183
$this->setUserValue($token->getUser());
8284
}
8385
}
@@ -88,7 +90,6 @@ public function onKernelRequest(GetResponseEvent $event)
8890
public function onKernelException(GetResponseForExceptionEvent $event)
8991
{
9092
$exception = $event->getException();
91-
9293
foreach ($this->skipCapture as $className) {
9394
if ($exception instanceof $className) {
9495
return;
@@ -121,14 +122,18 @@ public function onConsoleException(ConsoleExceptionEvent $event)
121122
*/
122123
private function setUserValue($user)
123124
{
124-
switch (true) {
125-
case $user instanceof UserInterface:
126-
$this->client->set_user_data($user->getUsername());
127-
return;
128-
case is_object($user):
129-
case is_string($user):
130-
$this->client->set_user_data((string) $user);
131-
return;
125+
if ($user instanceof UserInterface) {
126+
$this->client->set_user_data($user->getUsername());
127+
return;
128+
}
129+
130+
if (is_string($user)) {
131+
$this->client->set_user_data($user);
132+
return;
133+
}
134+
135+
if (is_object($user) && method_exists($user, '__toString')) {
136+
$this->client->set_user_data($user->__toString());
132137
}
133138
}
134139
}
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
<?php
2+
3+
namespace Sentry\SentryBundle\Test\DependencyInjection;
4+
5+
use Sentry\SentryBundle\DependencyInjection\SentryExtension;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
8+
class ExtensionTest extends \PHPUnit_Framework_TestCase
9+
{
10+
const CONFIG_ROOT = 'sentry';
11+
12+
public function test_that_it_uses_kernel_root_as_app_path_by_default()
13+
{
14+
$container = $this->getContainer();
15+
16+
$this->assertSame(
17+
'kernel/root',
18+
$container->getParameter('sentry.app_path')
19+
);
20+
}
21+
22+
public function test_that_it_uses_app_path_value()
23+
{
24+
$container = $this->getContainer([
25+
static::CONFIG_ROOT => array(
26+
'app_path' => 'sentry/app/path',
27+
),
28+
]);
29+
30+
$this->assertSame(
31+
'sentry/app/path',
32+
$container->getParameter('sentry.app_path')
33+
);
34+
}
35+
36+
public function test_that_it_uses_defined_class_as_client_class_by_default()
37+
{
38+
$container = $this->getContainer();
39+
40+
$this->assertSame(
41+
'Sentry\SentryBundle\SentrySymfonyClient',
42+
$container->getParameter('sentry.client')
43+
);
44+
}
45+
46+
public function test_that_it_uses_client_value()
47+
{
48+
$container = $this->getContainer([
49+
static::CONFIG_ROOT => array(
50+
'client' => 'clientClass',
51+
),
52+
]);
53+
54+
$this->assertSame(
55+
'clientClass',
56+
$container->getParameter('sentry.client')
57+
);
58+
}
59+
60+
public function test_that_it_uses_kernel_environment_as_environment_by_default()
61+
{
62+
$container = $this->getContainer();
63+
64+
$this->assertSame(
65+
'test',
66+
$container->getParameter('sentry.environment')
67+
);
68+
}
69+
70+
public function test_that_it_uses_environment_value()
71+
{
72+
$container = $this->getContainer(array(
73+
static::CONFIG_ROOT => array(
74+
'environment' => 'custom_env',
75+
),
76+
));
77+
78+
$this->assertSame(
79+
'custom_env',
80+
$container->getParameter('sentry.environment')
81+
);
82+
}
83+
84+
public function test_that_it_uses_null_as_dsn_default_value()
85+
{
86+
$container = $this->getContainer();
87+
88+
$this->assertSame(
89+
null,
90+
$container->getParameter('sentry.dsn')
91+
);
92+
}
93+
94+
public function test_that_it_uses_dsn_value()
95+
{
96+
$container = $this->getContainer([
97+
static::CONFIG_ROOT => [
98+
'dsn' => 'custom_dsn',
99+
],
100+
]);
101+
102+
$this->assertSame(
103+
'custom_dsn',
104+
$container->getParameter('sentry.dsn')
105+
);
106+
}
107+
108+
public function test_that_it_uses_defined_class_as_exception_listener_class_by_default()
109+
{
110+
$container = $this->getContainer();
111+
112+
$this->assertSame(
113+
'Sentry\SentryBundle\EventListener\ExceptionListener',
114+
$container->getParameter('sentry.exception_listener')
115+
);
116+
}
117+
118+
public function test_that_it_uses_exception_listener_value()
119+
{
120+
$container = $this->getContainer([
121+
static::CONFIG_ROOT => array(
122+
'exception_listener' => 'exceptionListenerClass',
123+
),
124+
]);
125+
126+
$this->assertSame(
127+
'exceptionListenerClass',
128+
$container->getParameter('sentry.exception_listener')
129+
);
130+
}
131+
132+
public function test_that_it_uses_array_with_http_exception_as_skipped_capture_by_default()
133+
{
134+
$container = $this->getContainer();
135+
136+
$this->assertSame(
137+
array(
138+
'Symfony\Component\HttpKernel\Exception\HttpExceptionInterface',
139+
),
140+
$container->getParameter('sentry.skip_capture')
141+
);
142+
}
143+
144+
public function test_that_it_uses_skipped_capture_value()
145+
{
146+
$container = $this->getContainer(array(
147+
static::CONFIG_ROOT => array(
148+
'skip_capture' => array(
149+
'classA',
150+
'classB',
151+
),
152+
),
153+
));
154+
155+
$this->assertSame(
156+
array('classA', 'classB'),
157+
$container->getParameter('sentry.skip_capture')
158+
);
159+
}
160+
161+
public function test_that_it_uses_null_as_release_by_default()
162+
{
163+
$container = $this->getContainer();
164+
165+
$this->assertSame(
166+
null,
167+
$container->getParameter('sentry.release')
168+
);
169+
}
170+
171+
public function test_that_it_uses_release_value()
172+
{
173+
$container = $this->getContainer(array(
174+
static::CONFIG_ROOT => array(
175+
'release' => '1.0',
176+
),
177+
));
178+
179+
$this->assertSame(
180+
'1.0',
181+
$container->getParameter('sentry.release')
182+
);
183+
}
184+
185+
public function test_that_it_uses_array_with_kernel_parent_as_prefix_by_default()
186+
{
187+
$container = $this->getContainer();
188+
189+
$this->assertSame(
190+
array('kernel/root/..'),
191+
$container->getParameter('sentry.prefixes')
192+
);
193+
}
194+
195+
public function test_that_it_uses_prefixes_value()
196+
{
197+
$container = $this->getContainer(array(
198+
static::CONFIG_ROOT => array(
199+
'prefixes' => array(
200+
'dirA',
201+
'dirB',
202+
),
203+
),
204+
));
205+
206+
$this->assertSame(
207+
array('dirA', 'dirB'),
208+
$container->getParameter('sentry.prefixes')
209+
);
210+
}
211+
212+
public function test_that_it_has_sentry_client_service_and_it_defaults_to_symfony_client()
213+
{
214+
$client = $this->getContainer()->get('sentry.client');
215+
$this->assertInstanceOf('Sentry\SentryBundle\SentrySymfonyClient', $client);
216+
}
217+
218+
public function test_that_it_has_sentry_exception_listener_and_it_defaults_to_default_exception_listener()
219+
{
220+
$client = $this->getContainer()->get('sentry.exception_listener');
221+
$this->assertInstanceOf('Sentry\SentryBundle\EventListener\ExceptionListener', $client);
222+
}
223+
224+
public function test_that_it_has_proper_event_listener_tags_for_exception_listener()
225+
{
226+
$containerBuilder = new ContainerBuilder();
227+
$extension = new SentryExtension();
228+
$extension->load(array(), $containerBuilder);
229+
230+
$definition = $containerBuilder->getDefinition('sentry.exception_listener');
231+
$tags = $definition->getTag('kernel.event_listener');
232+
233+
$this->assertSame(
234+
array(
235+
array('event' => 'kernel.request', 'method' => 'onKernelRequest'),
236+
array('event' => 'kernel.exception', 'method' => 'onKernelException'),
237+
array('event' => 'console.exception', 'method' => 'onConsoleException'),
238+
),
239+
$tags
240+
);
241+
}
242+
243+
private function getContainer(array $options = array())
244+
{
245+
$containerBuilder = new ContainerBuilder();
246+
$containerBuilder->setParameter('kernel.root_dir', 'kernel/root');
247+
$containerBuilder->setParameter('kernel.environment', 'test');
248+
249+
$extension = new SentryExtension();
250+
251+
$extension->load($options, $containerBuilder);
252+
253+
$containerBuilder->compile();
254+
255+
return $containerBuilder;
256+
}
257+
}

0 commit comments

Comments
 (0)