Skip to content

Commit f81d1a9

Browse files
committed
[path] Fix runtime autoload resolution for installed dev-tools
1 parent 0b4fe86 commit f81d1a9

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/Path/DevToolsPathResolver.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ public static function getResourcesPath(string $path = ''): string
8989
return self::getPackagePath(Path::join(self::RESOURCES, $path));
9090
}
9191

92+
/**
93+
* Returns the active Composer autoload file for the current DevTools installation mode.
94+
*
95+
* When DevTools runs as a dependency, the runtime autoloader lives at the
96+
* Composer vendor root. Repository checkouts instead use the package-local
97+
* `vendor/autoload.php`.
98+
*
99+
* @param string $packagePath an optional package root path; defaults to the current package root
100+
*/
101+
public static function getRuntimeAutoloadPath(string $packagePath = ''): string
102+
{
103+
$packagePath = Path::canonicalize('' === $packagePath ? self::getPackagePath() : $packagePath);
104+
105+
if (self::isInstalledAsDependency($packagePath)) {
106+
return Path::canonicalize(Path::join($packagePath, '..', '..', 'autoload.php'));
107+
}
108+
109+
return Path::join($packagePath, 'vendor', 'autoload.php');
110+
}
111+
92112
/**
93113
* Detects whether the provided path belongs to an installed vendor copy of DevTools.
94114
*

src/PhpUnit/Bootstrap/BootstrapShimGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function generate(string $projectBootstrap, string $cacheDirectory): stri
6565
*/
6666
private function render(string $projectBootstrap): string
6767
{
68-
$devToolsAutoload = DevToolsPathResolver::getPackagePath('vendor/autoload.php');
68+
$devToolsAutoload = DevToolsPathResolver::getRuntimeAutoloadPath();
6969

7070
return <<<PHP
7171
<?php

tests/Path/DevToolsPathResolverTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function itWillExposeCanonicalPackagePaths(): void
3737
self::assertSame(\dirname(__DIR__, 2), DevToolsPathResolver::getPackagePath());
3838
self::assertSame(\dirname(__DIR__, 2) . '/bin/dev-tools', DevToolsPathResolver::getBinaryPath());
3939
self::assertSame(\dirname(__DIR__, 2) . '/resources', DevToolsPathResolver::getResourcesPath());
40+
self::assertSame(\dirname(__DIR__, 2) . '/vendor/autoload.php', DevToolsPathResolver::getRuntimeAutoloadPath());
4041
self::assertSame(
4142
\dirname(__DIR__, 2) . '/resources/phpdocumentor.xml',
4243
DevToolsPathResolver::getResourcesPath('phpdocumentor.xml')
@@ -70,4 +71,20 @@ public function itWillDetectWhetherDevToolsRunsFromVendorOrRepositoryCheckout():
7071
DevToolsPathResolver::isRepositoryCheckout('/workspaces/project/vendor/fast-forward/dev-tools/src')
7172
);
7273
}
74+
75+
/**
76+
* @return void
77+
*/
78+
#[Test]
79+
public function itWillResolveRuntimeAutoloadPathsForRepositoryAndDependencyInstalls(): void
80+
{
81+
self::assertSame(
82+
'/workspaces/dev-tools/vendor/autoload.php',
83+
DevToolsPathResolver::getRuntimeAutoloadPath('/workspaces/dev-tools')
84+
);
85+
self::assertSame(
86+
'/workspaces/project/vendor/autoload.php',
87+
DevToolsPathResolver::getRuntimeAutoloadPath('/workspaces/project/vendor/fast-forward/dev-tools')
88+
);
89+
}
7390
}

tests/PhpUnit/Bootstrap/BootstrapShimGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function generateWillWriteBootstrapShimWithProjectAndDevToolsAutoloaders(
5757
$projectBootstrap = '/repo/vendor/autoload.php';
5858
$cacheDirectory = '/repo/.dev-tools/cache/phpunit';
5959
$generatedBootstrapPath = '/repo/.dev-tools/cache/phpunit/bootstrap.php';
60-
$devToolsAutoload = DevToolsPathResolver::getPackagePath('vendor/autoload.php');
60+
$devToolsAutoload = DevToolsPathResolver::getRuntimeAutoloadPath();
6161
$exportedProjectBootstrap = var_export($projectBootstrap, true);
6262
$exportedDevToolsAutoload = var_export($devToolsAutoload, true);
6363
$expectedContent = <<<PHP

0 commit comments

Comments
 (0)