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

phpunit --version doesn't display version when running unsupported PHP #2563

Closed
johnbillion opened this issue Mar 10, 2017 · 6 comments
Closed
Assignees

Comments

@johnbillion
Copy link
Contributor

Q A
PHPUnit version Unknown, probably 5.7.x
PHP version HHVM 3.18.1
Installation Method Composer

When running an unsupported version of PHP, the phpunit --version command becomes useless because it omits outputting the current version of PHPUnit.

$ phpunit --version
This version of PHPUnit is supported on PHP 7.0 and PHP 7.1.
You are using PHP 5.6.99-hhvm (/usr/bin/hhvm).

We're seeing a problem on Travis CI where we're getting the This version of PHPUnit is supported error when running HHVM builds. While attempting to debug the problem, we added phpunit --version but it fails to report the PHPUnit version.

@sebastianbergmann
Copy link
Owner

That is unfortunate but something that cannot be helped. The version information is stored in a class (PHPUnit\Runner\Version). This class can only be loaded after it has been established that PHPUnit is running on a supported version of PHP. Otherwise we might run into a syntax error as PHPUnit\Runner\Version could be using syntax not supported on the version of PHP trying to run PHPUnit.

@johnbillion
Copy link
Contributor Author

Can the version information be moved into a non-PHP file and populated during the release build? Then the --version command can use this file instead of reading a PHP file which might not get parsed.

@sebastianbergmann
Copy link
Owner

Good point. However, I am not comfortable introducing such a change in the 5.7 or 6.0 branches. Not yet, at least. I will need to think about this.

@sebastianbergmann
Copy link
Owner

Such a change, if made, would also remove the need for this hack.

@sebastianbergmann
Copy link
Owner

Proof of Concept:

diff --git a/.version b/.version
new file mode 100644
index 0000000..dcfab78
--- /dev/null
+++ b/.version
@@ -0,0 +1 @@
+5.7.15
\ No newline at end of file
diff --git a/build/version.php b/build/version.php
index 6639b10..96d0add 100755
--- a/build/version.php
+++ b/build/version.php
@@ -4,10 +4,6 @@
 
 use SebastianBergmann\Version;
 
-$buffer  = file_get_contents(__DIR__ . '/../src/Runner/Version.php');
-$start   = strpos($buffer, 'new Version(\'') + strlen('new Version(\'');
-$end     = strpos($buffer, '\'', $start);
-$version = substr($buffer, $start, $end - $start);
-$version = new Version($version, __DIR__ . '/../');
+$version = new Version(file_get_contents(__DIR__ . '/../.version'), __DIR__ . '/../');
 
 print $version->getVersion();
diff --git a/phpunit b/phpunit
index f288633..400ec92 100755
--- a/phpunit
+++ b/phpunit
@@ -13,10 +13,11 @@ if (version_compare('5.6.0', PHP_VERSION, '>')) {
     fwrite(
         STDERR,
         sprintf(
-            'This version of PHPUnit is supported on PHP 5.6, PHP 7.0, and PHP 7.1.' . PHP_EOL .
-            'You are using PHP %s (%s).' . PHP_EOL,
+            'You are trying to use PHPUnit %s with PHP %s%s.' . PHP_EOL .
+            'This version of PHPUnit is supported on PHP 5.6, PHP 7.0, and PHP 7.1.' . PHP_EOL,
+            file_get_contents(__DIR__ . '/.version'),
             PHP_VERSION,
-            PHP_BINARY
+            defined('PHP_BINARY') ? ' (' . PHP_BINARY . ')' : ''
         )
     );
 
diff --git a/src/Runner/Version.php b/src/Runner/Version.php
index f6ec0e9..c43e32e 100644
--- a/src/Runner/Version.php
+++ b/src/Runner/Version.php
@@ -30,7 +30,7 @@ public static function id()
         }
 
         if (self::$version === null) {
-            $version       = new Version('5.7.15', dirname(dirname(__DIR__)));
+            $version       = new Version(file_get_contents(dirname(dirname(__DIR__)) . '/.version'), dirname(dirname(__DIR__)));
             self::$version = $version->getVersion();
         }

@sebastianbergmann
Copy link
Owner

I did not feel comfortable with the approach shown in #2563 (comment) and have now implemented a solution for this that should work for the PHAR distribution of PHPUnit.

I understand that having multiple versions of PHP installed (I, for instance, have PHP 5.6, PHP 7.0, PHP 7.1, and PHP 7.2 installed on my machine) is not uncommon, but I do not want to add additional complexity to PHPUnit because of this. If you are able to install PHPUnit using Composer (which does check PHPUnit's compatibility with the version of PHP that is used to run composer) then you are on your own when you use different versions of PHP when you run composer and phpunit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants