So I have been using PHPUnit for quite some time now and find that it is a great library for testing. I also find that testing really helps in development. I also find that PHPUnit can be infuriating to use at times. For instance, I wanted to create a new base class that extendeds PHPUnit_Framework_TestCase so I can add some custom logging or something like that to all of my tests. So I simply create,
class lib_MyBaseUnitTest extends PHPUnit_Framework_TestCase { .... }Then I create my test cases,
class tests_MyFirstTest extends lib_MyBaseUnitTest { .... }Now whenever I run
phpunit tests/MyFirstTest.phpI get the error:
Class lib_MyBaseUnitTest could not be found in tests/MyFirstTest.phpSo clearly from the error the base class in not being loaded...? Actually, the problem is that the base class is being loaded by the autoloader when phpunit tries to load the test and the base class gets in the way. The phpunit code that causes the problem is located in StandardTestSuiteLoader.php.
PHPUnit_Util_Class::collectStart(); PHPUnit_Util_Fileloader::checkAndLoad($suiteClassFile, $syntaxCheck); $loadedClasses = PHPUnit_Util_Class::collectEnd();The collectStart call gathers the list of loaded classes and stores them in memory. The checkAndLoad call then includes each of the files listed in your suite, or the file specified on the commandline, thus loading more classes into memory. The collectEnd call now gathers the new list of loaded classes and diffs the result with the list collected in the collectStart call. Each of the resulting classes should be a test case that needs to be run. The problem is that the autoloader loaded both the specified test case and its base class. When PHPUnit tests to see if it can run the base class as a test, it can't and therefore throws and exception, "Class ... can not be found in ...".
So the solution in the end that I have taken is to place require_once calls in my bootstrap file for all custom base classes. This way the class will be in the initial list of classes created by the collectStart call. It will not be in the $loadedClasses variable and therefore will not cause an exception to be thrown.