Skip to content

Commit a23be47

Browse files
authored
Merge pull request #15 from OxCom/develop
issue #14: check that user provider functions were called properly
2 parents 2fc9c6f + 755d74d commit a23be47

File tree

6 files changed

+196
-2
lines changed

6 files changed

+196
-2
lines changed

Provider/RollbarHandler.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Monolog\Logger;
77
use Rollbar\Rollbar as RollbarNotifier;
88
use Symfony\Component\DependencyInjection\ContainerInterface;
9-
use Symfony\Component\HttpFoundation\Request;
109
use SymfonyRollbarBundle\DependencyInjection\SymfonyRollbarExtension;
1110
use Rollbar\Payload\Level;
1211
use SymfonyRollbarBundle\Provider\Api\Filter;
@@ -164,6 +163,9 @@ protected function mapConfigValues($rConfig)
164163
}
165164
$rConfig[$key] = \array_filter($rConfig[$key]);
166165

166+
// person should be an array or null
167+
$rConfig['person'] = empty($rConfig['person']) ? null : $rConfig['person'];
168+
167169
return $rConfig;
168170
}
169171

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
imports:
2+
- { resource: config.yml }
3+
- { resource: parameters.yml }
4+
5+
symfony_rollbar:
6+
enable: true
7+
exclude:
8+
- \Symfony\Component\Debug\Exception\FatalErrorException
9+
- \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException
10+
- \ParseError
11+
rollbar:
12+
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
13+
environment: '%kernel.environment%'
14+
person_fn: "get_awesome_person"
15+
check_ignore: "should_ignore"
16+
custom_data_method: "custom_data_provider"
17+
capture_email: true
18+
capture_username: true
19+
20+
rollbar_js:
21+
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
22+
payload:
23+
environment: '%kernel.environment%'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
imports:
2+
- { resource: config.yml }
3+
- { resource: parameters.yml }
4+
5+
symfony_rollbar:
6+
enable: true
7+
exclude:
8+
- \Symfony\Component\Debug\Exception\FatalErrorException
9+
- \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException
10+
- \ParseError
11+
rollbar:
12+
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
13+
environment: '%kernel.environment%'
14+
person:
15+
id: 42
16+
username: 'system'
17+
email: 'system@example.com'
18+
check_ignore: "should_ignore"
19+
custom_data_method: "custom_data_provider"
20+
21+
rollbar_js:
22+
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
23+
payload:
24+
environment: '%kernel.environment%'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
imports:
2+
- { resource: config.yml }
3+
- { resource: parameters.yml }
4+
5+
symfony_rollbar:
6+
enable: true
7+
exclude:
8+
- \Symfony\Component\Debug\Exception\FatalErrorException
9+
- \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException
10+
- \ParseError
11+
rollbar:
12+
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
13+
environment: '%kernel.environment%'
14+
person:
15+
id: 42
16+
username: 'system'
17+
email: 'system@example.com'
18+
check_ignore: "should_ignore"
19+
custom_data_method: "custom_data_provider"
20+
capture_email: true
21+
capture_username: true
22+
23+
rollbar_js:
24+
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
25+
payload:
26+
environment: '%kernel.environment%'

Tests/SymfonyRollbarBundle/Provider/PersonProviderTest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use SymfonyRollbarBundle\Provider\AbstractPersonProvider;
77
use SymfonyRollbarBundle\Provider\RollbarHandler;
88
use SymfonyRollbarBundle\Tests\Fixtures\AwesomePerson;
9+
use Rollbar\Rollbar as RollbarNotifier;
10+
use Rollbar\RollbarLogger;
911

1012
/**
1113
* Class PersonProviderTest
@@ -36,6 +38,7 @@ public function testPersonProvider($env, $expected)
3638
$this->assertNotEmpty($config['person_fn']);
3739

3840
$call = $config['person_fn'];
41+
$this->assertTrue(is_callable($call));
3942
$this->assertCount(2, $call, "The 'person_fn' should contains 2 elements");
4043

4144
/** @var AbstractPersonProvider $service */
@@ -84,6 +87,7 @@ public function testPersonProviderFunction()
8487
$this->assertNotEmpty($config['person_fn']);
8588

