mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-01 08:54:26 +02:00
Fix modifier default implementation and improve unit tests
This commit is contained in:
@@ -28,7 +28,7 @@ function smarty_modifiercompiler_default($params, Smarty_Internal_TemplateCompil
|
|||||||
foreach ($params as $param) {
|
foreach ($params as $param) {
|
||||||
|
|
||||||
if ($compiler->syntaxMatchesVariable($output)) {
|
if ($compiler->syntaxMatchesVariable($output)) {
|
||||||
$output = '(empty(' . $output . ') ? ' . $param . ' : ' . $output . ')';
|
$output = '(!isset(' . $output . ') || ' . $output . ' === \'\' ? ' . $param . ' : ' . $output . ')';
|
||||||
} else {
|
} else {
|
||||||
$output = '(($tmp = ' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)';
|
$output = '(($tmp = ' . $output . ')===null||$tmp===\'\' ? ' . $param . ' : $tmp)';
|
||||||
}
|
}
|
||||||
|
@@ -63,20 +63,31 @@ class PluginModifierDefaultTest extends PHPUnit_Smarty
|
|||||||
$this->assertEquals('B', $this->smarty->fetch($tpl));
|
$this->assertEquals('B', $this->smarty->fetch($tpl));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNull()
|
/**
|
||||||
|
* @dataProvider dataFalsyValues
|
||||||
|
*/
|
||||||
|
public function testFalsyValues($falsyvalue, $expected)
|
||||||
{
|
{
|
||||||
$tpl = $this->smarty->createTemplate('string:{$s|default:"B"}');
|
$tpl = $this->smarty->createTemplate('string:{$s|default:"B"}');
|
||||||
$this->smarty->assign('s', null);
|
$this->smarty->assign('s', $falsyvalue);
|
||||||
$this->assertEquals('B', $this->smarty->fetch($tpl));
|
$this->assertEquals($expected, $this->smarty->fetch($tpl));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFalse()
|
/**
|
||||||
{
|
* Data for ::testFalsyValues
|
||||||
$tpl = $this->smarty->createTemplate('string:{$s|default:"B"}');
|
*/
|
||||||
$this->smarty->assign('s', false);
|
public function dataFalsyValues() {
|
||||||
$this->assertEquals('B', $this->smarty->fetch($tpl));
|
return array(
|
||||||
|
array(false, false),
|
||||||
|
array(0, 0),
|
||||||
|
array('0', '0'),
|
||||||
|
array(array(), 'Array'),
|
||||||
|
array(null, 'B'),
|
||||||
|
array('', 'B'),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testFunctionCall()
|
public function testFunctionCall()
|
||||||
{
|
{
|
||||||
$tpl = $this->smarty->createTemplate('string:{strlen("a")|default:"B"}');
|
$tpl = $this->smarty->createTemplate('string:{strlen("a")|default:"B"}');
|
||||||
@@ -137,7 +148,4 @@ class PluginModifierDefaultTest extends PHPUnit_Smarty
|
|||||||
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user