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-12-03 21:42:43 +00:00
|
|
|
*
|
2016-12-11 12:27:49 +00:00
|
|
|
* @todo isolate tests from file handler
|
2015-04-08 15:55:19 +00:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
$log_files = array(
|
|
|
|
wc_get_log_file_path( 'unit-tests' ),
|
|
|
|
wc_get_log_file_path( 'log' ),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $log_files as $file ) {
|
|
|
|
if ( file_exists( $file ) && is_writable( $file ) ) {
|
|
|
|
unlink( $file );
|
|
|
|
}
|
|
|
|
}
|
2016-11-15 20:19:00 +00:00
|
|
|
parent::tearDown();
|
2016-11-13 17:45:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:55:19 +00:00
|
|
|
public function read_content( $handle ) {
|
|
|
|
return file_get_contents( wc_get_log_file_path( $handle ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 12:46:26 +00:00
|
|
|
$log = new WC_Logger( array( new WC_Log_Handler_File() ), 'debug' );
|
2015-04-08 15:55:19 +00:00
|
|
|
|
|
|
|
$log->add( 'unit-tests', 'this is a message' );
|
|
|
|
|
2015-09-12 14:18:41 +00:00
|
|
|
$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' ) );
|
2016-11-13 17:45:45 +00:00
|
|
|
$this->setExpectedDeprecated( 'WC_Logger::add' );
|
2015-04-08 15:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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() {
|
2016-12-11 12:27:49 +00:00
|
|
|
$log = new WC_Logger( null, 'debug' );
|
2016-11-13 22:56:11 +00:00
|
|
|
$log->clear( 'log' );
|
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
|
|
|
*
|
|
|
|
* @since 2.8
|
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
|
|
|
*
|
|
|
|
* @since 2.8
|
2016-11-13 17:45:45 +00:00
|
|
|
*/
|
|
|
|
public function test_log() {
|
2016-12-11 12:46:26 +00:00
|
|
|
$log = new WC_Logger( array( new WC_Log_Handler_File() ), 'debug' );
|
2016-11-13 17:45:45 +00:00
|
|
|
$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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-10 21:21:41 +00:00
|
|
|
* Test logs passed to all handlers.
|
2016-11-26 12:11:14 +00:00
|
|
|
*
|
|
|
|
* @since 2.8
|
2016-11-13 17:45:45 +00:00
|
|
|
*/
|
2016-12-10 21:21:41 +00:00
|
|
|
public function test_log_() {
|
|
|
|
add_filter( 'woocommerce_register_log_handlers', array( $this, 'return_assertion_handlers' ) );
|
2016-11-13 17:45:45 +00:00
|
|
|
|
2016-12-11 12:27:49 +00:00
|
|
|
$log = new WC_Logger( null, '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-12-10 21:21:41 +00:00
|
|
|
remove_filter( 'woocommerce_register_log_handlers', array( $this, 'return_assertion_handlers' ) );
|
2016-11-13 17:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test WC_Logger->[debug..emergency] methods
|
2016-11-26 12:11:14 +00:00
|
|
|
*
|
|
|
|
* @since 2.8
|
2016-11-13 17:45:45 +00:00
|
|
|
*/
|
|
|
|
public function test_level_methods() {
|
2016-12-11 12:46:26 +00:00
|
|
|
$log = new WC_Logger( array( new WC_Log_Handler_File() ), 'debug' );
|
2016-11-13 17:45:45 +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' );
|
|
|
|
|
|
|
|
$log_content = $this->read_content( 'log' );
|
|
|
|
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
|
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-10 21:21:41 +00:00
|
|
|
* Helper for log handler test.
|
2016-11-15 19:45:26 +00:00
|
|
|
*
|
2016-12-10 21:21:41 +00:00
|
|
|
* Returns an array of 3 mocked log handlers.
|
|
|
|
* The first handler always returns false.
|
|
|
|
* The second handler always returns true.
|
|
|
|
* All handlers expects to receive exactly 8 messages (1 for each level).
|
2016-11-15 19:45:26 +00:00
|
|
|
*
|
2016-11-26 12:11:14 +00:00
|
|
|
* @since 2.8
|
|
|
|
*
|
2016-12-10 21:21:41 +00:00
|
|
|
* @return WC_Log_Handler[] array of mocked log handlers.
|
2016-11-15 19:45:26 +00:00
|
|
|
*/
|
2016-12-10 21:21:41 +00:00
|
|
|
public function return_assertion_handlers() {
|
|
|
|
$false_handler = $this
|
2016-11-15 19:45:26 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler' )
|
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
2016-12-10 21:21:41 +00:00
|
|
|
$false_handler->expects( $this->exactly( 8 ) )->method( 'handle' )->will( $this->returnValue( false ) );
|
2016-11-15 19:45:26 +00:00
|
|
|
|
2016-12-10 21:21:41 +00:00
|
|
|
$true_handler = $this
|
2016-11-15 19:45:26 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler' )
|
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
2016-12-10 21:21:41 +00:00
|
|
|
$false_handler->expects( $this->exactly( 8 ) )->method( 'handle' )->will( $this->returnValue( true ) );
|
2016-11-15 19:45:26 +00:00
|
|
|
|
2016-12-10 21:21:41 +00:00
|
|
|
$final_handler = $this
|
2016-11-15 19:45:26 +00:00
|
|
|
->getMockBuilder( 'WC_Log_Handler' )
|
|
|
|
->setMethods( array( 'handle' ) )
|
|
|
|
->getMock();
|
2016-12-10 21:21:41 +00:00
|
|
|
$final_handler->expects( $this->exactly( 8 ) )->method( 'handle' );
|
2016-11-15 19:45:26 +00:00
|
|
|
|
2016-12-10 21:21:41 +00:00
|
|
|
return array( $false_handler, $true_handler, $final_handler );
|
2016-11-13 21:35:51 +00:00
|
|
|
}
|
2016-12-03 21:42:43 +00:00
|
|
|
|
2015-04-08 15:55:19 +00:00
|
|
|
}
|