diff --git a/Provider/RollbarHandler.php b/Provider/RollbarHandler.php index 16c655f..bd9ceb8 100755 --- a/Provider/RollbarHandler.php +++ b/Provider/RollbarHandler.php @@ -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; @@ -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; } diff --git a/Tests/Fixtures/app/config/config_test_ifc.yml b/Tests/Fixtures/app/config/config_test_ifc.yml new file mode 100755 index 0000000..19f159e --- /dev/null +++ b/Tests/Fixtures/app/config/config_test_ifc.yml @@ -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%' diff --git a/Tests/Fixtures/app/config/config_test_p.yml b/Tests/Fixtures/app/config/config_test_p.yml new file mode 100644 index 0000000..98e7094 --- /dev/null +++ b/Tests/Fixtures/app/config/config_test_p.yml @@ -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: 'system@example.com' + check_ignore: "should_ignore" + custom_data_method: "custom_data_provider" + + rollbar_js: + access_token: 'SOME_ROLLBAR_ACCESS_TOKEN_654321' + payload: + environment: '%kernel.environment%' diff --git a/Tests/Fixtures/app/config/config_test_pc.yml b/Tests/Fixtures/app/config/config_test_pc.yml new file mode 100644 index 0000000..8a83232 --- /dev/null +++ b/Tests/Fixtures/app/config/config_test_pc.yml @@ -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: 'system@example.com' + 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%' diff --git a/Tests/SymfonyRollbarBundle/Provider/PersonProviderTest.php b/Tests/SymfonyRollbarBundle/Provider/PersonProviderTest.php index 53a0aa5..9eef95d 100644 --- a/Tests/SymfonyRollbarBundle/Provider/PersonProviderTest.php +++ b/Tests/SymfonyRollbarBundle/Provider/PersonProviderTest.php @@ -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 @@ -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 */ @@ -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); @@ -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' => 'system@example.com', + ], + ], + ]; + } } diff --git a/Tests/SymfonyRollbarBundle/Provider/RollbarHandlerTest.php b/Tests/SymfonyRollbarBundle/Provider/RollbarHandlerTest.php index 2b78c4c..dd79de0 100755 --- a/Tests/SymfonyRollbarBundle/Provider/RollbarHandlerTest.php +++ b/Tests/SymfonyRollbarBundle/Provider/RollbarHandlerTest.php @@ -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',