Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fallback for $_composer_autoload_path #760

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Utils/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,21 @@ public static function vendorDir(?string $path = null): string

public static function autoloadPath(): string
{
/**
* if $_composer_autoload_path is undefined, fall back to vendor/autoload.php in the parent project's directory.
*
* @link https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-autoloader-from-a-binary
*/
global $_composer_autoload_path;
return realpath($_composer_autoload_path) ?: $_composer_autoload_path;
if (isset($_composer_autoload_path)) {
$autoload_path = $_composer_autoload_path;
} elseif (is_dir(codecept_root_dir('vendor'))) {
andronocean marked this conversation as resolved.
Show resolved Hide resolved
$autoload_path = codecept_root_dir('vendor') . DIRECTORY_SEPARATOR . 'autoload.php';
} 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';
}
return realpath($autoload_path) ?: $autoload_path;
}

public static function binDir(?string $path = null): string
Expand Down
63 changes: 63 additions & 0 deletions tests/unit/lucatume/WPBrowser/Utils/ComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,67 @@ public function should_build_on_the_project_composer_file_if_no_composer_file_sp
$composer->getDecodedContents()
);
}

/**
* The `autoloadPath` static method should return `$_composer_autoload_path` if it is defined
*
* @test
* @backupGlobals enabled
*/
public function static_autoload_path_should_return_global_composer_autoload_path(): void
{
global $_composer_autoload_path;
// ensure that it's set for this test
$_composer_autoload_path = codecept_root_dir() . 'vendor/autoload.php';

$this->assertSame($_composer_autoload_path, Composer::autoloadPath() );
}

/**
* The `autoloadPath` static method should return a best-guess fallback if the global `$_composer_autoload_path` is undefined
*
* @test
* @backupGlobals enabled
*/
public function static_autoload_path_should_use_fallback(): void
{
global $_composer_autoload_path;
// clear value to enable fallback
unset($GLOBALS['_composer_autoload_path']);

$autoloadPath = Composer::autoloadPath();

$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() );
}
}
Loading