From c0110c1051c1c4c370631130b4ea3430231172b6 Mon Sep 17 00:00:00 2001 From: uwetews Date: Fri, 11 Nov 2016 20:10:23 +0100 Subject: [PATCH] add tests for issuse 312 --- tests/Bootstrap.php | 21 +-- tests/PHPUnit_Smarty.php | 41 ++++- .../X_Issues/Issuse312/InheritanceTest.php | 163 ++++++++++++++++++ .../X_Issues/Issuse312/cache/dummy.txt | 0 .../Issuse312/templates/034_child.tpl | 1 + .../Issuse312/templates/034_parent.tpl | 1 + .../X_Issues/Issuse312/templates_c/dummy.txt | 0 7 files changed, 206 insertions(+), 21 deletions(-) create mode 100644 tests/UnitTests/X_Issues/Issuse312/InheritanceTest.php create mode 100644 tests/UnitTests/X_Issues/Issuse312/cache/dummy.txt create mode 100644 tests/UnitTests/X_Issues/Issuse312/templates/034_child.tpl create mode 100644 tests/UnitTests/X_Issues/Issuse312/templates/034_parent.tpl create mode 100644 tests/UnitTests/X_Issues/Issuse312/templates_c/dummy.txt diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 56411c5d..c407fe0b 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -7,19 +7,14 @@ * Smarty PHPUnit Bootstrap */ include_once dirname(__FILE__) . '/Config.php'; -// Locate Autoloader or SmartyBC class and load it -if (is_file(dirname(__FILE__) . '/../smarty/libs/Autoloader.php')) { - require_once dirname(__FILE__) . '/../smarty/libs/Autoloader.php'; - Smarty_Autoloader::register(true); -} elseif (is_file(dirname(__FILE__) . '/../libs/Autoloader.php')) { - require_once dirname(__FILE__) . '/../libs/Autoloader.php'; - Smarty_Autoloader::register(true); -} elseif (is_file(dirname(__FILE__) . '/../smarty/libs/SmartyBC.class.php')) { - require_once dirname(__FILE__) . '/../smarty/libs/SmartyBC.class.php'; -} elseif (is_file(dirname(__FILE__) . '/../libs/SmartyBC.class.php')) { - require_once dirname(__FILE__) . '/../libs/SmartyBC.class.php'; -} else { - throw new Exception('can not locate Smarty distribution'); +if (!class_exists('Smarty_Autoloader')) { + if (is_file(dirname(__FILE__) . '/../smarty/libs/bootstrap.php')) { + require_once dirname(__FILE__) . '/../smarty/libs/bootstrap.php'; + } elseif (is_file(dirname(__FILE__) . '/../libs/bootstrap.php')) { + require_once dirname(__FILE__) . '/../libs/bootstrap.php'; + } else { + throw new Exception('can not locate Smarty distribution'); + } } if (!defined('SMARTY_COMPOSER_INSTALL')) { foreach (array(dirname(__FILE__) . '/../../autoload.php', dirname(__FILE__) . '/../vendor/autoload.php', diff --git a/tests/PHPUnit_Smarty.php b/tests/PHPUnit_Smarty.php index 20982734..f850b954 100644 --- a/tests/PHPUnit_Smarty.php +++ b/tests/PHPUnit_Smarty.php @@ -72,13 +72,15 @@ class PHPUnit_Smarty extends PHPUnit_Framework_TestCase */ public static $pdo = null; + public static $pluginsdir = null; + /** * Default blacklist * * @var array */ protected $backupStaticAttributesBlacklist = array('PHPUnit_Smarty' => array('config', 'pdo', 'init', - 'testNumver'),); + 'testNumver', 'pluginsdir'),); /** * This method is called before the first test of this test class is run. @@ -88,6 +90,7 @@ class PHPUnit_Smarty extends PHPUnit_Framework_TestCase { error_reporting(E_ALL | E_STRICT); self::$init = true; + self::$pluginsdir =self::getSmartyPluginsDir(); } /** @@ -113,7 +116,7 @@ class PHPUnit_Smarty extends PHPUnit_Framework_TestCase define('individualFolders', true); } parent::__construct($name, $data, $dataName); - $this->backupStaticAttributesBlacklist[ get_class($this) ] = array('init', 'config', 'pdo', 'testNumber'); + $this->backupStaticAttributesBlacklist[ get_class($this) ] = array('init', 'config', 'pdo', 'testNumber'); } /** @@ -437,17 +440,32 @@ KEY `expire` (`expire`) * * @return string */ - public function normalizePath($path) + public function normalizePath($path, $ds =null ,$absolute = true) { - if ($path[ 0 ] == '.') { - $path = getcwd() . DIRECTORY_SEPARATOR . $path; + $ds = isset($ds) ? $ds : DIRECTORY_SEPARATOR; + $nds = $ds == '/' ? '\\' : '/'; + $getcwd = getcwd(); + // normalize $ds + $path = str_replace($nds, $ds, $path); + preg_match('#^([a-zA-Z][:])?([.]{1,2}[\/\\\]+)?([\\\])?([.]{0,2}[\/\\\]+)?([[:print:]]*)#', $path, $match); + if ($match[1] === '') { + if ($match[ 2 ] !== '' || $match[ 2 ] . $match[ 3 ] . $match[ 4 ] === '') { + $path = $getcwd . $ds . $path; + } else if (Smarty::$_IS_WINDOWS && $match[ 3 ] !== '') { + $path = substr($getcwd, 0, 2) . $path; + } } - $path = preg_replace('#[\\\/]+([.][\\\/]+)*#', DIRECTORY_SEPARATOR, $path); - while (strrpos($path, '.' . DIRECTORY_SEPARATOR) !== false) { + $path = preg_replace('#[\\\/]+([.][\\\/]+)*#', $ds, $path); + while (strrpos($path, '.' . $ds) !== false) { $path = - preg_replace('#([\\\/]([^\\\/]+[\\\/]){2}([.][.][\\\/]){2})|([\\\/][^\\\/]+[\\\/][.][.][\\\/])#', DIRECTORY_SEPARATOR, + preg_replace('#([\\\/]([^\\\/]+[\\\/]){2}([.][.][\\\/]){2})|([\\\/][^\\\/]+[\\\/][.][.][\\\/])#', $ds, $path); } + $cwd = preg_replace('#[\\\/]#', $ds, $getcwd); + $path = str_ireplace($cwd,$getcwd, $path); + if (!$absolute) { + $path = preg_replace('#'.$getcwd.'#', '', $path); + } return $path; } @@ -633,6 +651,13 @@ KEY `expire` (`expire`) return isset($this->smarty) ? $this->smarty : (isset($this->smartyBC) ? $this->smartyBC : null); } + public static function getSmartyPluginsDir(){ + if (is_dir(dirname(__FILE__) . '/../smarty/libs/plugins')) { + return dirname(__FILE__) . '/../smarty/libs/plugins'; + } else if(is_dir(dirname(__FILE__) . '/../libs/plugins')) { + return dirname(__FILE__) . '/../libs/plugins'; + } + } /** * Tears down the fixture * This method is called after a test is executed. diff --git a/tests/UnitTests/X_Issues/Issuse312/InheritanceTest.php b/tests/UnitTests/X_Issues/Issuse312/InheritanceTest.php new file mode 100644 index 00000000..4a4abf59 --- /dev/null +++ b/tests/UnitTests/X_Issues/Issuse312/InheritanceTest.php @@ -0,0 +1,163 @@ +id=$id; + } + + public function getHTML() + { + return static::class; + } +} + +class Child extends Base +{ + const TEMPLATE='034_child.tpl'; + + public function getText() + { + return $this->getHTML(); + } +} + +class BaseClone extends Base +{ + +} + +class InheritanceTest extends PHPUnit_Framework_TestCase +{ + protected $smarty; + + protected function setUp() + { + chdir(dirname(__FILE__)); + $this->smarty=new Smarty(); + $this->smarty->setTemplateDir('./templates'); + $this->smarty->setCompileDir('./templates_c'); + $this->smarty->setCacheDir('./cache'); + $this->smarty->caching=false; + } + + /** + * @dataProvider providerInheritance + */ + public function testInheritance($elements, $assertions) + { + foreach ($elements as $nr=> $element) + { + $this->smarty->assign('e', $element); + $this->assertSame($assertions[$nr], $this->smarty->fetch($element::TEMPLATE)); + } + } + /** + * @dataProvider providerInheritance + */ + public function testInheritanceCaching($elements, $assertions) + { + $this->smarty->caching = true; + foreach ($elements as $nr=> $element) + { + $this->smarty->assign('e', $element); + $this->assertSame($assertions[$nr], $this->smarty->fetch($element::TEMPLATE)); + } + } + /** + * @dataProvider providerInheritance + */ + public function testInheritanceCaching1($elements, $assertions) + { + $this->smarty->caching = true; + foreach ($elements as $nr=> $element) + { + $this->smarty->assign('e', $element); + $stat = $this->smarty->isCached($element::TEMPLATE); + $this->assertSame($assertions[$nr], $this->smarty->fetch($element::TEMPLATE)); + $this->assertTrue($stat); + } + } + + public function providerInheritance() + { + return [ + [ + //(#0) This test works as expected + [ + new Base(1), + new Base(2), + new BaseClone(3), + ], [ + '1 Base', + '2 Base', + '3 BaseClone', + ], + ], [ + //(#1) This test works as expected + [ + new Child(1), + new BaseClone(2), //This output is right(!) + ], [ + '1 Child', + '2 BaseClone', + ], + ], [ + //(#2) This test fails + [ + new Child(1), + new Child(2), + new BaseClone(3), + ], [ + '1 Child', + '2 Child', + '3 BaseClone', //Here the output is "2 Child" + ], + ], [ + //(#3) This test fails + [ + new Child(1), + new BaseClone(2), + new Child(3), + ], [ + '1 Child', + '2 BaseClone', + '3 Child', //Here the output is "2 Child" + ], + ], [ + //(#4) This test fails + [ + new Child(1), + new Child(2), + new BaseClone(3), + new Child(4), + ], [ + '1 Child', + '2 Child', + '3 BaseClone', //Here the output is also "2 Child" + '4 Child', + ], + ], [ + //(#5) This test fails + [ + new BaseClone(1), + new Child(2), + new Child(3), + new BaseClone(4), + ], [ + '1 BaseClone', //This output is right(!) + '2 Child', + '3 Child', + '4 BaseClone', //Here the output is "3 Child" + ], + ], + ]; + } +} diff --git a/tests/UnitTests/X_Issues/Issuse312/cache/dummy.txt b/tests/UnitTests/X_Issues/Issuse312/cache/dummy.txt new file mode 100644 index 00000000..e69de29b diff --git a/tests/UnitTests/X_Issues/Issuse312/templates/034_child.tpl b/tests/UnitTests/X_Issues/Issuse312/templates/034_child.tpl new file mode 100644 index 00000000..37608871 --- /dev/null +++ b/tests/UnitTests/X_Issues/Issuse312/templates/034_child.tpl @@ -0,0 +1 @@ +{extends '034_parent.tpl'}{block name=elementContent}{$e->getText() nocache}{/block} \ No newline at end of file diff --git a/tests/UnitTests/X_Issues/Issuse312/templates/034_parent.tpl b/tests/UnitTests/X_Issues/Issuse312/templates/034_parent.tpl new file mode 100644 index 00000000..390bf05c --- /dev/null +++ b/tests/UnitTests/X_Issues/Issuse312/templates/034_parent.tpl @@ -0,0 +1 @@ +{$e->id nocache} {block name=elementContent}{$e->getHTML() nocache}{/block} \ No newline at end of file diff --git a/tests/UnitTests/X_Issues/Issuse312/templates_c/dummy.txt b/tests/UnitTests/X_Issues/Issuse312/templates_c/dummy.txt new file mode 100644 index 00000000..e69de29b