diff --git a/projects/packages/test-environment/src/class-test-environment.php b/projects/packages/test-environment/src/class-test-environment.php index 159e5769a5d84..1de31f4db07dd 100644 --- a/projects/packages/test-environment/src/class-test-environment.php +++ b/projects/packages/test-environment/src/class-test-environment.php @@ -24,13 +24,29 @@ class Test_Environment { */ public static function init() { if ( ! defined( 'WORDBLESS_RUNNING' ) ) { - // Look for wordbless in tools/php-test-env first + // Try the simple path first (works for symlinked/development case) $test_env_vendor = dirname( __DIR__, 4 ) . '/tools/php-test-env/vendor/autoload.php'; + if ( ! file_exists( $test_env_vendor ) ) { + // If simple path fails, search for monorepo root + $dir = __DIR__; + $prev_dir = null; + while ( $dir !== $prev_dir ) { + if ( file_exists( $dir . '/tools/php-test-env' ) ) { + break; + } + $prev_dir = $dir; + $dir = dirname( $dir ); + } + + if ( ! file_exists( $dir . '/tools/php-test-env' ) ) { + throw new \RuntimeException( 'Could not locate monorepo root directory' ); + } + + $test_env_vendor = $dir . '/tools/php-test-env/vendor/autoload.php'; + } + if ( file_exists( $test_env_vendor ) ) { require_once $test_env_vendor; - } else { - // Fallback to local vendor if test-env isn't available - require_once dirname( __DIR__ ) . '/vendor/autoload.php'; } try {