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:
parent
0bea7e1305
commit
88836b9cca
|
@ -37,18 +37,15 @@ class WC_Logger {
|
||||||
* the filter 'woocommerce_register_log_handlers' will be used to define the handlers.
|
* 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
|
* If $handlers is provided, the filter will not be applied and the handlers will be
|
||||||
* used directly.
|
* used directly.
|
||||||
* @param string $threshold Optional. Define an explicit threshold. Defaults to global
|
* @param string $threshold Optional. Define an explicit threshold. Defaults to the global
|
||||||
* setting 'woocommerce_log_threshold'. If WP_DUBUG is true, $threshold will be set
|
* setting 'woocommerce_log_threshold' or 'notice' if the setting is not configured.
|
||||||
* to 'debug'.
|
|
||||||
*/
|
*/
|
||||||
public function __construct( $handlers = null, $threshold = null ) {
|
public function __construct( $handlers = null, $threshold = null ) {
|
||||||
if ( null === $handlers ) {
|
if ( null === $handlers ) {
|
||||||
$handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
|
$handlers = apply_filters( 'woocommerce_register_log_handlers', array() );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( WP_DEBUG ) {
|
if ( null === $threshold ) {
|
||||||
$threshold = 'debug';
|
|
||||||
} elseif ( null === $threshold ) {
|
|
||||||
$threshold = get_option( 'woocommerce_log_threshold', 'notice' );
|
$threshold = get_option( 'woocommerce_log_threshold', 'notice' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,6 @@
|
||||||
* Class WC_Tests_Logger
|
* Class WC_Tests_Logger
|
||||||
* @package WooCommerce\Tests\Log
|
* @package WooCommerce\Tests\Log
|
||||||
* @since 2.3
|
* @since 2.3
|
||||||
*
|
|
||||||
* @todo isolate tests from file handler
|
|
||||||
*/
|
*/
|
||||||
class WC_Tests_Logger extends WC_Unit_Test_Case {
|
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
|
* @since 2.4
|
||||||
*/
|
*/
|
||||||
public function test_add() {
|
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' );
|
$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' );
|
$this->setExpectedDeprecated( 'WC_Logger::add' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,18 +80,16 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
|
||||||
* @since 2.8
|
* @since 2.8
|
||||||
*/
|
*/
|
||||||
public function test_log() {
|
public function test_log() {
|
||||||
$log = new WC_Logger( array( new WC_Log_Handler_File() ), 'debug' );
|
$handler = $this->create_mock_handler();
|
||||||
$log->log( 'debug', 'debug' );
|
$log = new WC_Logger( array( $handler ), 'debug' );
|
||||||
$log->log( 'info', 'info' );
|
$log->log( 'debug', 'debug message' );
|
||||||
$log->log( 'notice', 'notice' );
|
$log->log( 'info', 'info message' );
|
||||||
$log->log( 'warning', 'warning' );
|
$log->log( 'notice', 'notice message' );
|
||||||
$log->log( 'error', 'error' );
|
$log->log( 'warning', 'warning message' );
|
||||||
$log->log( 'critical', 'critical' );
|
$log->log( 'error', 'error message' );
|
||||||
$log->log( 'alert', 'alert' );
|
$log->log( 'critical', 'critical message' );
|
||||||
$log->log( 'emergency', 'emergency' );
|
$log->log( 'alert', 'alert message' );
|
||||||
|
$log->log( 'emergency', 'emergency message' );
|
||||||
$log_content = $this->read_content( 'log' );
|
|
||||||
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,19 +134,16 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
|
||||||
* @since 2.8
|
* @since 2.8
|
||||||
*/
|
*/
|
||||||
public function test_level_methods() {
|
public function test_level_methods() {
|
||||||
$log = new WC_Logger( array( new WC_Log_Handler_File() ), 'debug' );
|
$handler = $this->create_mock_handler();
|
||||||
|
$log = new WC_Logger( array( $handler ), 'debug' );
|
||||||
$log->debug( 'debug' );
|
$log->debug( 'debug message' );
|
||||||
$log->info( 'info' );
|
$log->info( 'info message' );
|
||||||
$log->notice( 'notice' );
|
$log->notice( 'notice message' );
|
||||||
$log->warning( 'warning' );
|
$log->warning( 'warning message' );
|
||||||
$log->error( 'error' );
|
$log->error( 'error message' );
|
||||||
$log->critical( 'critical' );
|
$log->critical( 'critical message' );
|
||||||
$log->alert( 'alert' );
|
$log->alert( 'alert message' );
|
||||||
$log->emergency( 'emergency' );
|
$log->emergency( 'emergency message' );
|
||||||
|
|
||||||
$log_content = $this->read_content( 'log' );
|
|
||||||
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -160,6 +165,53 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
|
||||||
remove_filter( 'woocommerce_register_log_handlers', array( $this, 'return_assertion_handlers' ) );
|
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.
|
* Helper for 'woocommerce_register_log_handlers' filter test.
|
||||||
*
|
*
|
||||||
|
@ -180,4 +232,74 @@ class WC_Tests_Logger extends WC_Unit_Test_Case {
|
||||||
return array( $handler );
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue