Skip to content

Commit

Permalink
Added more test cases for various version constraints to download com…
Browse files Browse the repository at this point in the history
…mand
  • Loading branch information
asgrim committed May 16, 2024
1 parent 9be752b commit d6dd3ba
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/DependencyResolver/ResolveDependencyWithComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Php\Pie\Platform\TargetPlatform;

use function in_array;
use function preg_match;

/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
final class ResolveDependencyWithComposer implements DependencyResolver
Expand All @@ -23,11 +24,20 @@ public function __construct(

public function __invoke(TargetPlatform $targetPlatform, string $packageName, string|null $requestedVersion): Package
{
$preferredStability = 'stable';
$repoSetFlags = 0;

/** Stability options from {@see https://getcomposer.org/doc/04-schema.md#minimum-stability} */
if ($requestedVersion !== null && preg_match('#@(dev|alpha|beta|RC|stable)$#', $requestedVersion, $matches)) {
$preferredStability = $matches[1];
$repoSetFlags |= RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES;
}

$package = (new VersionSelector(
$this->repositorySet,
($this->resolveTargetPhpToPlatformRepository)($targetPlatform->phpBinaryPath),
))
->findBestCandidate($packageName, $requestedVersion, 'alpha', null, RepositorySet::ALLOW_UNACCEPTABLE_STABILITIES);
->findBestCandidate($packageName, $requestedVersion, $preferredStability, null, $repoSetFlags);

if (! $package instanceof CompletePackageInterface) {
throw UnableToResolveRequirement::fromRequirement($packageName, $requestedVersion);
Expand Down
70 changes: 64 additions & 6 deletions test/integration/Command/DownloadCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,94 @@
use Php\Pie\Container;
use Php\Pie\DependencyResolver\UnableToResolveRequirement;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

use function array_combine;
use function array_map;
use function file_exists;
use function is_executable;

use const PHP_VERSION;
use const PHP_VERSION_ID;

#[CoversClass(DownloadCommand::class)]
class DownloadCommandTest extends TestCase
{
private const TEST_PACKAGE = 'asgrim/example-pie-extension';

private CommandTester $commandTester;

public function setUp(): void
{
$this->commandTester = new CommandTester(Container::factory()->get(DownloadCommand::class));
}

public function testDownloadCommandWillDownloadCompatibleExtension(): void
/**
* @return array<non-empty-string, array{0: non-empty-string, 1: non-empty-string}>
*
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public static function validVersionsList(): array
{
$versionsAndExpected = [
[self::TEST_PACKAGE, self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE . ':^1.0', self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE . ':1.0.1-alpha.3@alpha', self::TEST_PACKAGE . ':1.0.1-alpha.3'],
[self::TEST_PACKAGE . ':*', self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE . ':~1.0.0@alpha', self::TEST_PACKAGE . ':1.0.1'],
[self::TEST_PACKAGE . ':^1.1.0@alpha', self::TEST_PACKAGE . ':1.1.0-alpha.1'],
[self::TEST_PACKAGE . ':~1.0.0', self::TEST_PACKAGE . ':1.0.1'],
// @todo in theory, these could work, on NonWindows at least
//[self::TEST_PACKAGE . ':dev-master', self::TEST_PACKAGE . ':???'],
//[self::TEST_PACKAGE . ':dev-master#769f906413d6d1e12152f6d34134cbcd347ca253', self::TEST_PACKAGE . ':???'],
];

return array_combine(
array_map(static fn ($item) => $item[0], $versionsAndExpected),
$versionsAndExpected,
);
}

#[DataProvider('validVersionsList')]
public function testDownloadCommandWillDownloadCompatibleExtension(string $requestedVersion, string $expectedVersion): void
{
if (PHP_VERSION_ID < 80300 || PHP_VERSION_ID >= 80400) {
self::markTestSkipped('This test can only run on PHP 8.3 - you are running ' . PHP_VERSION);
}

// 1.0.0 is only compatible with PHP 8.3.0
$this->commandTester->execute(['requested-package-and-version' => 'asgrim/example-pie-extension:^1.0']);
$this->commandTester->execute(['requested-package-and-version' => $requestedVersion]);

$this->commandTester->assertCommandIsSuccessful();

$outputString = $this->commandTester->getDisplay();
self::assertStringContainsString('Found package: ' . $expectedVersion . ' which provides', $outputString);
self::assertStringContainsString('Extracted ' . $expectedVersion . ' source to', $outputString);
}

#[DataProvider('validVersionsList')]
public function testDownloadingWithPhpConfig(string $requestedVersion, string $expectedVersion): void
{
// @todo This test makes an assumption you're using `ppa:ondrej/php` to have multiple PHP versions. This allows
// us to test scenarios where you run with PHP 8.1 but want to install to a PHP 8.3 instance, for example.
// However, this test isn't very portable, and won't run in CI, so we could do with improving this later.
$phpConfigPath = '/usr/bin/php-config8.3';

if (! file_exists($phpConfigPath) || ! is_executable($phpConfigPath)) {
self::markTestSkipped('This test can only run where "' . $phpConfigPath . '" exists and is executable, to target PHP 8.3');
}

$this->commandTester->execute([
'--with-php-config' => $phpConfigPath,
'requested-package-and-version' => $requestedVersion,
]);

$this->commandTester->assertCommandIsSuccessful();

$outputString = $this->commandTester->getDisplay();
self::assertStringContainsString('Found package: asgrim/example-pie-extension', $outputString);
self::assertStringContainsString('Extracted asgrim/example-pie-extension', $outputString);
self::assertStringContainsString('Found package: ' . $expectedVersion . ' which provides', $outputString);
self::assertStringContainsString('Extracted ' . $expectedVersion . ' source to', $outputString);
}

public function testDownloadCommandFailsWhenUsingIncompatiblePhpVersion(): void
Expand All @@ -48,6 +106,6 @@ public function testDownloadCommandFailsWhenUsingIncompatiblePhpVersion(): void

$this->expectException(UnableToResolveRequirement::class);
// 1.0.0 is only compatible with PHP 8.3.0
$this->commandTester->execute(['requested-package-and-version' => 'asgrim/example-pie-extension:1.0.0']);
$this->commandTester->execute(['requested-package-and-version' => self::TEST_PACKAGE . ':1.0.0']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testPackageThatCanBeResolved(): void
/**
* @return array<string, array{0: array<string, string>, 1: string, 2: string}>
*
* @psalm-suppress PossiblyUnusedMethod
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public static function unresolvableDependencies(): array
{
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Platform/ArchitectureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class ArchitectureTest extends TestCase
/**
* @return array<non-empty-string, array{0: non-empty-string, 1: Architecture}>
*
* @psalm-suppress PossiblyUnusedMethod
* @psalm-suppress PossiblyUnusedMethod https://github.com/psalm/psalm-plugin-phpunit/issues/131
*/
public static function architectureMapProvider(): array
{
Expand Down

0 comments on commit d6dd3ba

Please sign in to comment.