Skip to content

Commit 0b8745c

Browse files
committed
extend tests
1 parent a562c8b commit 0b8745c

File tree

5 files changed

+144
-45
lines changed

5 files changed

+144
-45
lines changed

DependencyInjection/Configuration.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,33 @@ public function getConfigTreeBuilder()
9292
->scalarNode('environment')->defaultValue(static::ENVIRONMENT)->end()
9393
->arrayNode('custom')
9494
->treatNullLike([])
95-
->prototype('scalar')->end()
95+
->useAttributeAsKey('key')
96+
->prototype('array')
97+
->children()
98+
->scalarNode('key')->end()
99+
->scalarNode('value')->end()
100+
->end()
101+
->end()
96102
->defaultValue([])
97103
->end()
98104
->arrayNode('error_sample_rates')
99105
->treatNullLike([])
100-
->useAttributeAsKey('type')
106+
->useAttributeAsKey('key')
101107
->prototype('array')
102108
->children()
103-
->integerNode('type')->end()
104-
->floatNode('rate')->end()
109+
->integerNode('key')->end()
110+
->floatNode('value')->end()
105111
->end()
106112
->end()
107113
->defaultValue([])
108114
->end()
109115
->arrayNode('exception_sample_rates')
110116
->treatNullLike([])
111-
->useAttributeAsKey('class')
117+
->useAttributeAsKey('key')
112118
->prototype('array')
113119
->children()
114-
->scalarNode('class')->end()
115-
->floatNode('rate')->end()
120+
->scalarNode('key')->end()
121+
->floatNode('value')->end()
116122
->end()
117123
->end()
118124
->defaultValue([])

