silverstripe-testsession/src/TestSessionRequestFilter.php

82 lines
2.6 KiB
PHP
Raw Normal View History

Refactor testsession module to use file-based session state storage. This is a major refactoring of the testsession module to use a persistent file storage instead of using $_SESSION storage. The primary reason for this is for out-of-band tests (e.g. simplifying Behat tests, and testing modules like silverstripe-resque (https://github.com/stojg/silverstripe-resque)). Testing the silverstripe-resque module without this is impossible as the PHP code running the job has been started and loaded into memory long before you started a testsession. By default, this will create a TESTS_RUNNING.json file in your webroot, which means that tests need to be run as a user who has permission to create files there. In practice, this means your webroot needs to be owned by your webserver user. The reason we store the file here is that it will show up as a changed file in version control, so it’s more prominent if developers can’t figure out why there are issues with database content. API CHANGES: - Add persistent file storage (using webroot/TESTS_RUNNING.json) as a base. - Update TestSessionController to use new TestSessionEnvironment class. - Moved extension points from TestSessionController to TestSessionEnvironment. - Moved loadFixtureIntoDb from TestSessionController to TestSessionEnvironment. - Moved setState from TestSessionController to TestSessionEnvironment. - Deprecated the use of TestSessionController::setState() FIXES: - Fixes TestSessionRequestFilter to use new TestSessionEnvironment instead of $_SESSION. MINOR: - Renamed TestSesssionRequestFilter.php to fix spelling error (three ’S’s) - Class did not need renaming, just the file itself.
2014-02-04 23:38:22 +01:00
<?php
2017-04-21 01:58:27 +02:00
namespace SilverStripe\TestSession;
2017-06-08 08:01:46 +02:00
use SilverStripe\Control\Director;
2017-04-21 01:58:27 +02:00
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\Email\Mailer;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
2017-06-08 08:01:46 +02:00
use SilverStripe\Control\RequestFilter;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\FieldType\DBDatetime;
Refactor testsession module to use file-based session state storage. This is a major refactoring of the testsession module to use a persistent file storage instead of using $_SESSION storage. The primary reason for this is for out-of-band tests (e.g. simplifying Behat tests, and testing modules like silverstripe-resque (https://github.com/stojg/silverstripe-resque)). Testing the silverstripe-resque module without this is impossible as the PHP code running the job has been started and loaded into memory long before you started a testsession. By default, this will create a TESTS_RUNNING.json file in your webroot, which means that tests need to be run as a user who has permission to create files there. In practice, this means your webroot needs to be owned by your webserver user. The reason we store the file here is that it will show up as a changed file in version control, so it’s more prominent if developers can’t figure out why there are issues with database content. API CHANGES: - Add persistent file storage (using webroot/TESTS_RUNNING.json) as a base. - Update TestSessionController to use new TestSessionEnvironment class. - Moved extension points from TestSessionController to TestSessionEnvironment. - Moved loadFixtureIntoDb from TestSessionController to TestSessionEnvironment. - Moved setState from TestSessionController to TestSessionEnvironment. - Deprecated the use of TestSessionController::setState() FIXES: - Fixes TestSessionRequestFilter to use new TestSessionEnvironment instead of $_SESSION. MINOR: - Renamed TestSesssionRequestFilter.php to fix spelling error (three ’S’s) - Class did not need renaming, just the file itself.
2014-02-04 23:38:22 +01:00
/**
* Sets state previously initialized through {@link TestSessionController}.
*/
2015-12-17 19:17:16 +01:00
class TestSessionRequestFilter implements RequestFilter
{
/**
* @var TestSessionEnvironment
*/
protected $testSessionEnvironment;
public function __construct()
{
2017-04-21 01:58:27 +02:00
$this->testSessionEnvironment = TestSessionEnvironment::singleton();
2015-12-17 19:17:16 +01:00
}
2017-06-08 08:01:46 +02:00
public function preRequest(HTTPRequest $request)
2015-12-17 19:17:16 +01:00
{
2017-04-21 01:58:27 +02:00
$isRunningTests = $this->testSessionEnvironment->isRunningTests();
2017-06-08 08:01:46 +02:00
$this->testSessionEnvironment->init($request);
2017-04-21 01:58:27 +02:00
if (!$isRunningTests) {
2015-12-17 19:17:16 +01:00
return;
}
$testState = $this->testSessionEnvironment->getState();
// Date and time
if (isset($testState->datetime)) {
DBDatetime::set_mock_now($testState->datetime);
2015-12-17 19:17:16 +01:00
}
// Register mailer
if (isset($testState->mailer)) {
$mailer = $testState->mailer;
2017-04-21 01:58:27 +02:00
Injector::inst()->registerService(new $mailer(), Mailer::class);
Email::config()->set("send_all_emails_to", null);
2015-12-17 19:17:16 +01:00
}
// Allows inclusion of a PHP file, usually with procedural commands
// to set up required test state. The file can be generated
// through {@link TestSessionStubCodeWriter}, and the session state
// set through {@link TestSessionController->set()} and the
// 'testsession.stubfile' state parameter.
if (isset($testState->stubfile)) {
$file = $testState->stubfile;
if (!Director::isLive() && $file && file_exists($file)) {
// Connect to the database so the included code can interact with it
2017-06-22 05:15:17 +02:00
$databaseConfig = DB::getConfig();
2015-12-17 19:17:16 +01:00
if ($databaseConfig) {
DB::connect($databaseConfig);
}
include_once($file);
}
}
}
2017-06-08 08:01:46 +02:00
public function postRequest(HTTPRequest $request, HTTPResponse $response)
2015-12-17 19:17:16 +01:00
{
if (!$this->testSessionEnvironment->isRunningTests()) {
return;
}
// Store PHP session
$state = $this->testSessionEnvironment->getState();
2017-06-12 04:59:20 +02:00
$state->session = $request->getSession()->getAll();
2015-12-17 19:17:16 +01:00
$this->testSessionEnvironment->applyState($state);
}
}