ENHANCEMENT: refactored runTests, using the new phpunit wrapper classes. (from r111041)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112878 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-19 03:33:51 +00:00
parent ce92190465
commit 3006b6ac36

View File

@ -4,24 +4,6 @@
* @subpackage testing * @subpackage testing
*/ */
// Check that PHPUnit is installed
function hasPhpUnit() {
$paths = explode(PATH_SEPARATOR, ini_get('include_path'));
foreach($paths as $path) {
if(substr($path,-1) == DIRECTORY_SEPARATOR) $path = substr($path,0,-1);
if(@file_exists("$path/PHPUnit/Framework.php")) return true;
}
return false;
}
/**
*/
if(hasPhpUnit()) {
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Util/Report.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
}
/** /**
* Controller that executes PHPUnit tests. * Controller that executes PHPUnit tests.
* *
@ -34,9 +16,20 @@ require_once 'PHPUnit/TextUI/TestRunner.php';
* @subpackage testing * @subpackage testing
*/ */
class TestRunner extends Controller { class TestRunner extends Controller {
/** @ignore */ /** @ignore */
private static $default_reporter; private static $default_reporter;
public static $phpunit_wrapper = null;
static function set_phpunit_wrapper($value) {
self::$phpunit_wrapper = $value;
}
static function get_phpunit_wrapper() {
return self::$phpunit_wrapper;
}
static $url_handlers = array( static $url_handlers = array(
'' => 'browse', '' => 'browse',
'coverage/module/$ModuleName' => 'coverageModule', 'coverage/module/$ModuleName' => 'coverageModule',
@ -86,7 +79,7 @@ class TestRunner extends Controller {
if (!self::$default_reporter) self::set_reporter(Director::is_cli() ? 'CliDebugView' : 'DebugView'); if (!self::$default_reporter) self::set_reporter(Director::is_cli() ? 'CliDebugView' : 'DebugView');
if(!hasPhpUnit()) { if(!PhpUnitWrapper::hasPhpUnit()) {
die("Please install PHPUnit using pear"); die("Please install PHPUnit using pear");
} }
} }
@ -265,39 +258,28 @@ class TestRunner extends Controller {
// Remove the error handler so that PHPUnit can add its own // Remove the error handler so that PHPUnit can add its own
restore_error_handler(); restore_error_handler();
/*, array("reportDirectory" => "/Users/sminnee/phpunit-report")*/
if(Director::is_cli()) $reporter = new CliTestReporter();
else $reporter = new SapphireTestReporter();
self::$default_reporter->writeHeader("Sapphire Test Runner"); self::$default_reporter->writeHeader("Sapphire Test Runner");
if (count($classList) > 1) { if (count($classList) > 1) {
self::$default_reporter->writeInfo("All Tests", "Running test cases: ",implode(", ", $classList)); self::$default_reporter->writeInfo("All Tests", "Running test cases: ",implode(", ", $classList));
} else { } else
if (count($classList) == 1) {
self::$default_reporter->writeInfo($classList[0], ""); self::$default_reporter->writeInfo($classList[0], "");
}
$results = new PHPUnit_Framework_TestResult();
$results->addListener($reporter);
if($coverage === true) {
foreach(self::$coverage_filter_dirs as $dir) {
PHPUnit_Util_Filter::addDirectoryToFilter(BASE_PATH . '/' . $dir);
}
$results->collectCodeCoverageInformation(true);
$suite->run($results);
if(!file_exists(ASSETS_PATH . '/coverage-report')) mkdir(ASSETS_PATH . '/coverage-report');
PHPUnit_Util_Report::render($results, ASSETS_PATH . '/coverage-report/');
$coverageApp = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/','_',preg_replace('/(\/$)|(^\/)/','',Director::baseFolder())) . '.html';
$coverageTemplates = ASSETS_PATH . '/coverage-report/' . preg_replace('/[^A-Za-z0-9]/','_',preg_replace('/(\/$)|(^\/)/','',realpath(TEMP_FOLDER))) . '.html';
echo "<p>Coverage reports available here:<ul>
<li><a href=\"$coverageApp\">Coverage report of the application</a></li>
<li><a href=\"$coverageTemplates\">Coverage report of the templates</a></li>
</ul>";
} else { } else {
$suite->run($results); // border case: no tests are available.
self::$default_reporter->writeInfo("", "");
} }
// perform unit tests (use PhpUnitWrapper or derived versions)
$phpunitwrapper = TestRunner::get_phpunit_wrapper();
$phpunitwrapper->setSuite($suite);
$phpunitwrapper->setCoverageStatus($coverage);
$phpunitwrapper->runTests();
// get results of the PhpUnitWrapper class
$reporter = $phpunitwrapper->getReporter();
$results = $phpunitwrapper->getFrameworkTestResults();
if(!Director::is_cli()) echo '<div class="trace">'; if(!Director::is_cli()) echo '<div class="trace">';
$reporter->writeResults(); $reporter->writeResults();
@ -470,17 +452,3 @@ HTML;
DB::set_alternative_database_name(null); DB::set_alternative_database_name(null);
} }
} }
// This class is here to help with documentation.
if(!hasPhpUnit()) {
/**
* PHPUnit is a testing framework that can be installed using PEAR.
* It's not bundled with Sapphire, you will need to install it yourself.
*
* @package sapphire
* @subpackage testing
*/
class PHPUnit_Framework_TestCase {
}
}