diff --git a/src/Utils/Composer.php b/src/Utils/Composer.php index 3162aecb5..ac8815767 100644 --- a/src/Utils/Composer.php +++ b/src/Utils/Composer.php @@ -4,6 +4,7 @@ namespace lucatume\WPBrowser\Utils; +use Codeception\Codecept; use JsonException; use lucatume\WPBrowser\Adapters\Symfony\Component\Process\Process; use lucatume\WPBrowser\Exceptions\RuntimeException; @@ -37,14 +38,13 @@ public static function autoloadPath(): string */ global $_composer_autoload_path; if (isset($_composer_autoload_path)) { - $autoload_path = $_composer_autoload_path; - } elseif (is_dir(codecept_root_dir('vendor'))) { - $autoload_path = codecept_root_dir('vendor') . DIRECTORY_SEPARATOR . 'autoload.php'; + $autoloadPath = $_composer_autoload_path; } else { - // vendor-dir was renamed, try our best to find the right directory based on where wp-browser is installed - $autoload_path = dirname(__FILE__, 5) . DIRECTORY_SEPARATOR . 'autoload.php'; + // we use the Codecept class to find the location of Composer's vendor-dir, even if a project has renamed it + $vendorDir = dirname((string)(new \ReflectionClass(Codecept::class))->getFilename(), 5); + $autoloadPath = $vendorDir . DIRECTORY_SEPARATOR . 'autoload.php'; } - return realpath($autoload_path) ?: $autoload_path; + return realpath($autoloadPath) ?: $autoloadPath; } public static function binDir(?string $path = null): string diff --git a/tests/unit/lucatume/WPBrowser/Utils/ComposerTest.php b/tests/unit/lucatume/WPBrowser/Utils/ComposerTest.php index 0c3ff51ef..e2556ac93 100644 --- a/tests/unit/lucatume/WPBrowser/Utils/ComposerTest.php +++ b/tests/unit/lucatume/WPBrowser/Utils/ComposerTest.php @@ -233,7 +233,7 @@ public function static_autoload_path_should_return_global_composer_autoload_path } /** - * The `autoloadPath` static method should return a best-guess fallback if the global `$_composer_autoload_path` is undefined + * The `autoloadPath` static method should find the autoload.php file itself if the global `$_composer_autoload_path` is undefined * * @test * @backupGlobals enabled @@ -249,34 +249,4 @@ public function static_autoload_path_should_use_fallback(): void $this->assertSame(codecept_root_dir() . 'vendor/autoload.php', $autoloadPath ); $this->assertFileExists( $autoloadPath ); } - - /** - * The `autoloadPath` static method should still return a best-guess fallback for `$_composer_autoload_path` - * if Composer's vendor-dir was renamed. - * - * @test - * @backupGlobals enabled - */ - public function static_autoload_path_should_use_fallback_if_vendor_dir_renamed_or_missing(): void - { - global $_composer_autoload_path; - // clear value to enable fallback - unset($GLOBALS['_composer_autoload_path']); - // pretend that wp-browser's own vendor dir does not exist - $this->setFunctionReturn('is_dir', false ); - - // The method has to find the renamed vendor-dir by directory traversal. - // i.e. if wp-browser is installed in `project/pkgs/lucatume/wp-browser`, find the `project/pkgs` dir. - // Let's figure out how far it has to go: - $mockVendorDir = dirname(codecept_root_dir(), 2); - $pathToClass = (new \ReflectionClass(Composer::class))->getFileName(); - $relPathToVendorDir = substr($pathToClass, strlen($mockVendorDir)); - - $this->assertSame(5, - // counting directory levels - substr_count($relPathToVendorDir, '/'), - "The method is hardcoded to look 5 levels up for the parent project's vendor-dir. If this test fails, the class has moved." - ); - $this->assertSame( dirname( $pathToClass, 5 ) . '/autoload.php', Composer::autoloadPath() ); - } }