testSessionEnvironment = TestSessionEnvironment::singleton(); } public function process(HTTPRequest $request, callable $delegate) { // Init environment $this->testSessionEnvironment->init($request); // If not running tests, just pass through $isRunningTests = $this->testSessionEnvironment->isRunningTests(); if (!$isRunningTests) { return $delegate($request); } // Load test state $this->loadTestState($request); $this->incrementModelState(); // Call with safe teardown try { return $delegate($request); } finally { $this->restoreTestState($request); $this->decrementModelState(); } } protected function incrementModelState() { $schema = DataObject::getSchema(); $update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(TestSessionState::class))) ->addWhere(['ID' => 1]) ->addAssignments([ 'PendingRequests' => [ '"PendingRequests" + 1' => [] ] ]); $update->execute(); } protected function decrementModelState() { $schema = DataObject::getSchema(); $update = SQLUpdate::create(sprintf('"%s"', $schema->tableName(TestSessionState::class))) ->addWhere(['ID' => 1]) ->addAssignments([ '"PendingRequests"' => [ '"PendingRequests" - 1' => [] ], '"LastResponseTimestamp"' => microtime(true) * 10000 ]); $update->execute(); } /** * Load test state from environment into "real" environment * * @param HTTPRequest $request */ protected function loadTestState(HTTPRequest $request) { $testState = $this->testSessionEnvironment->getState(); // Date and time if (isset($testState->datetime)) { DBDatetime::set_mock_now($testState->datetime); } // Register mailer if (isset($testState->mailer)) { $mailer = $testState->mailer; Injector::inst()->registerService(new $mailer(), Mailer::class); Email::config()->set("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 $databaseConfig = DB::getConfig(); if ($databaseConfig) { DB::connect($databaseConfig); } include_once($file); } } } protected function restoreTestState(HTTPRequest $request) { // Store PHP session $state = $this->testSessionEnvironment->getState(); $state->session = $request->getSession()->getAll(); $this->testSessionEnvironment->applyState($state); } }