diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php index 81f9686050..35d0a62e54 100644 --- a/tests/phpunit/includes/testcase.php +++ b/tests/phpunit/includes/testcase.php @@ -53,6 +53,8 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $this->start_transaction(); $this->expectDeprecated(); add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) ); + + add_filter( 'wp_mail', array( $this, 'set_wp_mail_globals' ) ); } /** @@ -569,4 +571,30 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase { $time_array = explode( ' ', $microtime ); return array_sum( $time_array ); } + + /** + * When `wp_mail()` is called, make sure `$_SERVER['SERVER_NAME']` is faked. + * + * @since 4.3.0 + * + * @param array $args `wp_mail()` arguments. + * @return array $args + */ + public function set_wp_mail_globals( $args ) { + if ( ! isset( $_SERVER['SERVER_NAME'] ) ) { + $_SERVER['SERVER_NAME'] = 'example.com'; + add_action( 'phpmailer_init', array( $this, 'tear_down_wp_mail_globals' ) ); + } + + return $args; + } + + /** + * Tear down the faked `$_SERVER['SERVER_NAME']` global used in `wp_mail()`. + * + * @since 4.3.0 + */ + public function tear_down_wp_mail_globals() { + unset( $_SERVER['SERVER_NAME'] ); + } } diff --git a/tests/phpunit/tests/mail.php b/tests/phpunit/tests/mail.php index 4053966d71..dc000f46e3 100644 --- a/tests/phpunit/tests/mail.php +++ b/tests/phpunit/tests/mail.php @@ -6,15 +6,9 @@ class Tests_Mail extends WP_UnitTestCase { function setUp() { parent::setUp(); - $_SERVER['SERVER_NAME'] = 'example.com'; unset( $GLOBALS['phpmailer']->mock_sent ); } - function tearDown() { - unset( $_SERVER['SERVER_NAME'] ); - parent::tearDown(); - } - function test_wp_mail_custom_boundaries() { $to = 'user@example.com'; $subject = 'Test email with custom boundaries';