Skip to content

Commit

Permalink
feat(WPLoader) add wpdb connection id strict check
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed May 25, 2024
1 parent 06b97bc commit a37d494
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 101 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### Changed

- Better messaging when throwing due to disconnected database.
- Add the `WPLoader::beStrictAboutWpdbConnectionId` configuration parameter, defaults to `true`, to throw if db connection changes during setup before class.

## [4.2.1] 2024-05-24;

Expand Down
28 changes: 19 additions & 9 deletions config/patches/core-phpunit/includes/abstract-testcase.php.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
diff --git a/includes/core-phpunit/includes/abstract-testcase.php b/includes/core-phpunit/includes/abstract-testcase.php
index f2978644..e092beca 100644
index f2978644..22427643 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 {
@@ -1,5 +1,7 @@
<?php

+use lucatume\WPBrowser\TestCase\WPTestCase;
+
require_once __DIR__ . '/factory.php';
require_once __DIR__ . '/trac.php';

@@ -20,6 +22,8 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
protected $expected_doing_it_wrong = array();
protected $caught_doing_it_wrong = array();

Expand All @@ -11,7 +19,7 @@ index f2978644..e092beca 100644
protected static $hooks_saved = array();
protected static $ignore_files;

@@ -37,7 +39,7 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
@@ -37,7 +41,7 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase {
*
* @return WP_UnitTest_Factory The fixture factory.
*/
Expand All @@ -20,7 +28,7 @@ index f2978644..e092beca 100644
static $factory = null;
if ( ! $factory ) {
$factory = new WP_UnitTest_Factory();
@@ -53,7 +55,7 @@ protected static function factory() {
@@ -53,7 +57,7 @@ protected static function factory() {
* @return string The class name.
*/
public static function get_called_class() {
Expand All @@ -29,14 +37,16 @@ index f2978644..e092beca 100644
}

/**
@@ -66,10 +68,12 @@ public static function set_up_before_class() {
@@ -66,10 +70,14 @@ public static function set_up_before_class() {

$wpdb->suppress_errors = false;
$wpdb->show_errors = true;
- $wpdb->db_connect();
- ini_set( 'display_errors', 1 );
+ if ( empty( lucatume\WPBrowser\Utils\Property::readPrivate( $wpdb, 'dbh' ) ) ) {
+ if ( WPTestCase::isStrictAboutWpdbConnectionId() && $wpdb->get_var( 'SELECT CONNECTION_ID()' ) !== WPTestCase::getWpdbConnectionId() ) {
+ self::fail( 'The database connection went away. A `setUpBeforeClassMethod` likely closed the connection.' );
+ } else {
+ $wpdb->check_connection(false);
+ }
+ ini_set( 'display_errors', 1 );

Expand All @@ -45,7 +55,7 @@ index f2978644..e092beca 100644

if ( method_exists( $class, 'wpSetUpBeforeClass' ) ) {
call_user_func( array( $class, 'wpSetUpBeforeClass' ), static::factory() );
@@ -82,7 +86,7 @@ public static function set_up_before_class() {
@@ -82,7 +90,7 @@ public static function set_up_before_class() {
* Runs the routine after all tests have been run.
*/
public static function tear_down_after_class() {
Expand All @@ -54,7 +64,7 @@ index f2978644..e092beca 100644

if ( method_exists( $class, 'wpTearDownAfterClass' ) ) {
call_user_func( array( $class, 'wpTearDownAfterClass' ) );
@@ -651,7 +655,7 @@ public function expectedDeprecated() {
@@ -651,7 +659,7 @@ public function expectedDeprecated() {
*
* @since 4.2.0
*/
Expand All @@ -63,7 +73,7 @@ index f2978644..e092beca 100644
$this->expectedDeprecated();
}

@@ -1660,4 +1664,9 @@ public static function touch( $file ) {
@@ -1660,4 +1668,9 @@ public static function touch( $file ) {

touch( $file );
}
Expand Down
2 changes: 2 additions & 0 deletions docs/modules/WPLoader.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ When used in this mode, the module supports the following configuration paramete
the following runs. To force the installation to run again, rerun the suite using the WPLoader module using
the `--debug` flag or delete the `_wploader-state.sql` file in the suite directory. This configuration parameter is
ignored when the `loadOnly` parameter is set to `true`.
* `beStrictAboutWpdbConnectionId` - a boolean value to indicate if the `WPTestCase` class should throw an exception if
the database connection is closed during any `setUpBeforeClass` method; default is `true`.

This is an example of an integration suite configured to use the module:

Expand Down
6 changes: 5 additions & 1 deletion includes/core-phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use lucatume\WPBrowser\TestCase\WPTestCase;

require_once __DIR__ . '/factory.php';
require_once __DIR__ . '/trac.php';

Expand Down Expand Up @@ -68,8 +70,10 @@ public static function set_up_before_class() {

$wpdb->suppress_errors = false;
$wpdb->show_errors = true;
if ( empty( lucatume\WPBrowser\Utils\Property::readPrivate( $wpdb, 'dbh' ) ) ) {
if ( WPTestCase::isStrictAboutWpdbConnectionId() && $wpdb->get_var( 'SELECT CONNECTION_ID()' ) !== WPTestCase::getWpdbConnectionId() ) {
self::fail( 'The database connection went away. A `setUpBeforeClassMethod` likely closed the connection.' );
} else {
$wpdb->check_connection(false);
}
ini_set( 'display_errors', 1 );

Expand Down
19 changes: 17 additions & 2 deletions src/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use lucatume\WPBrowser\Process\Loop;
use lucatume\WPBrowser\Process\ProcessException;
use lucatume\WPBrowser\Process\WorkerException;
use lucatume\WPBrowser\TestCase\WPTestCase;
use lucatume\WPBrowser\Utils\Arr;
use lucatume\WPBrowser\Utils\CorePHPUnit;
use lucatume\WPBrowser\Utils\Db as DbUtils;
Expand Down Expand Up @@ -127,6 +128,7 @@ class WPLoader extends Module
* backupStaticAttributes?: bool,
* backupStaticAttributesExcludeList?: array<string,string[]>,
* skipInstall?: bool,
* beStrictAboutWpdbConnectionId?: bool
* }
*/
protected array $config = [
Expand Down Expand Up @@ -164,7 +166,8 @@ class WPLoader extends Module
'backupGlobalsExcludeList' => [],
'backupStaticAttributes' => false,
'backupStaticAttributesExcludeList' => [],
'skipInstall' => false
'skipInstall' => false,
'beStrictAboutWpdbConnectionId' => true
];

private string $wpBootstrapFile;
Expand Down Expand Up @@ -318,6 +321,14 @@ protected function validateConfig(): void
);
}

if (isset($this->config['beStrictAboutWpdbConnectionId'])
&& !is_bool($this->config['beStrictAboutWpdbConnectionId'])) {
throw new ModuleConfigException(
__CLASS__,
'The `beStrictAboutWpdbConnectionId` configuration parameter must be a boolean.'
);
}

parent::validateConfig();
}

Expand Down Expand Up @@ -378,7 +389,8 @@ public function _initialize(): void
* backupGlobalsExcludeList: string[],
* backupStaticAttributes: bool,
* backupStaticAttributesExcludeList: array<string,string[]>,
* skipInstall: bool
* skipInstall: bool,
* beStrictAboutWpdbConnectionId: bool
* } $config
*/
$config = $this->config;
Expand Down Expand Up @@ -489,6 +501,8 @@ public function _initialize(): void
// If the database does not already exist, then create it now.
$db->create();

WPTestCase::beStrictAboutWpdbConnectionId($config['beStrictAboutWpdbConnectionId']);

$this->loadWordPress();
}

Expand Down Expand Up @@ -1002,6 +1016,7 @@ private function includeCorePHPUniteSuiteBootstrapFile(): void

try {
require $this->wpBootstrapFile;
WPTestCase::setWpdbConnectionId((string)$GLOBALS['wpdb']->get_var('SELECT CONNECTION_ID()'));
} catch (Throwable $t) {
// Not an early exit: Codeception will handle the Exception and print it.
$this->earlyExit = false;
Expand Down
23 changes: 23 additions & 0 deletions src/TestCase/WPTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class WPTestCase extends Unit
{
use WPTestCasePHPUnitMethodsTrait;

public static bool $beStrictAboutWpdbConnectionId = true;
private static ?string $wpdbConnectionId = null;

/**
* @var string[]|null
*/
Expand Down Expand Up @@ -247,6 +250,26 @@ private static function getCoreTestCase(): WP_UnitTestCase
return $coreTestCase;
}

public static function isStrictAboutWpdbConnectionId(): bool
{
return self::$beStrictAboutWpdbConnectionId;
}

public static function beStrictAboutWpdbConnectionId(bool $beStrictAboutWpdbConnectionId): void
{
self::$beStrictAboutWpdbConnectionId = $beStrictAboutWpdbConnectionId;
}

public static function getWpdbConnectionId(): ?string
{
return self::$wpdbConnectionId;
}

public static function setWpdbConnectionId(string $wpdbConnectionId): void
{
self::$wpdbConnectionId = $wpdbConnectionId;
}

protected function backupAdditionalGlobals(): void
{
if (isset($GLOBALS['_wp_registered_theme_features'])) {
Expand Down
2 changes: 1 addition & 1 deletion tests/_support/_generated/WploaderTesterActions.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php //[STAMP] a9a237b1518f3878f1c2f5e7920998b6
<?php //[STAMP] 99485bb9e0d9c8892cecb0507c40dcf3
// phpcs:ignoreFile
namespace _generated;

Expand Down

This file was deleted.

Loading

0 comments on commit a37d494

Please sign in to comment.