woocommerce/tests/unit-tests/log/log-handler-db.php

138 lines
4.3 KiB
PHP
Raw Normal View History

2016-11-26 20:45:36 +00:00
<?php
/**
* Class WC_Tests_Log_Handler_DB
* @package WooCommerce\Tests\Log
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-26 20:45:36 +00:00
*/
2016-11-27 18:37:20 +00:00
class WC_Tests_Log_Handler_DB extends WC_Unit_Test_Case {
public function setUp() {
parent::setUp();
WC_Log_Handler_DB::flush();
}
2016-11-26 20:45:36 +00:00
public function tearDown() {
WC_Log_Handler_DB::flush();
parent::tearDown();
}
/**
* Test handle writes to database correctly.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-26 20:45:36 +00:00
*/
public function test_handle() {
global $wpdb;
$handler = new WC_Log_Handler_DB( array( 'threshold' => 'debug' ) );
$time = time();
$context = array( 1, 2, 'a', 'b', 'key' => 'value' );
2016-11-26 20:45:36 +00:00
2016-12-21 19:15:19 +00:00
$handler->handle( $time, 'debug', 'msg_debug', array( 'source' => 'source_debug' ) );
$handler->handle( $time, 'info', 'msg_info', array( 'source' => 'source_info' ) );
$handler->handle( $time, 'notice', 'msg_notice', array( 'source' => 'source_notice' ) );
$handler->handle( $time, 'warning', 'msg_warning', array( 'source' => 'source_warning' ) );
$handler->handle( $time, 'error', 'msg_error', array( 'source' => 'source_error' ) );
$handler->handle( $time, 'critical', 'msg_critical', array( 'source' => 'source_critical' ) );
$handler->handle( $time, 'alert', 'msg_alert', array( 'source' => 'source_alert' ) );
$handler->handle( $time, 'emergency', 'msg_emergency', array( 'source' => 'source_emergency' ) );
2016-11-26 20:45:36 +00:00
$handler->handle( $time, 'debug', 'context_test', $context );
2016-12-21 19:15:19 +00:00
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source, context FROM {$wpdb->prefix}woocommerce_log", ARRAY_A );
2016-11-26 20:45:36 +00:00
$expected_ts = date( 'Y-m-d H:i:s', $time );
$expected = array(
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'debug' ),
2016-11-26 20:45:36 +00:00
'message' => 'msg_debug',
2016-12-21 19:15:19 +00:00
'source' => 'source_debug',
'context' => serialize( array( 'source' => 'source_debug' ) ),
2016-11-26 20:45:36 +00:00
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'info' ),
2016-11-26 20:45:36 +00:00
'message' => 'msg_info',
2016-12-21 19:15:19 +00:00
'source' => 'source_info',
'context' => serialize( array( 'source' => 'source_info' ) ),
2016-11-26 20:45:36 +00:00
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'notice' ),
2016-11-26 20:45:36 +00:00
'message' => 'msg_notice',
2016-12-21 19:15:19 +00:00
'source' => 'source_notice',
'context' => serialize( array( 'source' => 'source_notice' ) ),
2016-11-26 20:45:36 +00:00
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'warning' ),
2016-11-26 20:45:36 +00:00
'message' => 'msg_warning',
2016-12-21 19:15:19 +00:00
'source' => 'source_warning',
'context' => serialize( array( 'source' => 'source_warning' ) ),
2016-11-26 20:45:36 +00:00
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'error' ),
2016-11-26 20:45:36 +00:00
'message' => 'msg_error',
2016-12-21 19:15:19 +00:00
'source' => 'source_error',
'context' => serialize( array( 'source' => 'source_error' ) ),
2016-11-26 20:45:36 +00:00
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'critical' ),
2016-11-26 20:45:36 +00:00
'message' => 'msg_critical',
2016-12-21 19:15:19 +00:00
'source' => 'source_critical',
'context' => serialize( array( 'source' => 'source_critical' ) ),
2016-11-26 20:45:36 +00:00
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'alert' ),
2016-11-26 20:45:36 +00:00
'message' => 'msg_alert',
2016-12-21 19:15:19 +00:00
'source' => 'source_alert',
'context' => serialize( array( 'source' => 'source_alert' ) ),
2016-11-26 20:45:36 +00:00
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'emergency' ),
2016-11-26 20:45:36 +00:00
'message' => 'msg_emergency',
2016-12-21 19:15:19 +00:00
'source' => 'source_emergency',
'context' => serialize( array( 'source' => 'source_emergency' ) ),
),
array(
'timestamp' => $expected_ts,
'level' => WC_Log_Levels::get_level_severity( 'debug' ),
'message' => 'context_test',
'source' => pathinfo( __FILE__, PATHINFO_FILENAME ),
'context' => serialize( $context ),
2016-11-26 20:45:36 +00:00
),
);
$this->assertEquals( $log_entries, $expected );
}
/**
* Test flush.
*
2017-03-15 16:36:53 +00:00
* @since 3.0.0
2016-11-26 20:45:36 +00:00
*/
public function test_flush() {
global $wpdb;
$handler = new WC_Log_Handler_DB( array( 'threshold' => 'debug' ) );
$time = time();
$handler->handle( $time, 'debug', '', array() );
2016-12-21 19:15:19 +00:00
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source FROM {$wpdb->prefix}woocommerce_log" );
2016-11-26 20:45:36 +00:00
$this->assertCount( 1, $log_entries );
WC_Log_Handler_DB::flush();
2016-12-21 19:15:19 +00:00
$log_entries = $wpdb->get_results( "SELECT timestamp, level, message, source FROM {$wpdb->prefix}woocommerce_log" );
2016-11-26 20:45:36 +00:00
$this->assertCount( 0, $log_entries );
}
}