Skip to content

Commit

Permalink
Merge pull request #760 from andronocean/composer-autoload-path-fallback
Browse files Browse the repository at this point in the history
Use fallback for `$_composer_autoload_path`
  • Loading branch information
lucatume authored Nov 2, 2024
2 parents 49b0f6c + ea697c6 commit a5ef8b5
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ ignore-hidden = false
# To handle the hipster default blog content about bike messengers gettin' caught in the rain.
"gettin" = "gettin"
"Automattic" = "Automattic"
"Symplify" = "Symplify"
16 changes: 15 additions & 1 deletion 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 All @@ -30,8 +31,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)) {
$autoloadPath = $_composer_autoload_path;
} else {
// We use the Codecept class to find the location of Composer's vendor-dir.
$vendorDir = dirname((string)(new \ReflectionClass(Codecept::class))->getFilename(), 5);
$autoloadPath = $vendorDir . DIRECTORY_SEPARATOR . 'autoload.php';
}
return realpath($autoloadPath) ?: $autoloadPath;
}

public static function binDir(?string $path = null): string
Expand Down
32 changes: 32 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,36 @@ 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
{
// Ensure that it's set for this test.
global $_composer_autoload_path;
$_composer_autoload_path = codecept_root_dir() . 'vendor/autoload.php';

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

/**
* The `autoloadPath` static method should find the autoload.php file itself if the global `$_composer_autoload_path` is undefined
*
* @test
* @backupGlobals enabled
*/
public function static_autoload_path_should_use_fallback(): void
{
// 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 );
}
}

0 comments on commit a5ef8b5

Please sign in to comment.