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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
if (!self::$default_reporter) self::set_reporter('DebugReporter');
|
|
|
|
}
|
|
|
|
|
2007-08-16 08:35:27 +02:00
|
|
|
/**
|
|
|
|
* Run all test classes
|
|
|
|
*/
|
2007-08-15 08:38:41 +02:00
|
|
|
function index() {
|
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
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function only() {
|
|
|
|
$className = $this->urlParams['ID'];
|
|
|
|
if(class_exists($className)) {
|
|
|
|
$this->runTests(array($className));
|
|
|
|
} else {
|
|
|
|
echo "Class '$className' not found";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-05-26 08:21:30 +02:00
|
|
|
function runTests($classList, $coverage = false) {
|
2008-05-15 10:46:40 +02:00
|
|
|
if(!Director::is_cli()) {
|
|
|
|
self::$default_reporter->writeHeader();
|
|
|
|
echo '<div class="info">';
|
|
|
|
echo "<h1>Sapphire PHPUnit Test Runner</h1>";
|
|
|
|
echo "<p>Using the following subclasses of SapphireTest for testing: " . implode(", ", $classList) . "</p>";
|
|
|
|
echo "</div>";
|
|
|
|
echo '<div class="trace">';
|
|
|
|
echo "<pre>";
|
|
|
|
} else {
|
|
|
|
echo "Sapphire PHPUnit Test Runner\n";
|
|
|
|
echo "Using the following subclasses of SapphireTest for testing: " . implode(", ", $classList) . "\n\n";
|
|
|
|
}
|
2008-05-15 06:58:13 +02:00
|
|
|
|
|
|
|
// Remove our error handler so that PHP can use its own
|
2008-05-15 10:46:40 +02:00
|
|
|
//restore_error_handler();
|
2008-05-15 06:58:13 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-08-16 08:35:27 +02:00
|
|
|
/*, array("reportDirectory" => "/Users/sminnee/phpunit-report")*/
|
2008-05-26 08:21:30 +02:00
|
|
|
if($coverage) {
|
|
|
|
$testResult = PHPUnit_TextUI_TestRunner::run($suite, array("reportDirectory" => "../assets/coverage-report"));
|
|
|
|
} else {
|
|
|
|
$testResult = PHPUnit_TextUI_TestRunner::run($suite);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($coverage) {
|
|
|
|
$coverageURL = Director::absoluteURL('assets/coverage-report');
|
|
|
|
echo "<p><a href=\"$coverageURL\">Coverage report available here</a></p>";
|
|
|
|
}
|
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
|
|
|
}
|