Update scope tests

This commit is contained in:
uwetews
2016-03-09 00:29:15 +01:00
parent d717439a6b
commit 531df47a97
18 changed files with 372 additions and 370 deletions

View File

@@ -62,59 +62,4 @@ class CompileAppendTest extends PHPUnit_Smarty
array('{$foo[]=2}{$foo|var_export:true}', 'array(0=>2,)', '', $i ++), array('{$foo[]=2}{$foo|var_export:true}', 'array(0=>2,)', '', $i ++),
); );
} }
/**
* Test scope
*
* @run InSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestScope
*/
public function testScope($code, $useSmarty, $result, $testName, $testNumber)
{
$file = "testScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code . '{checkvar var=foo}');
$this->smarty->assignGlobal('file', $file);
$this->smarty->assign('foo', 'smarty');
$this->smarty->assignGlobal('foo', 'global');
$data1 = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data = $this->smarty->createData($data1);
$data1->assign('foo', 'data1');
$data->assign('foo', 'data');
$this->assertEquals($this->strip($result), $this->strip($this->smarty->fetch('scope_tag.tpl', $data)), "test - {$code} - {$testName}");
}
/*
* Data provider für testscope
*/
public function dataTestScope()
{
$i = 0;
/*
* Code
* use Smarty object
* result
* test name
*/
return array(
array(
'{$foo[] = \'newvar\' scope=tpl_root}', true,
'#testScope_0.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#data:$foo=\'data\'#data:$foo=\'data1\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo[] = \'newvar\' scope=tpl_root bubble_up}', true,
'#testScope_1.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#data:$foo=\'data\'#data:$foo=\'data1\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{append var=foo value=\'newvar\' scope=tpl_root}', true,
'#testScope_2.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#data:$foo=\'data\'#data:$foo=\'data1\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{append var=foo value=\'newvar\' scope=tpl_root bubble_up}', true,
'#testScope_3.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#data:$foo=\'data\'#data:$foo=\'data1\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
),
);
}
} }

View File

@@ -83,172 +83,4 @@ class CompileAssignTest extends PHPUnit_Smarty
), ),
); );
} }
/**
* Test scope
*
* @not runInSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestScope
*/
public function testScope($code, $useSmarty, $result, $testName, $testNumber)
{
$file = "testScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code . '{checkvar var=foo}');
$this->smarty->assignGlobal('file', $file);
$this->smarty->assign('foo', 'smarty');
$this->smarty->assignGlobal('foo', 'global');
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data->assign('foo', 'data');
$this->assertEquals($this->strip('#' . $file . $result), $this->strip($this->smarty->fetch('scope_tag.tpl', $data)), "test - {$code} - {$testName}");
}
/*
* Data provider für testscope
*/
public function dataTestScope()
{
$i = 1;
/*
* Code
* use Smarty object
* result
* test name
*/
return array(
array(
'{$foo = \'newvar\'}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\'}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=local}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=local bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\' bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\' scope=local}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\' scope=local bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=parent}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=parent bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\' scope=parent}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\' scope=parent bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=tpl_root}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=tpl_root bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=global}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=global bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=root}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=root bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=root}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'no smarty', $i ++,
), array(
'{$foo = \'newvar\' scope=root bubble_up}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'no smarty', $i ++,
), array(
'{$foo = \'newvar\' scope=smarty}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=smarty bubble_up}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'#global:$foo=\'global\'',
'no smarty', $i ++,
),
);
}
/**
* Test scope nocache
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestScopeNocache
*/
public function testScopeNocache($var, $file, $result)
{
$this->smarty->setCaching(true);
$this->smarty->assign('bar', $var, true);
$this->smarty->assign('buh', $var);
$this->smarty->assign('foo', 'smarty');
$this->smarty->assignGlobal('foo', 'global');
$this->assertEquals($this->strip($result), $this->strip($this->smarty->fetch($file)), "test - {$file} {$var}");
}
/*
* Data provider für testscopenocache
*/
public function dataTestScopeNocache()
{
/*
* variable value
* result
*/
return array(
array(
'b1', 'test_scope_assignbar.tpl',
'#test_scope_assignbar.tpl:$foo=\'b1\'#Smarty:$foo=\'smarty\'#global:$foo=\'b1\'',
), array(
'b2', 'test_scope_assignbar.tpl',
'#test_scope_assignbar.tpl:$foo=\'b2\'#Smarty:$foo=\'smarty\'#global:$foo=\'b2\'',
), array(
'b1', 'test_scope_assignnocache.tpl',
'#test_scope_assignnocache.tpl:$foo=\'b1\'#Smarty:$foo=\'smarty\'#global:$foo=\'b1\'',
), array(
'b2', 'test_scope_assignnocache.tpl',
'#test_scope_assignnocache.tpl:$foo=\'b2\'#Smarty:$foo=\'smarty\'#global:$foo=\'b2\'',
),
);
}
} }

