Drop WP_DEBUG override threshold. Improve logger tests.

This may be unexpected behavior for a user, their level is overridden with no
way to change it. It also greatly complicates testing default thresholds.

Dependency on WC_Log_Handler_File has been removed from logger tests.
This commit is contained in:
Jon Surrell 2016-12-11 17:24:30 +01:00
parent 0bea7e1305
commit 88836b9cca
2 changed files with 155 additions and 36 deletions

View File

@ -37,18 +37,15 @@ class WC_Logger {
* the filter 'woocommerce_register_log_handlers' will be used to define the handlers.
* If $handlers is provided, the filter will not be applied and the handlers will be
* used directly.
* @param string $threshold Optional. Define an explicit threshold. Defaults to global
* setting 'woocommerce_log_threshold'. If WP_DUBUG is true, $threshold will be set
* to 'debug'.
* @param string $threshold Optional. Define an explicit threshold. Defaults to the global
* setting 'woocommerce_log_threshold' or 'notice' if the setting is not configured.
*/
public function __construct( $handlers = null, $threshold = null ) {
if ( null === $handlers ) {
$handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
}
if ( WP_DEBUG ) {
$threshold = 'debug';
} elseif ( null === $threshold ) {
if ( null === $threshold ) {
$threshold = get_option( 'woocommerce_log_threshold', 'notice' );
}

View File

@ -4,8 +4,6 @@
* Class WC_Tests_Logger
* @package WooCommerce\Tests\Log
* @since 2.3
*
* @todo isolate tests from file handler
*/
class WC_Tests_Logger extends WC_Unit_Test_Case {
@ -33,12 +31,24 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
* @since 2.4
*/
public function test_add() {
$log = new WC_Logger( array( new WC_Log_Handler_File() ), 'debug' );
$time = time();
$handler = $this
->getMockBuilder( 'WC_Log_Handler' )
->setMethods( array( 'handle' ) )
->getMock();
$handler
->expects( $this->once() )
->method( 'handle' )
->with(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'info' ),
$this->equalTo( 'this is a message' ),
$this->equalTo( array( 'tag' => 'unit-tests', '_legacy' => true ) )
);
$log = new WC_Logger( array( $handler ), 'debug' );
$log->add( 'unit-tests', 'this is a message' );
$this->assertStringMatchesFormat( '%d-%d-%d @ %d:%d:%d - %s', $this->read_content( 'unit-tests' ) );
$this->assertStringEndsWith( ' - this is a message' . PHP_EOL, $this->read_content( 'unit-tests' ) );
$this->setExpectedDeprecated( 'WC_Logger::add' );
}
@ -70,18 +80,16 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
* @since 2.8
*/
public function test_log() {
$log = new WC_Logger( array( new WC_Log_Handler_File() ), 'debug' );
$log->log( 'debug', 'debug' );
$log->log( 'info', 'info' );
$log->log( 'notice', 'notice' );
$log->log( 'warning', 'warning' );
$log->log( 'error', 'error' );
$log->log( 'critical', 'critical' );
$log->log( 'alert', 'alert' );
$log->log( 'emergency', 'emergency' );
$log_content = $this->read_content( 'log' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
$handler = $this->create_mock_handler();
$log = new WC_Logger( array( $handler ), 'debug' );
$log->log( 'debug', 'debug message' );
$log->log( 'info', 'info message' );
$log->log( 'notice', 'notice message' );
$log->log( 'warning', 'warning message' );
$log->log( 'error', 'error message' );
$log->log( 'critical', 'critical message' );
$log->log( 'alert', 'alert message' );
$log->log( 'emergency', 'emergency message' );
}
/**
@ -126,19 +134,16 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
* @since 2.8
*/
public function test_level_methods() {
$log = new WC_Logger( array( new WC_Log_Handler_File() ), 'debug' );
$log->debug( 'debug' );
$log->info( 'info' );
$log->notice( 'notice' );
$log->warning( 'warning' );
$log->error( 'error' );
$log->critical( 'critical' );
$log->alert( 'alert' );
$log->emergency( 'emergency' );
$log_content = $this->read_content( 'log' );
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
$handler = $this->create_mock_handler();
$log = new WC_Logger( array( $handler ), 'debug' );
$log->debug( 'debug message' );
$log->info( 'info message' );
$log->notice( 'notice message' );
$log->warning( 'warning message' );
$log->error( 'error message' );
$log->critical( 'critical message' );
$log->alert( 'alert message' );
$log->emergency( 'emergency message' );
}
/**
@ -160,6 +165,53 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
remove_filter( 'woocommerce_register_log_handlers', array( $this, 'return_assertion_handlers' ) );
}
/**
* Test default threshold 'notice' or read from option.
*
* @since 2.8
*/
public function test_threshold_defaults() {
$time = time();
// Test option setting.
update_option( 'woocommerce_log_threshold', 'alert' );
$handler = $this
->getMockBuilder( 'WC_Log_Handler' )
->setMethods( array( 'handle' ) )
->getMock();
$handler
->expects( $this->once() )
->method( 'handle' )
->with(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'alert' ),
$this->equalTo( 'alert message' ),
$this->equalTo( array() )
);
$log = new WC_Logger( array( $handler ) );
$log->critical( 'critical message' );
$log->alert( 'alert message' );
// Test 'notice' default when option is not set.
delete_option( 'woocommerce_log_threshold' );
$handler = $this
->getMockBuilder( 'WC_Log_Handler' )
->setMethods( array( 'handle' ) )
->getMock();
$handler
->expects( $this->once() )
->method( 'handle' )
->with(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'notice' ),
$this->equalTo( 'notice message' ),
$this->equalTo( array() )
);
$log = new WC_Logger( array( $handler ) );
$log->info( 'info message' );
$log->notice( 'notice message' );
}
/**
* Helper for 'woocommerce_register_log_handlers' filter test.
*
@ -180,4 +232,74 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
return array( $handler );
}
/**
* Mock handler that expects sequential calls to each level.
*
* Calls should have the message '[level] message'
*
* @since 2.8
*
* @return WC_Log_Handler mock object
*/
public function create_mock_handler() {
$time = time();
$handler = $this
->getMockBuilder( 'WC_Log_Handler' )
->setMethods( array( 'handle' ) )
->getMock();
$handler
->expects( $this->exactly( 8 ) )
->method( 'handle' )
->withConsecutive(
array(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'debug' ),
$this->equalTo( 'debug message' ),
$this->equalTo( array() ),
),
array(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'info' ),
$this->equalTo( 'info message' ),
$this->equalTo( array() ),
),
array(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'notice' ),
$this->equalTo( 'notice message' ),
$this->equalTo( array() ),
),
array(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'warning' ),
$this->equalTo( 'warning message' ),
$this->equalTo( array() ),
),
array(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'error' ),
$this->equalTo( 'error message' ),
$this->equalTo( array() ),
),
array(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'critical' ),
$this->equalTo( 'critical message' ),
$this->equalTo( array() ),
),
array(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'alert' ),
$this->equalTo( 'alert message' ),
$this->equalTo( array() ),
),
array(
$this->greaterThanOrEqual( $time ),
$this->equalTo( 'emergency' ),
$this->equalTo( 'emergency message' ),
$this->equalTo( array() ),
)
);
return $handler;
}
}