Provider/RollbarHandler.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ public function __construct(ContainerInterface $container, $level = Logger::ERRO
8888
*/
8989
protected function initialize()
9090
{
91+
$container = $this->getContainer();
92+
9193
try {
92-
$config = $this->getContainer()->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
94+
$config = $container->getParameter(SymfonyRollbarExtension::ALIAS . '.config');
9395
} catch (\Exception $e) {
9496
return null;
9597
}
9698

97-
$kernel = $this->container->get('kernel');
99+
$kernel = $container->get('kernel');
98100
$rConfig = $config['rollbar'];
99101

100102
// override specific values
@@ -116,24 +118,30 @@ protected function initialize()
116118
$rConfig[$option] = $this->injectService($rConfig[$option], $method);
117119
}
118120

119-
// map rates fields
120-
$key = 'exception_sample_rates';
121-
$rConfig[$key] = \array_map(function ($data) {
122-
return empty($data['rate']) ? null : $data['rate'];
123-
}, $rConfig[$key]);
124-
$rConfig[$key] = \array_filter($rConfig[$key]);
121+
$rConfig = $this->mapConfigValues($rConfig);
122+
123+
$this->exclude = empty($config['exclude']) ? [] : $config['exclude'];
125124

125+
return $rConfig;
126+
}
127+
128+
/**
129+
* Map specific fields in configurations fields
130+
*
131+
* @param array $rConfig
132+
* @return array
133+
*/
134+
protected function mapConfigValues($rConfig)
135+
{
126136
$key = 'error_sample_rates';
127-
foreach ($rConfig[$key] as $const => $data) {
137+
foreach ($rConfig[$key] as $const => $value) {
128138
$newKey = constant($const);
129139
unset($rConfig[$key][$const]);
130140

131-
$rConfig[$key][$newKey] = empty($data['rate']) ? null : $data['rate'];
141+
$rConfig[$key][$newKey] = $value;
132142
}
133143
$rConfig[$key] = \array_filter($rConfig[$key]);
134144

135-
$this->exclude = empty($config['exclude']) ? [] : $config['exclude'];
136-
137145
return $rConfig;
138146
}
139147

@@ -147,10 +155,12 @@ protected function initialize()
147155
*/
148156
protected function injectService($name, $method)
149157
{
150-
if ($this->container->has($name)) {
151-
$service = $this->container->get($name);
158+
$container = $this->getContainer();
159+
160+
if ($container->has($name)) {
161+
$service = $container->get($name);
152162
} elseif (class_exists($name)) {
153-
$service = new $name($this->container);
163+
$service = new $name($container);
154164
}
155165

156166
if (!empty($service) && method_exists($service, $method)) {

Tests/Fixtures/app/config/config_test.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ symfony_rollbar:
1414
environment: '%kernel.environment%'
1515
person_fn: \SymfonyRollbarBundle\Tests\Fixtures\PersonProvider
1616
checkIgnore: \SymfonyRollbarBundle\Tests\Fixtures\CheckIgnoreProvider
17+
custom:
18+
- {key: hello, value: world}
19+
- {key: key, value: value}
1720
exception_sample_rates:
18-
- {class: \Symfony\Component\Security\Core\Exception\AccessDeniedException, rate: 0.1}
19-
- {class: \Symfony\Component\HttpKernel\Exception\NotFoundHttpException, rate: 0.5}
20-
- {class: \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException, rate: 0.5}
21-
- {class: \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException, rate: 1}
21+
- {key: \Symfony\Component\Security\Core\Exception\AccessDeniedException, value: 0.1}
22+
- {key: \Symfony\Component\HttpKernel\Exception\NotFoundHttpException, value: 0.5}
23+
- {key: \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException, value: 0.5}
24+
- {key: \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException, value: 1}
2225
error_sample_rates:
23-
- {type: E_NOTICE, rate: 0.1}
24-
- {type: E_USER_ERROR, rate: 0.5}
25-
- {type: E_USER_NOTICE, rate: 0.1}
26+
- {key: E_NOTICE, value: 0.1}
27+
- {key: E_USER_ERROR, value: 0.5}
28+
- {key: E_USER_NOTICE, value: 0.1}
2629

2730
rollbar_js:
2831
accessToken: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'

Tests/SymfonyRollbarBundle/DependencyInjection/ConfigurationTest.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,21 @@ public function testParameters()
2727
$exclude[] = '\Symfony\Component\HttpKernel\Exception\HttpExceptionInterface';
2828

2929
$errorRates = [
30-
'E_NOTICE' => ['rate' => 0.1],
31-
'E_USER_ERROR' => ['rate' => 0.5],
32-
'E_USER_NOTICE' => ['rate' => 0.1],
30+
'E_NOTICE' => 0.1,
31+
'E_USER_ERROR' => 0.5,
32+
'E_USER_NOTICE' => 0.1,
3333
];
3434

3535
$exceptionRates = [
36-
'\Symfony\Component\Security\Core\Exception\AccessDeniedException' => [
37-
'rate' => 0.1,
38-
],
39-
'\Symfony\Component\HttpKernel\Exception\NotFoundHttpException' => [
40-
'rate' => 0.5,
41-
],
42-
'\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException' => [
43-
'rate' => 0.5,
44-
],
45-
'\Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException' => [
46-
'rate' => 1,
47-
],
36+
'\Symfony\Component\Security\Core\Exception\AccessDeniedException' => 0.1,
37+
'\Symfony\Component\HttpKernel\Exception\NotFoundHttpException' => 0.5,
38+
'\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException' => 0.5,
39+
'\Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException' => 1,
40+
];
41+
42+
$custom = [
43+
'hello' => 'world',
44+
'key' => 'value',
4845
];
4946

5047
$default = [
@@ -77,7 +74,7 @@ public function testParameters()
7774
'proxy' => null,
7875
'allow_exec' => true,
7976
'endpoint' => Configuration::API_ENDPOINT,
80-
'custom' => [],
77+
'custom' => $custom,
8178
'exception_sample_rates' => $exceptionRates,
8279
'fluent_host' => '127.0.0.1',
8380
'fluent_port' => 24224,

Tests/SymfonyRollbarBundle/Provider/RollbarHandlerTest.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace SymfonyRollbarBundle\Tests\Provider;
44

55
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
6+
use SymfonyRollbarBundle\DependencyInjection\Configuration;
67
use SymfonyRollbarBundle\Provider\RollbarHandler;
78
use SymfonyRollbarBundle\Tests\Fixtures\ApiClientMock;
89
use SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException;
@@ -280,4 +281,86 @@ public function testTrackBuildDisabledRollbar()
280281
/** @var \SymfonyRollbarBundle\Provider\ApiClient $client */
281282
$this->assertFalse($container->has('symfony_rollbar.provider.api_client'));
282283
}
284+
285+
public function testInitialize()
286+
{
287+
$container = static::$kernel->getContainer();
288+
289+
$mock = $this->createMock(RollbarHandler::class);
290+
291+
$mock->method('getContainer')
292+
->willReturn($container);
293+
294+
$method = new \ReflectionMethod($mock, 'initialize');
295+
$method->setAccessible(true);
296+
$config = $method->invoke($mock);
297+
298+
$defaultErrorMask = E_ERROR | E_WARNING | E_PARSE | E_CORE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
299+
300+
$errorRates = [
301+
E_NOTICE => 0.1,
302+
E_USER_ERROR => 0.5,
303+
E_USER_NOTICE => 0.1,
304+
];
305+
306+
$exceptionRates = [
307+
'\Symfony\Component\Security\Core\Exception\AccessDeniedException' => 0.1,
308+
'\Symfony\Component\HttpKernel\Exception\NotFoundHttpException' => 0.5,
309+
'\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException' => 0.5,
310+
'\Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException' => 1,
311+
];
312+
313+
$custom = [
314+
'hello' => 'world',
315+
'key' => 'value',
316+
];
317+
318+
$default = [
319+
'access_token' => 'SOME_ROLLBAR_ACCESS_TOKEN_123456',
320+
'agent_log_location' => static::$kernel->getLogDir() . '/rollbar.log',
321+
'base_api_url' => Configuration::API_ENDPOINT,
322+
'branch' => Configuration::BRANCH,
323+
'capture_error_stacktraces' => true,
324+
'checkIgnore' => [
325+
new \SymfonyRollbarBundle\Tests\Fixtures\CheckIgnoreProvider(),
326+
'checkIgnore',
327+
],
328+
'code_version' => '',
329+
'enable_utf8_sanitization' => true,
330+
'environment' => static::$kernel->getEnvironment(),
331+
'error_sample_rates' => $errorRates,
332+
'handler' => Configuration::HANDLER_BLOCKING,
333+
'include_error_code_context' => false,
334+
'include_exception_code_context' => false,
335+
'included_errno' => $defaultErrorMask,
336+
'logger' => null,
337+
'person' => [],
338+
'person_fn' => [
339+
new \SymfonyRollbarBundle\Tests\Fixtures\PersonProvider($container),
340+
'getPerson',
341+
],
342+
'root' => static::$kernel->getRootDir(),
343+
'scrub_fields' => Configuration::$scrubFieldsDefault,
344+
'shift_function' => true,
345+
'timeout' => 3,
346+
'report_suppressed' => false,
347+
'use_error_reporting' => false,
348+
'proxy' => null,
349+
'allow_exec' => true,
350+
'endpoint' => Configuration::API_ENDPOINT,
351+
'custom' => $custom,
352+
'exception_sample_rates' => $exceptionRates,
353+
'fluent_host' => '127.0.0.1',
354+
'fluent_port' => 24224,
355+
'fluent_tag' => 'rollbar',
356+
'host' => null,
357+
'scrub_whitelist' => null,
358+
'send_message_trace' => false,
359+
'include_raw_request_body' => false,
360+
'local_vars_dump' => false,
361+
'framework' => 'Symfony ' . \Symfony\Component\HttpKernel\Kernel::VERSION,
362+
];
363+
364+
$this->assertEquals($default, $config);
365+
}
283366
}

0 commit comments

Comments
 (0)