Skip to content

Commit a4b8466

Browse files
committed
Added unit tests to prevent regressions of issue #1100 that was fixed in v4
1 parent 5d1ea58 commit a4b8466

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
// first class callables where introduced in PHP 8.1
3+
if (PHP_VERSION_ID >= 80100) {
4+
5+
/**
6+
* class for register modifier with (first class) callables tests
7+
*
8+
* @runTestsInSeparateProcess
9+
* @preserveGlobalState disabled
10+
* @backupStaticAttributes enabled
11+
*/
12+
class RegisterModifierFirstClassCallablesTest extends PHPUnit_Smarty
13+
{
14+
public function setUp(): void
15+
{
16+
$this->setUpSmarty(__DIR__);
17+
}
18+
19+
20+
public function testInit()
21+
{
22+
$this->cleanDirs();
23+
}
24+
25+
public function testRegisterFirstClassCallable()
26+
{
27+
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', eval('return strrev(...);'));
28+
$this->assertEquals('mosredna', $this->smarty->fetch('string:{"andersom"|testmodifier}'));
29+
}
30+
31+
public function testRegisterFirstClassCallableSameName()
32+
{
33+
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'mymodifier', eval('return strrev(...);'));
34+
$this->assertEquals('mosredna', $this->smarty->fetch('string:{"andersom"|mymodifier}'));
35+
}
36+
37+
public function testRegisterFirstClassCallableAsFunc()
38+
{
39+
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'kprint_r_out', eval('return strrev(...);'));
40+
$this->smarty->assign('myVar', 'andersom');
41+
$this->assertEquals('mosredna', $this->smarty->fetch('string:{kprint_r_out($myVar)}'));
42+
}
43+
44+
public function testRegisterFirstClassCallableSameNameAsPhpFunc()
45+
{
46+
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'mymodifierfcc', eval('return strrev(...);'));
47+
$this->assertEquals('mosredna', $this->smarty->fetch('string:{mymodifierfcc("andersom")}'));
48+
}
49+
50+
}
51+
}
52+
function mymodifierfcc($a, $b, $c)
53+
{
54+
return "$a function $b $c";
55+
}

tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ public function testSetExtensions($template, $expectedValue)
135135
$this->assertEquals($expectedValue, $this->smarty->fetch('string:' . $template));
136136
}
137137

138+
public function testRegisterNativePhpFuncAsString()
139+
{
140+
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'strrev', 'strrev');
141+
$this->smarty->assign('myVar', 'andersom');
142+
$this->assertEquals('mosredna', $this->smarty->fetch('string:{strrev($myVar)}'));
143+
}
144+
145+
public function testRegisterNativePhpFuncUnderDifferentName()
146+
{
147+
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'k_xyz_a', 'strrev');
148+
$this->smarty->assign('myVar', 'andersom');
149+
$this->assertEquals('mosredna', $this->smarty->fetch('string:{k_xyz_a($myVar)}'));
150+
}
151+
138152
}
139153

140154
class WildcardExtension extends \Smarty\Extension\Base {

0 commit comments

Comments
 (0)