Files
smarty/tests/UnitTests/SmartyMethodsTests/RegisterModifier/RegisterModifierTest.php
T

167 lines
4.7 KiB
PHP
Raw Normal View History

<?php
/**
* Smarty PHPunit tests register->modifier / unregister->modifier methods
*
2023-08-08 00:04:14 +02:00
* @author Uwe Tews
*/
/**
* class for register->modifier / unregister->modifier methods tests
*
2023-08-08 00:04:14 +02:00
*
*
*
*/
class RegisterModifierTest extends PHPUnit_Smarty
{
2021-10-13 12:15:17 +02:00
public function setUp(): void
{
$this->setUpSmarty(__DIR__);
}
public function testInit()
{
$this->cleanDirs();
}
/**
* test register->modifier method for function
*/
public function testRegisterModifier()
{
2023-08-08 00:04:14 +02:00
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', 'mymodifier');
$this->assertEquals('mymodifier', $this->smarty->getRegisteredPlugin(\Smarty\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()
{
2023-08-08 00:04:14 +02:00
$this->smarty->registerPlugin(\Smarty\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;
2023-08-08 00:04:14 +02:00
$this->smarty->registerPlugin(\Smarty\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()
{
2023-08-08 00:04:14 +02:00
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier', 'mymodifier');
$this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier');
$this->assertNull($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier'));
}
/**
* test unregister->modifier method not registered
*/
public function testUnregisterModifierNotRegistered()
{
2023-08-08 00:04:14 +02:00
$this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier');
$this->assertNull($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier'));
}
/**
* test unregister->modifier method other registered
*/
public function testUnregisterModifierOtherRegistered()
{
2023-08-08 00:04:14 +02:00
$this->smarty->registerPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testmodifier', 'mymodifier');
$this->smarty->unregisterPlugin(\Smarty\Smarty::PLUGIN_MODIFIER, 'testmodifier');
$this->assertIsArray($this->smarty->getRegisteredPlugin(\Smarty\Smarty::PLUGIN_BLOCK, 'testmodifier'));
}
2023-08-08 00:04:14 +02:00
/**
* test cannot call native PHP fuctions by default
* @dataProvider dataUnknownModifiers
*/
public function testNativePHPModifiers($template, $expectedValue)
{
$this->cleanDirs();
$this->expectException(\Smarty\CompilerException::class);
$this->expectExceptionMessage('unknown modifier');
$this->smarty->fetch('string:' . $template);
}
public function dataUnknownModifiers(): array {
return [
['{"blah"|substr:1:2}', 'la'],
['{"blah"|ucfirst}', 'Blah'],
['{"blah"|md5}', md5('blah')],
];
}
/**
* test register wildcard modifier using extension
* @dataProvider dataUnknownModifiers
*/
public function testUnregisterModifiers($template, $expectedValue)
{
$this->cleanDirs();
$this->smarty->addExtension(new WildcardExtension());
$this->assertEquals($expectedValue, $this->smarty->fetch('string:' . $template));
}
/**
* test register wildcard modifier using setExtensions
* @dataProvider dataUnknownModifiers
*/
public function testSetExtensions($template, $expectedValue)
{
$this->cleanDirs();
$this->smarty->setExtensions([
new \Smarty\Extension\CoreExtension(),
new WildcardExtension()
]);
$this->assertEquals($expectedValue, $this->smarty->fetch('string:' . $template));
}
}
class WildcardExtension extends \Smarty\Extension\Base {
public function getModifierCallback(string $modifierName) {
if (is_callable($modifierName)) {
return $modifierName;
}
return null;
}
}
function mymodifier($a, $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";
}
}