To run my tests using the project's PHPUnit I do the following : php vendor/bin/phpunit tests/SomeClassTest.php
which works fine given the following class declaration :
class SomeClassTest extends PHPUnit_Framework_TestCase {
public function test_someMethod() {}
}
But it fails when I do this :
use PHPUnit\Framework\TestCase;
class SomeClassTest extends TestCase {
public function test_someMethod() {}
}
I get PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found
...
Class TestCase
exists since PHPUnit 5.4. You can see it on github if you set 5.3 tag (look for ForwardCompatibility
folder) or you can compare doc for 5.3 and 5.4 in the 2. Writing Tests for PHPUnit
section where it says:
"ClassTest inherits (most of the time) from PHPUnit_Framework_TestCase." for PHPUnit 5.3
and
"ClassTest inherits (most of the time) from PHPUnit\Framework\TestCase." for PHPUnit 5.4
In my library that I still have marked as usable by PHP 5.4, I've had to add this to my top-level testcase class in order to bridge the non-namespaced / namespaced difference, depending on which version of PHPUnit gets installed by Composer based on the runtime PHP version.
/*
* Allow for PHPUnit 4.* while XML_Util is still usable on PHP 5.4
*/
if (!class_exists('PHPUnit_Framework_TestCase')) {
class PHPUnit_Framework_TestCase extends \PHPUnit\Framework\TestCase {}
}
abstract class AbstractUnitTests extends \PHPUnit_Framework_TestCase
{
This works fine on PHP 5.4 (PHPUnit 4.8.34) up to PHP 7.1 (PHPUnit 6.0.2).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With