Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit can't found the "TestCase" class

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

like image 804
flawyte Avatar asked Oct 14 '16 14:10

flawyte


2 Answers

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

like image 195
martin Avatar answered Oct 14 '22 21:10

martin


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

like image 30
ashnazg Avatar answered Oct 14 '22 21:10

ashnazg