title: PHPUnit configuration (phpunit.xml) -- loading in a bootstrap?
body: <h1>Situation</h1>
  
  <p>We're using PHPUnit in our project and are using a <code>phpunit.xml</code> to ensure things like <code>backupGlobals</code> is turned off.</p>
  
  <p>To further ensure the include path is set and autoloading is active, we also cascade our test bootstraps. That is to say, every test and alltests-suite has a <code>require_once(__DIR__ . '/../bootstrap.php');</code> at the top, all the way up to the base folder level, where it obviously reads <code>require_once(__DIR__ . '/bootstrap.php');</code>, and the actual bootstrap file resides.</p>
  
  <p>Essentially, our tests are autonomous. You can call any <code>AllTests.php</code> in any folder and any <code>*Test.php</code> by itself and they'll run with the right configuration.</p>
  
  <p>Except no. '<em>Wait a moment.</em>'</p>
  
  <p>That's only true if we either force our developers to use <code>phpunit --configuration=path/to/phpunit.xml</code> or they're in the folder with the <code>phpunit.xml</code> (so that PHPUnit pulls it out of the current working directory when it is executed).</p>
  
  <p>Occasionally, this makes it incredibly hard to determine why tests on one developer's machine are breaking and why they're running on another. It just takes forgetting that the bootstrap is <em>not</em> the only thing we need to have the same test environment. Keep in mind that since you couldn't forget the bootstrap if you tried, because it's in the tests themselves, forgetting other settings, especially usually-optional ones like that (if you're in the folder with the <code>phpunit.xml</code>, it's automatically pulled), is easy.</p>
  
  <p>In fact - it's happened a few times.</p>
  
  <h1>Question</h1>
  
  <p><strong>Is there a way I can supply which <code>phpunit.xml</code> to use in the test file being run,</strong> such as in our conveniently ubiquitous bootstrap file, <strong>rather than supplying it to PHPUnit beforehand</strong>, be that by command-line switch or by being in its directory <strong>?</strong></p>
  
  <p>A cursory glance at the code suggests the answer is no - configuration well and truly seems to be loaded before test files are even pulled:</p>
  
  <pre><code>[PHPUnit/TextUI/Command.php]
  ...
  if (isset($this-&gt;arguments['configuration'])) {
      $configuration = PHPUnit_Util_Configuration::getInstance(
          $this-&gt;arguments['configuration']
      );
      $phpunit = $configuration-&gt;getPHPUnitConfiguration();
      ...
  </code></pre>
  
  <p>That does make some sense, given that the configuration can contain test white- or blacklists.</p>
  
  <p>Really, it wouldn't make sense to load test <em>filters</em> in the test bootstrap itself, so that's half the potential configuration out the window with, but the actual behavioural flags of PHPUnit...</p>
  
  <pre><code>[sample of part of our phpunit.xml]
  &lt;phpunit
      backupGlobals="false" 
      backupStaticAttributes="false" 
      convertErrorsToExceptions="true"  
      convertNoticesToExceptions="true"
      convertWarningsToExceptions="true"
      syntaxCheck="false"
      processIsolation="false"
      colors="true"&gt;
  </code></pre>
  
  <p>...perhaps with the exception of 'colors' strikes me as something that the test itself should be able to decide on some level.</p>
  
  <h1>Consolation prize for...</h1>
  
  <p>Admittedly, right now I'd be happy just knowing if I can teach PHPUnit <code>backupGlobals="false"</code> from the bootstrap file, if someone knows of a way.</p>
  
  <p><em>(If fruitless, the practical answer I'll pursue will probably be to copy the <code>phpunit.xml</code> into all subfolders. I'd like to avoid that solution since it creates redundant copies, and if we ever choose to change a setting... yeah, ouch!)</em></p>