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->smarty->registerResource("rr", array("rr_get_template",
"rr_get_timestamp",
"rr_get_secure",
"rr_get_trusted"));
$this->smarty->registerResource("rr", new RegisteredResourceTest_Resource1());
}
@@ -59,7 +56,7 @@ class RegisteredResourceTest extends PHPUnit_Smarty
*/
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->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() {
$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'));
}
/**
@@ -80,83 +77,55 @@ class RegisteredResourceTest extends PHPUnit_Smarty
*
*/
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'));
}
}
class RegisteredResourceTest_Resource1 extends Smarty_Resource_Custom {
/**
* resource functions
*/
function rr_get_template($tpl_name, &$tpl_source, $smarty_obj)
{
// populating $tpl_source
$tpl_source = '{$x="hello world"}{$x}';
protected function fetch($name, &$source, &$mtime) {
$source = '{$x="hello world"}{$x}';
$mtime = 1000000000;
}
return true;
}
function rr_get_timestamp($tpl_name, &$tpl_timestamp, $smarty_obj)
{
// $tpl_timestamp.
$tpl_timestamp = (int) floor(time() / 100) * 100;
class RegisteredResourceTest_Resource2 extends Smarty_Resource_Custom {
protected function fetch($name, &$source, &$mtime) {
// 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)
{
// assume all templates are secure
return true;
class RegisteredResourceTest_Resource3 extends Smarty_Resource_Custom {
protected function fetch($name, &$source, &$mtime) {
$source = 'template = {$smarty.template}';
$mtime = 1000000000;
}
}
function rr_get_trusted($tpl_name, $smarty_obj)
{
// not used for templates
}
// 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;
class RegisteredResourceTest_Resource4 extends Smarty_Resource_Custom {
protected function fetch($name, &$source, &$mtime) {
$source = 'current_dir = {$smarty.current_dir}';
$mtime = 1000000000;
}
}

View File

@@ -1,4 +1,5 @@
<?php
/*
* Smarty plugin
* -------------------------------------------------------------
@@ -8,30 +9,21 @@
* 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;
}
function smarty_resource_db_timestamp($tpl_name, &$tpl_timestamp, $smarty)
{
// $tpl_timestamp.
$tpl_timestamp = (int) floor(time() / 100) * 100;
return true;
}
function smarty_resource_db_secure($tpl_name, $smarty)
{
// assume all templates are secure
return true;
}
function smarty_resource_db_trusted($tpl_name, $smarty)
{
// not used for templates
class Smarty_Resource_Db extends Smarty_Resource_Recompiled {
public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) {
$source->filepath = 'db:';
$source->uid = sha1($source->resource);
$source->timestamp = 0;
$source->exists = true;
}
public function populateTimestamp(Smarty_Template_Source $source): int {
return 1000000000;
}
public function getContent(Smarty_Template_Source $source) {
return '{$x="hello world"}{$x}';
}
}