Moved autoloading of `Automattic\WooCommerce\Testing\Tools` namespace into test bootstrap

Since we need to load all of these files before WooCommerce has initialized we can't rely on Composer to handle the autoloading. We should take this namespace out of Composer altogether and just have our test autoloader take care of it.
This commit is contained in:
Christopher Allford 2020-07-14 11:27:50 -07:00 committed by Nestor Soriano
parent 24f9738913
commit 53e1f23af5
1 changed files with 25 additions and 0 deletions

View File

@ -39,6 +39,8 @@ class WC_Unit_Tests_Bootstrap {
$this->tests_dir = dirname( __FILE__ ); $this->tests_dir = dirname( __FILE__ );
$this->plugin_dir = dirname( dirname( $this->tests_dir ) ); $this->plugin_dir = dirname( dirname( $this->tests_dir ) );
$this->register_autoloader_for_testing_tools();
$this->initialize_code_hacker(); $this->initialize_code_hacker();
ini_set( 'display_errors', 'on' ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted ini_set( 'display_errors', 'on' ); // phpcs:ignore WordPress.PHP.IniSet.display_errors_Blacklisted
@ -72,6 +74,29 @@ class WC_Unit_Tests_Bootstrap {
$this->initialize_dependency_injection(); $this->initialize_dependency_injection();
} }
/**
* 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(
function ( $class ) {
$prefix = 'Automattic\\WooCommerce\\Testing\\Tools\\';
$base_dir = dirname( dirname( __FILE__ ) ) . '/Tools/';
$len = strlen( $prefix );
if ( strncmp( $prefix, $class, $len ) !== 0 ) {
// no, move to the next registered autoloader.
return;
}
$relative_class = substr( $class, $len );
$file = $base_dir . str_replace( '\\', '/', $relative_class ) . '.php';
if ( ! file_exists( $file ) ) {
throw new \Exception( 'Autoloader for unit tests: file not found: ' . $file );
}
require $file;
}
);
}
/** /**
* Initialize the code hacker. * Initialize the code hacker.
* *