inf3rno
inf3rno

Reputation: 26147

PHPUnit - Can I run tests depending on PHP version?

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

Upvotes: 10

Views: 2816

Answers (5)

gontrollez
gontrollez

Reputation: 6548

One proper way to archieve this can be annotating your tests with @group depending on the version the feature is intended for:

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

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

Now you can execute tests that belong to a certain group, or ignore a group:

phpunit --group 5.5
phpunit --group 5.4
phpunit --exclude-group 5.5

Documentation at PHPUnit website.

Upvotes: 7

Tomas Votruba
Tomas Votruba

Reputation: 24298

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.

Upvotes: 7

inf3rno
inf3rno

Reputation: 26147

According to Sebastian, I should use the @requires annotation to do that. It cannot be done with groups, because I cannot exclude them automatically depending on php version.

Btw. it does not help because I run always into parse errors by version 5.3, because of using yield and ::class...

He suggests to move the version dependent code to another files and use this:

<testsuite name="My Test Suite">
  <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">/path/to/files</directory>
  <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file>
</testsuite>

The file should not be under the /path/to/files directory, unless you want it to be included...

Finally I added 2 new suffixes for tests related to higher php version:

    <testsuite name="unit tests">
        <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">test/unit</directory>
        <directory suffix="Test54.php" phpVersion="5.4.0" phpVersionOperator=">=">test/unit</directory>
        <directory suffix="Test55.php" phpVersion="5.5.0" phpVersionOperator=">=">test/unit</directory>
    </testsuite>

Upvotes: 0

clami219
clami219

Reputation: 3038

I know it's not a best practice for phpunit tests organization, but if you are able to have those methods in different files according to the required php version, you could use the following in the XML configuration file:

   <testsuites>
    <testsuite name="My Test Suite">
      <directory suffix="Test.php" phpVersion="5.3.0" phpVersionOperator=">=">/path/to/files</directory>
      <file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file>
    </testsuite>
  </testsuites>

(see http://phpunit.de/manual/3.7/en/appendixes.configuration.html#appendixes.configuration.testsuites)

Upvotes: 3

PolishDeveloper
PolishDeveloper

Reputation: 950

Use the version_compare function (https://www.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
    }
 }

Upvotes: 5

Related Questions