10

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

2
  • What version of PHPUnit you're using? Commented Oct 14, 2016 at 14:08
  • @Martin PHPUnit 4.8.24 Commented Oct 14, 2016 at 14:14

2 Answers 2

8

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

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

Comments

3

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

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.