FIX: Added proper state-restoration code to the ThemeContext:

This commit is contained in:
Sam Minnee 2014-02-14 12:31:37 +13:00
parent ab4b72d486
commit c0e6bf7132

View File

@ -21,6 +21,9 @@ require_once 'PHPUnit/Framework/Assert/Functions.php';
*/ */
class ThemeContext extends BehatContext { class ThemeContext extends BehatContext {
protected $restoreFiles = array();
protected $restoreDirectories = array();
/** /**
* Create a test theme * Create a test theme
* *
@ -29,8 +32,9 @@ class ThemeContext extends BehatContext {
public function stepCreateTheme($theme) { public function stepCreateTheme($theme) {
if(!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) throw new \InvalidArgumentException("Bad theme '$theme'"); if(!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) throw new \InvalidArgumentException("Bad theme '$theme'");
if(!file_exists(BASE_PATH . '/themes/' . $theme)) mkdir(BASE_PATH . '/themes/' . $theme); $this->requireDir(BASE_PATH . '/themes');
if(!file_exists(BASE_PATH . '/themes/' . $theme . '/templates')) mkdir(BASE_PATH . '/themes/' . $theme . '/templates'); $this->requireDir(BASE_PATH . '/themes/' . $theme);
$this->requireDir(BASE_PATH . '/themes/' . $theme . '/templates');
} }
/** /**
@ -42,6 +46,64 @@ class ThemeContext extends BehatContext {
if(!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) throw new \InvalidArgumentException("Bad theme '$theme'"); if(!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) throw new \InvalidArgumentException("Bad theme '$theme'");
if(!preg_match('/^(Layout\/)?[0-9a-zA-Z_-]+\.ss$/', $template)) throw new \InvalidArgumentException("Bad template '$template'"); if(!preg_match('/^(Layout\/)?[0-9a-zA-Z_-]+\.ss$/', $template)) throw new \InvalidArgumentException("Bad template '$template'");
file_put_contents(BASE_PATH . '/themes/' . $theme . '/templates/' . $template, $content); $this->stepCreateTheme($theme);
$this->requireFile(BASE_PATH . '/themes/' . $theme . '/templates/' . $template, $content);
}
protected function requireFile($filename, $content) {
// Already exists
if(file_exists($filename)) {
// If the content is different, remember old content for restoration
$origContent = file_get_contents($filename);
if($origContent != $content) {
file_put_contents($filename, $content);
$this->restoreFiles[$filename] = $origContent;
}
// Doesn't exist, mark it for deletion after test
} else {
file_put_contents($filename, $content);
$this->restoreFiles[$filename] = null;
}
}
protected function requireDir($dirname) {
// Directory doesn't exist, create it and mark it for deletion
if(!file_exists($dirname)) {
mkdir($dirname);
$this->restoreDirectories[] = $dirname;
}
}
/**
* Clean up any theme manipulation
*
* @AfterScenario
*/
public function cleanThemesAfterScenario() {
// Restore any created/modified files.
// - If modified, revert then to original contnet
// - If created, delete them
if($this->restoreFiles) {
foreach($this->restoreFiles as $file => $origContent) {
if($origContent === null) {
unlink($file);
} else {
file_put_contents($file, $origContent);
}
}
$this->restoreFiles = array();
}
// Restore any created directories: that is, delete them
if($this->restoreDirectories) {
// Flip the order so that nested direcotires are unlinked() first
$this->restoreDirectories = array_reverse($this->restoreDirectories);
foreach($this->restoreDirectories as $dir) {
rmdir($dir);
}
$this->restoreDirectories = array();
}
} }
} }