View File

@@ -135,95 +135,4 @@ class CompileConfigLoadTest extends PHPUnit_Smarty
{ {
$this->smarty->fetch('eval:{config_load file=\'test_error.conf\'}'); $this->smarty->fetch('eval:{config_load file=\'test_error.conf\'}');
} }
/**
* Test scope
*
* @not runInSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestScope
*/
public function testScope($code, $useSmarty, $result, $testName, $testNumber)
{
$file = "testScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code . '{checkconfigvar var=foo}');
$this->smarty->assignGlobal('file', $file);
$this->smarty->configLoad('smarty.conf');
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data->configLoad('data.conf');
$this->assertEquals($this->strip('#' . $file . $result), $this->strip($this->smarty->fetch('scope_tag.tpl', $data)), "test - {$code} - {$testName}");
}
/*
* Data provider für testscope
*/
public function dataTestScope()
{
$i = 1;
/*
* Code
* use Smarty object
* result
* test name
*/
return array(
array(
'{config_load \'template.conf\'}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=local}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=local bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=parent}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=parent bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=tpl_root}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=tpl_root bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=root}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=root bubble_up}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=root}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'',
'no smarty', $i ++,
), array(
'{config_load \'template.conf\' scope=root bubble_up}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'',
'no smarty', $i ++,
), array(
'{config_load \'template.conf\' scope=smarty}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=smarty bubble_up}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'',
'no smarty', $i ++,
),
);
}
} }

View File

