title: PHPUnit loads all classes at once. Causes PHP Fatal error: Cannot redeclare class
body: <p>I've done my due diligence, but I don't think any of the questions so far have touched on this problem.</p>
  
  <p>I have a situation where my PHP code generates class definitions based on config properties.</p>
  
  <p>The class definitions are basically data holders and can have either:</p>
  
  <ul>
  <li>public properties </li>
  <li>or protected properties with a public interface that supplies getter/setters. </li>
  </ul>
  
  <p>In certain config cases, the generated CLASS NAMES WILL BE THE SAME but their FILE NAMES WILL BE DIFFERENT. In a real environment, the user just generates a single set of class definitions based on the config they require and uses the classes in their app. No problems there, even if the user generates multiple sets of class definitions. PHP will happily churn along, providing only one set of definitions is referenced in the app.</p>
  
  <p>I have two PHPUnit test cases where I do basically the same tests using the classes with the different definitions generated by either config. I use "require_once" in all my code and do the same in unit tests, "require_once"ing only the class files that I need for that test.</p>
  
  <p>PHPUnit is configured to run all tests that it finds under my test directory and was running as normal. When  I added tests for the different class generation, PHPUnit stopped working with a PHP Fatal error:  Cannot redeclare class.</p>
  
  <p>Now, the default behaviour of PHPUnit seems to be to load ALL the classes that ALL the tests need to run in one go. This causes the fatal PHP error even before any test are run because PHPUnit picks up a second definition for the generated classes (they have the same class name but different file names) even though I have only "require_once"ed the correct class definitions in each test file.</p>
  
  <p>I can still run tests, but only with a single test file at a time, so currently I have to run all my tests
  either manually or by a batch file. A full code coverage report across all tests is not possible.</p>
  
  <p>The behaviour I would like is for PHPUnit to only load the classes that have been "require_once"ed per test file, not for all the test files it sees as being run from the phpunit.xml config. Or a better way would be for PHPUnit to only load its own classes for all tests specified and leave PHP itself to do the users "require_once"ing for each test.</p>
  
  <p>Is there any way to get around this? I have looked at PHPUnit config settings but none seem to fit the particular problem. I am not really familiar with PHPUnit under the covers so building another testsuite loader would be difficult.</p>
  
  <p>It's probably best to do a simple example. Here are a couple of PHPUnit test files foo1Test.php and foo2test.php. The only
  difference between them is they require_once a different class file which has a definition of the Foo class. It doesn't matter
  that all these classes are empty, they are all perfectly legitimate and it demonstrates the problem well. I just put all these
  files in a 'testfoo' directory and ran PHPunit on them. You can do it too and you will get the same result.</p>
  
  <p>foo1Test.php</p>
  
  <pre><code>&lt;?php
  
  require_once dirname ( __FILE__ ).'/Foo1.php';
  
  class Foo1TestCase extends PHPUnit_Framework_TestCase
  {
  }
  
  ?&gt;
  </code></pre>
  
  <p>foo2Test.php</p>
  
  <pre><code>&lt;?php
  
  require_once dirname ( __FILE__ ).'/Foo2.php';
  
  class Foo2TestCase extends PHPUnit_Framework_TestCase
  {
  }
  
  ?&gt;
  </code></pre>
  
  <p>Foo1.php</p>
  
  <pre><code>&lt;?php
  
  class Foo
  {
  }
  
  ?&gt;
  </code></pre>
  
  <p>Foo2.php</p>
  
  <pre><code>&lt;?php
  
  class Foo
  {
  }
  
  ?&gt;
  </code></pre>
  
  <p>The two test files foo1Test.php and foo2Test.php are perfectly valid files and run through the following PHPunit command lines</p>
  
  <pre><code>phpunit foo1Test.php
  phpunit foo2Test.php
  </code></pre>
  
  <p>As you would expect, PHPUnit fails complaining about no tests which is fine.</p>
  
  <p>I also have a standard phpunit.xml config file which will run all tests under the current directory.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;phpunit backupStaticAttributes="false"
           cacheTokens="false"
           colors="false"
           convertErrorsToExceptions="true"
           convertNoticesToExceptions="true"
           convertWarningsToExceptions="true"
           forceCoversAnnotation="false"
           mapTestClassNameToCoveredClassName="false"
           printerClass="PHPUnit_TextUI_ResultPrinter"
           processIsolation="false"
           stopOnError="false"
           stopOnFailure="false"
           stopOnIncomplete="false"
           stopOnSkipped="false"
           testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader"
           strict="false"
           verbose="true"&gt;
      &lt;testsuite name="fooTests"&gt;
          &lt;directory&gt;.&lt;/directory&gt;
      &lt;/testsuite&gt;
  &lt;/phpunit&gt;
  </code></pre>
  
  <p>I ran PHPUnit with this config as follows</p>
  
  <p>phpunit</p>
  
  <p>The output is:</p>
  
  <pre><code>PHP Fatal error:  Cannot redeclare class Foo in /var/www/testfoo/Foo2.php on line 4
  PHP Stack trace:
  PHP   1. {main}() /usr/bin/phpunit:0
  PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
  PHP   3. PHPUnit_TextUI_Command-&gt;run() /usr/share/php/PHPUnit/TextUI/Command.php:130
  PHP   4. PHPUnit_TextUI_Command-&gt;handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:139
  PHP   5. PHPUnit_Util_Configuration-&gt;getTestSuiteConfiguration() /usr/share/php/PHPUnit/TextUI/Command.php:671
  PHP   6. PHPUnit_Util_Configuration-&gt;getTestSuite() /usr/share/php/PHPUnit/Util/Configuration.php:768
  PHP   7. PHPUnit_Framework_TestSuite-&gt;addTestFiles() /usr/share/php/PHPUnit/Util/Configuration.php:848
  PHP   8. PHPUnit_Framework_TestSuite-&gt;addTestFile() /usr/share/php/PHPUnit/Framework/TestSuite.php:419
  PHP   9. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/Framework/TestSuite.php:358
  PHP  10. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:79
  PHP  11. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:95
  PHP  12. require_once() /var/www/testfoo/foo2Test.php:3
  </code></pre>
  
  <p>That is what this question was about. Although the test files are perfectly valid in isolation, PHPUnit wont run the tests under a testsuite because it has tried to load all the classes it needs for the whole testsuite BEFORE it runs any tests.</p>
  
  <p>To my way of thinking PHPUnit is doing the wrong thing here by picking up dependencies that it shouldn't for a particular test.</p>