2014-02-04 23:38:22 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Sets state previously initialized through {@link TestSessionController}.
|
|
|
|
*/
|
|
|
|
class TestSessionRequestFilter {
|
2014-03-01 00:57:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var TestSessionEnvironment
|
|
|
|
*/
|
|
|
|
protected $testSessionEnvironment;
|
|
|
|
|
|
|
|
public function __construct() {
|
2014-03-03 04:19:38 +01:00
|
|
|
$this->testSessionEnvironment = Injector::inst()->get('TestSessionEnvironment');
|
2014-03-01 00:57:59 +01:00
|
|
|
}
|
2014-02-04 23:38:22 +01:00
|
|
|
|
|
|
|
public function preRequest($req, $session, $model) {
|
2014-03-01 00:57:59 +01:00
|
|
|
if(!$this->testSessionEnvironment->isRunningTests()) return;
|
2014-02-04 23:38:22 +01:00
|
|
|
|
2014-03-01 00:57:59 +01:00
|
|
|
$testState = $this->testSessionEnvironment->getState();
|
2014-02-04 23:38:22 +01:00
|
|
|
|
|
|
|
// Date and time
|
|
|
|
if(isset($testState->datetime)) {
|
|
|
|
SS_Datetime::set_mock_now($testState->datetime);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register mailer
|
|
|
|
if(isset($testState->mailer)) {
|
|
|
|
$mailer = $testState->mailer;
|
|
|
|
Email::set_mailer(new $mailer());
|
|
|
|
Config::inst()->update("Email","send_all_emails_to", null);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
global $databaseConfig;
|
|
|
|
if ($databaseConfig) DB::connect($databaseConfig);
|
|
|
|
include_once($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postRequest() {
|
2014-03-01 00:57:59 +01:00
|
|
|
if(!$this->testSessionEnvironment->isRunningTests()) return;
|
|
|
|
|
|
|
|
// Store PHP session
|
|
|
|
$state = $this->testSessionEnvironment->getState();
|
|
|
|
$state->session = Session::get_all();
|
2014-03-01 06:28:34 +01:00
|
|
|
$this->testSessionEnvironment->applyState($state);
|
2014-02-04 23:38:22 +01:00
|
|
|
}
|
|
|
|
}
|