Skip to main content

How to detect version of PHPUnit [Resolved]

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?


Question Credit: soletan
Question Reference
Asked June 13, 2016
Tags: , phpunit
Posted Under: Programming
9 views
3 Answers

You can get version using PHPUnit_Runner_Version class: https://github.com/sebastianbergmann/phpunit/blob/3.5/PHPUnit/Runner/Version.php

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


credit: zerkms
Answered June 13, 2016
 
I'd do if stackoverflow.com would allow me to do so (keyword: badges). – soletan Mar 17 '11 at 20:07
 CanDoerz  3 years ago
 
@David Harkness: hehe, thanks for notification ;-) – zerkms Feb 5 '11 at 9:00
 CanDoerz  3 years ago
 
@soletan: Don't forget to accept useful answers. :) – David Harkness Feb 5 '11 at 8:58
 CanDoerz  3 years ago
 
Thanks! That's what I was looking for. – soletan Jan 31 '11 at 11:11
 CanDoerz  3 years ago

To make the test incomplete for older versions:

    if (
        !method_exists('PHPUnit_Runner_Version','id')
        || version_compare(PHPUnit_Runner_Version::id(), '3.7', '<=')
    ) {
        $this->markTestIncomplete('need phpunit >= 3.7 to run this test');
    }

credit: Elvis Ciotti
Answered June 13, 2016
 
sorry I had no time to check the PHPUnit commit history to write a line less of code – Elvis Ciotti Mar 20 '13 at 14:18
 CanDoerz  3 years ago
 
It exists since 3.0, which is more than 6 years old :-) Checking if method exists in this case is something strange :-) – zerkms Feb 23 '13 at 3:30
 CanDoerz  3 years ago

You can add an annotation before the test:

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

credit: Andrew
Answered June 13, 2016
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA