From 26da68ba2d8dde7f0244d97329eaeb075f20ed7e Mon Sep 17 00:00:00 2001 From: Artur Moczulski Date: Tue, 30 Apr 2019 13:39:44 +0200 Subject: [PATCH] feat(dev options): verbosity tests Closes re #453 --- tests/VerboseTest.php | 269 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 223 insertions(+), 46 deletions(-) diff --git a/tests/VerboseTest.php b/tests/VerboseTest.php index 6d5c06a9..babffe05 100644 --- a/tests/VerboseTest.php +++ b/tests/VerboseTest.php @@ -18,12 +18,23 @@ class VerbosityTest extends BaseRollbarTest { + /** + * Prepare session + * + * @return void + */ public function setUp() { $_SESSION = array(); parent::setUp(); } + /** + * Clean up Rollbar and the verbose logger handler mock for + * the next test + * + * @return void + */ public function tearDown() { $this->verboseHandlerMock = null; @@ -31,24 +42,51 @@ public function tearDown() parent::tearDown(); } + /** + * Test verbosity of \Rollbar\RollbarLogger::log with + * `enabled` == true. Example: + * + * \Rollbar\Rollbar::init(['enabled' => true]); + * + * @return void + */ public function testRollbarLoggerEnabled() { - $this->rollbarLogVerbosityTest( + $this->rollbarLogTest( array( // config "access_token" => $this->getTestAccessToken(), "environment" => "testing-php", "enabled" => true ), function() { // verbosity expectations - $this->expectLog(0, '/Attempting to log: \[warning\] Testing PHP Notifier/', \Psr\Log\LogLevel::INFO); - $this->expectLog(1, '/Occurrence successfully logged/', \Psr\Log\LogLevel::INFO); + $this->expectLog( + 0, + '/Attempting to log: \[warning\] Testing PHP Notifier/', + \Psr\Log\LogLevel::INFO + ); + $this->expectLog( + 1, + '/Occurrence successfully logged/', + \Psr\Log\LogLevel::INFO + ); } ); } + /** + * Test verbosity of \Rollbar\RollbarLogger::log with + * `enabled` == false. Example: + * + * \Rollbar\Rollbar::init([ + * 'enabled' => false, + * 'verbose' => \Psr\Log\LogLevel::NOTICE + * ]); + * + * @return void + */ public function testRollbarLoggerDisabled() { - $this->rollbarLogVerbosityTest( + $this->rollbarLogTest( array( // config "access_token" => $this->getTestAccessToken(), "environment" => "testing-php", @@ -60,9 +98,20 @@ function() { // verbosity expectations ); } + /** + * Test verbosity of \Rollbar\RollbarLogger::log with + * an invalid log level passed in the method call: + * + * \Rollbar\Rollbar::init([ + * 'verbose' => \Psr\Log\LogLevel::ERROR + * ]); + * \Rollbar\Rollbar::log('nolevel', 'Test message'); + * + * @return void + */ public function testRollbarLoggerInvalidLogLevel() { - $this->rollbarLogVerbosityTest( + $this->rollbarLogTest( array( // config "access_token" => $this->getTestAccessToken(), "environment" => "testing-php" @@ -74,11 +123,22 @@ function() { // verbosity expectations ); } - public function testInternalCheckIgnored() + /** + * Test verbosity of \Rollbar\RollbarLogger::log when an + * occurrence gets ignored for whatever reason. + * + * \Rollbar\Rollbar::init([ + * 'verbose' => \Psr\Log\LogLevel::INFO + * ]); + * \Rollbar\Rollbar::log('nolevel', 'Test message'); + * + * @return void + */ + public function testRollbarLoggerInternalCheckIgnoredSuppressed() { $errorReporting = 0; - $this->rollbarLogVerbosityTest( - + $this->rollbarLogTest( + array( // config "access_token" => $this->getTestAccessToken(), "environment" => "testing-php" @@ -106,6 +166,53 @@ function() use ($errorReporting) { // test setup ); } + /** + * Test verbosity of \Rollbar\Config::internalCheckIgnored + * when error_reporting === 0. + * + * \error_reporting(0); + * \Rollbar\Rollbar::init([ + * 'verbose' => \Psr\Log\LogLevel::DEBUG + * ]); + * \Rollbar\Rollbar::log('nolevel', 'Test message'); + * + * @return void + */ + public function testRollbarConfigInternalCheckIgnoredShouldSuppress() + { + $config = $this->verboseRollbarConfig(array( // config + "access_token" => $this->getTestAccessToken(), + "environment" => "testing-php" + )); + $errorReporting = \error_reporting(); + + $this->configurableObjectVerbosityTest( + + $config, + + function() use ($config) { // logic under test + $config->internalCheckIgnored(\Psr\Log\LogLevel::WARNING, "Some message"); + }, + + function() { // verbosity expectations + $this->expectLog( + 0, + '/Ignoring due to error reporting has been disabled in PHP config/', + \Psr\Log\LogLevel::DEBUG + ); + }, + + function() use ($errorReporting) { // test setup + + $errorReporting = \error_reporting(); + \error_reporting(0); + + }, function() use ($errorReporting) { // test cleanup + + \error_reporting($errorReporting); + } + ); + } /** * @var mock $verboseHandlerMock The verboser log handler used for @@ -114,20 +221,23 @@ function() use ($errorReporting) { // test setup private $verboseHandlerMock; /** - * This is a convenience method for creating properly configured - * Rollbar logger objects for testing verbosity. It also sets up - * the $this->verboseHandlerMock to the one used in the created - * Rollbar logger. + * Test helper for creating \Rollbar\RollbarLogger or + * \Rollbar\Config objects. It also sets up + * the $this->verboseHandlerMock to the one used in + * the created object. * - * @param array $config Configuration options for Rollbar - * @return \Rollbar\RollbarLogger + * @param array $config Config array used to configure + * the $object. + * @param \Rollbar\RollbarLogger|\Rollbar\Config $object + * Object to be set up for the test. */ - private function verboseRollbarLogger($config) + private function prepareForLogMocking($config, $object) { $verboseLogger = new \Monolog\Logger('rollbar.verbose.test'); - $config['verbose_logger'] = $verboseLogger; - $rollbarLogger = new RollbarLogger($config); + $object->configure(array_merge($config, array( + 'verbose_logger' => $verboseLogger + ))); $verbose = isset($config['verbose']) ? $config['verbose'] : @@ -148,7 +258,41 @@ private function verboseRollbarLogger($config) $this->verboseHandlerMock = $handlerMock; - return $rollbarLogger; + return $object; + } + + /** + * This is a convenience method for creating properly configured + * Rollbar config objects for testing verbosity. It also sets up + * the $this->verboseHandlerMock to the one used in the created + * Rollbar logger. + * + * @param array $config Configuration options for Rollbar + * @return \Rollbar\Config + */ + private function verboseRollbarConfig($config) + { + return $this->prepareForLogMocking( + $config, + new \Rollbar\Config($config) + ); + } + + /** + * This is a convenience method for creating properly configured + * Rollbar logger objects for testing verbosity. It also sets up + * the $this->verboseHandlerMock to the one used in the created + * Rollbar logger. + * + * @param array $config Configuration options for Rollbar + * @return \Rollbar\RollbarLogger + */ + private function verboseRollbarLogger($config) + { + return $this->prepareForLogMocking( + $config, + new \Rollbar\RollbarLogger($config) + ); } /** @@ -214,41 +358,76 @@ private function expectQuiet($handlerMock = null) } /** - * Conenience test helper for Rollbar logger log test with - * a verbose logger handler mock that checks against the - * quiet and verbose scenarios. + * Test helper providing a quiet and verbose scenario testing + * for given functionality. * - * @param array $config Configuration for Rollbar logger. + * @param \Rollbar\RollbarLogger|\Rollbar\Config $object Object under test. + * @param callback $test Logic under test * @param callback $verboseExpectations A callback with * expectations to be set on the verbose logger handler mock * in the verbose scenario. - * @param string $messageLevel (optional) The level of the Rollbar log - * message invoked. * @param callback $pre (optional) Logic to be executed before test. * @param callback $post (optional) Logic to be executed after the test */ - private function rollbarLogVerbosityTest( - $config, + private function configurableObjectVerbosityTest( + $object, + $test, $verboseExpectations, - $messageLevel = Level::WARNING, $pre = null, $post = null ) { // Quiet scenario - $config['verbose'] = \Rollbar\Config::VERBOSE_NONE; - $this->rollbarLogTest( - $config, + $this->prepareForLogMocking( + array('verbose' => \Rollbar\Config::VERBOSE_NONE), + $object + ); + $this->withTestLambdas( + $test, function() { $this->expectQuiet(); }, - $messageLevel, $pre, $post ); // Verbose scenario - $config['verbose'] = \Psr\Log\LogLevel::DEBUG; - $this->rollbarLogTest($config, $verboseExpectations, $messageLevel, $pre, $post); + $this->prepareForLogMocking( + array('verbose' => \Psr\Log\LogLevel::DEBUG), + $object + ); + $this->withTestLambdas( + $test, + $verboseExpectations, + $pre, + $post + ); + } + + /** + * Test helper for performing verbosity tests + * + * @param callback $test Logic under test. + * @param callback $expectations Logic with expectations. + * @param callback $pre (optional) Test set up. + * @param callback $post (optional) Test tear down. + */ + private function withTestLambdas( + $test, + $expectations, + $pre = null, + $post = null + ) { + if ($pre !== null) { + $pre(); + } + + $expectations(); + + $test(); + + if ($post !== null) { + $post(); + } } /** @@ -270,20 +449,18 @@ private function rollbarLogTest( $pre = null, $post = null ) { - if ($pre !== null) { - $pre(); - } - $rollbarLogger = $this->verboseRollbarLogger($config); - $expectations(); - - try { - $rollbarLogger->log($messageLevel, "Testing PHP Notifier", array()); - } catch(\Exception $exception) {} // discard exceptions - that's what's under test here - - if ($post !== null) { - $post(); - } + $this->configurableObjectVerbosityTest( + $rollbarLogger, + function() use ($rollbarLogger, $messageLevel) { + try { + $rollbarLogger->log($messageLevel, "Testing PHP Notifier", array()); + } catch(\Exception $exception) {} // discard exceptions - that's what's under test here + }, + $expectations, + $pre, + $post + ); } } \ No newline at end of file