Skip to content

Commit 5193fa5

Browse files
authored
Merge pull request #456 from asgrim/451-fix-windows-relative-ext-path
Fix windows relative ext path
2 parents 2ebd9dc + 702e0da commit 5193fa5

File tree

5 files changed

+64
-27
lines changed

5 files changed

+64
-27
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -486,18 +486,6 @@ parameters:
486486
count: 1
487487
path: test/unit/Installing/InstallForPhpProject/InstallSelectedPackageTest.php
488488

489-
-
490-
message: '#^Parameter \#1 \$filename of function file_exists expects string, string\|false given\.$#'
491-
identifier: argument.type
492-
count: 1
493-
path: test/unit/Platform/TargetPhp/PhpBinaryPathTest.php
494-
495-
-
496-
message: '#^Parameter \#1 \$filename of function is_dir expects string, string\|false given\.$#'
497-
identifier: argument.type
498-
count: 1
499-
path: test/unit/Platform/TargetPhp/PhpBinaryPathTest.php
500-
501489
-
502490
message: '#^Method Php\\PieUnitTest\\SelfManage\\Verify\\FallbackVerificationUsingOpenSslTest\:\:prepareCertificateAndSignature\(\) should return array\{string, string\} but returns array\{mixed, mixed\}\.$#'
503491
identifier: return.type

src/Building/WindowsBuild.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ public function __invoke(
2626
): BinaryFile {
2727
$prebuiltDll = WindowsExtensionAssetName::determineDllName($targetPlatform, $downloadedPackage);
2828

29-
$io->write(sprintf(
30-
'<info>Nothing to do on Windows, prebuilt DLL found:</info> %s',
31-
$prebuiltDll,
32-
));
29+
$io->write(
30+
sprintf(
31+
'<info>Nothing to build on Windows, prebuilt DLL found:</info> %s',
32+
$prebuiltDll,
33+
),
34+
verbosity: IOInterface::VERBOSE,
35+
);
3336

3437
return BinaryFile::fromFileWithSha256Checksum($prebuiltDll);
3538
}

src/Platform/TargetPhp/PhpBinaryPath.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,20 @@ public function extensionPath(): string
107107
return $extensionPath;
108108
}
109109

110+
// `extension_dir` may be a relative URL on Windows, so resolve it according to the location of PHP
111+
if (self::operatingSystem() === OperatingSystem::Windows) {
112+
$phpPath = dirname($this->phpBinaryPath);
113+
$attemptExtensionPath = $phpPath . DIRECTORY_SEPARATOR . $extensionPath;
114+
115+
if (file_exists($attemptExtensionPath) && is_dir($attemptExtensionPath)) {
116+
return $attemptExtensionPath;
117+
}
118+
}
119+
110120
// if the path is absolute, try to create it
111121
if (mkdir($extensionPath, 0777, true) && file_exists($extensionPath) && is_dir($extensionPath)) {
112122
return $extensionPath;
113123
}
114-
115-
// `extension_dir` may be a relative URL on Windows, so resolve it according to the location of PHP
116-
$phpPath = dirname($this->phpBinaryPath);
117-
$attemptExtensionPath = $phpPath . DIRECTORY_SEPARATOR . $extensionPath;
118-
119-
if (file_exists($attemptExtensionPath) && is_dir($attemptExtensionPath)) {
120-
return $attemptExtensionPath;
121-
}
122124
}
123125

124126
throw new RuntimeException('Could not determine extension path for ' . $this->phpBinaryPath);

test/integration/Command/BuildCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testBuildCommandWillBuildTheExtension(): void
3131
$outputString = $this->commandTester->getDisplay();
3232

3333
if (Platform::isWindows()) {
34-
self::assertStringContainsString('Nothing to do on Windows', $outputString);
34+
self::assertStringContainsString('Found prebuilt archive', $outputString);
3535

3636
return;
3737
}

test/unit/Platform/TargetPhp/PhpBinaryPathTest.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use Php\Pie\Util\Process;
1717
use PHPUnit\Framework\Attributes\CoversClass;
1818
use PHPUnit\Framework\Attributes\DataProvider;
19+
use PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily;
1920
use PHPUnit\Framework\TestCase;
21+
use ReflectionMethod;
2022
use Symfony\Component\Console\Output\OutputInterface;
2123
use Symfony\Component\Process\PhpExecutableFinder;
2224

@@ -35,11 +37,13 @@
3537
use function ini_get;
3638
use function is_dir;
3739
use function is_executable;
40+
use function mkdir;
3841
use function php_uname;
3942
use function phpversion;
4043
use function sprintf;
4144
use function strtolower;
4245
use function sys_get_temp_dir;
46+
use function trim;
4347
use function uniqid;
4448

4549
use const DIRECTORY_SEPARATOR;
@@ -235,11 +239,28 @@ public function testPhpIntSize(): void
235239
);
236240
}
237241

238-
public function testExtensionPath(): void
242+
#[RequiresOperatingSystemFamily('Linux')]
243+
public function testExtensionPathOnLinuxThatAlreadyExists(): void
239244
{
240245
$phpBinary = PhpBinaryPath::fromCurrentProcess();
241246

242-
$expectedExtensionDir = ini_get('extension_dir');
247+
$expectedExtensionDir = (string) ini_get('extension_dir');
248+
self::assertNotEmpty($expectedExtensionDir);
249+
self::assertDirectoryExists($expectedExtensionDir);
250+
251+
self::assertSame(
252+
$expectedExtensionDir,
253+
$phpBinary->extensionPath(),
254+
);
255+
}
256+
257+
#[RequiresOperatingSystemFamily('Windows')]
258+
public function testExtensionPathOnWindows(): void
259+
{
260+
$phpBinary = PhpBinaryPath::fromCurrentProcess();
261+
262+
$expectedExtensionDir = (string) ini_get('extension_dir');
263+
self::assertNotEmpty($expectedExtensionDir);
243264

244265
// `extension_dir` may be a relative URL on Windows (e.g. "ext"), so resolve it according to the location of PHP
245266
if (! file_exists($expectedExtensionDir) || ! is_dir($expectedExtensionDir)) {
@@ -255,9 +276,32 @@ public function testExtensionPath(): void
255276
);
256277
}
257278

279+
#[RequiresOperatingSystemFamily('Windows')]
280+
public function testRelativeExtensionPathOnWindowsIsFilled(): void
281+
{
282+
$phpBinary = $this->createPartialMock(PhpBinaryPath::class, ['phpinfo']);
283+
(new ReflectionMethod($phpBinary, '__construct'))
284+
->invoke($phpBinary, trim((string) (new PhpExecutableFinder())->find()), null);
285+
286+
$configuredExtensionPath = 'foo';
287+
self::assertDirectoryDoesNotExist($configuredExtensionPath, 'test cannot run if the same-named extension dir already exists in cwd');
288+
289+
$fullExtensionPath = dirname($phpBinary->phpBinaryPath) . DIRECTORY_SEPARATOR . $configuredExtensionPath;
290+
mkdir($fullExtensionPath, 0777, true);
291+
self::assertDirectoryExists($fullExtensionPath);
292+
293+
$phpBinary->expects(self::once())
294+
->method('phpinfo')
295+
->willReturn(sprintf('extension_dir => %s => %s', $configuredExtensionPath, $configuredExtensionPath));
296+
297+
self::assertSame($fullExtensionPath, $phpBinary->extensionPath());
298+
}
299+
258300
public function testExtensionPathIsImplicitlyCreated(): void
259301
{
260302
$phpBinary = $this->createPartialMock(PhpBinaryPath::class, ['phpinfo']);
303+
(new ReflectionMethod($phpBinary, '__construct'))
304+
->invoke($phpBinary, trim((string) (new PhpExecutableFinder())->find()), null);
261305

262306
$configuredExtensionPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('PIE_non_existent_extension_path', true);
263307
self::assertDirectoryDoesNotExist($configuredExtensionPath);

0 commit comments

Comments
 (0)