Skip to content

Commit b281d3f

Browse files
authored
fix: make Autoloader composer path injectable to fix parallel test race condition (#10082)
1 parent fc737a5 commit b281d3f

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

.github/scripts/run-random-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
################################################################################
2424

2525
set -u
26+
export LC_NUMERIC=C
2627
trap 'kill "${bg_pids[@]:-}" 2>/dev/null; wait 2>/dev/null' EXIT INT TERM
2728

2829
################################################################################

system/Autoloader/Autoloader.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class Autoloader
9393
*/
9494
protected $helpers = ['url'];
9595

96+
public function __construct(private readonly string $composerPath = COMPOSER_PATH)
97+
{
98+
}
99+
96100
/**
97101
* Reads in the configuration array (described above) and stores
98102
* the valid parts that we'll need.
@@ -127,7 +131,7 @@ public function initialize(Autoload $config, Modules $modules)
127131
$this->helpers = [...$this->helpers, ...$config->helpers];
128132
}
129133

130-
if (is_file(COMPOSER_PATH)) {
134+
if (is_file($this->composerPath)) {
131135
$this->loadComposerAutoloader($modules);
132136
}
133137

@@ -139,11 +143,11 @@ private function loadComposerAutoloader(Modules $modules): void
139143
// The path to the vendor directory.
140144
// We do not want to enforce this, so set the constant if Composer was used.
141145
if (! defined('VENDORPATH')) {
142-
define('VENDORPATH', dirname(COMPOSER_PATH) . DIRECTORY_SEPARATOR);
146+
define('VENDORPATH', dirname($this->composerPath) . DIRECTORY_SEPARATOR);
143147
}
144148

145149
/** @var ClassLoader $composer */
146-
$composer = include COMPOSER_PATH;
150+
$composer = include $this->composerPath;
147151

148152
// Should we load through Composer's namespaces, also?
149153
if ($modules->discoverInComposer) {
@@ -451,14 +455,14 @@ private function loadComposerNamespaces(ClassLoader $composer, array $composerPa
451455
*/
452456
protected function discoverComposerNamespaces()
453457
{
454-
if (! is_file(COMPOSER_PATH)) {
458+
if (! is_file($this->composerPath)) {
455459
return;
456460
}
457461

458462
/**
459463
* @var ClassLoader $composer
460464
*/
461-
$composer = include COMPOSER_PATH;
465+
$composer = include $this->composerPath;
462466
$paths = $composer->getPrefixesPsr4();
463467
$classes = $composer->getClassMap();
464468

tests/system/Autoloader/AutoloaderTest.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,12 @@ public function testComposerPackagesOnlyAndExclude(): void
367367

368368
public function testFindsComposerRoutesWithComposerPathNotFound(): void
369369
{
370-
$composerPath = COMPOSER_PATH;
371-
372370
$config = new Autoload();
373371
$modules = new Modules();
374372
$modules->discoverInComposer = true;
375373

376-
$loader = new Autoloader();
377-
378-
rename(COMPOSER_PATH, COMPOSER_PATH . '.backup');
374+
$loader = new Autoloader('/nonexistent/path/autoload.php');
379375
$loader->initialize($config, $modules);
380-
rename(COMPOSER_PATH . '.backup', $composerPath);
381376

382377
$namespaces = $loader->getNamespace();
383378
$this->assertArrayNotHasKey('Laminas\\Escaper', $namespaces);

0 commit comments

Comments
 (0)