开发者

How to detect version of PHPUnit

I'm writing unit tests using an older version of PHPUnit (3.4) and thus can't use all supported assertions listed in manual of 3.5 and 3.6. Though I could reengineer tests for instant support in my environment here, I'd like to make my tests dependent on current version of PHPUnit, so that it's using assertsInstanceOf() as soon as my or any other one's testing environment is providing PHPUnit 3.5+.

I thought there would be some sort of constant automatically defined by PHPUnit, but I couldn't find any documentation开发者_Go百科 on it.

Is there any way of achieving this without requiring definition of constant when calling on command line?


You can get the version of PHPUnit running using the \PHPUnit\Runner\Version [git] class and its static id() method:

$phpUnitVersion = \PHPUnit\Runner\Version::id(); # 9.5.19

And based on that - halt your tests execution or do whatever you want.


Before PHPUnit fully switched to PHP namespaces (PHPUnit 6), the old class-name was PHPUnit_Runner_Version [git].


Works on mac:

phpunit --version

@Marc B


You can add an annotation before the test:

/**
 * @requires PHPUnit 3.7.32
 */
function testRequiringCertainPHPUnit() {
}


2020 Solution

use PHPUnit\Runner\Version as PHPUnitVersion;

die(PHP_EOL . PHPUnitVersion::id() . PHP_EOL); // 9.2.6
die(PHP_EOL . PHPUnitVersion::series() . PHP_EOL); // 9.2
die(PHP_EOL . explode('.', PHPUnitVersion::id())[0] . PHP_EOL); // 9


The answers above are more likely to match your case but if you:

  1. Want to use the terminal.
  2. You have projects with different versions of PHPunit and want to check specific one.

Go to the home directory of your PHPunit installation (most likely vendor) and run:

egrep -r "Version.*\('.*'" phpunit/phpunit/src/Runner/Version.php --include=*.php --color
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