add tests for issuse 312

This commit is contained in:
uwetews
2016-11-11 20:10:23 +01:00
parent 6c5e729c9c
commit c0110c1051
7 changed files with 206 additions and 21 deletions

View File

@@ -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',

View File

@@ -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.

View File

@@ -0,0 +1,163 @@
<?php
//require_once '../../../../../vendor/autoload.php';
class Base
{
const TEMPLATE='034_parent.tpl';
public $id=null;
public function __construct($id)
{
$this->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"
],
],
];
}
}

View File

View File

@@ -0,0 +1 @@
{extends '034_parent.tpl'}{block name=elementContent}{$e->getText() nocache}{/block}

View File

@@ -0,0 +1 @@
{$e->id nocache} {block name=elementContent}{$e->getHTML() nocache}{/block}