Skip to content

Commit

Permalink
Simplify $_composer_autoload_path fallback cases
Browse files Browse the repository at this point in the history
The `Codeception\Codecept` class will always give us the location of the vendor dir,
even during wp-browser development or if a consumer project has moved it.
(Thank you, @lucatume, for the suggestion.)

Unit test simplified as we don't need the tedious dir traversal anymore — the
single fallback test will detect any problems if `Codecept` moves.
  • Loading branch information
andronocean committed Nov 1, 2024
1 parent 403716b commit 29c5612
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 37 deletions.
12 changes: 6 additions & 6 deletions src/Utils/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
32 changes: 1 addition & 31 deletions tests/unit/lucatume/WPBrowser/Utils/ComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() );
}
}

0 comments on commit 29c5612

Please sign in to comment.