Skip to content

Commit

Permalink
transpile #760
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Nov 2, 2024
1 parent 6e2b9ee commit a4693a3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

## Fixed

- Fallback for missing `$_composer_autoload_path` file. (thanks @andronocean)


## [3.7.6] 2024-10-18;

### Changed
Expand Down
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 Down Expand Up @@ -33,8 +34,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 @@ -228,4 +228,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 a4693a3

Please sign in to comment.