From 53e1f23af55f121fb3399cadc020d538e56a7322 Mon Sep 17 00:00:00 2001 From: Christopher Allford Date: Tue, 14 Jul 2020 11:27:50 -0700 Subject: [PATCH] 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. --- tests/legacy/bootstrap.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/legacy/bootstrap.php b/tests/legacy/bootstrap.php index d8215fe79a0..153f12b4678 100644 --- a/tests/legacy/bootstrap.php +++ b/tests/legacy/bootstrap.php @@ -39,6 +39,8 @@ class WC_Unit_Tests_Bootstrap { $this->tests_dir = dirname( __FILE__ ); $this->plugin_dir = dirname( dirname( $this->tests_dir ) ); + $this->register_autoloader_for_testing_tools(); + $this->initialize_code_hacker(); 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(); } + /** + * 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. *