@@ -172,62 +172,6 @@ class CompileIncludeTest extends PHPUnit_Smarty
} }
/**
* Test scope
*
* @run InSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestScope
*/
public function testScope($code, $useSmarty, $result, $testName, $testNumber = null)
{
if ($testNumber) {
$file = "testScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code);
$this->smarty->assignGlobal('file', $file);
}
$this->smarty->assign('foo', 'smarty');
$this->smarty->assignGlobal('foo', 'global');
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data->assign('foo', 'data');
if (!$useSmarty) {
$testName .= 'no smarty';
}
$this->assertEquals($this->strip($result), $this->strip($this->smarty->fetch('test_scope.tpl', $data)),
"test - {$code} - {$testName}");
}
/*
* Data provider for testscope
*/
public function dataTestScope()
{
$i = 1;
return array(/*
* Code
* use Smarty object
* result
* test name
*/
array('{include \'test_scope_assign.tpl\'}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' bubble_up}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=parent}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=parent bubble_up}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=tpl_root}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=tpl_root bubble_up}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=root}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=root bubble_up}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=root}', false, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=root bubble_up}', false, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=smarty}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=smarty bubble_up}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=global}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=global bubble_up}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'', '', $i++),
array('{include \'test_scope_pluginassign.tpl\' scope=global}', true, '#test_scope_pluginassign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'', '', $i++),
array('{include \'test_scope_pluginassign.tpl\' scope=global bubble_up}', true, '#test_scope_pluginassign.tpl:$foo=\'newvar\'#testScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'', '', $i++),
);
}
/** /**

View File

@@ -0,0 +1,321 @@
<?php
/**
* Smarty PHPunit tests for scopes
*
* @package PHPunit
* @author Uwe Tews
*/
/**
* class scope tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class ScopeTest extends PHPUnit_Smarty
{
public function setUp()
{
$this->setUpSmarty(dirname(__FILE__));
$this->smarty->addPluginsDir("../../__shared/PHPunitplugins/");
$this->smarty->addTemplateDir("../../__shared/templates/");
$this->smarty->addTemplateDir("./templates_tmp");
}
public function testInit()
{
$this->cleanDirs();
}
/**
* Test scope
*
* @run InSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestAppendScope
*/
public function testAppendScope($code, $useSmarty, $result, $testName, $testNumber)
{
$file = "testAppendScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code . '{checkvar var=foo}');
$this->smarty->assignGlobal('file', $file);
$this->smarty->assign('foo', 'smarty');
$this->smarty->assignGlobal('foo', 'global');
$data1 = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data = $this->smarty->createData($data1);
$data1->assign('foo', 'data1');
$data->assign('foo', 'data');
$this->assertEquals($this->strip($result), $this->strip($this->smarty->fetch('scope_tag.tpl', $data)), "test - {$code} - {$testName}");
}
/*
* Data provider für testAppendScope
*/
public function dataTestAppendScope()
{
$i = 0;
/*
* Code
* use Smarty object
* result
* test name
*/
return array(
array(
'{$foo[] = \'newvar\' scope=tpl_root}', true,
'#testAppendScope_0.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#data:$foo=\'data\'#data:$foo=\'data1\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{append var=foo value=\'newvar\' scope=tpl_root}', true,
'#testAppendScope_1.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#data:$foo=\'data\'#data:$foo=\'data1\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{append var=foo value=\'newvar\' scope=global}', true,
'#testAppendScope_2.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#data:$foo=\'data\'#data:$foo=\'data1\'#Smarty:$foo=\'smarty\'#global:$foo=array(0=>\'data\',1=>\'newvar\',)',
'', $i ++,
), array(
'{append var=foo value=\'newvar\' scope=smarty}', true,
'#testAppendScope_3.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_include.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#scope_tag.tpl:$foo=array(0=>\'data\',1=>\'newvar\',)#data:$foo=\'data\'#data:$foo=\'data1\'#Smarty:$foo=array(0=>\'data\',1=>\'newvar\',)#global:$foo=\'global\'',
'', $i ++,
),
);
}
/**
* Test scope
*
* @not runInSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestAssignScope
*/
public function testAssignScope($code, $useSmarty, $result, $testName, $testNumber)
{
$file = "testAssignScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code . '{checkvar var=foo}');
$this->smarty->assignGlobal('file', $file);
$this->smarty->assign('foo', 'smarty');
$this->smarty->assignGlobal('foo', 'global');
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data->assign('foo', 'data');
$this->assertEquals($this->strip('#' . $file . $result), $this->strip($this->smarty->fetch('scope_tag.tpl', $data)), "test - {$code} - {$testName}");
}
/*
* Data provider für testAssignScope
*/
public function dataTestAssignScope()
{
$i = 1;
/*
* Code
* use Smarty object
* result
* test name
*/
return array(
array(
'{$foo = \'newvar\'}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\'}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=local}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\' scope=local}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=parent}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{assign var=foo value=\'newvar\' scope=parent}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=tpl_root}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=global}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=root}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'', $i ++,
), array(
'{$foo = \'newvar\' scope=root}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'',
'no smarty', $i ++,
), array(
'{$foo = \'newvar\' scope=smarty}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'#global:$foo=\'global\'',
'no smarty', $i ++,
),
);
}
/**
* Test scope nocache
*
* @runInSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestScopeNocache
*/
public function testScopeNocache($var, $file, $result)
{
$this->smarty->setCaching(true);
$this->smarty->assign('bar', $var, true);
$this->smarty->assign('buh', $var);
$this->smarty->assign('foo', 'smarty');
$this->smarty->assignGlobal('foo', 'global');
$this->assertEquals($this->strip($result), $this->strip($this->smarty->fetch($file)), "test - {$file} {$var}");
}
/*
* Data provider für testscopenocache
*/
public function dataTestScopeNocache()
{
/*
* variable value
* result
*/
return array(
array(
'b1', 'test_scope_assignbar.tpl',
'#test_scope_assignbar.tpl:$foo=\'b1\'#Smarty:$foo=\'smarty\'#global:$foo=\'b1\'',
), array(
'b2', 'test_scope_assignbar.tpl',
'#test_scope_assignbar.tpl:$foo=\'b2\'#Smarty:$foo=\'smarty\'#global:$foo=\'b2\'',
), array(
'b1', 'test_scope_assignnocache.tpl',
'#test_scope_assignnocache.tpl:$foo=\'b1\'#Smarty:$foo=\'smarty\'#global:$foo=\'b1\'',
), array(
'b2', 'test_scope_assignnocache.tpl',
'#test_scope_assignnocache.tpl:$foo=\'b2\'#Smarty:$foo=\'smarty\'#global:$foo=\'b2\'',
),
);
}
/**
* Test scope
*
* @run InSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestIncludeScope
*/
public function testIncludeScope($code, $useSmarty, $result, $testName, $testNumber = null)
{
if ($testNumber) {
$file = "testIncludeScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code);
$this->smarty->assignGlobal('file', $file);
}
$this->smarty->assign('foo', 'smarty');
$this->smarty->assignGlobal('foo', 'global');
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data->assign('foo', 'data');
if (!$useSmarty) {
$testName .= 'no smarty';
}
$this->assertEquals($this->strip($result), $this->strip($this->smarty->fetch('test_scope.tpl', $data)),
"test - {$code} - {$testName}");
}
/*
* Data provider for testIncludeScope
*/
public function dataTestIncludeScope()
{
$i = 1;
return array(/*
* Code
* use Smarty object
* result
* test name
*/
array('{include \'test_scope_assign.tpl\'}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testIncludeScope_'.$i.'.tpl:$foo=\'data\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=parent}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testIncludeScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=tpl_root}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testIncludeScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=root}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testIncludeScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=root}', false, '#test_scope_assign.tpl:$foo=\'newvar\'#testIncludeScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=smarty}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testIncludeScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'#global:$foo=\'global\'', '', $i++),
array('{include \'test_scope_assign.tpl\' scope=global}', true, '#test_scope_assign.tpl:$foo=\'newvar\'#testIncludeScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'', '', $i++),
array('{include \'test_scope_pluginassign.tpl\' scope=global}', true, '#test_scope_pluginassign.tpl:$foo=\'newvar\'#testIncludeScope_'.$i.'.tpl:$foo=\'newvar\'#test_scope.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'#global:$foo=\'newvar\'', '', $i++),
);
}
/**
* Test scope
*
* @not runInSeparateProcess
* @preserveGlobalState disabled
* @dataProvider dataTestConfigScope
*/
public function testConfigScope($code, $useSmarty, $result, $testName, $testNumber)
{
$file = "testConfigScope_{$testNumber}.tpl";
$this->makeTemplateFile($file, $code . '{checkconfigvar var=foo}');
$this->smarty->assignGlobal('file', $file);
$this->smarty->configLoad('smarty.conf');
$data = $this->smarty->createData($useSmarty ? $this->smarty : null);
$data->configLoad('data.conf');
$this->assertEquals($this->strip('#' . $file . $result), $this->strip($this->smarty->fetch('scope_tag.tpl', $data)), "test - {$code} - {$testName}");
}
/*
* Data provider für testConfigScope
*/
public function dataTestConfigScope()
{
$i = 1;
/*
* Code
* use Smarty object
* result
* test name
*/
return array(
array(
'{config_load \'template.conf\'}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=local}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'data\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=parent}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'data\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=tpl_root}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=root}', true,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'',
'', $i ++,
), array(
'{config_load \'template.conf\' scope=root}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'newvar\'#Smarty:$foo=\'smarty\'',
'no smarty', $i ++,
), array(
'{config_load \'template.conf\' scope=smarty}', false,
':$foo=\'newvar\'#scope_include.tpl:$foo=\'newvar\'#scope_tag.tpl:$foo=\'newvar\'#data:$foo=\'data\'#Smarty:$foo=\'newvar\'',
'no smarty', $i ++,
),
);
}
public function testFunctionScope() {
$this->smarty->assign('scope', 'none');
$r = $this->smarty->fetch('test_function_scope.tpl');
}
}

View File

@@ -0,0 +1 @@
foo = data

View File

@@ -0,0 +1 @@
foo = smarty

View File

@@ -0,0 +1 @@
foo = newvar

View File

@@ -0,0 +1,35 @@
{strip}
{function 'a'}
{call 'b'}
{/function}
{function 'b'}
{include 'test_function_scope_include.tpl'}
{/function}
{function c}
{call $scope}
{/function}
{function 'none'}
{$foo = 'newvar'}
{checkvar var=foo}
{/function}
{function 'local'}
{$foo = 'newvar' scope='local'}
{checkvar var=foo}
{/function}
{function 'parent'}
{$foo = 'newvar' scope='parent'}
{checkvar var=foo}
{/function}
{function 'tpl_root'}
{$foo = 'newvar' scope='tpl_root'}
{checkvar var=foo}
{/function}
{function 'smarty'}
{$foo = 'newvar' scope='smarty'}
{checkvar var=foo}
{/function}
{function 'global'}
{$foo = 'newvar' scope='global'}
{checkvar var=foo}
{/function}
{call 'a'}

View File

@@ -0,0 +1,2 @@
{strip}
{call 'c'}

View File

@@ -0,0 +1 @@
{include $file}

View File

@@ -0,0 +1 @@
{$foo = 'newvar'}{checkvar var=foo}

View File

@@ -0,0 +1 @@
{$foo = $bar scope=global}{checkvar var=foo nocache}

View File

@@ -0,0 +1 @@
{$foo = $buh scope=global nocache}{checkvar var=foo nocache}

View File

@@ -0,0 +1 @@
{pluginassign var=foo value='newvar'}{checkvar var=foo}

View File

@@ -27,6 +27,12 @@ function smarty_function_checkvar($params, $template)
if (in_array('template', $types) && $ptr instanceof Smarty_Internal_Template) { if (in_array('template', $types) && $ptr instanceof Smarty_Internal_Template) {
$output .= "#{$ptr->source->name}:\${$var} ="; $output .= "#{$ptr->source->name}:\${$var} =";
$output .= isset($ptr->tpl_vars[$var]) ? preg_replace('/\s/', '', var_export($ptr->tpl_vars[$var]->value, true)) : '>unassigned<'; $output .= isset($ptr->tpl_vars[$var]) ? preg_replace('/\s/', '', var_export($ptr->tpl_vars[$var]->value, true)) : '>unassigned<';
$i = 0;
while (isset($ptr->_cache[ 'varStack' ][ $i ])) {
$output .= "#{$ptr->_cache[ 'varStack' ][ $i ]['name']} = ";
$output .= isset($ptr->_cache[ 'varStack' ][ $i ][ 'tpl' ][$var]) ? preg_replace('/\s/', '', var_export($ptr->_cache[ 'varStack' ][ $i ][ 'tpl' ][$var]->value, true)) : '>unassigned<';
$i ++;
}
$ptr = $ptr->parent; $ptr = $ptr->parent;
} elseif (in_array('data', $types) && $ptr instanceof Smarty_Data) { } elseif (in_array('data', $types) && $ptr instanceof Smarty_Data) {
$output .= "#data:\${$var} ="; $output .= "#data:\${$var} =";