TestSessionDatabaseState for shared state between processes

This commit is contained in:
Ingo Schommer 2013-12-09 01:41:44 +01:00
parent c43a26a998
commit 3dbaf8f92a
3 changed files with 51 additions and 1 deletions

View File

@ -5,4 +5,7 @@ Injector:
RequestProcessor:
properties:
filters:
- '%$TestSessionRequestFilter'
- '%$TestSessionRequestFilter'
Member:
extensions:
- TestSessionMemberExtension

View 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
);
}

View 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();
}
}