From 1bd6911b34e3f9cabd2b98d6be33392b28a6ea94 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Sat, 18 May 2024 15:50:36 +0200 Subject: [PATCH 1/2] fix(core-phpunit) avoid calling db_connect twice --- .../core-phpunit/includes/abstract-testcase.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/includes/core-phpunit/includes/abstract-testcase.php b/includes/core-phpunit/includes/abstract-testcase.php index 49e517ff9..f2978644f 100644 --- a/includes/core-phpunit/includes/abstract-testcase.php +++ b/includes/core-phpunit/includes/abstract-testcase.php @@ -20,8 +20,6 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { protected $expected_doing_it_wrong = array(); protected $caught_doing_it_wrong = array(); - private static ?string $calledClass = null; - protected static $hooks_saved = array(); protected static $ignore_files; @@ -39,7 +37,7 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * * @return WP_UnitTest_Factory The fixture factory. */ - public static function factory() { + protected static function factory() { static $factory = null; if ( ! $factory ) { $factory = new WP_UnitTest_Factory(); @@ -55,7 +53,7 @@ public static function factory() { * @return string The class name. */ public static function get_called_class() { - return self::$called_class ?? get_called_class(); + return get_called_class(); } /** @@ -71,7 +69,7 @@ public static function set_up_before_class() { $wpdb->db_connect(); ini_set( 'display_errors', 1 ); - $class = self::$calledClass ?? get_called_class(); + $class = get_called_class(); if ( method_exists( $class, 'wpSetUpBeforeClass' ) ) { call_user_func( array( $class, 'wpSetUpBeforeClass' ), static::factory() ); @@ -84,7 +82,7 @@ public static function set_up_before_class() { * Runs the routine after all tests have been run. */ public static function tear_down_after_class() { - $class = self::$calledClass ?? get_called_class(); + $class = get_called_class(); if ( method_exists( $class, 'wpTearDownAfterClass' ) ) { call_user_func( array( $class, 'wpTearDownAfterClass' ) ); @@ -653,7 +651,7 @@ public function expectedDeprecated() { * * @since 4.2.0 */ - public function assert_post_conditions() { + protected function assert_post_conditions() { $this->expectedDeprecated(); } @@ -1662,9 +1660,4 @@ public static function touch( $file ) { touch( $file ); } - - public function setCalledClass(string $class): void - { - self::$calledClass = $class; - } } From e7fd8094217b8e47faecd2fc49ffb5d6bad91f1d Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Sat, 18 May 2024 15:55:15 +0200 Subject: [PATCH 2/2] fix(WPLoader) avoid calling wpdb::db_connect twice --- CHANGELOG.md | 4 ++++ .../includes/abstract-testcase.php.patch | 21 +++++++++++------ .../includes/abstract-testcase.php | 23 +++++++++++++------ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c0cf32a6..e6ae16c48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [unreleased] Unreleased +## Fixed + +- Avoid calling `wpdb::db_connect()` twice during `WPLoader` bootstrap. (thanks @calvinalkan) + ## [4.1.8] 2024-05-13; ### Changed diff --git a/config/patches/core-phpunit/includes/abstract-testcase.php.patch b/config/patches/core-phpunit/includes/abstract-testcase.php.patch index 8ea69b774..58b21b7b5 100644 --- a/config/patches/core-phpunit/includes/abstract-testcase.php.patch +++ b/config/patches/core-phpunit/includes/abstract-testcase.php.patch @@ -1,5 +1,5 @@ diff --git a/includes/core-phpunit/includes/abstract-testcase.php b/includes/core-phpunit/includes/abstract-testcase.php -index 3600722f..67c4d71c 100644 +index f2978644..e092beca 100644 --- a/includes/core-phpunit/includes/abstract-testcase.php +++ b/includes/core-phpunit/includes/abstract-testcase.php @@ -20,6 +20,8 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { @@ -29,16 +29,23 @@ index 3600722f..67c4d71c 100644 } /** -@@ -69,7 +71,7 @@ public static function set_up_before_class() { - $wpdb->db_connect(); - ini_set( 'display_errors', 1 ); +@@ -66,10 +68,12 @@ public static function set_up_before_class() { + + $wpdb->suppress_errors = false; + $wpdb->show_errors = true; +- $wpdb->db_connect(); +- ini_set( 'display_errors', 1 ); ++ if ( ! $wpdb->check_connection() ) { ++ $wpdb->db_connect(); ++ } ++ ini_set( 'display_errors', 1 ); - $class = get_called_class(); + $class = self::$calledClass ?? get_called_class(); if ( method_exists( $class, 'wpSetUpBeforeClass' ) ) { call_user_func( array( $class, 'wpSetUpBeforeClass' ), static::factory() ); -@@ -82,7 +84,7 @@ public static function set_up_before_class() { +@@ -82,7 +86,7 @@ public static function set_up_before_class() { * Runs the routine after all tests have been run. */ public static function tear_down_after_class() { @@ -47,7 +54,7 @@ index 3600722f..67c4d71c 100644 if ( method_exists( $class, 'wpTearDownAfterClass' ) ) { call_user_func( array( $class, 'wpTearDownAfterClass' ) ); -@@ -646,7 +648,7 @@ public function expectedDeprecated() { +@@ -651,7 +655,7 @@ public function expectedDeprecated() { * * @since 4.2.0 */ @@ -56,7 +63,7 @@ index 3600722f..67c4d71c 100644 $this->expectedDeprecated(); } -@@ -1655,4 +1657,9 @@ public static function touch( $file ) { +@@ -1660,4 +1664,9 @@ public static function touch( $file ) { touch( $file ); } diff --git a/includes/core-phpunit/includes/abstract-testcase.php b/includes/core-phpunit/includes/abstract-testcase.php index f2978644f..e092beca7 100644 --- a/includes/core-phpunit/includes/abstract-testcase.php +++ b/includes/core-phpunit/includes/abstract-testcase.php @@ -20,6 +20,8 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { protected $expected_doing_it_wrong = array(); protected $caught_doing_it_wrong = array(); + private static ?string $calledClass = null; + protected static $hooks_saved = array(); protected static $ignore_files; @@ -37,7 +39,7 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { * * @return WP_UnitTest_Factory The fixture factory. */ - protected static function factory() { + public static function factory() { static $factory = null; if ( ! $factory ) { $factory = new WP_UnitTest_Factory(); @@ -53,7 +55,7 @@ protected static function factory() { * @return string The class name. */ public static function get_called_class() { - return get_called_class(); + return self::$called_class ?? get_called_class(); } /** @@ -66,10 +68,12 @@ public static function set_up_before_class() { $wpdb->suppress_errors = false; $wpdb->show_errors = true; - $wpdb->db_connect(); - ini_set( 'display_errors', 1 ); + if ( ! $wpdb->check_connection() ) { + $wpdb->db_connect(); + } + ini_set( 'display_errors', 1 ); - $class = get_called_class(); + $class = self::$calledClass ?? get_called_class(); if ( method_exists( $class, 'wpSetUpBeforeClass' ) ) { call_user_func( array( $class, 'wpSetUpBeforeClass' ), static::factory() ); @@ -82,7 +86,7 @@ public static function set_up_before_class() { * Runs the routine after all tests have been run. */ public static function tear_down_after_class() { - $class = get_called_class(); + $class = self::$calledClass ?? get_called_class(); if ( method_exists( $class, 'wpTearDownAfterClass' ) ) { call_user_func( array( $class, 'wpTearDownAfterClass' ) ); @@ -651,7 +655,7 @@ public function expectedDeprecated() { * * @since 4.2.0 */ - protected function assert_post_conditions() { + public function assert_post_conditions() { $this->expectedDeprecated(); } @@ -1660,4 +1664,9 @@ public static function touch( $file ) { touch( $file ); } + + public function setCalledClass(string $class): void + { + self::$calledClass = $class; + } }