ZF-9380: Redeclaration of PHPUnit_Test_Case


A simple

php -r "require_once 'PHPUnit/Framework/TestCase.php';"

produces the error

Fatal error: Cannot redeclare class PHPUnit_Framework_TestCase in C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php on line 116

The PHPUnit documentation says to include/require PHPUnit/Framework.php to write own derived test cases.

My application needs to load the Zend_Test_PHPUnit_ControllerTestCase class which uses the above require_once statement and therefore produced the error message about class redeclaration. When I replace the statement, my application and my very own tests run fine.


Patch attached

I have the same problem with PHPUnit 3.4.11 and ZF 1.10.2 and PHP 5.3.

When I apply the patch to /Zend/Test/PHPUnit/ControllerTestCase.php (remove the line with require_once('...TestCase.php')) I get the following error: Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /usr/local/zend/share/ZendFramework/library/Zend/Test/PHPUnit/ControllerTestCase.php on line 54

Did you just remove the line or did you replace it with the new

require_once 'PHPUnit/Framework.php';


When I follow one of these instructions: *… *… I don't need any modification. I guess it is not well documented in the programmer's reference. But if you have a phpunit.xml and call phpunit via the command-line, it should work.

Thanks for your links to further explanations of setting up testing environments. But I'm am not running tests, instead I wanted to reuse a function from one of my tests, so I instantiated the test class and stumbled over the redeclaration error.

When running my tests (without applied patch), everything works fine. Instantiating the test class within the application in normal operation mode needs the modified require statement.

Right, I had the same problem when I tried to execute my unit tests within my application and not via the phpunit command line interface. Therefore I prefer applying your patch in order to execute unit tests within let's say a regular controller (TestController.php) and access the returned unit test results from the website/ within the application.

I had this issue also when I had a server that included the whole Zend Framework library in it's include path, while the ZF application had the ZF library in it's own "libary" folder.

Non the less, I don't consider it an issue since I cannot reproduce it. The patch file won't do any good, since require_once won't load it twice, unless you have two instances of the whole framework running. Besides, with my test setup using only PHPUnit/Framework breaks my unit tests since I rely too much on those TestCases (e.g. integration testing)

I can't reproduce this either with 10.2 and the same phpunit version.

It sounds like it's a "bug" somewhere else? Maybe you mixed include/require with include_once/require_once.

I usually suggest to strip all of those statements from the library code and use an autoloader instead. Maybe that's why I'm not seeing any of it period.

Sorry, I cannot help any further on this issue, because I am no longer working at that system that produced the error.

If you wish so, you can close this issue. Thanks for your patience.

This issue should be fixed, could you please verify so we can close this issue?

I am unable to reproduce the issue against trunk using PHPUnit 3.4.15. I vote for closing as "Not an Issue"