Fixed unit tests for removed deprecated code in previous commit

This commit is contained in:
Simon Wisselink
2021-01-15 13:32:30 +01:00
parent 170db9b898
commit 1430f61295
6 changed files with 58 additions and 223 deletions

View File

@@ -1,121 +0,0 @@
<?php
/**
* Smarty PHPunit tests of filter
*
* @package PHPunit
* @author Rodney Rehm
*/
/**
* class for filter tests
*
* @runTestsInSeparateProcess
* @preserveGlobalState disabled
* @backupStaticAttributes enabled
*/
class MuteExpectedErrorsTest extends PHPUnit_Smarty
{
protected $_errors = array();
public function setUp(): void
{
$this->setUpSmarty(dirname(__FILE__));
set_error_handler(array($this, 'error_handler'));
}
public function tearDown(): void {
restore_error_handler();
parent::tearDown();
}
public function testInit()
{
$this->cleanDirs();
}
public function error_handler($errno, $errstr, $errfile, $errline, $errcontext = array())
{
$this->_errors[] = $errfile . ' line ' . $errline;
}
public function testMuted()
{
Smarty::muteExpectedErrors();
$this->smarty->clearCache('default.tpl');
$this->smarty->clearCompiledTemplate('default.tpl');
$this->smarty->fetch('default.tpl');
$this->assertEquals($this->_errors, array());
@filemtime('ckxladanwijicajscaslyxck');
$error = array(__FILE__ . ' line ' . (__LINE__ - 1));
$this->assertEquals($this->_errors, $error);
Smarty::unmuteExpectedErrors();
}
/**
*
* @rrunInSeparateProcess
*
*/
public function testUnmuted()
{
$this->smarty->clearCache('default.tpl');
$this->smarty->clearCompiledTemplate('default.tpl');
$this->smarty->fetch('default.tpl');
$this->assertEquals($this->_errors, array());
@filemtime('ckxladanwijicajscaslyxck');
$this->assertEquals(1, count($this->_errors));
}
/**
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testMutedCaching()
{
Smarty::muteExpectedErrors();
$this->smarty->caching = true;
$this->smarty->clearCache('default.tpl');
$this->smarty->clearCompiledTemplate('default.tpl');
$this->smarty->fetch('default.tpl');
$this->assertEquals($this->_errors, array());
@filemtime('ckxladanwijicajscaslyxck');
$error = array(__FILE__ . ' line ' . (__LINE__ - 1));
$this->assertEquals($error, $this->_errors);
Smarty::unmuteExpectedErrors();
}
/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*
*/
public function testUnmutedCaching()
{
$this->smarty->caching = true;
$this->smarty->clearCache('default.tpl');
$this->smarty->clearCompiledTemplate('default.tpl');
$this->smarty->fetch('default.tpl');
$this->assertEquals($this->_errors, array());
@filemtime('ckxladanwijicajscaslyxck');
$this->assertEquals(1, count($this->_errors));
restore_error_handler();
}
}

View File

@@ -1,2 +0,0 @@
# Ignore anything in here, but keep this directory
*

View File

@@ -1 +0,0 @@
{$foo|default:""} /* should compile something with @silence error suppression */

View File

@@ -1,2 +0,0 @@
# Ignore anything in here, but keep this directory
*

View File

