Skip to content

Commit

Permalink
Merge pull request #50 from open-sausages/pulls/4.0/update-middleware
Browse files Browse the repository at this point in the history
API Replace request filter with middleware
  • Loading branch information
Damian Mooyman authored Jun 27, 2017
2 parents 81130c7 + 3e08c1a commit 62cd630
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
10 changes: 7 additions & 3 deletions _config/processors.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
Name: testsessionprocessors
After:
- requestprocessors
Before:
- coresecurity
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Control\RequestProcessor:
SilverStripe\Control\Director:
properties:
filters:
- '%$SilverStripe\TestSession\TestSessionRequestFilter'
Middlewares:
TestSessionHTTPMiddleware: %$SilverStripe\TestSession\TestSessionHTTPMiddleware
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
"dev-master": "2.0.x-dev"
}
},
"scripts": {
Expand Down
5 changes: 2 additions & 3 deletions src/TestSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace SilverStripe\TestSession;

use FilesystemIterator;
use LogicException;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\DropdownField;
Expand All @@ -24,8 +25,6 @@
use SilverStripe\Security\Security;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use LogicException;
use FilesystemIterator;

/**
* Requires PHP's mycrypt extension in order to set the database name as an encrypted cookie.
Expand Down
6 changes: 3 additions & 3 deletions src/TestSessionEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function __construct($id = null)
public function init(HTTPRequest $request)
{
if (!$this->id) {
$request->getSession()->init();
$request->getSession()->init($request);
// $_SESSION != Session::get() in some execution paths, suspect Controller->pushCurrent()
// as part of the issue, easiest resolution is to use session directly for now
$this->id = $request->getSession()->get('TestSessionId');
Expand All @@ -98,9 +98,9 @@ public function init(HTTPRequest $request)
public function getFilePath()
{
if ($this->id) {
$path = Director::getAbsFile(sprintf($this->config()->test_state_id_file, $this->id));
$path = Director::getAbsFile(sprintf($this->config()->get('test_state_id_file'), $this->id));
} else {
$path = Director::getAbsFile($this->config()->test_state_file);
$path = Director::getAbsFile($this->config()->get('test_state_file'));
}

return $path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Email\Mailer;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\RequestFilter;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime;

/**
* Sets state previously initialized through {@link TestSessionController}.
*/
class TestSessionRequestFilter implements RequestFilter
class TestSessionHTTPMiddleware implements HTTPMiddleware
{
/**
* @var TestSessionEnvironment
Expand All @@ -27,14 +26,35 @@ public function __construct()
$this->testSessionEnvironment = TestSessionEnvironment::singleton();
}

public function preRequest(HTTPRequest $request)
public function process(HTTPRequest $request, callable $delegate)
{
$isRunningTests = $this->testSessionEnvironment->isRunningTests();
// Init environment
$this->testSessionEnvironment->init($request);

// If not running tests, just pass through
$isRunningTests = $this->testSessionEnvironment->isRunningTests();
if (!$isRunningTests) {
return;
return $delegate($request);
}

// Load test state
$this->loadTestState($request);

// Call with safe teardown
try {
return $delegate($request);
} finally {
$this->restoreTestState($request);
}
}

/**
* Load test state from environment into "real" environment
*
* @param HTTPRequest $request
*/
protected function loadTestState(HTTPRequest $request)
{
$testState = $this->testSessionEnvironment->getState();

// Date and time
Expand Down Expand Up @@ -67,12 +87,8 @@ public function preRequest(HTTPRequest $request)
}
}

public function postRequest(HTTPRequest $request, HTTPResponse $response)
protected function restoreTestState(HTTPRequest $request)
{
if (!$this->testSessionEnvironment->isRunningTests()) {
return;
}

// Store PHP session
$state = $this->testSessionEnvironment->getState();
$state->session = $request->getSession()->getAll();
Expand Down

0 comments on commit 62cd630

Please sign in to comment.