Files
smarty/tests/UnitTests/SmartyMethodsTests/RegisterCompiler/RegisterCompilerFunctionTest.php
Simon Wisselink 39b69f0142 Feature/php8 support (#629)
Adds support for PHP8.0, dropping support for PHP7.0 and below.

Backwards incompatible changes:
- Dropped support for php asp tags in templates (removed from php since php7.0)
- Dropped deprecated API calls that where only accessible through SmartyBC
- Dropped support for {php} and {include_php} tags and embedded PHP in templates. Embedded PHP will now be passed through as is.
- Removed all PHP_VERSION_ID and compare_version checks and conditional code blocks that are now no longer required
- Dropped deprecated SMARTY_RESOURCE_CHAR_SET and SMARTY_RESOURCE_DATE_FORMAT constants
- Dropped deprecated Smarty::muteExpectedErrors and Smarty::unmuteExpectedErrors API methods
- Dropped deprecated $smarty->getVariable() method. Use $smarty->getTemplateVars() instead.
- $smarty->registerResource() no longer accepts an array of callback functions

See the changelog for more details.

Switched CI from Travis to Github CI.
2021-10-13 12:15:17 +02:00

125 lines
4.1 KiB
PHP

<?php
/**
* Smarty PHPunit tests register->compilerFunction / unregister->compilerFunction methods
*
* @package PHPunit
* @author Uwe Tews
*/
/**
* class for register->compilerFunction / unregister->compilerFunction methods tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class RegisterCompilerFunctionTest extends PHPUnit_Smarty
{
public function setUp(): void
{
$this->setUpSmarty(dirname(__FILE__));
}
public function testInit()
{
$this->cleanDirs();
}
/**
* test register->compilerFunction method for function
*/
public function testRegisterCompilerFunction()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction');
$this->assertEquals('mycompilerfunction', $this->smarty->registered_plugins['compiler']['testcompilerfunction'][0]);
$this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testcompilerfunction var=1}'));
}
/**
* test register->compilerFunction method for blocks
*/
public function testRegisterCompilerFunctionBlock()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'foo', 'mycompilerfunctionopen');
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'fooclose', 'mycompilerfunctionclose');
$result = $this->smarty->fetch('eval:{foo} hallo {/foo}');
$this->assertEquals('open tag hallo close tag', $result);
}
/**
* test register->compilerFunction method for static class
*/
public function testRegisterCompilerFunctionClass()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', array('mycompilerfunctionclass', 'execute'));
$this->assertEquals('hello world 2', $this->smarty->fetch('eval:{testcompilerfunction var1=2}'));
}
/**
* test register->compilerFunction method for objects
*/
public function testRegisterCompilerFunctionObject()
{
$obj = new mycompilerfunctionclass;
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', array($obj, 'compile'));
$this->assertEquals('hello world 3', $this->smarty->fetch('eval:{testcompilerfunction var2=3}'));
}
/**
* test unregister->compilerFunction method
*/
public function testUnregisterCompilerFunction()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction', 'mycompilerfunction');
$this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction');
$this->assertFalse(isset($this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER]['testcompilerfunction']));
}
/**
* test unregister->compilerFunction method not registered
*/
public function testUnregisterCompilerFunctionNotRegistered()
{
$this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction');
$this->assertFalse(isset($this->smarty->registered_plugins[Smarty::PLUGIN_COMPILER]['testcompilerfunction']));
}
/**
* test unregister->compilerFunction method other registered
*/
public function testUnregisterCompilerFunctionOtherRegistered()
{
$this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testcompilerfunction', 'mycompilerfunction');
$this->smarty->unregisterPlugin(Smarty::PLUGIN_COMPILER, 'testcompilerfunction');
$this->assertTrue(isset($this->smarty->registered_plugins[Smarty::PLUGIN_BLOCK]['testcompilerfunction']));
}
}
function mycompilerfunction($params, $smarty)
{
return "<?php echo 'hello world {$params['var']}';?>";
}
function mycompilerfunctionopen($params, $smarty)
{
return "<?php echo 'open tag';?>";
}
function mycompilerfunctionclose($params, $smarty)
{
return "<?php echo 'close tag';?>";
}
class mycompilerfunctionclass
{
static function execute($params, $smarty)
{
return "<?php echo 'hello world {$params['var1']}';?>";
}
public function compile($params, $smarty)
{
return "<?php echo 'hello world {$params['var2']}';?>";
}
}