Skip to content

Commit

Permalink
feat(dev options): verbosity tests
Browse files Browse the repository at this point in the history
Closes re #453
  • Loading branch information
ArturMoczulski committed Apr 30, 2019
1 parent a10eb75 commit 26da68b
Showing 1 changed file with 223 additions and 46 deletions.
269 changes: 223 additions & 46 deletions tests/VerboseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,75 @@
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;
Rollbar::destroy();
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",
Expand All @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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'] :
Expand All @@ -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)
);
}

/**
Expand Down Expand Up @@ -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();
}
}

/**
Expand All @@ -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
);
}
}

0 comments on commit 26da68b

Please sign in to comment.