2015-04-08 15:55:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Class Log.
|
2015-04-08 15:55:19 +00:00
|
|
|
* @package WooCommerce\Tests\Util
|
|
|
|
* @since 2.3
|
|
|
|
*/
|
2016-03-23 12:14:13 +00:00
|
|
|
class WC_Tests_Log extends WC_Unit_Test_Case {
|
2016-11-13 17:45:45 +00:00
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
$log_files = array(
|
|
|
|
wc_get_log_file_path( 'unit-tests' ),
|
|
|
|
wc_get_log_file_path( 'log' ),
|
2016-11-13 21:14:35 +00:00
|
|
|
wc_get_log_file_path( 'A' ),
|
2016-11-13 17:45:45 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $log_files as $file ) {
|
|
|
|
if ( file_exists( $file ) && is_writable( $file ) ) {
|
|
|
|
unlink( $file );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-08-08 12:59:23 +00:00
|
|
|
$log = wc_get_logger();
|
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-08-08 12:59:23 +00:00
|
|
|
$log = wc_get_logger();
|
2016-11-12 18:42:54 +00:00
|
|
|
$log->clear();
|
2016-11-13 17:45:45 +00:00
|
|
|
$this->setExpectedDeprecated( 'WC_Logger::clear' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test log() complains for bad levels.
|
|
|
|
*/
|
|
|
|
public function test_bad_level() {
|
|
|
|
$log = wc_get_logger();
|
|
|
|
$log->log( 'this-is-an-invalid-level', '' );
|
|
|
|
$this->setExpectedIncorrectUsage( 'WC_Logger::log' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test log().
|
|
|
|
*/
|
|
|
|
public function test_log() {
|
|
|
|
$log = wc_get_logger();
|
|
|
|
$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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test consumed logs do not bubble.
|
|
|
|
*/
|
|
|
|
public function test_log_entry_is_consumed() {
|
2016-11-13 21:35:51 +00:00
|
|
|
add_filter( 'woocommerce_register_log_handlers', array( $this, '_return_consume_error_handlers' ) );
|
2016-11-13 17:45:45 +00:00
|
|
|
|
|
|
|
$log = wc_get_logger();
|
|
|
|
$log->log( 'emergency', '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test unconsumed logs bubble.
|
|
|
|
*/
|
|
|
|
public function test_log_entry_bubbles() {
|
2016-11-13 21:35:51 +00:00
|
|
|
add_filter( 'woocommerce_register_log_handlers', array( $this, '_return_bubble_required_handlers' ) );
|
2016-11-13 17:45:45 +00:00
|
|
|
|
|
|
|
$log = wc_get_logger();
|
|
|
|
$log->log( 'emergency', '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test WC_Logger->[debug..emergency] methods
|
|
|
|
*/
|
|
|
|
public function test_level_methods() {
|
|
|
|
$log = wc_get_logger();
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
/**
|
|
|
|
* Test 'tag' context determines log file.
|
|
|
|
*/
|
|
|
|
public function test_log_file_tag() {
|
|
|
|
$log = wc_get_logger();
|
2016-11-13 22:28:38 +00:00
|
|
|
$context_tag = array( 'tag' => 'A' );
|
2016-11-13 20:05:41 +00:00
|
|
|
|
2016-11-13 22:28:38 +00:00
|
|
|
$log->info( 'info', $context_tag );
|
|
|
|
$log->notice( 'notice', $context_tag );
|
|
|
|
$log->warning( 'warning', $context_tag );
|
|
|
|
$log->error( 'error', $context_tag );
|
|
|
|
$log->critical( 'critical', $context_tag );
|
|
|
|
$log->alert( 'alert', $context_tag );
|
|
|
|
$log->emergency( 'emergency', $context_tag );
|
2016-11-13 20:05:41 +00:00
|
|
|
|
|
|
|
$log_content = $this->read_content( 'A' );
|
|
|
|
$this->assertStringMatchesFormatFile( dirname( __FILE__ ) . '/test_log_expected.txt', $log_content );
|
|
|
|
}
|
2016-11-13 21:35:51 +00:00
|
|
|
|
|
|
|
public function _return_bubble_required_handlers() {
|
2016-11-13 22:28:38 +00:00
|
|
|
$bubble = $this->createMock( 'WC_Log_Handler' );
|
2016-11-13 21:35:51 +00:00
|
|
|
$bubble->method( 'handle' )->willReturn( true );
|
|
|
|
|
2016-11-13 22:28:38 +00:00
|
|
|
$required = $this->createMock( 'WC_Log_Handler' );
|
2016-11-13 21:35:51 +00:00
|
|
|
$required->expects( $this->once() )->method( 'handle' );
|
|
|
|
|
|
|
|
return array( $bubble, $required );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function _return_consume_error_handlers() {
|
2016-11-13 22:28:38 +00:00
|
|
|
$consume = $this->createMock( 'WC_Log_Handler' );
|
2016-11-13 21:35:51 +00:00
|
|
|
$consume->method( 'handle' )->willReturn( false );
|
|
|
|
|
2016-11-13 22:28:38 +00:00
|
|
|
$error = $this->createMock( 'WC_Log_Handler' );
|
2016-11-13 21:35:51 +00:00
|
|
|
$error->method( 'handle' )->will( $this->throwException( new Exception( 'Log was not consumed.' ) ) );
|
|
|
|
|
|
|
|
return array( $consume, $error );
|
|
|
|
}
|
2015-04-08 15:55:19 +00:00
|
|
|
}
|