summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSimon Wisselink <wisskid@users.noreply.github.com>2024-02-26 14:35:19 +0100
committerGitHub <noreply@github.com>2024-02-26 14:35:19 +0100
commit41d80b99ac4fe8d8bdd9ae370841dfd081a7669b (patch)
tree58401f2a0c735f9ebd08212ceafe3b3108eec1be /tests
parent2b0ba0eabcd0da5ee97326e81d293de03e3c6a37 (diff)
downloadsmarty-41d80b99ac4fe8d8bdd9ae370841dfd081a7669b.tar.gz
smarty-41d80b99ac4fe8d8bdd9ae370841dfd081a7669b.tar.bz2
smarty-41d80b99ac4fe8d8bdd9ae370841dfd081a7669b.zip
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
Diffstat (limited to 'tests')
-rw-r--r--tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php3
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginFunction/IssetTest.php4
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php4
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php61
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJoinTest.php43
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php72
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSplitTest.php53
-rw-r--r--tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php44
8 files changed, 280 insertions, 4 deletions
diff --git a/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php b/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php
index 267448d9..05ee7459 100644
--- a/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php
+++ b/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php
@@ -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')],
];
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/IssetTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/IssetTest.php
index a54b244a..d0316346 100644
--- a/tests/UnitTests/TemplateSource/TagTests/PluginFunction/IssetTest.php
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginFunction/IssetTest.php
@@ -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}"));
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php
index ecefc6f8..8b689b17 100644
--- a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierExplodeTest.php
@@ -18,9 +18,7 @@ class PluginModifierExplodeTest extends \PHPUnit_Smarty
}
/**
- * @return void
- * @throws \Smarty\Exception
- *
+ * @deprecated
* @dataProvider explodeDataProvider
*/
public function testExplode($template, $subject, $expectedString)
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php
new file mode 100644
index 00000000..aa0d6acd
--- /dev/null
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierImplodeTest.php
@@ -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));
+ }
+
+}
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJoinTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJoinTest.php
new file mode 100644
index 00000000..7fb6a958
--- /dev/null
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJoinTest.php
@@ -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));
+ }
+
+}
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php
new file mode 100644
index 00000000..9a287812
--- /dev/null
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierJsonEncodeTest.php
@@ -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}}'],
+ ];
+ }
+
+}
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSplitTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSplitTest.php
new file mode 100644
index 00000000..bfb769bf
--- /dev/null
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSplitTest.php
@@ -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' => '[""]',
+ ],
+ ];
+ }
+}
diff --git a/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php
new file mode 100644
index 00000000..cc59e107
--- /dev/null
+++ b/tests/UnitTests/TemplateSource/TagTests/PluginModifier/PluginModifierSubstrTest.php
@@ -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));
+ }
+
+}