From 1430f612951057d25f2e016a60d0bcadd2e097c0 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Fri, 15 Jan 2021 13:32:30 +0100 Subject: [PATCH] Fixed unit tests for removed deprecated code in previous commit --- .../MuteExpectedErrorsTest.php | 121 ------------------ .../MuteExpectedErrors/cache/.gitignore | 2 - .../MuteExpectedErrors/templates/default.tpl | 1 - .../MuteExpectedErrors/templates_c/.gitignore | 2 - .../Registered/RegisteredResourceTest.php | 113 ++++++---------- .../PHPunitplugins/resource.db.php | 42 +++--- 6 files changed, 58 insertions(+), 223 deletions(-) delete mode 100644 tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php delete mode 100644 tests/UnitTests/A_Core/MuteExpectedErrors/cache/.gitignore delete mode 100644 tests/UnitTests/A_Core/MuteExpectedErrors/templates/default.tpl delete mode 100644 tests/UnitTests/A_Core/MuteExpectedErrors/templates_c/.gitignore diff --git a/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php b/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php deleted file mode 100644 index d05b9f38..00000000 --- a/tests/UnitTests/A_Core/MuteExpectedErrors/MuteExpectedErrorsTest.php +++ /dev/null @@ -1,121 +0,0 @@ -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(); - } -} diff --git a/tests/UnitTests/A_Core/MuteExpectedErrors/cache/.gitignore b/tests/UnitTests/A_Core/MuteExpectedErrors/cache/.gitignore deleted file mode 100644 index 1d34e205..00000000 --- a/tests/UnitTests/A_Core/MuteExpectedErrors/cache/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Ignore anything in here, but keep this directory -* \ No newline at end of file diff --git a/tests/UnitTests/A_Core/MuteExpectedErrors/templates/default.tpl b/tests/UnitTests/A_Core/MuteExpectedErrors/templates/default.tpl deleted file mode 100644 index 585945d3..00000000 --- a/tests/UnitTests/A_Core/MuteExpectedErrors/templates/default.tpl +++ /dev/null @@ -1 +0,0 @@ -{$foo|default:""} /* should compile something with @silence error suppression */ \ No newline at end of file diff --git a/tests/UnitTests/A_Core/MuteExpectedErrors/templates_c/.gitignore b/tests/UnitTests/A_Core/MuteExpectedErrors/templates_c/.gitignore deleted file mode 100644 index 1d34e205..00000000 --- a/tests/UnitTests/A_Core/MuteExpectedErrors/templates_c/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# Ignore anything in here, but keep this directory -* \ No newline at end of file diff --git a/tests/UnitTests/ResourceTests/Registered/RegisteredResourceTest.php b/tests/UnitTests/ResourceTests/Registered/RegisteredResourceTest.php index 584ab67f..41143605 100644 --- a/tests/UnitTests/ResourceTests/Registered/RegisteredResourceTest.php +++ b/tests/UnitTests/ResourceTests/Registered/RegisteredResourceTest.php @@ -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; + } + } diff --git a/tests/UnitTests/ResourceTests/ResourcePlugins/PHPunitplugins/resource.db.php b/tests/UnitTests/ResourceTests/ResourcePlugins/PHPunitplugins/resource.db.php index 3b066db1..c297f5cd 100644 --- a/tests/UnitTests/ResourceTests/ResourcePlugins/PHPunitplugins/resource.db.php +++ b/tests/UnitTests/ResourceTests/ResourcePlugins/PHPunitplugins/resource.db.php @@ -1,4 +1,5 @@ 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}'; + } }