Autoload test helpers and exclude from test suites. (#39586)

* Autoload test helpers and exclude from test suites.

This avoids loading order problems, and eliminates warnings (about HposTestCase) not containing any tests.

* Add changefile(s) from automation for the following project(s): woocommerce

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Barry Hughes 2023-08-09 11:48:04 -07:00 committed by GitHub
parent a5539edd14
commit f36cb3a50c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 4 deletions

View File

@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: Improves organization of test helper code; does not merit its own changelog entry.

View File

@ -12,11 +12,13 @@
<testsuite name="wc-unittests-all">
<directory suffix=".php">./tests/legacy/unit-tests</directory>
<directory suffix=".php">./tests/php</directory>
<exclude>./tests/php/helpers</exclude>
</testsuite>
<testsuite name="shard1">
<directory suffix=".php">./tests/legacy/unit-tests</directory>
<directory suffix=".php">./tests/php</directory>
<exclude>./tests/legacy/unit-tests/woocommerce-admin</exclude>
<exclude>./tests/php/helpers</exclude>
</testsuite>
<testsuite name="shard2">
<directory suffix=".php">./tests/legacy/unit-tests/woocommerce-admin</directory>

View File

@ -102,10 +102,24 @@ class WC_Unit_Tests_Bootstrap {
* Register autoloader for the files in the 'tests/tools' directory, for the root namespace 'Automattic\WooCommerce\Testing\Tools'.
*/
protected static function register_autoloader_for_testing_tools() {
return spl_autoload_register(
spl_autoload_register(
function ( $class ) {
$tests_directory = dirname( __FILE__, 2 );
$helpers_directory = $tests_directory . '/php/helpers';
// Support loading top-level classes from the `php/helpers` directory.
if ( ! str_contains( $class, '\\' ) ) {
$helper_path = realpath( "$helpers_directory/$class.php" );
if ( dirname( $helper_path ) === $helpers_directory && file_exists( $helper_path ) ) {
require $helper_path;
return;
}
}
// Otherwise, check if this might relate to an Automattic\WooCommerce\Testing\Tools class.
$prefix = 'Automattic\\WooCommerce\\Testing\\Tools\\';
$base_dir = dirname( dirname( __FILE__ ) ) . '/Tools/';
$base_dir = $tests_directory . '/Tools/';
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
// no, move to the next registered autoloader.

View File

@ -8,8 +8,6 @@ use Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper;
use Automattic\WooCommerce\RestApi\UnitTests\HPOSToggleTrait;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
require_once __DIR__ . '/../../../../helpers/HPOSToggleTrait.php';
/**
* Tests for DataSynchronizer class.
*/