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

Conversation

andronocean
Copy link
Contributor

This fixes a bug I discovered where the lucatume\WPBrowser\Utils\Composer::autoloadPath() method will throw a TypeError if the $_composer_autoload_path global is undefined. The previous code passed the global to realpath() without checking if it actually exist.

Apparently $_composer_autoload_path can be undefined in various binary contexts — I stumbled across this when trying to use PhpStorm's test runner on test classes & methods that use #[RunTestsInSeparateProcesses] and the IsolationSupport extension.

Therefore, Composer recommends having a fallback. (Moreover, without one, the method signature may be incorrect and usages like the vendorDir() method will fail if they expect a string.)

I created a fallback condition that first looks for vendor in the same directory as codeception.yml. If that directory is not found (perhaps the parent project has customized Composer's vendor-dir), it guesses what the autoload path ought to be by walking up directories to where wp-browser should be installed.

I have not changed the original behavior of always returning a string, even if realpath() complains that the file does not exist.

Unit tests for various scenarios are included. Obviously they have to manipulate the global, so I've used @backupGlobals enabled, which appears to be sufficient.

There are cases where this global may be undefined, and Composer recommends
having a fallback. Without one, method signature is incorrect and usages like the
`vendorDir()` method will fail if they expect a string.

The fallback first looks for `vendor` in the same directory as `codeception.yml`,
and if that's not found it guesses what the autoload path _ought_ to be.

More info: https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-autoloader-from-a-binary
src/Utils/Composer.php Outdated Show resolved Hide resolved
@lucatume
Copy link
Owner

Thanks @andronocean for the PR.
I've left a comment that, I think, could simplify things and get rid of hard-coding and guessing.

andronocean and others added 4 commits November 1, 2024 12:17
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.
@lucatume lucatume merged commit a5ef8b5 into lucatume:master Nov 2, 2024
47 checks passed
lucatume added a commit that referenced this pull request Nov 2, 2024
@lucatume
Copy link
Owner

lucatume commented Nov 2, 2024

Thanks @andronocean for the PR, out in 4.3.6.

lucatume added a commit that referenced this pull request Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants