Merge pull request #3 from chillu/pulls/stub-code-writer

NEW Write PHP code for later inclusion
This commit is contained in:
Sean Harvey 2013-11-27 12:30:53 -08:00
commit c43a26a998
5 changed files with 165 additions and 16 deletions

8
_config/_config.yml Normal file
View File

@ -0,0 +1,8 @@
---
Name: requestprocessors
---
Injector:
RequestProcessor:
properties:
filters:
- '%$TestSessionRequestFilter'

View File

@ -88,6 +88,10 @@ class TestSessionController extends Controller {
} }
SapphireTest::empty_temp_db(); SapphireTest::empty_temp_db();
if(isset($_SESSION['_testsession_codeblocks'])) {
unset($_SESSION['_testsession_codeblocks']);
}
return "Cleared database and test state"; 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 // Database name is set in cookie (next request), ensure its available on this request already
global $databaseConfig; global $databaseConfig;
DB::connect(array_merge($databaseConfig, array('database' => $dbname))); DB::connect(array_merge($databaseConfig, array('database' => $dbname)));
unset($data['database']);
} }
// Fixtures // Fixtures
$fixtureFile = (isset($data['fixture'])) ? $data['fixture'] : null; $fixtureFile = (isset($data['fixture'])) ? $data['fixture'] : null;
if($fixtureFile) { if($fixtureFile) {
$this->loadFixtureIntoDb($fixtureFile); $this->loadFixtureIntoDb($fixtureFile);
unset($data['fixture']);
} }
// Mailer // Mailer
@ -175,6 +181,7 @@ class TestSessionController extends Controller {
// Configured through testsession/_config.php // Configured through testsession/_config.php
Session::set('testsession.mailer', $mailer); Session::set('testsession.mailer', $mailer);
unset($data['mailer']);
} }
// Date // Date
@ -190,6 +197,12 @@ class TestSessionController extends Controller {
// Configured through testsession/_config.php // Configured through testsession/_config.php
Session::set('testsession.date', $date); 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, 'Value' => $dbname,
)); ));
} }
if($fixtures = Session::get('testsession.fixtures')) { $sessionStates = Session::get('testsession');
if($sessionStates) foreach($sessionStates as $k => $v) {
$state[] = new ArrayData(array( $state[] = new ArrayData(array(
'Name' => 'Fixture', 'Name' => $k,
'Value' => implode(',', array_unique($fixtures)), 'Value' => var_export($v)
)); ));
}
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,
));
} }
return new ArrayList($state); return new ArrayList($state);

View File

@ -0,0 +1,73 @@
<?php
/**
* Writes PHP to a file which can be included in SilverStripe runs on existence.
* The generated file is included in page execution through {@link TestSessionRequestFilter}.
*/
class TestSessionStubCodeWriter {
/**
* @var boolean Add debug statements to the generated PHP about
* the generator's origin code location.
*/
protected $debug = false;
/**
* @var String Absolute path to a PHP file, essentially the "name" of the stub.
*/
protected $filePath;
public function __construct($filePath = null) {
$this->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 <?php)
* @param boolean $eval Sanity check on code.
*/
public function write($php, $eval = true) {
$trace = $this->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 .= "<?php\n// Generated by " . $trace[1]['class'] . " on " . date('Y-m-d H:i:s') . "\n\n";
} else {
$header .= "<?php\n";
}
}
// Add content
if($this->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;
}
}

View File

@ -0,0 +1,23 @@
<?php
/**
* 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
* '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() {
}
}

View File

@ -0,0 +1,43 @@
<?php
class TestSessionStubCodeWriterTest extends SapphireTest {
public function tearDown() {
parent::tearDown();
$file = TEMP_FOLDER . '/TestSessionStubCodeWriterTest-file.php';
if(file_exists($file)) unlink($file);
}
public function testWritesHeaderOnNewFile() {
$file = TEMP_FOLDER . '/TestSessionStubCodeWriterTest-file.php';
$writer = new TestSessionStubCodeWriter($file);
$writer->write('foo();', false);
$this->assertFileExists($file);
$this->assertEquals(
file_get_contents($writer->getFilePath()),
"<?php\nfoo();\n"
);
}
public function testWritesWithAppendOnExistingFile() {
$file = TEMP_FOLDER . '/TestSessionStubCodeWriterTest-file.php';
$writer = new TestSessionStubCodeWriter($file);
$writer->write('foo();', false);
$writer->write('bar();', false);
$this->assertFileExists($file);
$this->assertEquals(
file_get_contents($writer->getFilePath()),
"<?php\nfoo();\nbar();\n"
);
}
public function testReset() {
$file = TEMP_FOLDER . '/TestSessionStubCodeWriterTest-file.php';
$writer = new TestSessionStubCodeWriter($file);
$writer->write('foo();', false);
$this->assertFileExists($file);
$writer->reset();
$this->assertFileNotExists($file);
}
}