Skip to content

Commit 2b90095

Browse files
committed
Ensure package resolved is a php-ext or php-ext-zend
1 parent 892c536 commit 2b90095

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

src/DependencyResolver/Package.php

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
1010
final class Package
1111
{
12+
public const TYPE_PHP_MODULE = 'php-ext';
13+
public const TYPE_ZEND_EXTENSION = 'php-ext-zend';
14+
1215
private function __construct(
1316
public readonly string $name,
1417
public readonly string $version,

src/DependencyResolver/ResolveDependencyWithComposer.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Php\Pie\TargetPhp\PhpBinaryPath;
1111
use Php\Pie\TargetPhp\ResolveTargetPhpToPlatformRepository;
1212

13+
use function in_array;
14+
1315
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
1416
final class ResolveDependencyWithComposer implements DependencyResolver
1517
{
@@ -27,12 +29,15 @@ public function __invoke(PhpBinaryPath $phpBinaryPath, string $packageName, stri
2729
))
2830
->findBestCandidate($packageName, $requestedVersion);
2931

30-
// @todo check it is a `php-ext` or `php-ext-zend`
31-
3232
if (! $package instanceof CompletePackageInterface) {
3333
throw UnableToResolveRequirement::fromRequirement($packageName, $requestedVersion);
3434
}
3535

36+
$type = $package->getType();
37+
if (! in_array($type, [Package::TYPE_PHP_MODULE, Package::TYPE_ZEND_EXTENSION])) {
38+
throw UnableToResolveRequirement::toPhpOrZendExtension($package, $packageName, $requestedVersion);
39+
}
40+
3641
return Package::fromComposerCompletePackage($package);
3742
}
3843
}

src/DependencyResolver/UnableToResolveRequirement.php

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Php\Pie\DependencyResolver;
66

7+
use Composer\Package\PackageInterface;
78
use RuntimeException;
89

910
use function sprintf;
@@ -18,4 +19,14 @@ public static function fromRequirement(string $requiredPackageName, string|null
1819
$requiredVersion !== null ? sprintf(' for version %s.', $requiredVersion) : '.',
1920
));
2021
}
22+
23+
public static function toPhpOrZendExtension(PackageInterface $locatedComposerPackage, string $requiredPackageName, string|null $requiredVersion): self
24+
{
25+
return new self(sprintf(
26+
'Package %s was not of type php-ext or php-ext-zend (requested %s%s)',
27+
$locatedComposerPackage->getName(),
28+
$requiredPackageName,
29+
$requiredVersion !== null ? sprintf(' for version %s.', $requiredVersion) : '.',
30+
));
31+
}
2132
}

test/unit/DependencyResolver/ResolveDependencyWithComposerTest.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ public function setUp(): void
3535
public function testPackageThatCanBeResolved(): void
3636
{
3737
$phpBinaryPath = $this->createMock(PhpBinaryPath::class);
38-
$phpBinaryPath->expects(self::once())
38+
$phpBinaryPath->expects(self::any())
3939
->method('version')
40-
->willReturn('8.2.0');
40+
->willReturn('8.3.0');
4141

4242
$package = (new ResolveDependencyWithComposer(
4343
$this->repositorySet,
4444
$this->resolveTargetPhpToPlatformRepository,
45-
))($phpBinaryPath, 'phpunit/phpunit', '^11.0');
45+
))($phpBinaryPath, 'asgrim/example-pie-extension', '1.0.0');
4646

47-
self::assertSame('phpunit/phpunit', $package->name);
47+
self::assertSame('asgrim/example-pie-extension', $package->name);
48+
self::assertSame('1.0.0', $package->version);
4849
}
4950

5051
/**
@@ -55,8 +56,9 @@ public function testPackageThatCanBeResolved(): void
5556
public static function unresolvableDependencies(): array
5657
{
5758
return [
58-
'phpVersionTooOld' => [['php' => '8.1.0'], 'phpunit/phpunit', '^11.0'],
59-
'phpVersionTooNew' => [['php' => '8.3.0'], 'roave/signature', '1.4.*'],
59+
'phpVersionTooOld' => [['php' => '8.1.0'], 'asgrim/example-pie-extension', '1.0.0'],
60+
'phpVersionTooNew' => [['php' => '8.4.0'], 'asgrim/example-pie-extension', '1.0.0'],
61+
'notAPhpExtension' => [['php' => '8.3.0'], 'ramsey/uuid', '^4.7'],
6062
];
6163
}
6264

0 commit comments

Comments
 (0)