2007-08-15 08:38:41 +02:00
|
|
|
<?php
|
2008-01-08 07:37:50 +01:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-01-08 07:37:50 +01:00
|
|
|
* @subpackage testing
|
|
|
|
*/
|
|
|
|
|
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.
|
2010-10-19 05:43:01 +02:00
|
|
|
*
|
|
|
|
* Alternatively, you can also use the "phpunit" binary directly by
|
|
|
|
* pointing it to a file or folder containing unit tests.
|
|
|
|
* See phpunit.dist.xml in the webroot for configuration details.
|
2009-07-01 00:35:46 +02:00
|
|
|
*
|
|
|
|
* <h2>URL Options</h2>
|
|
|
|
* - SkipTests: A comma-separated list of test classes to skip (useful when running dev/tests/all)
|
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
|
|
|
*
|
2009-07-01 00:30:33 +02:00
|
|
|
* See {@link browse()} output for generic usage instructions.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-01-09 05:18:36 +01:00
|
|
|
* @subpackage testing
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
class TestRunner extends Controller {
|
2010-10-19 05:33:51 +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
|
|
|
/** @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',
|
2010-05-25 05:22:44 +02:00
|
|
|
'coverage/module/$ModuleName' => 'coverageModule',
|
2010-05-25 05:43:11 +02:00
|
|
|
'coverage/$TestCase!' => 'coverageOnly',
|
2010-05-25 05:22:44 +02:00
|
|
|
'coverage' => 'coverageAll',
|
2010-10-15 02:01:23 +02:00
|
|
|
'sessionloadyml' => 'sessionloadyml',
|
2008-08-27 10:19:46 +02:00
|
|
|
'startsession' => 'startsession',
|
|
|
|
'endsession' => 'endsession',
|
2012-08-07 19:03:53 +02:00
|
|
|
'setdb' => 'setdb',
|
2009-03-04 08:31:23 +01:00
|
|
|
'cleanupdb' => 'cleanupdb',
|
2010-10-19 02:54:31 +02:00
|
|
|
'emptydb' => 'emptydb',
|
2009-07-01 00:30:33 +02:00
|
|
|
'module/$ModuleName' => 'module',
|
2009-10-23 05:06:59 +02:00
|
|
|
'all' => 'all',
|
|
|
|
'build' => 'build',
|
2012-05-18 06:05:05 +02:00
|
|
|
'$TestCase' => 'only'
|
2008-08-09 06:06:52 +02:00
|
|
|
);
|
|
|
|
|
2011-02-13 23:14:51 +01:00
|
|
|
static $allowed_actions = array(
|
2012-05-18 06:05:05 +02:00
|
|
|
'index',
|
|
|
|
'browse',
|
|
|
|
'coverage',
|
|
|
|
'coverageAll',
|
|
|
|
'coverageModule',
|
|
|
|
'coverageOnly',
|
|
|
|
'startsession',
|
|
|
|
'endsession',
|
2012-08-07 19:03:53 +02:00
|
|
|
'setdb',
|
2012-05-18 06:05:05 +02:00
|
|
|
'cleanupdb',
|
|
|
|
'module',
|
|
|
|
'all',
|
|
|
|
'build',
|
|
|
|
'only'
|
2011-02-13 23:14:51 +01:00
|
|
|
);
|
|
|
|
|
2010-10-15 05:07:08 +02:00
|
|
|
/**
|
|
|
|
* @var Array Blacklist certain directories for the coverage report.
|
|
|
|
* Filepaths are relative to the webroot, without leading slash.
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* @see http://www.phpunit.de/manual/current/en/appendixes.configuration.html
|
|
|
|
* #appendixes.configuration.blacklist-whitelist
|
2010-10-15 05:07:08 +02:00
|
|
|
*/
|
|
|
|
static $coverage_filter_dirs = array(
|
2012-03-24 04:38:57 +01:00
|
|
|
'*/thirdparty',
|
|
|
|
'*/tests',
|
|
|
|
'*/lang',
|
2010-10-15 05:07:08 +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
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function set_reporter($reporter) {
|
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
|
|
|
if (is_string($reporter)) $reporter = new $reporter;
|
|
|
|
self::$default_reporter = $reporter;
|
|
|
|
}
|
2011-03-18 07:38:02 +01:00
|
|
|
|
|
|
|
/**
|
2011-03-24 11:30:57 +01:00
|
|
|
* Pushes a class and template manifest instance that include tests onto the
|
|
|
|
* top of the loader stacks.
|
2011-03-18 07:38:02 +01:00
|
|
|
*/
|
|
|
|
public static function use_test_manifest() {
|
2012-05-28 04:25:34 +02:00
|
|
|
$classManifest = new SS_ClassManifest(
|
2011-03-18 07:38:02 +01:00
|
|
|
BASE_PATH, true, isset($_GET['flush'])
|
2012-05-28 04:25:34 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
SS_ClassLoader::instance()->pushManifest($classManifest);
|
|
|
|
SapphireTest::set_test_class_manifest($classManifest);
|
2011-03-24 11:30:57 +01:00
|
|
|
|
|
|
|
SS_TemplateLoader::instance()->pushManifest(new SS_TemplateManifest(
|
|
|
|
BASE_PATH, true, isset($_GET['flush'])
|
|
|
|
));
|
2011-03-18 07:38:02 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function init() {
|
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
|
|
|
parent::init();
|
2009-09-10 03:49:56 +02:00
|
|
|
|
|
|
|
$canAccess = (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"));
|
|
|
|
if(!$canAccess) return Security::permissionFailure($this);
|
|
|
|
|
2008-08-13 03:47:05 +02:00
|
|
|
if (!self::$default_reporter) self::set_reporter(Director::is_cli() ? 'CliDebugView' : 'DebugView');
|
2009-05-25 04:29:05 +02:00
|
|
|
|
2010-10-19 05:35:14 +02:00
|
|
|
if(!PhpUnitWrapper::has_php_unit()) {
|
2011-03-11 02:45:49 +01:00
|
|
|
die("Please install PHPUnit using pear");
|
2009-05-25 04:29:05 +02:00
|
|
|
}
|
2012-07-06 12:00:38 +02:00
|
|
|
|
|
|
|
if(!isset($_GET['flush']) || !$_GET['flush']) {
|
|
|
|
Debug::message(
|
|
|
|
"WARNING: Manifest not flushed. " .
|
|
|
|
"Add flush=1 as an argument to discover new classes or files.\n",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
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
|
|
|
/**
|
2009-10-23 05:06:59 +02:00
|
|
|
* Run test classes that should be run with every commit.
|
|
|
|
* Currently excludes PhpSyntaxTest
|
2007-08-16 08:35:27 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function all($request, $coverage = false) {
|
2011-03-18 07:38:02 +01:00
|
|
|
self::use_test_manifest();
|
2009-05-25 04:29:05 +02:00
|
|
|
$tests = ClassInfo::subclassesFor('SapphireTest');
|
|
|
|
array_shift($tests);
|
|
|
|
unset($tests['FunctionalTest']);
|
2009-10-23 05:06:59 +02:00
|
|
|
|
|
|
|
// Remove tests that don't need to be executed every time
|
|
|
|
unset($tests['PhpSyntaxTest']);
|
|
|
|
|
2009-07-01 00:35:46 +02:00
|
|
|
foreach($tests as $class => $v) {
|
|
|
|
$reflection = new ReflectionClass($class);
|
|
|
|
if(!$reflection->isInstantiable()) unset($tests[$class]);
|
|
|
|
}
|
2012-06-19 12:33:19 +02:00
|
|
|
|
2010-10-19 05:45:26 +02:00
|
|
|
$this->runTests($tests, $coverage);
|
2007-08-16 08:35:27 +02:00
|
|
|
}
|
2008-05-26 08:21:30 +02:00
|
|
|
|
2009-10-23 05:06:59 +02:00
|
|
|
/**
|
|
|
|
* Run test classes that should be run before build - i.e., everything possible.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function build() {
|
2011-03-18 07:38:02 +01:00
|
|
|
self::use_test_manifest();
|
2009-10-23 05:06:59 +02:00
|
|
|
$tests = ClassInfo::subclassesFor('SapphireTest');
|
|
|
|
array_shift($tests);
|
|
|
|
unset($tests['FunctionalTest']);
|
|
|
|
foreach($tests as $class => $v) {
|
|
|
|
$reflection = new ReflectionClass($class);
|
|
|
|
if(!$reflection->isInstantiable()) unset($tests[$class]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->runTests($tests);
|
|
|
|
}
|
|
|
|
|
2008-08-09 08:18:32 +02:00
|
|
|
/**
|
|
|
|
* Browse all enabled test cases in the environment
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function browse() {
|
2011-03-18 07:38:02 +01:00
|
|
|
self::use_test_manifest();
|
2008-08-09 08:40:50 +02:00
|
|
|
self::$default_reporter->writeHeader();
|
2008-11-09 16:34:29 +01:00
|
|
|
self::$default_reporter->writeInfo('Available Tests', false);
|
|
|
|
if(Director::is_cli()) {
|
|
|
|
$tests = ClassInfo::subclassesFor('SapphireTest');
|
|
|
|
$relativeLink = Director::makeRelative($this->Link());
|
|
|
|
echo "sake {$relativeLink}all: Run all " . count($tests) . " tests\n";
|
|
|
|
echo "sake {$relativeLink}coverage: Runs all tests and make test coverage report\n";
|
2009-07-01 00:30:33 +02:00
|
|
|
echo "sake {$relativeLink}module/<modulename>: Runs all tests in a module folder\n";
|
2008-11-09 16:34:29 +01:00
|
|
|
foreach ($tests as $test) {
|
|
|
|
echo "sake {$relativeLink}$test: Run $test\n";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
echo '<div class="trace">';
|
|
|
|
$tests = ClassInfo::subclassesFor('SapphireTest');
|
2008-12-04 23:38:32 +01:00
|
|
|
asort($tests);
|
2008-11-09 16:34:29 +01:00
|
|
|
echo "<h3><a href=\"" . $this->Link() . "all\">Run all " . count($tests) . " tests</a></h3>";
|
|
|
|
echo "<h3><a href=\"" . $this->Link() . "coverage\">Runs all tests and make test coverage report</a></h3>";
|
|
|
|
echo "<hr />";
|
|
|
|
foreach ($tests as $test) {
|
|
|
|
echo "<h3><a href=\"" . $this->Link() . "$test\">Run $test</a></h3>";
|
|
|
|
}
|
|
|
|
echo '</div>';
|
2008-08-09 08:18:32 +02:00
|
|
|
}
|
2008-11-09 16:34:29 +01:00
|
|
|
|
2008-08-09 08:40:50 +02:00
|
|
|
self::$default_reporter->writeFooter();
|
2008-08-09 08:18:32 +02:00
|
|
|
}
|
|
|
|
|
2010-05-25 05:22:44 +02:00
|
|
|
/**
|
|
|
|
* Run a coverage test across all modules
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function coverageAll($request) {
|
2011-03-18 07:38:02 +01:00
|
|
|
self::use_test_manifest();
|
2010-10-19 05:44:52 +02:00
|
|
|
$this->all($request, true);
|
2010-05-25 05:22:44 +02:00
|
|
|
}
|
2011-02-21 22:53:58 +01:00
|
|
|
|
2010-05-25 05:22:44 +02:00
|
|
|
/**
|
|
|
|
* Run only a single coverage test class or a comma-separated list of tests
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function coverageOnly($request) {
|
2010-05-25 05:22:44 +02:00
|
|
|
$this->only($request, true);
|
|
|
|
}
|
2009-05-25 04:29:05 +02:00
|
|
|
|
2010-05-25 05:22:44 +02:00
|
|
|
/**
|
|
|
|
* Run coverage tests for one or more "modules".
|
2012-03-24 04:38:57 +01:00
|
|
|
* A module is generally a toplevel folder, e.g. "mysite" or "framework".
|
2010-05-25 05:22:44 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function coverageModule($request) {
|
2010-05-25 05:22:44 +02:00
|
|
|
$this->module($request, true);
|
2008-05-26 08:21:30 +02:00
|
|
|
}
|
2009-03-04 08:31:23 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function cleanupdb() {
|
2009-03-04 08:31:23 +01:00
|
|
|
SapphireTest::delete_all_temp_dbs();
|
|
|
|
}
|
2007-08-16 08:35:27 +02:00
|
|
|
|
|
|
|
/**
|
2009-03-31 21:42:08 +02:00
|
|
|
* Run only a single test class or a comma-separated list of tests
|
2007-08-16 08:35:27 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function only($request, $coverage = false) {
|
2011-03-18 07:38:02 +01:00
|
|
|
self::use_test_manifest();
|
2009-05-06 08:36:16 +02:00
|
|
|
if($request->param('TestCase') == 'all') {
|
2009-03-31 21:42:08 +02:00
|
|
|
$this->all();
|
2007-08-16 08:35:27 +02:00
|
|
|
} else {
|
2009-05-06 08:36:16 +02:00
|
|
|
$classNames = explode(',', $request->param('TestCase'));
|
2009-03-31 21:42:08 +02:00
|
|
|
foreach($classNames as $className) {
|
2009-07-17 01:56:36 +02:00
|
|
|
if(!class_exists($className) || !is_subclass_of($className, 'SapphireTest')) {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("TestRunner::only(): Invalid TestCase '$className', cannot find matching class",
|
|
|
|
E_USER_ERROR);
|
2009-03-31 21:42:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-25 05:22:44 +02:00
|
|
|
$this->runTests($classNames, $coverage);
|
2007-08-16 08:35:27 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-01 00:30:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run tests for one or more "modules".
|
2012-03-24 04:38:57 +01:00
|
|
|
* A module is generally a toplevel folder, e.g. "mysite" or "framework".
|
2009-07-01 00:30:33 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function module($request, $coverage = false) {
|
2011-03-18 07:38:02 +01:00
|
|
|
self::use_test_manifest();
|
2009-07-01 00:30:33 +02:00
|
|
|
$classNames = array();
|
|
|
|
$moduleNames = explode(',', $request->param('ModuleName'));
|
2011-09-26 05:08:41 +02:00
|
|
|
|
2012-06-19 12:33:19 +02:00
|
|
|
$ignored = array('functionaltest', 'phpsyntaxtest');
|
|
|
|
|
2009-07-01 00:30:33 +02:00
|
|
|
foreach($moduleNames as $moduleName) {
|
|
|
|
$classesForModule = ClassInfo::classes_for_folder($moduleName);
|
2011-09-26 05:08:41 +02:00
|
|
|
|
|
|
|
if($classesForModule) {
|
|
|
|
foreach($classesForModule as $className) {
|
|
|
|
if(class_exists($className) && is_subclass_of($className, 'SapphireTest')) {
|
2012-06-19 12:33:19 +02:00
|
|
|
if(!in_array($className, $ignored))
|
|
|
|
$classNames[] = $className;
|
2011-09-26 05:08:41 +02:00
|
|
|
}
|
2009-07-02 02:17:29 +02:00
|
|
|
}
|
2009-07-01 00:30:33 +02:00
|
|
|
}
|
|
|
|
}
|
2012-06-19 12:33:19 +02:00
|
|
|
|
2010-05-25 05:22:44 +02:00
|
|
|
$this->runTests($classNames, $coverage);
|
2009-07-01 00:30:33 +02:00
|
|
|
}
|
2007-08-16 08:35:27 +02:00
|
|
|
|
2009-07-01 00:35:46 +02:00
|
|
|
/**
|
|
|
|
* @param array $classList
|
|
|
|
* @param boolean $coverage
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function runTests($classList, $coverage = false) {
|
2009-08-21 05:02:43 +02:00
|
|
|
$startTime = microtime(true);
|
2012-04-18 13:11:53 +02:00
|
|
|
|
|
|
|
// disable xdebug, as it messes up test execution
|
2009-03-04 04:44:11 +01:00
|
|
|
if(function_exists('xdebug_disable')) xdebug_disable();
|
2012-04-18 13:11:53 +02:00
|
|
|
|
|
|
|
ini_set('max_execution_time', 0);
|
|
|
|
|
2008-08-13 04:47:14 +02:00
|
|
|
$this->setUp();
|
2012-04-18 13:11:53 +02:00
|
|
|
|
2009-07-01 00:35:46 +02:00
|
|
|
// Optionally skip certain tests
|
|
|
|
$skipTests = array();
|
|
|
|
if($this->request->getVar('SkipTests')) {
|
|
|
|
$skipTests = explode(',', $this->request->getVar('SkipTests'));
|
|
|
|
}
|
2012-04-18 13:11:53 +02:00
|
|
|
|
2009-07-01 00:35:46 +02:00
|
|
|
$classList = array_diff($classList, $skipTests);
|
|
|
|
|
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();
|
2009-05-01 01:52:55 +02:00
|
|
|
natcasesort($classList);
|
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);
|
2009-04-22 05:22:09 +02:00
|
|
|
$suite->addTest(new SapphireTestSuite($className));
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2008-08-11 08:10:59 +02:00
|
|
|
|
|
|
|
// Remove the error handler so that PHPUnit can add its own
|
|
|
|
restore_error_handler();
|
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
self::$default_reporter->writeHeader("SilverStripe Test Runner");
|
2012-04-18 13:11:53 +02:00
|
|
|
if (count($classList) > 1) {
|
2009-12-16 06:38:42 +01:00
|
|
|
self::$default_reporter->writeInfo("All Tests", "Running test cases: ",implode(", ", $classList));
|
2012-04-18 13:11:53 +02:00
|
|
|
} elseif (count($classList) == 1) {
|
|
|
|
self::$default_reporter->writeInfo($classList[0], '');
|
2010-10-19 05:33:51 +02:00
|
|
|
} else {
|
2012-04-18 13:11:53 +02:00
|
|
|
// border case: no tests are available.
|
|
|
|
self::$default_reporter->writeInfo('', '');
|
2008-08-13 03:47:05 +02:00
|
|
|
}
|
2008-08-09 08:18:32 +02:00
|
|
|
|
2010-10-19 05:33:51 +02:00
|
|
|
// perform unit tests (use PhpUnitWrapper or derived versions)
|
2010-10-19 05:35:14 +02:00
|
|
|
$phpunitwrapper = PhpUnitWrapper::inst();
|
2010-10-19 05:33:51 +02:00
|
|
|
$phpunitwrapper->setSuite($suite);
|
|
|
|
$phpunitwrapper->setCoverageStatus($coverage);
|
2008-08-25 03:41:58 +02:00
|
|
|
|
2010-10-19 05:33:51 +02:00
|
|
|
$phpunitwrapper->runTests();
|
|
|
|
|
|
|
|
// get results of the PhpUnitWrapper class
|
|
|
|
$reporter = $phpunitwrapper->getReporter();
|
|
|
|
$results = $phpunitwrapper->getFrameworkTestResults();
|
2008-05-15 10:46:40 +02:00
|
|
|
|
2008-09-23 05:22:13 +02:00
|
|
|
if(!Director::is_cli()) echo '<div class="trace">';
|
2008-08-09 08:18:32 +02:00
|
|
|
$reporter->writeResults();
|
2009-08-21 05:02:43 +02:00
|
|
|
|
|
|
|
$endTime = microtime(true);
|
|
|
|
if(Director::is_cli()) echo "\n\nTotal time: " . round($endTime-$startTime,3) . " seconds\n";
|
2012-09-01 01:58:52 +02:00
|
|
|
else echo "<p class=\"total-time\">Total time: " . round($endTime-$startTime,3) . " seconds</p>\n";
|
2008-08-09 08:18:32 +02:00
|
|
|
|
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();
|
2008-08-13 03:47:05 +02:00
|
|
|
|
2008-08-13 04:47:14 +02:00
|
|
|
$this->tearDown();
|
|
|
|
|
2008-05-15 10:46:40 +02:00
|
|
|
// Todo: we should figure out how to pass this data back through Director more cleanly
|
2008-08-13 03:47:05 +02:00
|
|
|
if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2);
|
2007-08-15 12:01:35 +02:00
|
|
|
}
|
2008-08-13 04:47:14 +02:00
|
|
|
|
2008-08-27 10:19:46 +02:00
|
|
|
/**
|
|
|
|
* Start a test session.
|
2012-09-26 23:34:00 +02:00
|
|
|
* 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.
|
2012-08-07 19:03:53 +02:00
|
|
|
*
|
|
|
|
* See {@link setdb()} for an alternative approach which just sets a database
|
|
|
|
* name, and is used for more advanced use cases like interacting with test databases
|
|
|
|
* directly during functional tests.
|
2008-08-27 10:19:46 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function startsession() {
|
2008-08-27 10:19:46 +02:00
|
|
|
if(!Director::isLive()) {
|
|
|
|
if(SapphireTest::using_temp_db()) {
|
2012-03-05 02:36:38 +01:00
|
|
|
$endLink = Director::baseURL() . "dev/tests/endsession";
|
2012-09-26 23:34:00 +02:00
|
|
|
return "<p><a id=\"end-session\" href=\"$endLink\">You're in the middle of a test session;"
|
|
|
|
. " click here to end it.</a></p>";
|
2008-08-27 10:19:46 +02:00
|
|
|
|
|
|
|
} else if(!isset($_GET['fixture'])) {
|
2012-03-05 02:36:38 +01:00
|
|
|
$me = Director::baseURL() . "dev/tests/startsession";
|
2008-08-27 10:19:46 +02:00
|
|
|
return <<<HTML
|
|
|
|
<form action="$me">
|
2012-09-26 23:34:00 +02:00
|
|
|
<p>Enter a fixture file name to start a new test session. Don't forget to visit dev/tests/endsession when
|
|
|
|
you're done!</p>
|
2010-04-13 03:55:09 +02:00
|
|
|
<p>Fixture file (leave blank to start with default set-up): <input id="fixture-file" name="fixture" /></p>
|
2008-08-27 10:19:46 +02:00
|
|
|
<input type="hidden" name="flush" value="1">
|
|
|
|
<p><input id="start-session" value="Start test session" type="submit" /></p>
|
|
|
|
</form>
|
|
|
|
HTML;
|
|
|
|
} else {
|
|
|
|
$fixtureFile = $_GET['fixture'];
|
2010-04-13 03:55:09 +02:00
|
|
|
|
|
|
|
if($fixtureFile) {
|
|
|
|
// Validate fixture file
|
|
|
|
$realFile = realpath(BASE_PATH.'/'.$fixtureFile);
|
|
|
|
$baseDir = realpath(Director::baseFolder());
|
|
|
|
if(!$realFile || !file_exists($realFile)) {
|
|
|
|
return "<p>Fixture file doesn't exist</p>";
|
|
|
|
} else if(substr($realFile,0,strlen($baseDir)) != $baseDir) {
|
|
|
|
return "<p>Fixture file must be inside $baseDir</p>";
|
|
|
|
} else if(substr($realFile,-4) != '.yml') {
|
|
|
|
return "<p>Fixture file must be a .yml file</p>";
|
|
|
|
} else if(!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) {
|
|
|
|
return "<p>Fixture file must be inside the tests subfolder of one of your modules.</p>";
|
|
|
|
}
|
2008-08-27 10:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$dbname = SapphireTest::create_temp_db();
|
2008-10-09 20:49:24 +02:00
|
|
|
|
2008-08-27 10:19:46 +02:00
|
|
|
DB::set_alternative_database_name($dbname);
|
2010-04-13 03:55:09 +02:00
|
|
|
|
|
|
|
// Fixture
|
|
|
|
if($fixtureFile) {
|
|
|
|
$fixture = new YamlFixture($fixtureFile);
|
|
|
|
$fixture->saveIntoDatabase();
|
|
|
|
|
|
|
|
// If no fixture, then use defaults
|
|
|
|
} else {
|
|
|
|
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
|
|
|
array_shift($dataClasses);
|
|
|
|
foreach($dataClasses as $dataClass) singleton($dataClass)->requireDefaultRecords();
|
|
|
|
}
|
2008-08-27 10:19:46 +02:00
|
|
|
|
2012-09-26 23:34:00 +02:00
|
|
|
return "<p>Started testing session with fixture '$fixtureFile'.
|
|
|
|
Time to start testing; where would you like to start?</p>
|
2008-08-27 10:19:46 +02:00
|
|
|
<ul>
|
|
|
|
<li><a id=\"home-link\" href=\"" .Director::baseURL() . "\">Homepage - published site</a></li>
|
2012-09-26 23:34:00 +02:00
|
|
|
<li><a id=\"draft-link\" href=\"" .Director::baseURL() . "?stage=Stage\">Homepage - draft site
|
|
|
|
</a></li>
|
2008-08-27 10:19:46 +02:00
|
|
|
<li><a id=\"admin-link\" href=\"" .Director::baseURL() . "admin/\">CMS Admin</a></li>
|
2012-09-26 23:34:00 +02:00
|
|
|
<li><a id=\"endsession-link\" href=\"" .Director::baseURL() . "dev/tests/endsession\">
|
|
|
|
End your test session</a></li>
|
2008-08-27 10:19:46 +02:00
|
|
|
</ul>";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return "<p>startession can only be used on dev and test sites</p>";
|
|
|
|
}
|
|
|
|
}
|
2012-08-07 19:03:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an alternative database name in the current browser session.
|
|
|
|
* Useful for functional testing libraries like behat to create a "clean slate".
|
|
|
|
* Does not actually create the database, that's usually handled
|
|
|
|
* by {@link SapphireTest::create_temp_db()}.
|
|
|
|
*
|
|
|
|
* The database names are limited to a specific naming convention as a security measure:
|
|
|
|
* The "tmpdb" prefix and a random sequence of seven digits.
|
|
|
|
* This avoids the user gaining access to other production databases
|
|
|
|
* available on the same connection.
|
|
|
|
*
|
|
|
|
* See {@link startsession()} for a different approach which actually creates
|
|
|
|
* the DB and loads a fixture file instead.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setdb() {
|
2012-08-07 19:03:53 +02:00
|
|
|
if(Director::isLive()) {
|
|
|
|
return $this->permissionFailure("dev/tests/setdb can only be used on dev and test sites");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!isset($_GET['database'])) {
|
|
|
|
return $this->permissionFailure("dev/tests/setdb must be used with a 'database' parameter");
|
|
|
|
}
|
|
|
|
|
|
|
|
$database_name = $_GET['database'];
|
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
|
|
|
$pattern = strtolower(sprintf('#^%stmpdb\d{7}#', $prefix));
|
|
|
|
if(!preg_match($pattern, $database_name)) {
|
|
|
|
return $this->permissionFailure("Invalid database name format");
|
|
|
|
}
|
|
|
|
|
|
|
|
DB::set_alternative_database_name($database_name);
|
|
|
|
|
|
|
|
return "<p>Set database session to '$database_name'. Time to start testing; where would you like to start?</p>
|
|
|
|
<ul>
|
|
|
|
<li><a id=\"home-link\" href=\"" .Director::baseURL() . "\">Homepage - published site</a></li>
|
|
|
|
<li><a id=\"draft-link\" href=\"" .Director::baseURL() . "?stage=Stage\">Homepage - draft site</a></li>
|
|
|
|
<li><a id=\"admin-link\" href=\"" .Director::baseURL() . "admin/\">CMS Admin</a></li>
|
2012-09-26 23:34:00 +02:00
|
|
|
<li><a id=\"endsession-link\" href=\"" .Director::baseURL() . "dev/tests/endsession\">
|
|
|
|
End your test session</a></li>
|
2012-08-07 19:03:53 +02:00
|
|
|
</ul>";
|
|
|
|
}
|
2008-08-27 10:19:46 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function emptydb() {
|
2010-10-19 02:54:31 +02:00
|
|
|
if(SapphireTest::using_temp_db()) {
|
|
|
|
SapphireTest::empty_temp_db();
|
|
|
|
|
|
|
|
if(isset($_GET['fixture']) && ($fixtureFile = $_GET['fixture'])) {
|
|
|
|
$fixture = new YamlFixture($fixtureFile);
|
|
|
|
$fixture->saveIntoDatabase();
|
2012-09-26 23:34:00 +02:00
|
|
|
return "<p>Re-test the test database with fixture '$fixtureFile'. Time to start testing; where would"
|
|
|
|
. " you like to start?</p>";
|
2010-10-19 02:54:31 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return "<p>Re-test the test database. Time to start testing; where would you like to start?</p>";
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
return "<p>dev/tests/emptydb can only be used with a temporary database. Perhaps you should use"
|
|
|
|
. " dev/tests/startsession first?</p>";
|
2010-10-19 02:54:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function endsession() {
|
2008-08-27 10:19:46 +02:00
|
|
|
SapphireTest::kill_temp_db();
|
|
|
|
DB::set_alternative_database_name(null);
|
|
|
|
|
2010-04-13 03:55:09 +02:00
|
|
|
return "<p>Test session ended.</p>
|
|
|
|
<ul>
|
|
|
|
<li><a id=\"home-link\" href=\"" .Director::baseURL() . "\">Return to your site</a></li>
|
2012-09-26 23:34:00 +02:00
|
|
|
<li><a id=\"startsession-link\" href=\"" .Director::baseURL() . "dev/tests/startsession\">
|
|
|
|
Start a new test session</a></li>
|
2010-04-13 03:55:09 +02:00
|
|
|
</ul>";
|
2008-08-27 10:19:46 +02:00
|
|
|
}
|
2010-10-15 02:01:23 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function sessionloadyml() {
|
2010-10-15 02:01:23 +02:00
|
|
|
// Load incremental YAML fixtures
|
|
|
|
// TODO: We will probably have to filter out the admin member here,
|
|
|
|
// as it is supplied by Bare.yml
|
|
|
|
if(Director::isLive()) {
|
|
|
|
return "<p>sessionloadyml can only be used on dev and test sites</p>";
|
|
|
|
}
|
|
|
|
if (!SapphireTest::using_temp_db()) {
|
|
|
|
return "<p>Please load /dev/tests/startsession first</p>";
|
|
|
|
}
|
|
|
|
|
|
|
|
$fixtureFile = isset($_GET['fixture']) ? $_GET['fixture'] : null;
|
|
|
|
if (empty($fixtureFile)) {
|
|
|
|
$me = Director::baseURL() . "/dev/tests/sessionloadyml";
|
|
|
|
return <<<HTML
|
|
|
|
<form action="$me">
|
|
|
|
<p>Enter a fixture file name to load a new YAML fixture into the session.</p>
|
|
|
|
<p>Fixture file <input id="fixture-file" name="fixture" /></p>
|
|
|
|
<input type="hidden" name="flush" value="1">
|
|
|
|
<p><input id="session-load-yaml" value="Load yml fixture" type="submit" /></p>
|
|
|
|
</form>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
// Validate fixture file
|
|
|
|
$realFile = realpath(BASE_PATH.'/'.$fixtureFile);
|
|
|
|
$baseDir = realpath(Director::baseFolder());
|
|
|
|
if(!$realFile || !file_exists($realFile)) {
|
|
|
|
return "<p>Fixture file doesn't exist</p>";
|
|
|
|
} else if(substr($realFile,0,strlen($baseDir)) != $baseDir) {
|
|
|
|
return "<p>Fixture file must be inside $baseDir</p>";
|
|
|
|
} else if(substr($realFile,-4) != '.yml') {
|
|
|
|
return "<p>Fixture file must be a .yml file</p>";
|
|
|
|
} else if(!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) {
|
|
|
|
return "<p>Fixture file must be inside the tests subfolder of one of your modules.</p>";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fixture
|
|
|
|
$fixture = new YamlFixture($fixtureFile);
|
|
|
|
$fixture->saveIntoDatabase();
|
|
|
|
|
|
|
|
return "<p>Loaded fixture '$fixtureFile' into session</p>";
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2009-11-27 01:24:56 +01:00
|
|
|
// The first DB test will sort out the DB, we don't have to
|
2008-10-16 21:48:12 +02:00
|
|
|
SSViewer::flush_template_cache();
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDown() {
|
2008-08-13 04:47:14 +02:00
|
|
|
SapphireTest::kill_temp_db();
|
2009-02-03 22:17:23 +01:00
|
|
|
DB::set_alternative_database_name(null);
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
2007-08-15 12:01:35 +02:00
|
|
|
}
|