@@ -20,10 +20,7 @@ class RegisteredResourceTest extends PHPUnit_Smarty
{ {
$this->setUpSmarty(dirname(__FILE__)); $this->setUpSmarty(dirname(__FILE__));
$this->smarty->registerResource("rr", array("rr_get_template", $this->smarty->registerResource("rr", new RegisteredResourceTest_Resource1());
"rr_get_timestamp",
"rr_get_secure",
"rr_get_trusted"));
} }
@@ -59,7 +56,7 @@ class RegisteredResourceTest extends PHPUnit_Smarty
*/ */
public function testResourceCompileIdChange() public function testResourceCompileIdChange()
{ {
$this->smarty->registerResource('myresource', array('getSource', 'getTimestamp', 'getSecure', 'getTrusted')); $this->smarty->registerResource('myresource', new RegisteredResourceTest_Resource2());
$this->smarty->compile_id = 'a'; $this->smarty->compile_id = 'a';
$this->assertEquals('this is template 1', $this->smarty->fetch('myresource:some')); $this->assertEquals('this is template 1', $this->smarty->fetch('myresource:some'));
$this->assertEquals('this is template 1', $this->smarty->fetch('myresource:some')); $this->assertEquals('this is template 1', $this->smarty->fetch('myresource:some'));
@@ -72,7 +69,7 @@ class RegisteredResourceTest extends PHPUnit_Smarty
* *
*/ */
public function testSmartyTemplate() { public function testSmartyTemplate() {
$this->smarty->registerResource('mytpl', array('getTemplate', 'getTimestamp', 'getSecure', 'getTrusted')); $this->smarty->registerResource('mytpl', new RegisteredResourceTest_Resource3());
$this->assertEquals('template = mytpl:foo', $this->smarty->fetch('mytpl:foo')); $this->assertEquals('template = mytpl:foo', $this->smarty->fetch('mytpl:foo'));
} }
/** /**
@@ -80,83 +77,55 @@ class RegisteredResourceTest extends PHPUnit_Smarty
* *
*/ */
public function testSmartyCurrentDir() { public function testSmartyCurrentDir() {
$this->smarty->registerResource('mytpl', array('getCurrentDir', 'getTimestamp', 'getSecure', 'getTrusted')); $this->smarty->registerResource('mytpl', new RegisteredResourceTest_Resource4());
$this->assertEquals('current_dir = .', $this->smarty->fetch('mytpl:bar')); $this->assertEquals('current_dir = .', $this->smarty->fetch('mytpl:bar'));
} }
} }
class RegisteredResourceTest_Resource1 extends Smarty_Resource_Custom {
/** protected function fetch($name, &$source, &$mtime) {
* resource functions $source = '{$x="hello world"}{$x}';
*/ $mtime = 1000000000;
function rr_get_template($tpl_name, &$tpl_source, $smarty_obj) }
{
// populating $tpl_source
$tpl_source = '{$x="hello world"}{$x}';
return true;
} }
function rr_get_timestamp($tpl_name, &$tpl_timestamp, $smarty_obj) class RegisteredResourceTest_Resource2 extends Smarty_Resource_Custom {
{
// $tpl_timestamp. protected function fetch($name, &$source, &$mtime) {
$tpl_timestamp = (int) floor(time() / 100) * 100;
// we update a counter, so that we return a new source for every call
static $counter = 0;
$counter ++;
// construct a new source
$source = "this is template $counter";
$mtime = 1000000000;
}
protected function fetchTimestamp($name)
{
return 1000000000;
}
return true;
} }
function rr_get_secure($tpl_name, $smarty_obj) class RegisteredResourceTest_Resource3 extends Smarty_Resource_Custom {
{
// assume all templates are secure protected function fetch($name, &$source, &$mtime) {
return true; $source = 'template = {$smarty.template}';
$mtime = 1000000000;
}
} }
function rr_get_trusted($tpl_name, $smarty_obj) class RegisteredResourceTest_Resource4 extends Smarty_Resource_Custom {
{
// not used for templates protected function fetch($name, &$source, &$mtime) {
} $source = 'current_dir = {$smarty.current_dir}';
$mtime = 1000000000;
// resource functions for compile_id change test }
function getSecure($name, $smarty)
{
return true;
}
function getTrusted($name, $smarty)
{
}
function getSource($name, &$source, $smarty)
{
// we update a counter, so that we return a new source for every call
static $counter = 0;
$counter ++;
// construct a new source
$source = "this is template $counter";
return true;
}
function getTemplate($name, &$source, $smarty)
{
// construct a new source
$source = 'template = {$smarty.template}';
return true;
}
function getCurrentDir($name, &$source, $smarty)
{
// construct a new source
$source = 'current_dir = {$smarty.current_dir}';
return true;
}
function getTimestamp($name, &$timestamp, $smarty)
{
// always pretend the template is brand new
$timestamp = (int) floor(time() / 100) * 100;
return true;
} }

View File

@@ -1,4 +1,5 @@
<?php <?php
/* /*
* Smarty plugin * Smarty plugin
* ------------------------------------------------------------- * -------------------------------------------------------------
@@ -8,30 +9,21 @@
* Purpose: Fetches templates from a database * Purpose: Fetches templates from a database
* ------------------------------------------------------------- * -------------------------------------------------------------
*/ */
function smarty_resource_db_source($tpl_name, &$tpl_source, $smarty)
{
// do database call here to fetch your template,
// populating $tpl_source
$tpl_source = '{$x="hello world"}{$x}';
return true; class Smarty_Resource_Db extends Smarty_Resource_Recompiled {
}
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) {
function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, $smarty) $source->filepath = 'db:';
{ $source->uid = sha1($source->resource);
// $tpl_timestamp. $source->timestamp = 0;
$tpl_timestamp = (int) floor(time() / 100) * 100; $source->exists = true;
}
return true;
} public function populateTimestamp(Smarty_Template_Source $source): int {
return 1000000000;
function smarty_resource_db_secure($tpl_name, $smarty) }
{
// assume all templates are secure public function getContent(Smarty_Template_Source $source) {
return true; return '{$x="hello world"}{$x}';
} }
function smarty_resource_db_trusted($tpl_name, $smarty)
{
// not used for templates
} }