mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-02 17:34:26 +02:00
Added unit tests
This commit is contained in:
@@ -1,156 +1,55 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
// first class callables where introduced in PHP 8.1
|
||||||
* Smarty PHPunit tests register->modifier / unregister->modifier methods
|
if (PHP_VERSION_ID >= 80100) {
|
||||||
*
|
|
||||||
* @package PHPunit
|
|
||||||
* @author Uwe Tews
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* class for register->modifier / unregister->modifier methods tests
|
|
||||||
*
|
|
||||||
* @runTestsInSeparateProcess
|
|
||||||
* @preserveGlobalState disabled
|
|
||||||
* @backupStaticAttributes enabled
|
|
||||||
*/
|
|
||||||
class RegisterModifierTest extends PHPUnit_Smarty
|
|
||||||
{
|
|
||||||
public function setUp(): void
|
|
||||||
{
|
|
||||||
$this->setUpSmarty(__DIR__);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function testInit()
|
|
||||||
{
|
|
||||||
$this->cleanDirs();
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* test register->modifier method for function
|
|
||||||
*/
|
|
||||||
public function testRegisterModifier()
|
|
||||||
{
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier', 'mymodifier');
|
|
||||||
$this->assertEquals('mymodifier', $this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER]['testmodifier'][0]);
|
|
||||||
$this->smarty->assign('foo', 'foo');
|
|
||||||
$this->smarty->assign('bar', 'bar');
|
|
||||||
$this->assertEquals('foo function blar bar', $this->smarty->fetch('eval:{$foo|testmodifier:blar:$bar}'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test register->modifier method for classes
|
|
||||||
*/
|
|
||||||
public function testRegisterModifierClass()
|
|
||||||
{
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier', array('mymodifierclass', 'static_method'));
|
|
||||||
$this->smarty->assign('foo', 'foo');
|
|
||||||
$this->smarty->assign('bar', 'bar');
|
|
||||||
$this->assertEquals('foo static blar bar', $this->smarty->fetch('eval:{$foo|testmodifier:blar:$bar}'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test register->modifier method for objects
|
|
||||||
*/
|
|
||||||
public function testRegisterModifierObject()
|
|
||||||
{
|
|
||||||
$obj = new mymodifierclass;
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier', array($obj, 'object_method'));
|
|
||||||
$this->smarty->assign('foo', 'foo');
|
|
||||||
$this->smarty->assign('bar', 'bar');
|
|
||||||
$this->assertEquals('foo object blar bar', $this->smarty->fetch('eval:{$foo|testmodifier:blar:$bar}'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test unregister->modifier method
|
|
||||||
*/
|
|
||||||
public function testUnregisterModifier()
|
|
||||||
{
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier', 'mymodifier');
|
|
||||||
$this->smarty->unregisterPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier');
|
|
||||||
$this->assertFalse(isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER]['testmodifier']));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test unregister->modifier method not registered
|
|
||||||
*/
|
|
||||||
public function testUnregisterModifierNotRegistered()
|
|
||||||
{
|
|
||||||
$this->smarty->unregisterPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier');
|
|
||||||
$this->assertFalse(isset($this->smarty->registered_plugins[Smarty::PLUGIN_MODIFIER]['testmodifier']));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* test unregister->modifier method other registered
|
|
||||||
*/
|
|
||||||
public function testUnregisterModifierOtherRegistered()
|
|
||||||
{
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testmodifier', 'mymodifier');
|
|
||||||
$this->smarty->unregisterPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier');
|
|
||||||
$this->assertTrue(isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK]['testmodifier']));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRegisterFirstClassCallable() {
|
|
||||||
// skip test if PHP version is lower than 8.1
|
|
||||||
if (PHP_VERSION_ID < 81000) {
|
|
||||||
$this->markTestSkipped('PHP 8.1 or later is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier', strrev(...));
|
|
||||||
$this->assertEquals('mosredna', $this->smarty->fetch('string:{"andersom"|testmodifier}'));
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRegisterFirstClassCallableSameName() {
|
|
||||||
|
|
||||||
// skip test if PHP version is lower than 8.1
|
|
||||||
if (PHP_VERSION_ID < 81000) {
|
|
||||||
$this->markTestSkipped('PHP 8.1 or later is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'mymodifier', mymodifier(...));
|
|
||||||
$this->assertEquals('mosredna', $this->smarty->fetch('string:{"andersom"|mymodifier:"":""}'));
|
|
||||||
}
|
|
||||||
public function testRegisterFirstClassCallableAsFunc() {
|
|
||||||
// skip test if PHP version is lower than 8.1
|
|
||||||
if (PHP_VERSION_ID < 81000) {
|
|
||||||
$this->markTestSkipped('PHP 8.1 or later is required');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'kprint_r_out', strrev(...));
|
|
||||||
$this->smarty->assign('myVar', 'andersom');
|
|
||||||
$this->assertEquals('mosredna', $this->smarty->fetch('string:{kprint_r_out($myVar)}'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* class for register modifier with (first class) callables tests
|
||||||
* @throws SmartyException
|
*
|
||||||
* @group RegisterFirstClassCallableSameNameAsPhpFunc
|
* @runTestsInSeparateProcess
|
||||||
|
* @preserveGlobalState disabled
|
||||||
|
* @backupStaticAttributes enabled
|
||||||
*/
|
*/
|
||||||
public function testRegisterFirstClassCallableSameNameAsPhpFunc() {
|
class RegisterModifierFirstClassCallablesTest extends PHPUnit_Smarty
|
||||||
// skip test if PHP version is lower than 8.1
|
{
|
||||||
if (PHP_VERSION_ID < 81000) {
|
public function setUp(): void
|
||||||
$this->markTestSkipped('PHP 8.1 or later is required');
|
{
|
||||||
|
$this->setUpSmarty(__DIR__);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function testInit()
|
||||||
|
{
|
||||||
|
$this->cleanDirs();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRegisterFirstClassCallable()
|
||||||
|
{
|
||||||
|
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'testmodifier', eval('return strrev(...);'));
|
||||||
|
$this->assertEquals('mosredna', $this->smarty->fetch('string:{"andersom"|testmodifier}'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRegisterFirstClassCallableSameName()
|
||||||
|
{
|
||||||
|
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'mymodifier', eval('return strrev(...);'));
|
||||||
|
$this->assertEquals('mosredna', $this->smarty->fetch('string:{"andersom"|mymodifier}'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRegisterFirstClassCallableAsFunc()
|
||||||
|
{
|
||||||
|
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'kprint_r_out', eval('return strrev(...);'));
|
||||||
|
$this->smarty->assign('myVar', 'andersom');
|
||||||
|
$this->assertEquals('mosredna', $this->smarty->fetch('string:{kprint_r_out($myVar)}'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRegisterFirstClassCallableSameNameAsPhpFunc()
|
||||||
|
{
|
||||||
|
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'mymodifierfcc', eval('return strrev(...);'));
|
||||||
|
$this->assertEquals('mosredna', $this->smarty->fetch('string:{mymodifierfcc("andersom")}'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->smarty->registerPlugin(Smarty::PLUGIN_MODIFIER, 'mymodifier', strrev(...));
|
|
||||||
$this->assertEquals('mosredna', $this->smarty->fetch('string:{mymodifier("andersom","","")}'));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
function mymodifierfcc($a, $b, $c)
|
||||||
function mymodifier($a, $b, $c)
|
|
||||||
{
|
{
|
||||||
return "$a function $b $c";
|
return "$a function $b $c";
|
||||||
}
|
}
|
||||||
|
|
||||||
class mymodifierclass
|
|
||||||
{
|
|
||||||
static function static_method($a, $b, $c)
|
|
||||||
{
|
|
||||||
return "$a static $b $c";
|
|
||||||
}
|
|
||||||
|
|
||||||
public function object_method($a, $b, $c)
|
|
||||||
{
|
|
||||||
return "$a object $b $c";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user