2015-04-08 15:55:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2016-11-27 18:34:34 +00:00
|
|
|
* Class WC_Tests_Logger
|
|
|
|
* @package WooCommerce\Tests\Log
|
2015-04-08 15:55:19 +00:00
|
|
|
* @since 2.3
|
|
|
|
*/
|
2016-11-27 18:34:34 +00:00
|
|
|
class WC_Tests_Logger extends WC_Unit_Test_Case {
|
2016-11-13 17:45:45 +00:00
|
|
|
|
2015-04-08 15:55:19 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test add().
|
2015-04-08 15:55:19 +00:00
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
public function test_add() {
|
2016-12-11 16:24:30 +00:00
|
|
|
$time = time();
|
|
|
|
$handler = $this
|
2017-01-24 22:07:58 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler_Interface' )
|
2016-12-11 16:24:30 +00:00
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
|
|
|
$handler
|
|
|
|
->expects( $this->once() )
|
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
2016-12-19 06:57:54 +00:00
|
|
|
$this->equalTo( 'notice' ),
|
2016-12-11 16:24:30 +00:00
|
|
|
$this->equalTo( 'this is a message' ),
|
2016-12-21 19:15:19 +00:00
|
|
|
$this->equalTo( array( 'source' => 'unit-tests', '_legacy' => true ) )
|
2016-12-11 16:24:30 +00:00
|
|
|
);
|
|
|
|
$log = new WC_Logger( array( $handler ), 'debug' );
|
2015-04-08 15:55:19 +00:00
|
|
|
|
|
|
|
$log->add( 'unit-tests', 'this is a message' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Test clear().
|
2015-04-08 15:55:19 +00:00
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
public function test_clear() {
|
2017-01-12 21:19:29 +00:00
|
|
|
$file = wc_get_log_file_path( 'unit-tests' );
|
|
|
|
file_put_contents( $file, 'Test file content.' );
|
|
|
|
$log = new WC_Logger();
|
|
|
|
$log->clear( 'unit-tests' );
|
|
|
|
$this->assertEquals( '', file_get_contents( $file ) );
|
2016-11-13 17:45:45 +00:00
|
|
|
$this->setExpectedDeprecated( 'WC_Logger::clear' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test log() complains for bad levels.
|
2016-11-26 12:11:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-13 17:45:45 +00:00
|
|
|
*/
|
|
|
|
public function test_bad_level() {
|
2016-12-11 12:27:49 +00:00
|
|
|
$log = new WC_Logger( null, 'debug' );
|
2016-11-13 17:45:45 +00:00
|
|
|
$log->log( 'this-is-an-invalid-level', '' );
|
|
|
|
$this->setExpectedIncorrectUsage( 'WC_Logger::log' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test log().
|
2016-11-26 12:11:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-13 17:45:45 +00:00
|
|
|
*/
|
|
|
|
public function test_log() {
|
2016-12-11 16:24:30 +00:00
|
|
|
$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' );
|
2016-11-13 17:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-10 21:21:41 +00:00
|
|
|
* Test logs passed to all handlers.
|
2016-11-26 12:11:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-13 17:45:45 +00:00
|
|
|
*/
|
2016-12-11 12:55:22 +00:00
|
|
|
public function test_log_handlers() {
|
|
|
|
$false_handler = $this
|
2017-01-24 22:07:58 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler_Interface' )
|
2016-12-11 12:55:22 +00:00
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
|
|
|
$false_handler->expects( $this->exactly( 8 ) )->method( 'handle' )->will( $this->returnValue( false ) );
|
2016-11-13 17:45:45 +00:00
|
|
|
|
2016-12-11 12:55:22 +00:00
|
|
|
$true_handler = $this
|
2017-01-24 22:07:58 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler_Interface' )
|
2016-12-11 12:55:22 +00:00
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
|
|
|
$false_handler->expects( $this->exactly( 8 ) )->method( 'handle' )->will( $this->returnValue( true ) );
|
|
|
|
|
|
|
|
$final_handler = $this
|
2017-01-24 22:07:58 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler_Interface' )
|
2016-12-11 12:55:22 +00:00
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
|
|
|
$final_handler->expects( $this->exactly( 8 ) )->method( 'handle' );
|
|
|
|
|
|
|
|
$log = new WC_Logger( array( $false_handler, $true_handler, $final_handler ), 'debug' );
|
2016-11-15 20:04:48 +00:00
|
|
|
|
|
|
|
$log->debug( 'debug' );
|
|
|
|
$log->info( 'info' );
|
|
|
|
$log->notice( 'notice' );
|
|
|
|
$log->warning( 'warning' );
|
|
|
|
$log->error( 'error' );
|
|
|
|
$log->critical( 'critical' );
|
|
|
|
$log->alert( 'alert' );
|
|
|
|
$log->emergency( 'emergency' );
|
2016-11-13 17:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test WC_Logger->[debug..emergency] methods
|
2016-11-26 12:11:14 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-13 17:45:45 +00:00
|
|
|
*/
|
|
|
|
public function test_level_methods() {
|
2016-12-11 16:24:30 +00:00
|
|
|
$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' );
|
2015-04-08 15:55:19 +00:00
|
|
|
}
|
2016-11-13 20:05:41 +00:00
|
|
|
|
2016-11-15 19:45:26 +00:00
|
|
|
/**
|
2016-12-11 12:55:22 +00:00
|
|
|
* Test 'woocommerce_register_log_handlers' filter.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-11 12:55:22 +00:00
|
|
|
*/
|
|
|
|
public function test_woocommerce_register_log_handlers_filter() {
|
|
|
|
add_filter( 'woocommerce_register_log_handlers', array( $this, 'return_assertion_handlers' ) );
|
|
|
|
$log = new WC_Logger( null, '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' );
|
|
|
|
remove_filter( 'woocommerce_register_log_handlers', array( $this, 'return_assertion_handlers' ) );
|
|
|
|
}
|
|
|
|
|
2016-12-11 16:24:30 +00:00
|
|
|
/**
|
2016-12-22 22:21:39 +00:00
|
|
|
* Test no filtering by default
|
2016-12-11 16:24:30 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-11 16:24:30 +00:00
|
|
|
*/
|
|
|
|
public function test_threshold_defaults() {
|
|
|
|
$time = time();
|
|
|
|
|
2016-12-22 22:21:39 +00:00
|
|
|
// Test no filtering by default
|
|
|
|
delete_option( 'woocommerce_log_threshold' );
|
2016-12-11 16:24:30 +00:00
|
|
|
$handler = $this
|
2017-01-24 22:07:58 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler_Interface' )
|
2016-12-11 16:24:30 +00:00
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
|
|
|
$handler
|
2016-12-22 22:21:39 +00:00
|
|
|
->expects( $this->at( 0 ) )
|
2016-12-11 16:24:30 +00:00
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
2016-12-22 22:21:39 +00:00
|
|
|
$this->equalTo( 'bad-level' ),
|
|
|
|
$this->equalTo( 'bad-level message' ),
|
2016-12-11 16:24:30 +00:00
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
|
|
|
$handler
|
2016-12-22 22:21:39 +00:00
|
|
|
->expects( $this->at( 1 ) )
|
2016-12-11 16:24:30 +00:00
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
2016-12-22 22:21:39 +00:00
|
|
|
$this->equalTo( 'debug' ),
|
|
|
|
$this->equalTo( 'debug message' ),
|
2016-12-11 16:24:30 +00:00
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
2016-12-22 22:21:39 +00:00
|
|
|
|
2016-12-11 16:24:30 +00:00
|
|
|
$log = new WC_Logger( array( $handler ) );
|
2016-12-22 22:21:39 +00:00
|
|
|
|
|
|
|
// An invalid level has the minimum severity, but should not be filtered.
|
|
|
|
$log->log( 'bad-level', 'bad-level message' );
|
|
|
|
// Bad level also complains.
|
|
|
|
$this->setExpectedIncorrectUsage( 'WC_Logger::log' );
|
|
|
|
|
|
|
|
// Debug is lowest recognized level
|
|
|
|
$log->debug( 'debug message' );
|
2016-12-11 16:24:30 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 22:07:58 +00:00
|
|
|
/**
|
|
|
|
* Test that the logger validates handlers
|
|
|
|
*
|
|
|
|
* If a handler does not implement WC_Log_Handler_Interface, WC_Logger should complain
|
|
|
|
* (wc_doing_it_wrong) and not register the handler. This handler should receive no messages.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2017-01-24 22:07:58 +00:00
|
|
|
*/
|
|
|
|
public function test_validate_handler_interface() {
|
|
|
|
$handler = $this
|
|
|
|
->getMockBuilder( 'stdClass' )
|
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
|
|
|
$handler->expects( $this->never() )->method( 'handle' );
|
|
|
|
new WC_Logger( array( $handler ) );
|
|
|
|
$this->setExpectedIncorrectUsage( 'WC_Logger::__construct' );
|
|
|
|
}
|
|
|
|
|
2016-12-11 12:55:22 +00:00
|
|
|
/**
|
|
|
|
* Helper for 'woocommerce_register_log_handlers' filter test.
|
2016-11-15 19:45:26 +00:00
|
|
|
*
|
2016-12-11 12:55:22 +00:00
|
|
|
* Returns an array of 1 mocked handler.
|
|
|
|
* The handler expects to receive exactly 8 messages (1 for each level).
|
2016-11-15 19:45:26 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-26 12:11:14 +00:00
|
|
|
*
|
2016-12-11 12:55:22 +00:00
|
|
|
* @return WC_Log_Handler[] array of mocked handlers.
|
2016-11-15 19:45:26 +00:00
|
|
|
*/
|
2016-12-10 21:21:41 +00:00
|
|
|
public function return_assertion_handlers() {
|
2016-12-11 12:55:22 +00:00
|
|
|
$handler = $this
|
2017-01-24 22:07:58 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler_Interface' )
|
2016-11-15 19:45:26 +00:00
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
2016-12-11 12:55:22 +00:00
|
|
|
$handler->expects( $this->exactly( 8 ) )->method( 'handle' );
|
2016-11-15 19:45:26 +00:00
|
|
|
|
2016-12-11 12:55:22 +00:00
|
|
|
return array( $handler );
|
2016-11-13 21:35:51 +00:00
|
|
|
}
|
2016-12-03 21:42:43 +00:00
|
|
|
|
2016-12-11 16:24:30 +00:00
|
|
|
/**
|
|
|
|
* Mock handler that expects sequential calls to each level.
|
|
|
|
*
|
|
|
|
* Calls should have the message '[level] message'
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-12-11 16:24:30 +00:00
|
|
|
*
|
|
|
|
* @return WC_Log_Handler mock object
|
|
|
|
*/
|
|
|
|
public function create_mock_handler() {
|
|
|
|
$time = time();
|
|
|
|
$handler = $this
|
2017-01-24 22:07:58 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler_Interface' )
|
2016-12-11 16:24:30 +00:00
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
2016-12-16 20:46:49 +00:00
|
|
|
|
|
|
|
$handler
|
|
|
|
->expects( $this->at( 0 ) )
|
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
|
|
|
$this->equalTo( 'debug' ),
|
|
|
|
$this->equalTo( 'debug message' ),
|
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
|
|
|
$handler
|
|
|
|
->expects( $this->at( 1 ) )
|
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
|
|
|
$this->equalTo( 'info' ),
|
|
|
|
$this->equalTo( 'info message' ),
|
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
|
|
|
$handler
|
|
|
|
->expects( $this->at( 2 ) )
|
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
|
|
|
$this->equalTo( 'notice' ),
|
|
|
|
$this->equalTo( 'notice message' ),
|
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
|
|
|
$handler
|
|
|
|
->expects( $this->at( 3 ) )
|
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
|
|
|
$this->equalTo( 'warning' ),
|
|
|
|
$this->equalTo( 'warning message' ),
|
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
|
|
|
$handler
|
|
|
|
->expects( $this->at( 4 ) )
|
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
|
|
|
$this->equalTo( 'error' ),
|
|
|
|
$this->equalTo( 'error message' ),
|
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
|
|
|
$handler
|
|
|
|
->expects( $this->at( 5 ) )
|
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
|
|
|
$this->equalTo( 'critical' ),
|
|
|
|
$this->equalTo( 'critical message' ),
|
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
2016-12-11 16:24:30 +00:00
|
|
|
$handler
|
2016-12-16 20:46:49 +00:00
|
|
|
->expects( $this->at( 6 ) )
|
2016-12-11 16:24:30 +00:00
|
|
|
->method( 'handle' )
|
2016-12-16 20:46:49 +00:00
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
|
|
|
$this->equalTo( 'alert' ),
|
|
|
|
$this->equalTo( 'alert message' ),
|
|
|
|
$this->equalTo( array() )
|
2016-12-11 16:24:30 +00:00
|
|
|
);
|
2016-12-16 20:46:49 +00:00
|
|
|
$handler
|
|
|
|
->expects( $this->at( 7 ) )
|
|
|
|
->method( 'handle' )
|
|
|
|
->with(
|
|
|
|
$this->greaterThanOrEqual( $time ),
|
|
|
|
$this->equalTo( 'emergency' ),
|
|
|
|
$this->equalTo( 'emergency message' ),
|
|
|
|
$this->equalTo( array() )
|
|
|
|
);
|
|
|
|
|
2016-12-11 16:24:30 +00:00
|
|
|
return $handler;
|
|
|
|
}
|
2015-04-08 15:55:19 +00:00
|
|
|
}
|