API Use new TempDatabase service

This commit is contained in:
Damian Mooyman 2017-06-21 21:04:10 +12:00
parent 1651d5695a
commit f5ef9f4fbf
2 changed files with 18 additions and 10 deletions

View File

@ -16,6 +16,7 @@ use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\TextField; use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList; use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\Connect\TempDatabase;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Security\Permission; use SilverStripe\Security\Permission;
@ -315,8 +316,9 @@ class TestSessionController extends Controller
$this->extend('onBeforeClear'); $this->extend('onBeforeClear');
if (SapphireTest::using_temp_db()) { $tempDB = new TempDatabase();
SapphireTest::empty_temp_db(); if ($tempDB->isUsed()) {
$tempDB->clearAllData();
} }
if (isset($_SESSION['_testsession_codeblocks'])) { if (isset($_SESSION['_testsession_codeblocks'])) {
@ -359,7 +361,8 @@ class TestSessionController extends Controller
*/ */
public function isTesting() public function isTesting()
{ {
return SapphireTest::using_temp_db(); $tempDB = new TempDatabase();
return $tempDB->isUsed();
} }
/** /**

View File

@ -12,7 +12,7 @@ use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\FixtureFactory; use SilverStripe\Dev\FixtureFactory;
use SilverStripe\Dev\SapphireTest; use SilverStripe\ORM\Connect\TempDatabase;
use SilverStripe\ORM\DatabaseAdmin; use SilverStripe\ORM\DatabaseAdmin;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime; use SilverStripe\ORM\FieldType\DBDatetime;
@ -195,7 +195,7 @@ class TestSessionEnvironment
$this->extend('onBeforeApplyState', $state); $this->extend('onBeforeApplyState', $state);
// back up source // back up source
global $databaseConfig; $databaseConfig = DB::getConfig();
$this->oldDatabaseName = $databaseConfig['database']; $this->oldDatabaseName = $databaseConfig['database'];
// Load existing state from $this->state into $state, if there is any // Load existing state from $this->state into $state, if there is any
@ -243,7 +243,8 @@ class TestSessionEnvironment
if (!$dbExists) { if (!$dbExists) {
// Create a new one with a randomized name // Create a new one with a randomized name
$dbName = SapphireTest::create_temp_db(); $tempDB = new TempDatabase();
$dbName = $tempDB->build();
$state->database = $dbName; // In case it's changed by the call to SapphireTest::create_temp_db(); $state->database = $dbName; // In case it's changed by the call to SapphireTest::create_temp_db();
@ -355,9 +356,12 @@ class TestSessionEnvironment
$this->applyState($json); $this->applyState($json);
} catch (Exception $e) { } catch (Exception $e) {
throw new \Exception("A test session appears to be in progress, but we can't retrieve the details. " throw new Exception(
. "Try removing the " . $this->getFilePath() . " file. Inner " "A test session appears to be in progress, but we can't retrieve the details.\n"
. "error: " . $e->getMessage()); . "Try removing the " . $this->getFilePath() . " file.\n"
. "Inner error: " . $e->getMessage() . "\n"
. "Stacktrace: " . $e->getTraceAsString()
);
} }
} }
} }
@ -389,7 +393,8 @@ class TestSessionEnvironment
{ {
$this->extend('onBeforeEndTestSession'); $this->extend('onBeforeEndTestSession');
if (SapphireTest::using_temp_db()) { $tempDB = new TempDatabase();
if ($tempDB->isUsed()) {
$state = $this->getState(); $state = $this->getState();
$dbConn = DB::get_schema(); $dbConn = DB::get_schema();
$dbExists = $dbConn->databaseExists($state->database); $dbExists = $dbConn->databaseExists($state->database);