mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT: Added dev/tests/startsession and dev/tests/endsession to allow the use of fixtures with external test frameworks, such as Windmill
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@61627 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
872b24003b
commit
e9615040e1
@ -36,6 +36,15 @@ class DB {
|
||||
return DB::$globalConn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an alternative database to use for this browser session.
|
||||
* This is useful when using testing systems other than SapphireTest; for example, Windmill.
|
||||
* Set it to null to revert to the main database.
|
||||
*/
|
||||
static function set_alternative_database_name($dbname) {
|
||||
$_SESSION["alternativeDatabaseName"] = $dbname;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect to a database.
|
||||
* Given the database configuration, this method will create the correct subclass of Database,
|
||||
@ -43,6 +52,9 @@ class DB {
|
||||
* @param array $database A map of options. The 'type' is the name of the subclass of Database to use. For the rest of the options, see the specific class.
|
||||
*/
|
||||
static function connect($databaseConfig) {
|
||||
// This is used by TestRunner::startsession() to test up a test session using an alt
|
||||
if(isset($_SESSION["alternativeDatabaseName"]) && $dbname = $_SESSION["alternativeDatabaseName"]) $databaseConfig['database'] = $dbname;
|
||||
|
||||
if(!isset($databaseConfig['type']) || empty($databaseConfig['type'])) {
|
||||
user_error("DB::connect: Not passed a valid database config", E_USER_ERROR);
|
||||
}
|
||||
|
@ -163,10 +163,18 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if we are currently using a temporary database
|
||||
*/
|
||||
static function using_temp_db() {
|
||||
$dbConn = DB::getConn();
|
||||
return $dbConn && (substr($dbConn->currentDatabase(),0,5) == 'tmpdb');
|
||||
}
|
||||
|
||||
static function kill_temp_db() {
|
||||
// Delete our temporary database
|
||||
if(self::using_temp_db()) {
|
||||
$dbConn = DB::getConn();
|
||||
if($dbConn && substr($dbConn->currentDatabase(),0,5) == 'tmpdb') {
|
||||
$dbName = $dbConn->currentDatabase();
|
||||
if($dbName && DB::query("SHOW DATABASES LIKE '$dbName'")->value()) {
|
||||
// echo "Deleted temp database " . $dbConn->currentDatabase() . "\n";
|
||||
@ -188,6 +196,8 @@ class SapphireTest extends PHPUnit_Framework_TestCase {
|
||||
|
||||
$dbadmin = new DatabaseAdmin();
|
||||
$dbadmin->doBuild(true, false, true);
|
||||
|
||||
return $dbname;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -34,6 +34,8 @@ class TestRunner extends Controller {
|
||||
static $url_handlers = array(
|
||||
'' => 'browse',
|
||||
'coverage' => 'coverage',
|
||||
'startsession' => 'startsession',
|
||||
'endsession' => 'endsession',
|
||||
'$TestCase' => 'only',
|
||||
);
|
||||
|
||||
@ -176,6 +178,69 @@ class TestRunner extends Controller {
|
||||
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 "<p><a id=\"end-session\" href=\"$endLink\">You're in the middle of a test session; click here to end it.</a></p>";
|
||||
|
||||
} else if(!isset($_GET['fixture'])) {
|
||||
$me = Director::baseURL() . "/dev/tests/startsession";
|
||||
return <<<HTML
|
||||
<form action="$me">
|
||||
<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>
|
||||
<p>Fixture file: <input id="fixture-file" name="fixture" /></p>
|
||||
<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'];
|
||||
|
||||
// Validate fixture file
|
||||
$realFile = realpath('../' . $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>";
|
||||
}
|
||||
|
||||
$dbname = SapphireTest::create_temp_db();
|
||||
DB::set_alternative_database_name($dbname);
|
||||
|
||||
$fixture = new YamlFixture($_GET['fixture']);
|
||||
$fixture->saveIntoDatabase();
|
||||
|
||||
return "<p>Started testing session with fixture '$fixtureFile'. 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>
|
||||
</ul>";
|
||||
}
|
||||
|
||||
} else {
|
||||
return "<p>startession can only be used on dev and test sites</p>";
|
||||
}
|
||||
}
|
||||
|
||||
function endsession() {
|
||||
SapphireTest::kill_temp_db();
|
||||
DB::set_alternative_database_name(null);
|
||||
|
||||
return "<p>Test session ended.</p>";
|
||||
}
|
||||
|
||||
function setUp() {
|
||||
SapphireTest::create_temp_db();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user