Skip to content
Advertisement

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?

Advertisement

Answer

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

$phpUnitVersion = PHPUnitRunnerVersion::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].

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement