Skip to content

Commit

Permalink
Merge pull request #15 from OxCom/develop
Browse files Browse the repository at this point in the history
issue #14: check that user provider functions were called properly
  • Loading branch information
OxCom authored Sep 20, 2018
2 parents 2fc9c6f + 755d74d commit a23be47
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Provider/RollbarHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Monolog\Logger;
use Rollbar\Rollbar as RollbarNotifier;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use SymfonyRollbarBundle\DependencyInjection\SymfonyRollbarExtension;
use Rollbar\Payload\Level;
use SymfonyRollbarBundle\Provider\Api\Filter;
Expand Down Expand Up @@ -164,6 +163,9 @@ protected function mapConfigValues($rConfig)
}
$rConfig[$key] = \array_filter($rConfig[$key]);

// person should be an array or null
$rConfig['person'] = empty($rConfig['person']) ? null : $rConfig['person'];

return $rConfig;
}

Expand Down
23 changes: 23 additions & 0 deletions Tests/Fixtures/app/config/config_test_ifc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
imports:
- { resource: config.yml }
- { resource: parameters.yml }

symfony_rollbar:
enable: true
exclude:
- \Symfony\Component\Debug\Exception\FatalErrorException
- \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException
- \ParseError
rollbar:
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
environment: '%kernel.environment%'
person_fn: "get_awesome_person"
check_ignore: "should_ignore"
custom_data_method: "custom_data_provider"
capture_email: true
capture_username: true

rollbar_js:
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
payload:
environment: '%kernel.environment%'
24 changes: 24 additions & 0 deletions Tests/Fixtures/app/config/config_test_p.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
imports:
- { resource: config.yml }
- { resource: parameters.yml }

symfony_rollbar:
enable: true
exclude:
- \Symfony\Component\Debug\Exception\FatalErrorException
- \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException
- \ParseError
rollbar:
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
environment: '%kernel.environment%'
person:
id: 42
username: 'system'
email: '[email protected]'
check_ignore: "should_ignore"
custom_data_method: "custom_data_provider"

rollbar_js:
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
payload:
environment: '%kernel.environment%'
26 changes: 26 additions & 0 deletions Tests/Fixtures/app/config/config_test_pc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
imports:
- { resource: config.yml }
- { resource: parameters.yml }

symfony_rollbar:
enable: true
exclude:
- \Symfony\Component\Debug\Exception\FatalErrorException
- \SymfonyRollbarBundle\Tests\Fixtures\MyAwesomeException
- \ParseError
rollbar:
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_123456'
environment: '%kernel.environment%'
person:
id: 42
username: 'system'
email: '[email protected]'
check_ignore: "should_ignore"
custom_data_method: "custom_data_provider"
capture_email: true
capture_username: true

rollbar_js:
access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_654321'
payload:
environment: '%kernel.environment%'
119 changes: 119 additions & 0 deletions Tests/SymfonyRollbarBundle/Provider/PersonProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use SymfonyRollbarBundle\Provider\AbstractPersonProvider;
use SymfonyRollbarBundle\Provider\RollbarHandler;
use SymfonyRollbarBundle\Tests\Fixtures\AwesomePerson;
use Rollbar\Rollbar as RollbarNotifier;
use Rollbar\RollbarLogger;

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

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

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

$method = $config['person_fn'];
$this->assertTrue(is_callable($method));
$this->assertEquals('get_awesome_person', $method);

$person = call_user_func($method);
Expand All @@ -92,4 +96,119 @@ public function testPersonProviderFunction()
$this->assertEquals('global_username', $person['username']);
$this->assertEquals('global_email', $person['email']);
}

/**
* @dataProvider generatePersonProviderCalls
*
* @param $env
* @param $expected
*
* @throws \ReflectionException
*/
public function testPersonProviderWasCalled($env, $expected)
{
include_once __DIR__ . '/../../Fixtures/global_fn.php';
static::bootKernel(['environment' => $env]);

$container = static::$kernel->getContainer();
$handler = new RollbarHandler($container);

/** @var \Rollbar\RollbarLogger $notifier */
$logger = RollbarNotifier::logger();
$builder = $logger->getDataBuilder();

$method = new \ReflectionMethod($builder, 'getPerson');
$method->setAccessible(true);

/** @var \Rollbar\Payload\Person $person */
$person = $method->invoke($builder);

$this->assertEquals($expected, [
'id' => $person->getId(),
'username' => $person->getUsername(),
'email' => $person->getEmail(),
]);
}

/**
* @return array
*/
public function generatePersonProviderCalls()
{
return [
[
'test_if',
[
'id' => 'global_id',
'username' => null,
'email' => null,
],
],
[
'test_ifc',
[
'id' => 'global_id',
'username' => 'global_username',
'email' => 'global_email',
],
],
];
}

/**
* @dataProvider generateConstPersonCalls
*
* @param $env
* @param $expected
*
* @throws \ReflectionException
*/
public function testConstPerson($env, $expected)
{
static::bootKernel(['environment' => $env]);

$container = static::$kernel->getContainer();
$handler = new RollbarHandler($container);

/** @var \Rollbar\RollbarLogger $notifier */
$logger = RollbarNotifier::logger();
$builder = $logger->getDataBuilder();

$method = new \ReflectionMethod($builder, 'getPerson');
$method->setAccessible(true);

/** @var \Rollbar\Payload\Person $person */
$person = $method->invoke($builder);

$this->assertEquals($expected, [
'id' => $person->getId(),
'username' => $person->getUsername(),
'email' => $person->getEmail(),
]);
}

/**
* @return array
*/
public function generateConstPersonCalls()
{
return [
[
'test_p',
[
'id' => 42,
'username' => null,
'email' => null,
],
],
[
'test_pc',
[
'id' => 42,
'username' => 'system',
'email' => '[email protected]',
],
],
];
}
}
2 changes: 1 addition & 1 deletion Tests/SymfonyRollbarBundle/Provider/RollbarHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public function testInitialize()
'include_exception_code_context' => false,
'included_errno' => $defaultErrorMask,
'logger' => null,
'person' => [],
'person' => null,
'person_fn' => [
new \SymfonyRollbarBundle\Tests\Fixtures\PersonProvider($container),
'getPerson',
Expand Down

0 comments on commit a23be47

Please sign in to comment.