Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this test of PHP_VERSION at the begining of phpunit?

Tags:

php

phpunit

At the begigin of phpunit there is a condition, but i'm lacking of knowledge to understand it:

if (!version_compare(PHP_VERSION, PHP_VERSION, '=')) {
    fwrite(
        STDERR,
        sprintf(
            '%s declares an invalid value for PHP_VERSION.' . PHP_EOL .
            'This breaks fundamental functionality such as version_compare().' . PHP_EOL .
            'Please use a different PHP interpreter.' . PHP_EOL,

            PHP_BINARY
        )
    );

    die(1);
}

source: https://github.com/sebastianbergmann/phpunit/blob/main/phpunit

See doc php about version_compare: https://www.php.net/manual/en/function.version-compare.php

Version_compare — Compares two "PHP-standardized" version number strings. But here we are testing the same thing 'PHP_VERSION'.

In which case this condition could be TRUE?

It may be obvious, but I don't understand. Thank you for your help.

like image 929
Potivier Avatar asked Sep 12 '25 08:09

Potivier


1 Answers

If you git-blame the source code there, the commit contains a link to a blog post which explains the problem in detail: https://bbqsoftwares.com/blog/phpunit-big-sur.

In a nutshell: On specific versions of PHP, the internal version number would contain additional text and be technically invalid, breaking some PHPUnit code later on which tests PHP_VERSION using version_compare. To preempt this problem later on in the code, the author added this simple check which passes for all regular supported PHP versions, and fails for this specific PHP version which would cause obscure and unintuitive problems otherwise.

like image 117
deceze Avatar answered Sep 14 '25 22:09

deceze