Implemented support for substr, implode and json_encode as modifiers. (#940)

* Implemented support for substr, implode and json_encode as modifiers. Fixes #939
* Added split and join in favor of explode and implode modifiers.
* Documented all available modifiers
This commit is contained in:
Simon Wisselink
2024-02-26 14:35:19 +01:00
committed by GitHub
parent 2b0ba0eabc
commit 41d80b99ac
29 changed files with 723 additions and 70 deletions
@@ -103,7 +103,8 @@ class RegisterModifierTest extends PHPUnit_Smarty
public function dataUnknownModifiers(): array {
return [
['{"blah"|substr:1:2}', 'la'],
['{" blah"|ltrim:" "}', 'blah'],
['{"blah"|strrev}', 'halb'],
['{"blah"|ucfirst}', 'Blah'],
['{"blah"|md5}', md5('blah')],
];
@@ -15,6 +15,10 @@ class IssetTest extends \PHPUnit_Smarty {
$this->assertEquals("yay", $this->smarty->fetch("string:{if isset('')}yay{/if}"));
}
public function testEmptyStringIssetModifierSyntax() {
$this->assertEquals("yay", $this->smarty->fetch("string:{if ''|isset}yay{/if}"));
}
public function testFalseIsset() {
$this->smarty->assign('test', false);
$this->assertEquals("yay", $this->smarty->fetch("string:{if isset(\$test)}yay{/if}"));
@@ -18,9 +18,7 @@ class PluginModifierExplodeTest extends \PHPUnit_Smarty
}
/**
* @return void
* @throws \Smarty\Exception
*
* @deprecated
* @dataProvider explodeDataProvider
*/
public function testExplode($template, $subject, $expectedString)
@@ -0,0 +1,61 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;
class PluginModifierImplodeTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* @deprecated
*/
public function testDefault()
{
$tpl = $this->smarty->createTemplate('string:{$v|implode}');
$tpl->assign("v", ["1", "2"]);
$this->assertEquals("12", $this->smarty->fetch($tpl));
}
/**
* @deprecated
*/
public function testWithSeparator()
{
$tpl = $this->smarty->createTemplate('string:{$v|implode:","}');
$tpl->assign("v", ["a", "b"]);
$this->assertEquals("a,b", $this->smarty->fetch($tpl));
}
/**
* @deprecated
*/
public function testLegacyArgumentOrder()
{
$tpl = $this->smarty->createTemplate('string:{","|implode:$v}');
$tpl->assign("v", ["a", "b"]);
$this->assertEquals("a,b", $this->smarty->fetch($tpl));
}
/**
* @deprecated
*/
public function testInConditional()
{
$tpl = $this->smarty->createTemplate('string:{if implode($v) == "abc"}good{else}bad{/if}');
$tpl->assign("v", ['a','b','c']);
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
/**
* @deprecated
*/
public function testInConditionalWithSeparator()
{
$tpl = $this->smarty->createTemplate('string:{if implode($v, "-") == "a-b-c"}good{else}bad{/if}');
$tpl->assign("v", ['a','b','c']);
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
}
@@ -0,0 +1,43 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;
class PluginModifierJoinTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testDefault()
{
$tpl = $this->smarty->createTemplate('string:{$v|join}');
$tpl->assign("v", ["1", "2"]);
$this->assertEquals("12", $this->smarty->fetch($tpl));
}
public function testWithSeparator()
{
$tpl = $this->smarty->createTemplate('string:{$v|join:","}');
$tpl->assign("v", ["a", "b"]);
$this->assertEquals("a,b", $this->smarty->fetch($tpl));
}
public function testInConditional()
{
$tpl = $this->smarty->createTemplate('string:{if join($v) == "abc"}good{else}bad{/if}');
$tpl->assign("v", ['a','b','c']);
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
public function testInConditionalWithSeparator()
{
$tpl = $this->smarty->createTemplate('string:{if join($v, "-") == "a-b-c"}good{else}bad{/if}');
$tpl->assign("v", ['a','b','c']);
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
}
@@ -0,0 +1,72 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;
class PluginModifierJsonEncodeTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
/**
* @dataProvider dataForDefault
*/
public function testDefault($value, $expected)
{
$tpl = $this->smarty->createTemplate('string:{$v|json_encode}');
$tpl->assign("v", $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
/**
* @dataProvider dataForDefault
*/
public function testDefaultAsFunction($value, $expected)
{
$tpl = $this->smarty->createTemplate('string:{json_encode($v)}');
$tpl->assign("v", $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
public function dataForDefault() {
return [
["abc", '"abc"'],
[["abc"], '["abc"]'],
[["abc",["a"=>2]], '["abc",{"a":2}]'],
];
}
/**
* @dataProvider dataForForceObject
*/
public function testForceObject($value, $expected)
{
$tpl = $this->smarty->createTemplate('string:{$v|json_encode:16}');
$tpl->assign("v", $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
/**
* @dataProvider dataForForceObject
*/
public function testForceObjectAsFunction($value, $expected)
{
$tpl = $this->smarty->createTemplate('string:{json_encode($v,16)}');
$tpl->assign("v", $value);
$this->assertEquals($expected, $this->smarty->fetch($tpl));
}
public function dataForForceObject() {
return [
["abc", '"abc"'],
[["abc"], '{"0":"abc"}'],
[["abc",["a"=>2]], '{"0":"abc","1":{"a":2}}'],
];
}
}
@@ -0,0 +1,53 @@
<?php
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
/**
* class for modifier tests
*
*
*
*
*/
class PluginModifierSplitTest extends \PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
$this->smarty->registerPlugin('modifier', 'json_encode', 'json_encode');
}
/**
* @dataProvider explodeDataProvider
*/
public function testSplit($template, $subject, $expectedString)
{
$this->smarty->assign('subject', $subject);
$tpl = $this->smarty->createTemplate($template);
$res = $this->smarty->fetch($tpl);
$this->assertEquals($expectedString, $res);
}
public function explodeDataProvider()
{
return [
'default' => [
'template' => 'string:{$subject|split:","|json_encode}',
'subject' => 'a,b,c,d',
'expectedString' => '["a","b","c","d"]',
],
'withNoDelimiterFound' => [
'template' => 'string:{$subject|split:","|json_encode}',
'subject' => 'abcd',
'expectedString' => '["abcd"]',
],
'withNull' => [
'template' => 'string:{$subject|split:","|json_encode}',
'subject' => null,
'expectedString' => '[""]',
],
];
}
}
@@ -0,0 +1,44 @@
<?php
/**
* Smarty PHPunit tests of modifier
*/
namespace UnitTests\TemplateSource\TagTests\PluginModifier;
use PHPUnit_Smarty;
class PluginModifierSubstrTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testDefault()
{
$tpl = $this->smarty->createTemplate('string:{$v|substr:1}');
$tpl->assign("v", "abc");
$this->assertEquals("bc", $this->smarty->fetch($tpl));
}
public function testTwoArguments()
{
$tpl = $this->smarty->createTemplate('string:{$v|substr:1:1}');
$tpl->assign("v", "abc");
$this->assertEquals("b", $this->smarty->fetch($tpl));
}
public function testNegativeOffset()
{
$tpl = $this->smarty->createTemplate('string:{$v|substr:-1}');
$tpl->assign("v", "abc");
$this->assertEquals("c", $this->smarty->fetch($tpl));
}
public function testInConditional()
{
$tpl = $this->smarty->createTemplate('string:{if substr($v, -1) == "c"}good{else}bad{/if}');
$tpl->assign("v", "abc");
$this->assertEquals("good", $this->smarty->fetch($tpl));
}
}