Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit - Can I run tests depending on PHP version?

Tags:

php

phpunit

I am writing a lib which should work with PHP 5.3+. I want to use generators and closure binding, but those features are 5.5+ and 5.4+. Most of the lib can work without those features, so I want to run certain unit tests only when the php has the proper version. Is there a simple way to do this?

I am looking for something like this:

/** @version 5.4+*/
public function testUsingClosureBind(){...}

/** @version 5.5+*/
public function testUsingGenerators(){...}

but I am open for any suggestion...

like image 281
inf3rno Avatar asked Apr 14 '14 17:04

inf3rno


2 Answers

There is @requires annotation support since PHPUnit 3.7 (at least):

<?php

use PHPUnit\Framework\TestCase;

final class SomeTest extends TestCase
{
    /**
     * @requires PHP 5.3
     */
    public function testSome()
    {
    }
}

See the documentation for more.

like image 169
Tomas Votruba Avatar answered Sep 22 '22 07:09

Tomas Votruba


Use the version_compare function (http://us3.php.net/manual/en/function.version-compare.php). as an example :

public function testSomething() {
    if (version_compare(PHP_VERSION, '5.0', '>=')) {
        //do tests for PHP version 5.0 and higher
    } else {
        //do different tests for php lower than 5.0
    }
 }
like image 23
PolishDeveloper Avatar answered Sep 21 '22 07:09

PolishDeveloper