soletan
soletan

Reputation: 461

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 on it.

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

Upvotes: 25

Views: 29290

Answers (5)

zerkms
zerkms

Reputation: 255115

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].

Upvotes: 23

William Desportes
William Desportes

Reputation: 1711

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

Upvotes: 2

Samuil Banti
Samuil Banti

Reputation: 1795

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

Upvotes: 0

alakin_11
alakin_11

Reputation: 719

Works on mac:

phpunit --version

@Marc B

Upvotes: 20

Kornel
Kornel

Reputation: 100200

You can add an annotation before the test:

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

Upvotes: 8

Related Questions