[TYPO3-dev] TypoScript template for unit testing
Elmar Hinz
elmar07 at googlemail.com
Tue Dec 4 13:00:59 CET 2007
Hello,
for unit tests you need invariant test setups. This example shows a way
to set a defined TS template, that does not depend on a page in the
pagetree. It simply loads the TS from css_styled_content.
It works with the unit test extension phpunit. An empty
$GLOBALS['TSFE']->tmpl has already been created by the parent class.
class tx_example_testcase extends tx_phpunit_testcase {
protected function setup() {
require_once(PATH_t3lib.'class.t3lib_tsparser.php');
$dir = 'EXT:css_styled_content/static/';
$dir = t3lib_div::getFileAbsFileName($dir);
$constants = join('', file($dir . 'constants.txt'));
$TSparserObject = t3lib_div::makeInstance('t3lib_tsparser');
$TSparserObject->parse($constants);
$constants = $TSparserObject->setup;
$this->makeFlatConstantKeys($constants);
$setup = join('', file($dir . 'setup.txt'));
$setup = $this->substituteConstants($setup);
$TSparserObject->parse($setup);
$GLOBALS['TSFE']->tmpl->setup = $TSparserObject->setup;
}
protected function makeFlatConstantKeys($constantArray) {
foreach($constantArray as $key => $value) {
$tempKey = $this->flatConstantKey;
$this->flatConstantKey.= $key;
if(is_array($value)) $this->makeflatConstantKeys($value);
else $this->flatConstants[$this->flatConstantKey] = $value;
$this->flatConstantKey = $tempKey;
}
}
protected function substituteConstants($line) {
return preg_replace_callback('/\{\$(.[^}]+)\}/m',
array($this, 'substituteConstantsCallBack'), $line);
}
protected function substituteConstantsCallBack($matches) {
return isset($this->flatConstants[$matches[1]]) ?
$this->flatConstants[$matches[1]] : $matches[0];
}
}
More information about the TYPO3-dev
mailing list