mirror of
https://github.com/smarty-php/smarty.git
synced 2026-08-03 20:14:31 +02:00
ff2ef3b0cb
* Redirect test temp dirs to system temp directory. Fixes #1178 Move all test-generated output (compiled templates, cache files, and temporary template sources) from per-test-directory folders inside the working tree to a parallel structure under sys_get_temp_dir()/smarty-tests/. This removes 215 boilerplate .gitignore files from the repo and ensures running the test suite leaves zero uncommitted files in the working tree. All 2296 tests continue to pass with identical behavior. * Isolate each test class in a unique temp directory getTempDir() now appends a per-class uniqid token to the temp path, so concurrent or sequential test runs never share compiled/cached output. The token is generated lazily on first use and reset in tearDownAfterClass(), giving every test class a fresh isolated directory. As a result, the Bootstrap.php pre-run cleanup of smarty-tests/ is no longer needed for correctness (stale paths are unreachable) and was harmful to concurrent runs, so it has been removed. * Remove individualFolders dead code and spurious assertTrue from cleanDirs() - Remove the never-active individualFolders code path from setUpSmarty() (the constant was always true, making the branch unreachable) - Remove define('individualFolders') from Config.php and the constructor - Remove $this->assertTrue(true) from cleanDirs(): it existed solely to make testInit() count as a passing test; now that cleanDirs() is called from setUpSmarty() and from test methods directly, the assertion was spuriously inflating assertion counts - Add tests/**/templates_c/, cache/, templates_tmp/ to .gitignore to prevent stale test output from appearing as untracked files * Clean up each test class's unique temp dir in tearDownAfterClass() Add a private static removeDir() helper and call it from tearDownAfterClass() to recursively delete the per-class unique temp directory after each test class finishes. Cleanup failures are silently ignored (@ suppression) so they never cause test failures. Set KEEP_SMARTY_TEST_ARTIFACTS=1 in the environment to skip cleanup and keep the artifacts on disk for debugging. * cleanup of unused template files, non-shared files stored in __shared folder, no longer required calls to add template folders et cetera * fixed the unit tests * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove useless resetting of static properties in tearDownAfterClass * changed an incorrect doc and formatted some code. * add changelog --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
178 lines
5.6 KiB
PHP
178 lines
5.6 KiB
PHP
<?php
|
|
/**
|
|
* Smarty PHPunit tests deault plugin handler
|
|
*
|
|
|
|
* @author Uwe Tews
|
|
*/
|
|
|
|
/**
|
|
* class for plugin handler tests
|
|
*
|
|
*
|
|
*
|
|
*
|
|
*/
|
|
class DefaultPluginHandlerTest extends PHPUnit_Smarty
|
|
{
|
|
public function setUp(): void
|
|
{
|
|
$this->setUpSmarty(__DIR__);
|
|
$this->smarty->setForceCompile(true);
|
|
$this->smarty->disableSecurity();
|
|
$this->smarty->registerDefaultPluginHandler('my_plugin_handler');
|
|
}
|
|
|
|
|
|
public function testDefaultFunctionScript()
|
|
{
|
|
$this->assertEquals("scriptfunction foo bar", $this->smarty->fetch('test_default_function_script.tpl'));
|
|
}
|
|
|
|
public function testDefaultFunctionScriptNotCachable1()
|
|
{
|
|
$this->smarty->assign('foo', 'foo');
|
|
$this->smarty->caching = 1;
|
|
$this->assertEquals("scriptfunction foo", $this->smarty->fetch('test_default_function_script_notcachable.tpl'));
|
|
}
|
|
|
|
public function testDefaultFunctionScriptNotCachable2()
|
|
{
|
|
$this->smarty->assign('foo', 'bar');
|
|
$this->smarty->caching = 1;
|
|
$this->assertEquals("scriptfunction bar", $this->smarty->fetch('test_default_function_script_notcachable.tpl'));
|
|
}
|
|
|
|
public function testDefaultFunctionLocal()
|
|
{
|
|
$this->assertEquals("localfunction foo bar", $this->smarty->fetch('test_default_function_local.tpl'));
|
|
}
|
|
|
|
public function testDefaultCompilerFunctionScript()
|
|
{
|
|
$this->assertEquals("echo 'scriptcompilerfunction '.'foo bar';", $this->smarty->fetch('test_default_compiler_function_script.tpl'));
|
|
}
|
|
public function testDefaultCompilerObject()
|
|
{
|
|
$this->assertEquals('Public World', $this->smarty->fetch('test_default_compiler_object.tpl'));
|
|
}
|
|
|
|
public function testDefaultBlockScript()
|
|
{
|
|
$this->assertEquals("scriptblock foo bar", $this->smarty->fetch('test_default_block_script.tpl'));
|
|
}
|
|
|
|
public function testDefaultModifierScript()
|
|
{
|
|
$this->smarty->assign('foo', 'bar');
|
|
$this->assertEquals("scriptmodifier default bar", $this->smarty->fetch('test_default_modifier_script.tpl'));
|
|
}
|
|
|
|
public function testDefaultModifier()
|
|
{
|
|
$this->smarty->assign('foo', 'bar');
|
|
$this->assertEquals("localmodifier bar", $this->smarty->fetch('test_default_modifier.tpl'));
|
|
}
|
|
|
|
public function testDefaultModifierStaticClassMethodCaching1()
|
|
{
|
|
$this->smarty->assign('foo', 'bar');
|
|
$this->smarty->caching = 1;
|
|
$this->assertEquals("staticmodifier bar", $this->smarty->fetch('test_default_static_modifier.tpl'));
|
|
}
|
|
|
|
public function testDefaultModifierStaticClassMethodCaching2()
|
|
{
|
|
$this->smarty->assign('foo', 'bar');
|
|
$this->smarty->caching = 1;
|
|
$this->assertEquals("staticmodifier bar", $this->smarty->fetch('test_default_static_modifier.tpl'));
|
|
}
|
|
}
|
|
|
|
function my_plugin_handler($tag, $type, $template, &$callback, &$script, &$cachable)
|
|
{
|
|
switch ($type) {
|
|
case \Smarty\Smarty::PLUGIN_FUNCTION:
|
|
switch ($tag) {
|
|
case 'scriptfunction':
|
|
$script = './scripts/script_function_tag.php';
|
|
$callback = 'default_script_function_tag';
|
|
|
|
return true;
|
|
case 'scriptfunctionnotcachable':
|
|
$script = './scripts/script_function_tag.php';
|
|
$callback = 'default_script_function_tag';
|
|
$cachable = false;
|
|
|
|
return true;
|
|
case 'localfunction':
|
|
$callback = 'default_local_function_tag';
|
|
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
case \Smarty\Smarty::PLUGIN_COMPILER:
|
|
switch ($tag) {
|
|
case 'scriptcompilerfunction':
|
|
$script = './scripts/script_compiler_function_tag.php';
|
|
$callback = 'default_script_compiler_function_tag';
|
|
return true;
|
|
case 'compilerobject':
|
|
$callback = array(new CompilerDefaultPluginClass, 'compile');
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
case \Smarty\Smarty::PLUGIN_BLOCK:
|
|
switch ($tag) {
|
|
case 'scriptblock':
|
|
$script = './scripts/script_block_tag.php';
|
|
$callback = 'default_script_block_tag';
|
|
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
case \Smarty\Smarty::PLUGIN_MODIFIER:
|
|
switch ($tag) {
|
|
case 'scriptmodifier':
|
|
$script = './scripts/script_modifier.php';
|
|
$callback = 'default_script_modifier';
|
|
|
|
return true;
|
|
case 'mydefaultmodifier':
|
|
$callback = 'default_local_modifier';
|
|
|
|
return true;
|
|
case 'mydefaultstaticmodifier':
|
|
$script = './scripts/script_default_static_modifier.php';
|
|
$callback = array('DefModifier', 'default_static_modifier');
|
|
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function default_local_function_tag($params, $template)
|
|
{
|
|
return 'localfunction ' . $params['value'];
|
|
}
|
|
|
|
function default_local_modifier($input)
|
|
{
|
|
return 'localmodifier ' . $input;
|
|
}
|
|
class CompilerDefaultPluginClass
|
|
{
|
|
static function statCompile ($params, $compiler) {
|
|
return '<?php echo \'Static World\';?>';
|
|
}
|
|
public function compile ($params, $compiler) {
|
|
return '<?php echo \'Public World\';?>';
|
|
}
|
|
} |