Skip to content

Commit 7851088

Browse files
committed
TestCaseSourceLocatorFactory - multiple Composer classloaders
1 parent 5dc4cf3 commit 7851088

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/Testing/TestCaseSourceLocatorFactory.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,26 @@ public function __construct(
5555

5656
public function create(): SourceLocator
5757
{
58+
$classLoaders = ClassLoader::getRegisteredLoaders();
5859
$classLoaderReflection = new ReflectionClass(ClassLoader::class);
59-
if ($classLoaderReflection->getFileName() === false) {
60-
throw new ShouldNotHappenException('Unknown ClassLoader filename');
60+
$locators = [];
61+
if ($classLoaderReflection->hasProperty('vendorDir')) {
62+
$vendorDirProperty = $classLoaderReflection->getProperty('vendorDir');
63+
$vendorDirProperty->setAccessible(true);
64+
foreach ($classLoaders as $classLoader) {
65+
$composerProjectPath = dirname($vendorDirProperty->getValue($classLoader));
66+
if (!is_file($composerProjectPath . '/composer.json')) {
67+
throw new ShouldNotHappenException(sprintf('composer.json not found in directory %s', $composerProjectPath));
68+
}
69+
70+
$composerSourceLocator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerProjectPath);
71+
if ($composerSourceLocator === null) {
72+
throw new ShouldNotHappenException('Could not create composer source locator');
73+
}
74+
$locators[] = $composerSourceLocator;
75+
}
6176
}
6277

63-
$composerProjectPath = dirname($classLoaderReflection->getFileName(), 3);
64-
if (!is_file($composerProjectPath . '/composer.json')) {
65-
throw new ShouldNotHappenException(sprintf('composer.json not found in directory %s', $composerProjectPath));
66-
}
67-
68-
$composerSourceLocator = $this->composerJsonAndInstalledJsonSourceLocatorMaker->create($composerProjectPath);
69-
if ($composerSourceLocator === null) {
70-
throw new ShouldNotHappenException('Could not create composer source locator');
71-
}
72-
73-
$locators = [
74-
$composerSourceLocator,
75-
];
7678
$astLocator = new Locator($this->phpParser);
7779
$astPhp8Locator = new Locator($this->php8Parser);
7880

0 commit comments

Comments
 (0)