Increase PHPUnit Test Environment Resiliency (#37792)

This commit is contained in:
Barry Hughes 2023-04-18 13:04:37 -07:00 committed by GitHub
commit 4ec741f7b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 18 deletions

View File

@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: Changing PHPUnit tests

View File

@ -43,7 +43,9 @@ class WC_Template_Cache extends WC_Unit_Test_Case {
// Check individual templates.
foreach ( $templates as $cache_key => $template ) {
$this->assertEquals( $template, wp_cache_get( $cache_key, 'woocommerce' ) );
// The cache contents may be tokenized and we need to untokenize it for the assertion.
$from_cache = wc_untokenize_path( wp_cache_get( $cache_key, 'woocommerce' ), wc_get_path_define_tokens() );
$this->assertEquals( $template, $from_cache );
}
// Clear cache.

View File

@ -18,12 +18,20 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
public function setUp(): void {
parent::setUp();
reset_phpmailer_instance();
add_filter(
'wp_mail_from',
array( $this, 'filter_test_email' )
);
}
/**
* Test teardown.
*/
public function tearDown(): void {
remove_filter(
'wp_mail_from',
array( $this, 'filter_test_email' )
);
reset_phpmailer_instance();
parent::tearDown();
}
@ -41,8 +49,9 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
$handler = new WC_Log_Handler_Email();
$handler->handle( $time, 'emergency', 'msg_emergency', array() );
$handler->handle( $time, 'emergency', 'msg_emergency 2', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$this->assertEquals(
(
'You have received the following WooCommerce log messages:'
@ -67,7 +76,9 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
$this->assertEquals( get_option( 'admin_email' ), $mailer->get_recipient( 'to' )->address );
$handler->handle( $time, 'emergency', 'msg_emergency', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$this->assertEquals(
(
'You have received the following WooCommerce log message:'
@ -107,12 +118,16 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
$handler = new WC_Log_Handler_Email( null, WC_Log_Levels::DEBUG );
$handler->handle( $time, 'debug', '', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$handler->handle( $time, 'alert', '', array() );
$handler->handle( $time, 'critical', '', array() );
$handler->handle( $time, 'debug', '', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$this->assertEquals(
"[{$site_name}] DEBUG: 1 WooCommerce log message",
@ -140,7 +155,9 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
)
);
$handler->handle( time(), 'emergency', '', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$first_recipient = $mailer->get_recipient( 'to', 0, 0 );
$second_recipient = $mailer->get_recipient( 'to', 0, 1 );
@ -160,7 +177,9 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
$handler = new WC_Log_Handler_Email( 'User <user@test.com>' );
$handler->handle( time(), 'emergency', '', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$recipient = $mailer->get_recipient( 'to' );
$this->assertEquals( 'user@test.com', $recipient->address );
@ -177,14 +196,16 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
$handler = new WC_Log_Handler_Email( null, 'notice' );
$handler->handle( time(), 'info', '', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
// Info should not be handled, get_sent is false.
$this->assertFalse( $result );
$this->assertFalse( $mailer->get_sent( 0 ) );
$handler->handle( time(), 'notice', '', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$this->assertTrue( property_exists( $mailer->get_sent( 0 ), 'body' ) );
}
@ -198,15 +219,17 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
$handler = new WC_Log_Handler_Email( null, 'notice' );
$handler->handle( time(), 'info', '', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
// Info should not be handled, get_sent is false.
$this->assertFalse( $result );
$this->assertFalse( $mailer->get_sent( 0 ) );
$handler->set_threshold( 'info' );
$handler->handle( time(), 'info', '', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$this->assertTrue( property_exists( $mailer->get_sent( 0 ), 'body' ) );
}
@ -224,9 +247,13 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
$handler = new WC_Log_Handler_Email();
$time = time();
$handler->handle( $time, 'emergency', 'message 1', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$handler->handle( $time, 'emergency', 'message 2', array() );
$handler->send_log_email();
$result = $handler->send_log_email();
$this->assertTrue( $result );
$site_name = get_bloginfo( 'name' );
@ -262,4 +289,13 @@ class WC_Tests_Log_Handler_Email extends WC_Unit_Test_Case {
$this->normalize_eol( $mailer->get_sent( 1 )->body )
);
}
/**
* Filters the "from" address in emails to use the expected test email.
*
* @return string
*/
public function filter_test_email() {
return WP_TESTS_EMAIL;
}
}

View File

@ -402,7 +402,7 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
if ( $replacement_object->state ) {
return $replacement_object->state;
} else {
return $replacement_object->original_object->last_error;
return $replacement_object->decorated_object->last_error;
}
}
);
@ -514,8 +514,8 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
$wpdb_mock->register_method_replacement(
'get_results',
function( ...$args ) {
$wpdb_decorator = $args[0];
$wpdb_decorator->original_object->last_error = 'Something failed!';
$wpdb_decorator = $args[0];
$wpdb_decorator->decorated_object->last_error = 'Something failed!';
return false;
}
);
@ -591,8 +591,8 @@ WHERE order_id = {$order_id} AND meta_key = 'non_unique_key_1' AND meta_value in
$wpdb_mock->register_method_replacement(
'get_results',
function( ...$args ) {
$wpdb_decorator = $args[0];
$wpdb_decorator->original_object->last_error = 'Something failed!';
$wpdb_decorator = $args[0];
$wpdb_decorator->decorated_object->last_error = 'Something failed!';
return false;
}
);