From 172b4491afa8a8172a60edcda34ba76a866028c7 Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Fri, 24 May 2024 17:02:27 +0200 Subject: [PATCH] fix(abstract-testcase) fail on dropped db connection --- .../includes/abstract-testcase.php.patch | 4 +- .../includes/abstract-testcase.php | 4 +- .../WPLoaderArbitraryPluginLocationTest.php | 2 +- .../WPLoaderArbitraryThemeLocationTest.php | 2 +- .../Module/WPLoaderDbConnectionClosedTest.php | 88 +++++++++++++++++++ 5 files changed, 94 insertions(+), 6 deletions(-) create mode 100644 tests/unit/lucatume/WPBrowser/Module/WPLoaderDbConnectionClosedTest.php diff --git a/config/patches/core-phpunit/includes/abstract-testcase.php.patch b/config/patches/core-phpunit/includes/abstract-testcase.php.patch index 76c265abe..2c593caca 100644 --- a/config/patches/core-phpunit/includes/abstract-testcase.php.patch +++ b/config/patches/core-phpunit/includes/abstract-testcase.php.patch @@ -35,8 +35,8 @@ index f2978644..e092beca 100644 $wpdb->show_errors = true; - $wpdb->db_connect(); - ini_set( 'display_errors', 1 ); -+ if ( ! $wpdb->check_connection(false) ) { -+ $wpdb->db_connect(); ++ if ( empty( lucatume\WPBrowser\Utils\Property::readPrivate( $wpdb, 'dbh' ) ) ) { ++ self::fail( 'The database connection went away. A `setUpBeforeClassMethod` likely closed the connection.' ); + } + ini_set( 'display_errors', 1 ); diff --git a/includes/core-phpunit/includes/abstract-testcase.php b/includes/core-phpunit/includes/abstract-testcase.php index 7475e1ed4..4c233b408 100644 --- a/includes/core-phpunit/includes/abstract-testcase.php +++ b/includes/core-phpunit/includes/abstract-testcase.php @@ -68,8 +68,8 @@ public static function set_up_before_class() { $wpdb->suppress_errors = false; $wpdb->show_errors = true; - if ( ! $wpdb->check_connection(false) ) { - $wpdb->db_connect(); + if ( empty( lucatume\WPBrowser\Utils\Property::readPrivate( $wpdb, 'dbh' ) ) ) { + self::fail( 'The database connection went away. A `setUpBeforeClassMethod` likely closed the connection.' ); } ini_set( 'display_errors', 1 ); diff --git a/tests/unit/lucatume/WPBrowser/Module/WPLoaderArbitraryPluginLocationTest.php b/tests/unit/lucatume/WPBrowser/Module/WPLoaderArbitraryPluginLocationTest.php index 84c7c1bf3..839a45a00 100644 --- a/tests/unit/lucatume/WPBrowser/Module/WPLoaderArbitraryPluginLocationTest.php +++ b/tests/unit/lucatume/WPBrowser/Module/WPLoaderArbitraryPluginLocationTest.php @@ -1,6 +1,6 @@ mockModuleContainer = new ModuleContainer(new Di(), $moduleContainerConfig); + return new WPLoader($this->mockModuleContainer, ($moduleConfig ?? $this->config)); + } + + public function test_will_fail_if_db_connection_closed_during_setup_before_class(): void + { + $wpRootDir = FS::tmpDir('wploader_'); + $dbName = Random::dbName(); + $dbHost = Env::get('WORDPRESS_DB_HOST'); + $dbUser = Env::get('WORDPRESS_DB_USER'); + $dbPassword = Env::get('WORDPRESS_DB_PASSWORD'); + $db = new MysqlDatabase($dbName, $dbUser, $dbPassword, $dbHost); + Installation::scaffold($wpRootDir); + $db->create(); + $this->config = [ + 'wpRootFolder' => $wpRootDir, + 'dbUrl' => $db->getDbUrl() + ]; + $testcaseFile = $wpRootDir . '/BreakingTest.php'; + $testCaseFileContents = <<< PHP + close(); + + parent::set_up_before_class(); + } + + public function test_something():void{ + \$this->assertTrue(true); + } + } + PHP; + if(!file_put_contents($testcaseFile, $testCaseFileContents, LOCK_EX)) { + throw new \RuntimeException('Could not write BreakingTest.php.'); + } + + $wpLoader = $this->module(); + + $this->assertInIsolation(static function () use ($wpLoader, $testcaseFile) { + $wpLoader->_initialize(); + + require_once $testcaseFile; + + try { + \BreakingTest::setUpBeforeClass(); + } catch (\Throwable $e) { + Assert::assertStringContainsString( + 'The database connection went away. A `setUpBeforeClassMethod` likely closed the connection', + $e->getMessage() + ); + return; + } + + Assert::fail('The test should have failed.'); + }); + } +}