mirror of
https://github.com/silverstripe/silverstripe-testsession
synced 2024-10-22 14:06:00 +02:00
Compare commits
10 Commits
466c966e26
...
dc76ac2877
Author | SHA1 | Date | |
---|---|---|---|
|
dc76ac2877 | ||
|
d7c4b0c420 | ||
|
8f4771385c | ||
|
3eb61e353f | ||
|
a8efc75702 | ||
|
b966a32e0c | ||
|
c1cf2b4106 | ||
|
bc4cb87627 | ||
|
2a9fb2b1e3 | ||
|
b87e4d46d0 |
@ -12,7 +12,7 @@ TestSessionEnvironment::singleton()->loadFromFile();
|
||||
*/
|
||||
if (class_exists('Resque_Event') && class_exists('SSResqueRun')) {
|
||||
Resque_Event::listen('beforeFork', function ($data) {
|
||||
$databaseConfig = DB::getConfig();
|
||||
$databaseConfig = DB::getConfig(DB::CONN_PRIMARY);
|
||||
|
||||
// Reconnect to the database - this may connect to the old DB first, but is required because these processes
|
||||
// are long-lived, and MySQL connections often get closed in between worker runs. We need to connect before
|
||||
|
@ -15,9 +15,9 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^8.1",
|
||||
"php": "^8.3",
|
||||
"composer/installers": "*",
|
||||
"silverstripe/framework": "^5",
|
||||
"silverstripe/framework": "^6",
|
||||
"silverstripe/vendor-plugin": "^2"
|
||||
},
|
||||
"require-dev": {
|
||||
|
@ -16,14 +16,14 @@ use SilverStripe\Forms\Form;
|
||||
use SilverStripe\Forms\FormAction;
|
||||
use SilverStripe\Forms\HiddenField;
|
||||
use SilverStripe\Forms\TextField;
|
||||
use SilverStripe\ORM\ArrayList;
|
||||
use SilverStripe\Model\List\ArrayList;
|
||||
use SilverStripe\ORM\Connect\TempDatabase;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\FieldType\DBHTMLText;
|
||||
use SilverStripe\Security\Permission;
|
||||
use SilverStripe\Security\RandomGenerator;
|
||||
use SilverStripe\Security\Security;
|
||||
use SilverStripe\View\ArrayData;
|
||||
use SilverStripe\Model\ArrayData;
|
||||
use SilverStripe\View\Requirements;
|
||||
|
||||
/**
|
||||
@ -67,7 +67,7 @@ class TestSessionController extends Controller
|
||||
{
|
||||
parent::init();
|
||||
|
||||
$this->extend('init');
|
||||
$this->extend('onInit');
|
||||
|
||||
$canAccess = (
|
||||
!Director::isLive()
|
||||
@ -326,7 +326,7 @@ class TestSessionController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* As with {@link self::start()}, if you want to extend the functionality of this, then look at
|
||||
* As with {@link TestSessionController::start()}, if you want to extend the functionality of this, then look at
|
||||
* {@link TestSessionEnvironent::endTestSession()} as the extension points have moved to there now that the logic
|
||||
* is there.
|
||||
*/
|
||||
|
@ -15,9 +15,10 @@ use SilverStripe\Core\Extensible;
|
||||
use SilverStripe\Core\Injector\Injectable;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Dev\FixtureFactory;
|
||||
use SilverStripe\PolyExecution\Command\DbBuild;
|
||||
use SilverStripe\PolyExecution\PolyOutput;
|
||||
use SilverStripe\Dev\YamlFixture;
|
||||
use SilverStripe\ORM\Connect\TempDatabase;
|
||||
use SilverStripe\ORM\DatabaseAdmin;
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\FieldType\DBDatetime;
|
||||
use SilverStripe\Versioned\Versioned;
|
||||
@ -62,7 +63,7 @@ class TestSessionEnvironment
|
||||
/**
|
||||
* @var string The original database name, before we overrode it with our tmpdb.
|
||||
*
|
||||
* Used in {@link self::resetDatabaseName()} when we want to restore the normal DB connection.
|
||||
* Used in {@link TestSessionEnvironment::resetDatabaseName()} when we want to restore the normal DB connection.
|
||||
*/
|
||||
private $oldDatabaseName;
|
||||
|
||||
@ -136,14 +137,16 @@ class TestSessionEnvironment
|
||||
|
||||
/**
|
||||
* Creates a temp database, sets up any extra requirements, and writes the state file. The database will be
|
||||
* connected to as part of {@link self::applyState()}, so if you're continuing script execution after calling this
|
||||
* connected to as part of {@link TestSessionEnvironment::applyState()}, so if you're continuing script
|
||||
* execution after calling this
|
||||
* method, be aware that the database will be different - so various things may break (e.g. administrator logins
|
||||
* using the SS_DEFAULT_USERNAME / SS_DEFAULT_PASSWORD constants).
|
||||
*
|
||||
* If something isn't explicitly handled here, and needs special handling, then it should be taken care of by an
|
||||
* extension to TestSessionEnvironment. You can either extend onBeforeStartTestSession() or
|
||||
* onAfterStartTestSession(). Alternatively, for more fine-grained control, you can also extend
|
||||
* onBeforeApplyState() and onAfterApplyState(). See the {@link self::applyState()} method for more.
|
||||
* onBeforeApplyState() and onAfterApplyState(). See the {@link TestSessionEnvironment::applyState()}
|
||||
* method for more.
|
||||
*
|
||||
* @param array $state An array of test state options to write.
|
||||
* @param mixed $id
|
||||
@ -269,7 +272,7 @@ class TestSessionEnvironment
|
||||
$this->extend('onBeforeApplyState', $state);
|
||||
|
||||
// back up source
|
||||
$databaseConfig = DB::getConfig();
|
||||
$databaseConfig = DB::getConfig(DB::CONN_PRIMARY);
|
||||
$this->oldDatabaseName = $databaseConfig['database'];
|
||||
|
||||
// Load existing state from $this->state into $state, if there is any
|
||||
@ -370,9 +373,13 @@ class TestSessionEnvironment
|
||||
}
|
||||
|
||||
// In case the dump involved CREATE TABLE commands, we need to ensure the schema is still up to date
|
||||
$dbAdmin = new DatabaseAdmin();
|
||||
$dbBuild = new DbBuild();
|
||||
Versioned::set_reading_mode('');
|
||||
$dbAdmin->doBuild(true, $requireDefaultRecords);
|
||||
$output = new PolyOutput(
|
||||
Director::is_cli() ? PolyOutput::FORMAT_ANSI : PolyOutput::FORMAT_HTML,
|
||||
PolyOutput::VERBOSITY_QUIET
|
||||
);
|
||||
$dbBuild->doBuild($output, $requireDefaultRecords);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -380,9 +387,13 @@ class TestSessionEnvironment
|
||||
*/
|
||||
public function requireDefaultRecords()
|
||||
{
|
||||
$dbAdmin = new DatabaseAdmin();
|
||||
$dbBuild = new DbBuild();
|
||||
Versioned::set_reading_mode('');
|
||||
$dbAdmin->doBuild(true, true);
|
||||
$output = new PolyOutput(
|
||||
Director::is_cli() ? PolyOutput::FORMAT_ANSI : PolyOutput::FORMAT_HTML,
|
||||
PolyOutput::VERBOSITY_QUIET
|
||||
);
|
||||
$dbBuild->doBuild($output, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -436,7 +447,8 @@ class TestSessionEnvironment
|
||||
|
||||
/**
|
||||
* Cleans up the test session state by restoring the normal database connect (for the rest of this request, if any)
|
||||
* and removes the {@link self::$test_state_file} so that future requests don't use this test state.
|
||||
* and removes the {@link TestSessionEnvironment::$test_state_file} so that future requests don't use this
|
||||
* test state.
|
||||
*
|
||||
* Can be extended by implementing either onBeforeEndTestSession() or onAfterEndTestSession().
|
||||
*
|
||||
@ -505,14 +517,15 @@ class TestSessionEnvironment
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the database connection to use the original database. Called by {@link self::endTestSession()}.
|
||||
* Reset the database connection to use the original database.
|
||||
* Called by {@link TestSessionEnvironment::endTestSession()}.
|
||||
*/
|
||||
public function resetDatabaseName()
|
||||
{
|
||||
if ($this->oldDatabaseName) {
|
||||
$databaseConfig = DB::getConfig();
|
||||
$databaseConfig = DB::getConfig(DB::CONN_PRIMARY);
|
||||
$databaseConfig['database'] = $this->oldDatabaseName;
|
||||
DB::setConfig($databaseConfig);
|
||||
DB::setConfig($databaseConfig, DB::CONN_PRIMARY);
|
||||
|
||||
$conn = DB::get_conn();
|
||||
|
||||
@ -523,7 +536,7 @@ class TestSessionEnvironment
|
||||
}
|
||||
|
||||
/**
|
||||
* @return stdClass Data as taken from the JSON object in {@link self::loadFromFile()}
|
||||
* @return stdClass Data as taken from the JSON object in {@link TestSessionEnvironment::loadFromFile()}
|
||||
*/
|
||||
public function getState()
|
||||
{
|
||||
@ -555,7 +568,7 @@ class TestSessionEnvironment
|
||||
$state = $this->getState();
|
||||
}
|
||||
|
||||
$databaseConfig = DB::getConfig();
|
||||
$databaseConfig = DB::getConfig(DB::CONN_PRIMARY);
|
||||
|
||||
if (isset($state->database) && $state->database) {
|
||||
if (!DB::get_conn()) {
|
||||
@ -566,14 +579,14 @@ class TestSessionEnvironment
|
||||
}
|
||||
|
||||
// Connect to database
|
||||
DB::connect($databaseConfig);
|
||||
$this->connectToDB($databaseConfig);
|
||||
} else {
|
||||
// We've already connected to the database, do a fast check to see what database we're currently using
|
||||
$db = DB::get_conn()->getSelectedDatabase();
|
||||
if (isset($state->database) && $db != $state->database) {
|
||||
$this->oldDatabaseName = $databaseConfig['database'];
|
||||
$databaseConfig['database'] = $state->database;
|
||||
DB::connect($databaseConfig);
|
||||
$this->connectToDB($databaseConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -609,4 +622,17 @@ class TestSessionEnvironment
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private function connectToDB(array $databaseConfig): void
|
||||
{
|
||||
if (method_exists(DB::class, 'hasConfig')) {
|
||||
// CMS 5.4+ - ensure we connect the primary connection and not a replica
|
||||
// which can happen if we use the default value of DB::CONN_DYNAMIC
|
||||
// and there is a replica database configured
|
||||
DB::connect($databaseConfig, DB::CONN_PRIMARY);
|
||||
} else {
|
||||
// CMS 5.3 and below do not support replica connections, use 'default' connection
|
||||
DB::connect($databaseConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ class TestSessionState extends DataObject
|
||||
{
|
||||
$schema = DataObject::getSchema();
|
||||
|
||||
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class)))
|
||||
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(TestSessionState::class)))
|
||||
->addWhere(['ID' => 1])
|
||||
->assignSQL('"PendingRequests"', '"PendingRequests" + 1');
|
||||
|
||||
@ -50,10 +50,10 @@ class TestSessionState extends DataObject
|
||||
{
|
||||
$schema = DataObject::getSchema();
|
||||
|
||||
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(self::class)))
|
||||
$update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(TestSessionState::class)))
|
||||
->addWhere(['ID' => 1])
|
||||
->assignSQL('"PendingRequests"', '"PendingRequests" - 1')
|
||||
->assign('"LastResponseTimestamp"', self::millitime());
|
||||
->assign('"LastResponseTimestamp"', TestSessionState::millitime());
|
||||
|
||||
$update->execute();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user