SilverStripe\TestSession\TestSessionEnvironment
Responsible for starting and finalizing test sessions.
Since these session span across multiple requests, session information is persisted
in a file. This file is stored in the webroot by default, and the test session
is considered "in progress" as long as this file exists.
This allows for cross-request, cross-client sharing of the same testsession,
for example: Behat CLI starts a testsession, then opens a web browser which
makes a separate request picking up the same testsession.
An environment can have an optional identifier (), which allows
multiple environments to exist at the same time in the same webroot.
This enables parallel testing with (mostly) isolated state.
For a valid test session to exist, this needs to contain at least:
- database: The alternate database name that is being used for this test session (e.g. ss_tmpdb_1234567)
It can optionally contain other details that should be passed through many separate requests:
- datetime: Mocked SS_DateTime ()
- mailer: Mocked Email sender ()
- stubfile: Path to PHP stub file for setup ()
Extensions of TestSessionEnvironment can add extra fields in here to be saved and restored on each request.
See for default information stored in the test session.
Synopsis
- // members
- protected $id;
- private $oldDatabaseName;
- private static string $test_state_file = 'TESTS_RUNNING.json';
- private static [type] $test_state_id_file = 'TESTS_RUNNING-%s.json';
- // methods
- public void __construct()
- public void init()
- public string getFilePath()
- public void isRunningTests()
- public void setId()
- public String getId()
- public void startTestSession()
- public void updateTestSession()
- protected void backupAssets()
- public void restoreAssets()
- protected void moveRecursive()
- public void applyState()
- public void importDatabase()
- public void requireDefaultRecords()
- public void saveState()
- public void loadFromFile()
- private void removeStateFile()
- public void endTestSession()
- public FixtureFactory loadFixtureIntoDb()
- public void resetDatabaseName()
- public stdClass getState()
- protected string getAssetsBackupfolder()
- public void connectToDatabase()
- public bool waitForPendingRequests()
Hierarchy
Uses
- SilverStripe\Core\Injector\Injectable
- SilverStripe\Core\Config\Configurable
- SilverStripe\Core\Extensible
Members
private
- $oldDatabaseName — string
- $test_state_file — string
- $test_state_id_file — SilverStripe\TestSession\[type]
protected
- $id — int
Methods
private
protected
- backupAssets() — Backup all assets from /assets to /assets_backup.
- getAssetsBackupfolder() — Path where assets should be backed up during testing
- moveRecursive() — Recursively move files from one directory to another
public
- __construct()
- applyState() — Assumes the database has already been created in startTestSession(), as this method can be called from _config.php where we don't yet have a DB connection.
- connectToDatabase() — Ensure that there is a connection to the database
- endTestSession() — 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.
- getFilePath()
- getId()
- getState()
- importDatabase() — Import the database
- init()
- isRunningTests() — Tests for the existence of the file specified by $this->test_state_file
- loadFixtureIntoDb() — Loads a YAML fixture into the database as part of the {@link TestSessionController}.
- loadFromFile()
- requireDefaultRecords() — Build the database with default records, see {@link DataObject->requireDefaultRecords()}.
- resetDatabaseName() — Reset the database connection to use the original database. Called by {@link self::endTestSession()}.
- restoreAssets() — Restore all assets to /assets folder.
- saveState() — Sliented as if the file already exists by another process, we don't want to modify.
- setId()
- startTestSession() — 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 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).
- updateTestSession()
- waitForPendingRequests() — Wait for pending requests