API Replace TestSessionDatabaseState with JSON persistence, store PHP session

This commit is contained in:
Ingo Schommer 2014-03-01 12:57:59 +13:00
parent 2f85acdd18
commit c247392da0
5 changed files with 32 additions and 64 deletions

View File

@ -6,9 +6,6 @@ Injector:
properties: properties:
filters: filters:
- '%$TestSessionRequestFilter' - '%$TestSessionRequestFilter'
Member:
extensions:
- TestSessionMemberExtension
--- ---
Name: testsession-setup Name: testsession-setup
--- ---

View File

@ -1,18 +0,0 @@
<?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

@ -38,6 +38,16 @@ class TestSessionEnvironment extends Object {
*/ */
private static $test_state_file; private static $test_state_file;
public function __construct() {
parent::__construct();
if($this->isRunningTests()) {
$this->state = json_decode(file_get_contents(Director::getAbsFile($this->config()->test_state_file)));
} else {
$this->state = new stdClass;
}
}
/** /**
* Tests for the existence of the file specified by $this->test_state_file * Tests for the existence of the file specified by $this->test_state_file
*/ */
@ -248,8 +258,11 @@ class TestSessionEnvironment extends Object {
/** /**
* Writes $this->state JSON object into the $this->config()->test_state_file file. * Writes $this->state JSON object into the $this->config()->test_state_file file.
*/ */
private function persistState() { public function persistState() {
file_put_contents(Director::getAbsFile($this->config()->test_state_file), json_encode($this->state)); file_put_contents(
Director::getAbsFile($this->config()->test_state_file),
json_encode($this->state, JSON_PRETTY_PRINT)
);
} }
private function removeStateFile() { private function removeStateFile() {

View File

@ -1,39 +0,0 @@
<?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;
$this->setCurrentMemberState();
}
public function onRegister() {
if(!SapphireTest::using_temp_db()) return;
$this->setCurrentMemberState();
}
public function memberLoggedOut() {
if(!SapphireTest::using_temp_db()) return;
$state = TestSessionDatabaseState::get()->filter('Key', 'CurrentMemberID')->removeAll();
}
protected function setCurrentMemberState() {
$state = TestSessionDatabaseState::get()->find('Key', 'CurrentMemberID');
if(!$state) {
$state = new TestSessionDatabaseState(array(
'Key' => 'CurrentMemberID'
));
}
$state->Value = $this->owner->ID;
$state->write();
}
}

View File

@ -4,10 +4,19 @@
*/ */
class TestSessionRequestFilter { class TestSessionRequestFilter {
public function preRequest($req, $session, $model) { /**
if(!Injector::inst()->get('TestSessionEnvironment')->isRunningTests()) return; * @var TestSessionEnvironment
*/
protected $testSessionEnvironment;
$testState = Injector::inst()->get('TestSessionEnvironment')->getState(); public function __construct() {
$this->testSessionEnvironment = Injector::inst()->get('testSessionEnvironment');
}
public function preRequest($req, $session, $model) {
if(!$this->testSessionEnvironment->isRunningTests()) return;
$testState = $this->testSessionEnvironment->getState();
// Date and time // Date and time
if(isset($testState->datetime)) { if(isset($testState->datetime)) {
@ -38,5 +47,11 @@ class TestSessionRequestFilter {
} }
public function postRequest() { public function postRequest() {
if(!$this->testSessionEnvironment->isRunningTests()) return;
// Store PHP session
$state = $this->testSessionEnvironment->getState();
$state->session = Session::get_all();
$this->testSessionEnvironment->persistState();
} }
} }