diff --git a/src/TargetPhp/PhpBinaryPath.php b/src/TargetPhp/PhpBinaryPath.php index 9d364c0..cadb8b2 100644 --- a/src/TargetPhp/PhpBinaryPath.php +++ b/src/TargetPhp/PhpBinaryPath.php @@ -4,6 +4,7 @@ namespace Php\Pie\TargetPhp; +use Composer\Semver\VersionParser; use Symfony\Component\Process\PhpExecutableFinder; use Symfony\Component\Process\Process; use Webmozart\Assert\Assert; @@ -20,11 +21,18 @@ private function __construct(readonly string $phpBinaryPath) public function version(): string { - $phpVersion = trim((new Process([$this->phpBinaryPath, '-r', 'echo phpversion();'])) + $phpVersion = trim((new Process([ + $this->phpBinaryPath, + '-r', + 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION . "." . PHP_RELEASE_VERSION;', + ])) ->mustRun() ->getOutput()); Assert::stringNotEmpty($phpVersion, 'Could not determine PHP version'); + // normalizing the version will throw an exception if it is not a valid version + (new VersionParser())->normalize($phpVersion); + return $phpVersion; } diff --git a/test/unit/TargetPhp/PhpBinaryPathTest.php b/test/unit/TargetPhp/PhpBinaryPathTest.php index 05c7551..9b19793 100644 --- a/test/unit/TargetPhp/PhpBinaryPathTest.php +++ b/test/unit/TargetPhp/PhpBinaryPathTest.php @@ -15,6 +15,9 @@ final class PhpBinaryPathTest extends TestCase { public function testVersionFromCurrentProcess(): void { - self::assertSame(PHP_VERSION, PhpBinaryPath::fromCurrentProcess()->version()); + self::assertSame( + sprintf("%s.%s.%s", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION), + PhpBinaryPath::fromCurrentProcess()->version() + ); } }