Skip to content

Commit af06388

Browse files
committed
Fixed Composer autoload during SUT.
1 parent 95bad61 commit af06388

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

tests/phpunit/Traits/ComposerTrait.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ protected function composerCommandInit(): void {
2828
}
2929

3030
$this->tester = new ApplicationTester($application);
31+
32+
// Composer autoload uses per-project Composer binary, if the
33+
// `composer/composer` is included in the project as a dependency.
34+
//
35+
// When the test runs and creates SUT, the Composer binary used is
36+
// from the SUT's `vendor` directory. The Customizer may remove the
37+
// `vendor/composer/composer` directory as a part of the cleanup, resulting
38+
// in the Composer autoloader having an empty path to the Composer binary.
39+
//
40+
// This is extremely difficult to debug, because there is no clear error
41+
// message apart from `Could not open input file`.
42+
//
43+
// To prevent this, we set the `COMPOSER_BINARY` environment variable to the
44+
// Composer binary path found in the system.
45+
// @see \Composer\EventDispatcher::doDispatch().
46+
$composer_bin = shell_exec(escapeshellcmd('which composer'));
47+
if ($composer_bin === FALSE) {
48+
throw new \RuntimeException('Composer binary not found');
49+
}
50+
putenv('COMPOSER_BINARY=' . trim((string) $composer_bin));
3151
}
3252

3353
protected function assertComposerCommandSuccessOutputContains(string|array $strings): void {

0 commit comments

Comments
 (0)