Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add PHP version validation functionality #143

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Platform/TargetPhp/PhpBinaryPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,4 +273,23 @@ public static function fromCurrentProcess(): self

return new self($phpExecutable, null);
}

/**
* @param non-empty-string $minimumVersion
* @throws RuntimeException if version requirement not met
*/
public function assertMinimumVersion(string $minimumVersion): void
tal7aouy marked this conversation as resolved.
Show resolved Hide resolved
{
$versionParser = new VersionParser();
$normalized = $versionParser->normalize($this->version());
$normalizedMin = $versionParser->normalize($minimumVersion);

if (version_compare($normalized, $normalizedMin, '<')) {
throw new RuntimeException(sprintf(
'PHP version %s is less than required version %s',
$this->version(),
$minimumVersion
));
}
}
}
112 changes: 89 additions & 23 deletions test/unit/Platform/TargetPhp/PhpBinaryPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@

namespace Php\PieUnitTest\Platform\TargetPhp;

use Composer\Util\Platform;
use Php\Pie\Platform\Architecture;
use Php\Pie\Platform\OperatingSystem;
use Php\Pie\Platform\TargetPhp\Exception\InvalidPhpBinaryPath;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\PhpExecutableFinder;

use function array_column;
use function array_combine;
use function array_filter;
use function array_map;
use function array_unique;
use function assert;
use function is_dir;
use function defined;
use function dirname;
use function file_exists;
use function get_loaded_extensions;
use function ini_get;
use function is_dir;
use function is_executable;
use function php_uname;
use function phpversion;
use function sprintf;

use const DIRECTORY_SEPARATOR;
use RuntimeException;
use const PHP_INT_SIZE;
use function array_map;

use function php_uname;
use function phpversion;
use function file_exists;
use function array_column;
use function array_filter;
use function array_unique;
use Composer\Util\Platform;
use function array_combine;
use function is_executable;
use const PHP_MAJOR_VERSION;
use const PHP_MINOR_VERSION;
use const DIRECTORY_SEPARATOR;
use const PHP_RELEASE_VERSION;
use PHPUnit\Framework\TestCase;
use Php\Pie\Platform\Architecture;
use function get_loaded_extensions;

use Php\Pie\Platform\OperatingSystem;
use Php\Pie\Platform\TargetPhp\PhpBinaryPath;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\Process\PhpExecutableFinder;
use Php\Pie\Platform\TargetPhp\Exception\InvalidPhpBinaryPath;

#[CoversClass(PhpBinaryPath::class)]
final class PhpBinaryPathTest extends TestCase
Expand Down Expand Up @@ -259,4 +260,69 @@
self::assertGreaterThan(0, $php->phpIntSize());
self::assertNotEmpty($php->phpinfo());
}
/**
* @return array<string, array{0: string}>
*/
public static function validMinimumVersionProvider(): array

Check failure on line 266 in test/unit/Platform/TargetPhp/PhpBinaryPathTest.php

View workflow job for this annotation

GitHub Actions / static-analysis

PossiblyUnusedMethod

test/unit/Platform/TargetPhp/PhpBinaryPathTest.php:266:28: PossiblyUnusedMethod: Cannot find any calls to method Php\PieUnitTest\Platform\TargetPhp\PhpBinaryPathTest::validMinimumVersionProvider (see https://psalm.dev/087)
{
return [
'exact current version' => [
sprintf('%d.%d.%d', PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION),
],
'lower version' => [
sprintf('%d.%d.%d', PHP_MAJOR_VERSION - 1, PHP_MINOR_VERSION, PHP_RELEASE_VERSION),
],
];
}

/**
* @return array<string, array{0: string}>
*/
public static function invalidMinimumVersionProvider(): array

Check failure on line 281 in test/unit/Platform/TargetPhp/PhpBinaryPathTest.php

View workflow job for this annotation

GitHub Actions / static-analysis

PossiblyUnusedMethod

test/unit/Platform/TargetPhp/PhpBinaryPathTest.php:281:28: PossiblyUnusedMethod: Cannot find any calls to method Php\PieUnitTest\Platform\TargetPhp\PhpBinaryPathTest::invalidMinimumVersionProvider (see https://psalm.dev/087)
{
return [
'higher major version' => [
sprintf('%d.%d.%d', PHP_MAJOR_VERSION + 1, PHP_MINOR_VERSION, PHP_RELEASE_VERSION),
],
'higher minor version' => [
sprintf('%d.%d.%d', PHP_MAJOR_VERSION, PHP_MINOR_VERSION + 1, PHP_RELEASE_VERSION),
],
'higher patch version' => [
sprintf('%d.%d.%d', PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION + 1),
],
];
}

#[DataProvider('validMinimumVersionProvider')]
public function testValidMinimumVersions(string $version): void
{
$php = PhpBinaryPath::fromCurrentProcess();
$php->assertMinimumVersion($version);

Check failure on line 300 in test/unit/Platform/TargetPhp/PhpBinaryPathTest.php

View workflow job for this annotation

GitHub Actions / static-analysis

ArgumentTypeCoercion

test/unit/Platform/TargetPhp/PhpBinaryPathTest.php:300:36: ArgumentTypeCoercion: Argument 1 of Php\Pie\Platform\TargetPhp\PhpBinaryPath::assertMinimumVersion expects non-empty-string, but parent type string provided (see https://psalm.dev/193)
self::assertTrue(true, 'Version check passed as expected');
}

#[DataProvider('invalidMinimumVersionProvider')]
public function testInvalidMinimumVersions(string $version): void
{
$php = PhpBinaryPath::fromCurrentProcess();

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(sprintf(
'PHP version %s is less than required version %s',
$php->version(),
$version
));

$php->assertMinimumVersion($version);

Check failure on line 316 in test/unit/Platform/TargetPhp/PhpBinaryPathTest.php

View workflow job for this annotation

GitHub Actions / static-analysis

ArgumentTypeCoercion

test/unit/Platform/TargetPhp/PhpBinaryPathTest.php:316:36: ArgumentTypeCoercion: Argument 1 of Php\Pie\Platform\TargetPhp\PhpBinaryPath::assertMinimumVersion expects non-empty-string, but parent type string provided (see https://psalm.dev/193)
}

public function testMinimumVersionWithInvalidVersion(): void
{
$php = PhpBinaryPath::fromCurrentProcess();

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Invalid version string');

$php->assertMinimumVersion('not.a.version');
}
}
Loading