10

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

1
  • you shouldn't depend your test execution on the version. For example on development and integration machine you can use 5.5 and tests pass, but on production you have 5.4 and code fails. It looks like you have tests for them, but if you're not executing that all the tests it's really bad. In that case have different test servers with different php version and always run all test cases. Commented Apr 14, 2014 at 17:55

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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.

Comments

5

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

4 Comments

Why did you posted nearly the same as me but 30 minutes after me?
hmm, when I took a closer look you're right, they are very similar. Sorry, I just didn't read your thing carefully, I just skimmed it to find version_compare() function. I think I can put this as a comment under your answer
no no it's ok. I'm just asking. Normally a comment would be ok as you said. However, yours using version_compare() looks slightly better. +1 .. But next time you know! ;)
But that should only work, if you don't rely on annotations e.g. expected Exception
3

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)

2 Comments

I tried out, but it does not work by me: php 5.3.28, phpunit 3.7.34.
The problem was that the file should not be in the same directory, I just included... It does have just an include pattern, not an exclude... Maybe I should set different suffix to the files I want to test with the higher php version...
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.