mirror of
https://github.com/silverstripe/silverstripe-testsession
synced 2024-10-22 14:06:00 +02:00
API Replace TestSessionDatabaseState with JSON persistence, store PHP session
This commit is contained in:
parent
2f85acdd18
commit
c247392da0
@ -6,9 +6,6 @@ Injector:
|
|||||||
properties:
|
properties:
|
||||||
filters:
|
filters:
|
||||||
- '%$TestSessionRequestFilter'
|
- '%$TestSessionRequestFilter'
|
||||||
Member:
|
|
||||||
extensions:
|
|
||||||
- TestSessionMemberExtension
|
|
||||||
---
|
---
|
||||||
Name: testsession-setup
|
Name: testsession-setup
|
||||||
---
|
---
|
||||||
|
@ -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
|
|
||||||
);
|
|
||||||
}
|
|
@ -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() {
|
||||||
|
@ -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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -3,11 +3,20 @@
|
|||||||
* Sets state previously initialized through {@link TestSessionController}.
|
* Sets state previously initialized through {@link TestSessionController}.
|
||||||
*/
|
*/
|
||||||
class TestSessionRequestFilter {
|
class TestSessionRequestFilter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var TestSessionEnvironment
|
||||||
|
*/
|
||||||
|
protected $testSessionEnvironment;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->testSessionEnvironment = Injector::inst()->get('testSessionEnvironment');
|
||||||
|
}
|
||||||
|
|
||||||
public function preRequest($req, $session, $model) {
|
public function preRequest($req, $session, $model) {
|
||||||
if(!Injector::inst()->get('TestSessionEnvironment')->isRunningTests()) return;
|
if(!$this->testSessionEnvironment->isRunningTests()) return;
|
||||||
|
|
||||||
$testState = Injector::inst()->get('TestSessionEnvironment')->getState();
|
$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();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user