2007-08-15 08:38:41 +02:00
|
|
|
<?php
|
2008-01-08 07:37:50 +01:00
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage testing
|
|
|
|
*/
|
|
|
|
|
2007-08-15 12:01:35 +02:00
|
|
|
// Check that PHPUnit is installed
|
2008-03-19 21:38:52 +01:00
|
|
|
function hasPhpUnit() {
|
|
|
|
$paths = explode(PATH_SEPARATOR, ini_get('include_path'));
|
|
|
|
foreach($paths as $path) {
|
|
|
|
if(@file_exists("$path/PHPUnit/Framework.php")) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2007-08-15 12:01:35 +02:00
|
|
|
}
|
|
|
|
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
|
|
|
*/
|
2008-03-19 21:38:52 +01:00
|
|
|
if(hasPhpUnit()) {
|
2007-08-15 08:38:41 +02:00
|
|
|
require_once 'PHPUnit/Framework.php';
|
|
|
|
require_once 'PHPUnit/TextUI/TestRunner.php';
|
2008-03-19 21:38:52 +01:00
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
|
|
|
|
/**
|
Merged revisions 53150,53681,53700,53820,54200,54459 via svnmerge from
svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/roa
........
r53150 | ischommer | 2008-04-22 11:12:43 +1200 (Tue, 22 Apr 2008) | 1 line
FEATURE Added a "test mode" for /db/build which allows mock-DataObject-subclasses which are just built in a test run
........
r53681 | mrickerby | 2008-04-29 15:26:52 +1200 (Tue, 29 Apr 2008) | 1 line
adding default wrapping header and footer methods, and configurable reporting to the TestRunner
........
r53700 | mrickerby | 2008-04-29 16:41:57 +1200 (Tue, 29 Apr 2008) | 1 line
FEATURE: adding support for /dev/tests --> DevelopmentAdmin-->tests() --> TestRunner, /dev/tasks --> DevelopmentAdmin-->tasks() --> TaskRunner
........
r53820 | mrickerby | 2008-04-30 19:27:52 +1200 (Wed, 30 Apr 2008) | 1 line
BUGFIX fixing up BuildTask interface and task runner action
........
r54200 | sminnee | 2008-05-09 00:28:44 +1200 (Fri, 09 May 2008) | 1 line
Added TestSession object to help with the testing of forms
........
r54459 | sminnee | 2008-05-13 17:28:25 +1200 (Tue, 13 May 2008) | 1 line
Added a basic menu of options to /dev
........
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@54456 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-05-13 07:57:09 +02:00
|
|
|
* Controller that executes PHPUnit tests.
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage testing
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
class TestRunner extends Controller {
|
Merged revisions 53150,53681,53700,53820,54200,54459 via svnmerge from
svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/roa
........
r53150 | ischommer | 2008-04-22 11:12:43 +1200 (Tue, 22 Apr 2008) | 1 line
FEATURE Added a "test mode" for /db/build which allows mock-DataObject-subclasses which are just built in a test run
........
r53681 | mrickerby | 2008-04-29 15:26:52 +1200 (Tue, 29 Apr 2008) | 1 line
adding default wrapping header and footer methods, and configurable reporting to the TestRunner
........
r53700 | mrickerby | 2008-04-29 16:41:57 +1200 (Tue, 29 Apr 2008) | 1 line
FEATURE: adding support for /dev/tests --> DevelopmentAdmin-->tests() --> TestRunner, /dev/tasks --> DevelopmentAdmin-->tasks() --> TaskRunner
........
r53820 | mrickerby | 2008-04-30 19:27:52 +1200 (Wed, 30 Apr 2008) | 1 line
BUGFIX fixing up BuildTask interface and task runner action
........
r54200 | sminnee | 2008-05-09 00:28:44 +1200 (Fri, 09 May 2008) | 1 line
Added TestSession object to help with the testing of forms
........
r54459 | sminnee | 2008-05-13 17:28:25 +1200 (Tue, 13 May 2008) | 1 line
Added a basic menu of options to /dev
........
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@54456 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-05-13 07:57:09 +02:00
|
|
|
/** @ignore */
|
|
|
|
private static $default_reporter;
|
|
|
|
|
2008-08-09 06:06:52 +02:00
|
|
|
static $url_handlers = array(
|
2008-08-09 08:18:32 +02:00
|
|
|
'' => 'browse',
|
|
|
|
'$TestCase' => 'only',
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
Merged revisions 53150,53681,53700,53820,54200,54459 via svnmerge from
svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/roa
........
r53150 | ischommer | 2008-04-22 11:12:43 +1200 (Tue, 22 Apr 2008) | 1 line
FEATURE Added a "test mode" for /db/build which allows mock-DataObject-subclasses which are just built in a test run
........
r53681 | mrickerby | 2008-04-29 15:26:52 +1200 (Tue, 29 Apr 2008) | 1 line
adding default wrapping header and footer methods, and configurable reporting to the TestRunner
........
r53700 | mrickerby | 2008-04-29 16:41:57 +1200 (Tue, 29 Apr 2008) | 1 line
FEATURE: adding support for /dev/tests --> DevelopmentAdmin-->tests() --> TestRunner, /dev/tasks --> DevelopmentAdmin-->tasks() --> TaskRunner
........
r53820 | mrickerby | 2008-04-30 19:27:52 +1200 (Wed, 30 Apr 2008) | 1 line
BUGFIX fixing up BuildTask interface and task runner action
........
r54200 | sminnee | 2008-05-09 00:28:44 +1200 (Fri, 09 May 2008) | 1 line
Added TestSession object to help with the testing of forms
........
r54459 | sminnee | 2008-05-13 17:28:25 +1200 (Tue, 13 May 2008) | 1 line
Added a basic menu of options to /dev
........
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@54456 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-05-13 07:57:09 +02:00
|
|
|
/**
|
|
|
|
* Override the default reporter with a custom configured subclass.
|
|
|
|
*
|
|
|
|
* @param string $reporter
|
|
|
|
*/
|
|
|
|
static function set_reporter($reporter) {
|
|
|
|
if (is_string($reporter)) $reporter = new $reporter;
|
|
|
|
self::$default_reporter = $reporter;
|
|
|
|
}
|
|
|
|
|
|
|
|
function init() {
|
|
|
|
parent::init();
|
2008-08-11 01:03:35 +02:00
|
|
|
if (!self::$default_reporter) self::set_reporter('DebugView');
|
Merged revisions 53150,53681,53700,53820,54200,54459 via svnmerge from
svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/roa
........
r53150 | ischommer | 2008-04-22 11:12:43 +1200 (Tue, 22 Apr 2008) | 1 line
FEATURE Added a "test mode" for /db/build which allows mock-DataObject-subclasses which are just built in a test run
........
r53681 | mrickerby | 2008-04-29 15:26:52 +1200 (Tue, 29 Apr 2008) | 1 line
adding default wrapping header and footer methods, and configurable reporting to the TestRunner
........
r53700 | mrickerby | 2008-04-29 16:41:57 +1200 (Tue, 29 Apr 2008) | 1 line
FEATURE: adding support for /dev/tests --> DevelopmentAdmin-->tests() --> TestRunner, /dev/tasks --> DevelopmentAdmin-->tasks() --> TaskRunner
........
r53820 | mrickerby | 2008-04-30 19:27:52 +1200 (Wed, 30 Apr 2008) | 1 line
BUGFIX fixing up BuildTask interface and task runner action
........
r54200 | sminnee | 2008-05-09 00:28:44 +1200 (Fri, 09 May 2008) | 1 line
Added TestSession object to help with the testing of forms
........
r54459 | sminnee | 2008-05-13 17:28:25 +1200 (Tue, 13 May 2008) | 1 line
Added a basic menu of options to /dev
........
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@54456 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-05-13 07:57:09 +02:00
|
|
|
}
|
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
public function Link() {
|
|
|
|
return Controller::join_links(Director::absoluteBaseURL(), 'dev/tests/');
|
|
|
|
}
|
|
|
|
|
2007-08-16 08:35:27 +02:00
|
|
|
/**
|
|
|
|
* Run all test classes
|
|
|
|
*/
|
2008-08-09 08:18:32 +02:00
|
|
|
function all() {
|
2008-08-09 09:12:12 +02:00
|
|
|
die("here");
|
2008-03-19 21:38:52 +01:00
|
|
|
if(hasPhpUnit()) {
|
|
|
|
$tests = ClassInfo::subclassesFor('SapphireTest');
|
|
|
|
array_shift($tests);
|
2008-05-15 10:46:40 +02:00
|
|
|
unset($tests['FunctionalTest']);
|
2007-08-15 08:38:41 +02:00
|
|
|
|
2008-03-19 21:38:52 +01:00
|
|
|
$this->runTests($tests);
|
|
|
|
} else {
|
|
|
|
echo "Please install PHPUnit using pear";
|
|
|
|
}
|
2007-08-16 08:35:27 +02:00
|
|
|
}
|
2008-05-26 08:21:30 +02:00
|
|
|
|
2008-08-09 08:18:32 +02:00
|
|
|
/**
|
|
|
|
* Browse all enabled test cases in the environment
|
|
|
|
*/
|
|
|
|
function browse() {
|
2008-08-09 08:40:50 +02:00
|
|
|
self::$default_reporter->writeHeader();
|
|
|
|
echo '<div class="info">';
|
|
|
|
echo '<h1>Available Tests</h1>';
|
|
|
|
echo '</div>';
|
|
|
|
echo '<div class="trace">';
|
2008-08-09 08:18:32 +02:00
|
|
|
$tests = ClassInfo::subclassesFor('SapphireTest');
|
2008-08-09 08:40:50 +02:00
|
|
|
echo "<h3><a href=\"" . $this->Link() . "all\">Run all " . count($tests) . " tests</a></h3>";
|
|
|
|
echo "<br />";
|
2008-08-09 08:18:32 +02:00
|
|
|
foreach ($tests as $test) {
|
2008-08-09 08:40:50 +02:00
|
|
|
echo "<h3><a href=\"" . $this->Link() . "$test\">Run $test</a></h3>";
|
2008-08-09 08:18:32 +02:00
|
|
|
}
|
2008-08-09 08:40:50 +02:00
|
|
|
echo '</div>';
|
|
|
|
self::$default_reporter->writeFooter();
|
2008-08-09 08:18:32 +02:00
|
|
|
}
|
|
|
|
|
2008-05-26 08:21:30 +02:00
|
|
|
function coverage() {
|
|
|
|
if(hasPhpUnit()) {
|
|
|
|
ManifestBuilder::includeEverything();
|
|
|
|
$tests = ClassInfo::subclassesFor('SapphireTest');
|
|
|
|
array_shift($tests);
|
|
|
|
unset($tests['FunctionalTest']);
|
|
|
|
|
|
|
|
$this->runTests($tests, true);
|
|
|
|
} else {
|
|
|
|
echo "Please install PHPUnit using pear";
|
|
|
|
}
|
|
|
|
}
|
2007-08-16 08:35:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run only a single test class
|
|
|
|
*/
|
2008-08-09 06:06:52 +02:00
|
|
|
function only($request) {
|
|
|
|
$className = $request->param('TestCase');
|
2007-08-16 08:35:27 +02:00
|
|
|
if(class_exists($className)) {
|
|
|
|
$this->runTests(array($className));
|
|
|
|
} else {
|
2008-08-09 08:18:32 +02:00
|
|
|
if ($className == 'all') $this->all();
|
2007-08-16 08:35:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-26 08:21:30 +02:00
|
|
|
function runTests($classList, $coverage = false) {
|
2008-08-11 01:03:35 +02:00
|
|
|
// run tests before outputting anything to the client
|
2007-08-15 08:38:41 +02:00
|
|
|
$suite = new PHPUnit_Framework_TestSuite();
|
2007-08-16 08:35:27 +02:00
|
|
|
foreach($classList as $className) {
|
2008-01-08 03:12:18 +01:00
|
|
|
// Ensure that the autoloader pulls in the test class, as PHPUnit won't know how to do this.
|
|
|
|
class_exists($className);
|
2007-08-16 08:35:27 +02:00
|
|
|
$suite->addTest(new PHPUnit_Framework_TestSuite($className));
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2008-08-11 01:03:35 +02:00
|
|
|
/*, array("reportDirectory" => "/Users/sminnee/phpunit-report")*/
|
2008-08-09 08:18:32 +02:00
|
|
|
$reporter = new SapphireTestReporter();
|
|
|
|
$results = new PHPUnit_Framework_TestResult();
|
|
|
|
$results->addListener($reporter);
|
|
|
|
|
2007-08-16 08:35:27 +02:00
|
|
|
/*, array("reportDirectory" => "/Users/sminnee/phpunit-report")*/
|
2008-05-26 08:21:30 +02:00
|
|
|
if($coverage) {
|
2008-08-09 08:18:32 +02:00
|
|
|
$suite->run($results);
|
|
|
|
//$testResult = PHPUnit_TextUI_TestRunner::run($suite, array("reportDirectory" => "../assets/coverage-report"));
|
2008-05-26 08:21:30 +02:00
|
|
|
$coverageURL = Director::absoluteURL('assets/coverage-report');
|
|
|
|
echo "<p><a href=\"$coverageURL\">Coverage report available here</a></p>";
|
2008-08-09 08:18:32 +02:00
|
|
|
} else {
|
|
|
|
$suite->run($results);
|
|
|
|
//$testResult = PHPUnit_TextUI_TestRunner::run($suite);
|
2008-05-26 08:21:30 +02:00
|
|
|
}
|
2008-05-15 10:46:40 +02:00
|
|
|
|
2008-08-11 01:03:35 +02:00
|
|
|
self::$default_reporter->writeHeader();
|
|
|
|
if (count($classList) > 1) {
|
|
|
|
self::$default_reporter->writeInfo("All Tests", "Running test cases: " . implode(", ", $classList) .")");
|
|
|
|
} else {
|
|
|
|
self::$default_reporter->writeInfo($classList[0], "");
|
|
|
|
}
|
|
|
|
echo '<div class="trace">';
|
2008-08-09 08:18:32 +02:00
|
|
|
$reporter->writeResults();
|
|
|
|
|
2008-05-15 10:46:40 +02:00
|
|
|
if(!Director::is_cli()) echo '</div>';
|
2008-05-15 06:58:13 +02:00
|
|
|
|
|
|
|
// Put the error handlers back
|
|
|
|
Debug::loadErrorHandlers();
|
|
|
|
|
2008-05-15 10:46:40 +02:00
|
|
|
if(!Director::is_cli()) self::$default_reporter->writeFooter();
|
|
|
|
// Todo: we should figure out how to pass this data back through Director more cleanly
|
2008-05-16 06:14:50 +02:00
|
|
|
if(Director::is_cli() && ($testResult->failureCount() + $testResult->errorCount()) > 0) exit(2);
|
2007-08-15 12:01:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-10 01:33:18 +01:00
|
|
|
// This class is here to help with documentation.
|
2008-03-19 21:38:52 +01:00
|
|
|
if(!hasPhpUnit()) {
|
2008-01-10 01:33:18 +01:00
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
|
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|