PHPUnit - Can I run tests depending on PHP version?
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)
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.
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.
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
}
}