This is an ugly hack, but it works in PHPUnit 3.6. We already have our own custom test case base class that all others extend. If it doesn't matter when the files get added to the whitelist you could do this using a fake test case just to handle this part.
First, bootstrap.php calls BaseTestCase::addXXXToCodeCoverageWhitelist() as many times as necessary to populate an internal array of files to add later. Next, the first test to be executed adds those files to the code coverage filter via the TestResult.
abstract class BaseTestCase extends PHPUnit_Framework_TestCase
{
private static $_codeCoverageFiles = array();
public static function addDirectoryToCodeCoverageWhitelist($path) {
self::addFilesToCodeCoverageWhitelist(self::getFilesForDirectory($path));
}
public static function addFileToCodeCoverageWhitelist($path) {
self::addFilesToCodeCoverageWhitelist(array($path));
}
public static function addFilesToCodeCoverageWhitelist(array $paths) {
self::$_codeCoverageFiles = array_merge(self::$_codeCoverageFiles, $paths);
}
public static function getFilesForDirectory($path) {
$facade = new File_Iterator_Facade;
return $facade->getFilesAsArray($path, '.php');
}
private static function setCodeCoverageWhitelist(PHP_CodeCoverage $coverage = null) {
if ($coverage && self::$_codeCoverageFiles) {
$coverage->setProcessUncoveredFilesFromWhitelist(true); // pick your poison
$coverage->filter()->addFilesToWhitelist(self::$_codeCoverageFiles);
self::$_codeCoverageFiles = array();
}
}
public function runBare() {
self::setCodeCoverageWhitelist($this->getTestResultObject()->getCodeCoverage());
parent::runBare();
}
}
Update: For anyone that used the blacklist to keep framework classes from showing up in assertion failure stack traces as we did, add the following methods to the above class and call them from your bootstrap.php. This requires setAccessible() from PHP 5.3.
public static function ignoreDirectoryInStackTraces($path) {
ignoreFilesInStackTraces(self::getFilesForDirectory($path));
}
public static function ignoreFileInStackTraces($path) {
ignoreFilesInStackTraces(array($path));
}
public static function ignoreFilesInStackTraces($files) {
static $reflector = null;
if (!$reflector) {
PHPUnit_Util_GlobalState::phpunitFiles();
$reflector = new ReflectionProperty('PHPUnit_Util_GlobalState', 'phpunitFiles');
$reflector->setAccessible(true);
}
$map = $reflector->getValue();
foreach ($files as $file) {
$map[$file] = $file;
}
$reflector->setValue($map);
}
define()with some static text. The tests are in a directory fairly removed from the source directory so the developer must put the source path intoconfig.phpwhichbootstrap.phpautomatically includes. - David Harkness