mirror of
https://github.com/silverstripe/silverstripe-testsession
synced 2024-10-22 14:06:00 +02:00
TestSessionDatabaseState for shared state between processes
This commit is contained in:
parent
c43a26a998
commit
3dbaf8f92a
@ -5,4 +5,7 @@ Injector:
|
||||
RequestProcessor:
|
||||
properties:
|
||||
filters:
|
||||
- '%$TestSessionRequestFilter'
|
||||
- '%$TestSessionRequestFilter'
|
||||
Member:
|
||||
extensions:
|
||||
- TestSessionMemberExtension
|
18
code/TestSessionDatabaseState.php
Normal file
18
code/TestSessionDatabaseState.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
/**
|
||||
* Used to share arbitrary state between the browser session
|
||||
* and other processes such as Behat CLI execution.
|
||||
* Assumes that the temporary database is reset automatically
|
||||
* on ending the test session.
|
||||
*/
|
||||
class TestSessionDatabaseState extends DataObject {
|
||||
|
||||
private static $db = array(
|
||||
'Key' => 'Varchar(255)',
|
||||
'Value' => 'Text',
|
||||
);
|
||||
|
||||
private static $indexes = array(
|
||||
'Key' => true
|
||||
);
|
||||
}
|
29
code/TestSessionMemberExtension.php
Normal file
29
code/TestSessionMemberExtension.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
/**
|
||||
* Stores the currently logged in user in the database in addition to
|
||||
* PHP session. This means the information can be shared with other processes
|
||||
* such as a Behat CLI execution, without requiring this information to be available
|
||||
* through the UI (and potentially cause another page load via Selenium).
|
||||
*/
|
||||
class TestSessionMemberExtension extends DataExtension {
|
||||
|
||||
public function memberLoggedIn() {
|
||||
if(!SapphireTest::using_temp_db()) return;
|
||||
|
||||
$state = TestSessionDatabaseState::get()->find('Key', 'CurrentMemberID');
|
||||
if(!$state) {
|
||||
$state = new TestSessionDatabaseState(array(
|
||||
'Key' => 'CurrentMemberID'
|
||||
));
|
||||
}
|
||||
$state->Value = $this->owner->ID;
|
||||
$state->write();
|
||||
}
|
||||
|
||||
public function memberLoggedOut() {
|
||||
if(!SapphireTest::using_temp_db()) return;
|
||||
|
||||
$state = TestSessionDatabaseState::get()->filter('Key', 'CurrentMemberID')->removeAll();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user