';
}
self::$default_reporter->writeFooter();
}
function coverage() {
if(hasPhpUnit()) {
ManifestBuilder::load_all_classes();
$tests = ClassInfo::subclassesFor('SapphireTest');
array_shift($tests);
unset($tests['FunctionalTest']);
$this->runTests($tests, true);
} else {
echo "Please install PHPUnit using pear";
}
}
function cleanupdb() {
SapphireTest::delete_all_temp_dbs();
}
/**
* Run only a single test class or a comma-separated list of tests
*/
function only($request) {
if ($request->param('TestCase') == 'all') {
$this->all();
} else {
$classNames = explode(',',$request->param('TestCase'));
foreach($classNames as $className) {
if(!class_exists($className) || !(singleton($className) instanceof SapphireTest)) {
user_error("TestRunner::only(): Invalid TestCase '$className', cannot find matching class", E_USER_ERROR);
}
}
$this->runTests($classNames);
}
}
function runTests($classList, $coverage = false) {
// XDEBUG seem to cause problems with test execution :-(
if(function_exists('xdebug_disable')) xdebug_disable();
ini_set('max_execution_time', 0);
$this->setUp();
// run tests before outputting anything to the client
$suite = new PHPUnit_Framework_TestSuite();
foreach($classList as $className) {
// Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this.
class_exists($className);
$suite->addTest(new SapphireTestSuite($className));
}
// Remove the error handler so that PHPUnit can add its own
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");
if (count($classList) > 1) {
self::$default_reporter->writeInfo("All Tests", "Running test cases: " . implode(", ", $classList));
} else {
self::$default_reporter->writeInfo($classList[0], "");
}
$results = new PHPUnit_Framework_TestResult();
$results->addListener($reporter);
if($coverage) {
$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 "
';
// Put the error handlers back
Debug::loadErrorHandlers();
if(!Director::is_cli()) self::$default_reporter->writeFooter();
$this->tearDown();
// Todo: we should figure out how to pass this data back through Director more cleanly
if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2);
}
/**
* Start a test session.
* Usage: visit dev/tests/startsession?fixture=(fixturefile). A test database will be constructed, and your browser session will be amended
* to use this database. This can only be run on dev and test sites.
*/
function startsession() {
if(!Director::isLive()) {
if(SapphireTest::using_temp_db()) {
$endLink = Director::baseURL() . "/dev/tests/endsession";
return "
startession can only be used on dev and test sites
";
}
}
function endsession() {
SapphireTest::kill_temp_db();
DB::set_alternative_database_name(null);
return "
Test session ended.
";
}
function setUp() {
SapphireTest::create_temp_db();
SSViewer::flush_template_cache();
}
function tearDown() {
SapphireTest::kill_temp_db();
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 {
}
}