diff --git a/_config/_config.yml b/_config/_config.yml new file mode 100644 index 0000000..425ffd9 --- /dev/null +++ b/_config/_config.yml @@ -0,0 +1,8 @@ +--- +Name: requestprocessors +--- +Injector: + RequestProcessor: + properties: + filters: + - '%$TestSessionRequestFilter' \ No newline at end of file diff --git a/code/TestSessionController.php b/code/TestSessionController.php index bd5f96f..f3da08c 100644 --- a/code/TestSessionController.php +++ b/code/TestSessionController.php @@ -88,6 +88,10 @@ class TestSessionController extends Controller { } SapphireTest::empty_temp_db(); + + if(isset($_SESSION['_testsession_codeblocks'])) { + unset($_SESSION['_testsession_codeblocks']); + } return "Cleared database and test state"; } @@ -155,12 +159,14 @@ class TestSessionController extends Controller { // Database name is set in cookie (next request), ensure its available on this request already global $databaseConfig; DB::connect(array_merge($databaseConfig, array('database' => $dbname))); + unset($data['database']); } // Fixtures $fixtureFile = (isset($data['fixture'])) ? $data['fixture'] : null; if($fixtureFile) { $this->loadFixtureIntoDb($fixtureFile); + unset($data['fixture']); } // Mailer @@ -175,6 +181,7 @@ class TestSessionController extends Controller { // Configured through testsession/_config.php Session::set('testsession.mailer', $mailer); + unset($data['mailer']); } // Date @@ -190,6 +197,12 @@ class TestSessionController extends Controller { // Configured through testsession/_config.php Session::set('testsession.date', $date); + unset($data['date']); + } + + // Set all other keys without special handling + if($data) foreach($data as $k => $v) { + Session::set('testsession.' . $k, $v); } } @@ -204,23 +217,12 @@ class TestSessionController extends Controller { 'Value' => $dbname, )); } - if($fixtures = Session::get('testsession.fixtures')) { + $sessionStates = Session::get('testsession'); + if($sessionStates) foreach($sessionStates as $k => $v) { $state[] = new ArrayData(array( - 'Name' => 'Fixture', - 'Value' => implode(',', array_unique($fixtures)), - )); - } - if($mailer = Session::get('testsession.mailer')) { - $state[] = new ArrayData(array( - 'Name' => 'Mailer Class', - 'Value' => $mailer, - )); - } - if($date = Session::get('testsession.date')) { - $state[] = new ArrayData(array( - 'Name' => 'Date', - 'Value' => $date, - )); + 'Name' => $k, + 'Value' => var_export($v) + )); } return new ArrayList($state); diff --git a/code/TestSessionStubCodeWriter.php b/code/TestSessionStubCodeWriter.php new file mode 100644 index 0000000..823c6be --- /dev/null +++ b/code/TestSessionStubCodeWriter.php @@ -0,0 +1,73 @@ +filePath = $filePath ? $filePath : BASE_PATH . '/testSessionStubCode.php'; + } + + /** + * Writes arbitrary PHP code to {@link $filePath} for later inclusion. + * Creates the file if it doesn't exist. + * Adds debug information about the origin of this code if {@link $debug} is set. + * + * @param String $php Block of PHP code (without preceding debug ? debug_backtrace() : null; + $path = $this->getFilePath(); + $header = ''; + + // Create file incl. header if it doesn't exist + if(!file_exists($this->getFilePath())) { + touch($this->getFilePath()); + if($this->debug) { + $header .= "debug) { + $header .= "// Added by " . $trace[1]['class'] . '::' . $trace[1]['function'] . "\n"; + } + file_put_contents($path, $header . $php . "\n", FILE_APPEND); + } + + public function reset() { + if(file_exists($this->getFilePath())) { + unlink($this->getFilePath()); + } + } + + public function getFilePath() { + return $this->filePath; + } + + public function getDebug() { + return $this->debug; + } + + public function setDebug($debug) { + $this->debug = $debug; + + return $this; + } + +} \ No newline at end of file diff --git a/code/TestSesssionRequestFilter.php b/code/TestSesssionRequestFilter.php new file mode 100644 index 0000000..f69f3be --- /dev/null +++ b/code/TestSesssionRequestFilter.php @@ -0,0 +1,23 @@ +set()} and the + * 'testsessio.stubfile' state parameter. + */ +class TestSessionRequestFilter { + + public function preRequest($req, $session, $model) { + $file = $session->inst_get('testsession.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() { + } +} \ No newline at end of file diff --git a/tests/unit/TestSessionStubCodeWriterTest.php b/tests/unit/TestSessionStubCodeWriterTest.php new file mode 100644 index 0000000..eee52a1 --- /dev/null +++ b/tests/unit/TestSessionStubCodeWriterTest.php @@ -0,0 +1,43 @@ +write('foo();', false); + $this->assertFileExists($file); + $this->assertEquals( + file_get_contents($writer->getFilePath()), + "write('foo();', false); + $writer->write('bar();', false); + $this->assertFileExists($file); + $this->assertEquals( + file_get_contents($writer->getFilePath()), + "write('foo();', false); + $this->assertFileExists($file); + $writer->reset(); + $this->assertFileNotExists($file); + } + +} \ No newline at end of file