Why, Fatal error: Class 'PHPUnit_Framework_TestCase' not found in …?

后端 未结 13 1113
攒了一身酷
攒了一身酷 2020-11-29 19:23

Why I\'m getting this PHP error?

Fatal error: Class \'PHPUnit_Framework_TestCase\' not found in ...
13条回答
  •  -上瘾入骨i
    2020-11-29 20:21

    I was running PHPUnit tests on PHP5, and then, I needed to support PHP7 as well. This is what I did:

    In composer.json:

    "phpunit/phpunit": "~4.8|~5.7"
    

    In my PHPUnit bootstrap file (in my case, /tests/bootstrap.php):

    // PHPUnit 6 introduced a breaking change that
    // removed PHPUnit_Framework_TestCase as a base class,
    // and replaced it with \PHPUnit\Framework\TestCase
    if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase'))
        class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase');
    

    In other words, this will work for tests written originally for PHPUnit 4 or 5, but then needed to work on PHPUnit 6 as well.

提交回复
热议问题