8689
$method = $config['person_fn'];
90+
$this->assertTrue(is_callable($method));
8791
$this->assertEquals('get_awesome_person', $method);
8892

8993
$person = call_user_func($method);
@@ -92,4 +96,119 @@ public function testPersonProviderFunction()
9296
$this->assertEquals('global_username', $person['username']);
9397
$this->assertEquals('global_email', $person['email']);
9498
}
99+
100+
/**
101+
* @dataProvider generatePersonProviderCalls
102+
*
103+
* @param $env
104+
* @param $expected
105+
*
106+
* @throws \ReflectionException
107+
*/
108+
public function testPersonProviderWasCalled($env, $expected)
109+
{
110+
include_once __DIR__ . '/../../Fixtures/global_fn.php';
111+
static::bootKernel(['environment' => $env]);
112+
113+
$container = static::$kernel->getContainer();
114+
$handler = new RollbarHandler($container);
115+
116+
/** @var \Rollbar\RollbarLogger $notifier */
117+
$logger = RollbarNotifier::logger();
118+
$builder = $logger->getDataBuilder();
119+
120+
$method = new \ReflectionMethod($builder, 'getPerson');
121+
$method->setAccessible(true);
122+
123+
/** @var \Rollbar\Payload\Person $person */
124+
$person = $method->invoke($builder);
125+
126+
$this->assertEquals($expected, [
127+
'id' => $person->getId(),
128+
'username' => $person->getUsername(),
129+
'email' => $person->getEmail(),
130+
]);
131+
}
132+
133+
/**
134+
* @return array
135+
*/
136+
public function generatePersonProviderCalls()
137+
{
138+
return [
139+
[
140+
'test_if',
141+
[
142+
'id' => 'global_id',
143+
'username' => null,
144+
'email' => null,
145+
],
146+
],
147+
[
148+
'test_ifc',
149+
[
150+
'id' => 'global_id',
151+
'username' => 'global_username',
152+
'email' => 'global_email',
153+
],
154+
],
155+
];
156+
}
157+
158+
/**
159+
* @dataProvider generateConstPersonCalls
160+
*
161+
* @param $env
162+
* @param $expected
163+
*
164+
* @throws \ReflectionException
165+
*/
166+
public function testConstPerson($env, $expected)
167+
{
168+
static::bootKernel(['environment' => $env]);
169+
170+
$container = static::$kernel->getContainer();
171+
$handler = new RollbarHandler($container);
172+
173+
/** @var \Rollbar\RollbarLogger $notifier */
174+
$logger = RollbarNotifier::logger();
175+
$builder = $logger->getDataBuilder();
176+
177+
$method = new \ReflectionMethod($builder, 'getPerson');
178+
$method->setAccessible(true);
179+
180+
/** @var \Rollbar\Payload\Person $person */
181+
$person = $method->invoke($builder);
182+
183+
$this->assertEquals($expected, [
184+
'id' => $person->getId(),
185+
'username' => $person->getUsername(),
186+
'email' => $person->getEmail(),
187+
]);
188+
}
189+
190+
/**
191+
* @return array
192+
*/
193+
public function generateConstPersonCalls()
194+
{
195+
return [
196+
[
197+
'test_p',
198+
[
199+
'id' => 42,
200+
'username' => null,
201+
'email' => null,
202+
],
203+
],
204+
[
205+
'test_pc',
206+
[
207+
'id' => 42,
208+
'username' => 'system',
209+
'email' => 'system@example.com',
210+
],
211+
],
212+
];
213+
}
95214
}

Tests/SymfonyRollbarBundle/Provider/RollbarHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public function testInitialize()
334334
'include_exception_code_context' => false,
335335
'included_errno' => $defaultErrorMask,
336336
'logger' => null,
337-
'person' => [],
337+
'person' => null,
338338
'person_fn' => [
339339
new \SymfonyRollbarBundle\Tests\Fixtures\PersonProvider($container),
340340
'getPerson',

0 commit comments

Comments
 (0)