mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
调整template相关test case
This commit is contained in:
@@ -12,6 +12,7 @@
|
|||||||
namespace think;
|
namespace think;
|
||||||
|
|
||||||
use think\exception\TemplateNotFoundException;
|
use think\exception\TemplateNotFoundException;
|
||||||
|
use think\template\TagLib;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ThinkPHP分离出来的模板引擎
|
* ThinkPHP分离出来的模板引擎
|
||||||
@@ -121,7 +122,7 @@ class Template
|
|||||||
* 模板引擎配置项
|
* 模板引擎配置项
|
||||||
* @access public
|
* @access public
|
||||||
* @param array|string $config
|
* @param array|string $config
|
||||||
* @return void|array
|
* @return string|void|array
|
||||||
*/
|
*/
|
||||||
public function config($config)
|
public function config($config)
|
||||||
{
|
{
|
||||||
@@ -235,7 +236,7 @@ class Template
|
|||||||
* @access public
|
* @access public
|
||||||
* @param mixed $name 布局模板名称 false 则关闭布局
|
* @param mixed $name 布局模板名称 false 则关闭布局
|
||||||
* @param string $replace 布局模板内容替换标识
|
* @param string $replace 布局模板内容替换标识
|
||||||
* @return object
|
* @return Template
|
||||||
*/
|
*/
|
||||||
public function layout($name, $replace = '')
|
public function layout($name, $replace = '')
|
||||||
{
|
{
|
||||||
@@ -689,6 +690,7 @@ class Template
|
|||||||
} else {
|
} else {
|
||||||
$className = '\\think\\template\\taglib\\' . ucwords($tagLib);
|
$className = '\\think\\template\\taglib\\' . ucwords($tagLib);
|
||||||
}
|
}
|
||||||
|
/** @var Taglib $tLib */
|
||||||
$tLib = new $className($this);
|
$tLib = new $className($this);
|
||||||
$tLib->parseTag($content, $hide ? '' : $tagLib);
|
$tLib->parseTag($content, $hide ? '' : $tagLib);
|
||||||
return;
|
return;
|
||||||
@@ -1071,7 +1073,7 @@ class Template
|
|||||||
} else {
|
} else {
|
||||||
$path = isset($module) ? APP_PATH . $module . DS . basename($this->config['view_path']) . DS : $this->config['view_path'];
|
$path = isset($module) ? APP_PATH . $module . DS . basename($this->config['view_path']) . DS : $this->config['view_path'];
|
||||||
}
|
}
|
||||||
$template = $path . $template . '.' . ltrim($this->config['view_suffix'], '.');
|
$template = realpath($path . $template . '.' . ltrim($this->config['view_suffix'], '.'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is_file($template)) {
|
if (is_file($template)) {
|
||||||
|
|||||||
1
tests/application/views/display.phtml
Normal file
1
tests/application/views/display.phtml
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{$name??'default'}
|
||||||
@@ -40,13 +40,13 @@ class Foo extends Controller
|
|||||||
|
|
||||||
public function fetchTest()
|
public function fetchTest()
|
||||||
{
|
{
|
||||||
$template = dirname(__FILE__) . '/display.html';
|
$template = APP_PATH . 'views' . DS .'display.html';
|
||||||
return $this->fetch($template, ['name' => 'ThinkPHP']);
|
return $this->fetch($template, ['name' => 'ThinkPHP']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function displayTest()
|
public function displayTest()
|
||||||
{
|
{
|
||||||
$template = dirname(__FILE__) . '/display.html';
|
$template = APP_PATH . 'views' . DS .'display.html';
|
||||||
return $this->display($template, ['name' => 'ThinkPHP']);
|
return $this->display($template, ['name' => 'ThinkPHP']);
|
||||||
}
|
}
|
||||||
public function test()
|
public function test()
|
||||||
@@ -161,7 +161,7 @@ class controllerTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$controller = new Foo(Request::instance());
|
$controller = new Foo(Request::instance());
|
||||||
$view = $this->getView($controller);
|
$view = $this->getView($controller);
|
||||||
$template = dirname(__FILE__) . '/display.html';
|
$template = APP_PATH . 'views' . DS .'display.html';
|
||||||
$viewFetch = $view->fetch($template, ['name' => 'ThinkPHP']);
|
$viewFetch = $view->fetch($template, ['name' => 'ThinkPHP']);
|
||||||
$this->assertEquals($controller->fetchTest(), $viewFetch);
|
$this->assertEquals($controller->fetchTest(), $viewFetch);
|
||||||
}
|
}
|
||||||
@@ -170,7 +170,7 @@ class controllerTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$controller = new Foo;
|
$controller = new Foo;
|
||||||
$view = $this->getView($controller);
|
$view = $this->getView($controller);
|
||||||
$template = dirname(__FILE__) . '/display.html';
|
$template = APP_PATH . 'views' . DS .'display.html';
|
||||||
$viewFetch = $view->display($template, ['name' => 'ThinkPHP']);
|
$viewFetch = $view->display($template, ['name' => 'ThinkPHP']);
|
||||||
|
|
||||||
$this->assertEquals($controller->displayTest(), $viewFetch);
|
$this->assertEquals($controller->displayTest(), $viewFetch);
|
||||||
|
|||||||
@@ -16,331 +16,160 @@
|
|||||||
|
|
||||||
namespace tests\thinkphp\library\think;
|
namespace tests\thinkphp\library\think;
|
||||||
|
|
||||||
|
use think\Cache;
|
||||||
use think\Template;
|
use think\Template;
|
||||||
|
|
||||||
class templateTest extends \PHPUnit_Framework_TestCase
|
class templateTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
public function testVar()
|
/**
|
||||||
|
* @var Template
|
||||||
|
*/
|
||||||
|
protected $template;
|
||||||
|
|
||||||
|
public function setUp()
|
||||||
{
|
{
|
||||||
$template = new Template();
|
$this->template = new Template();
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a.b}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo \$name['a']['b']; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a??'test'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo isset(\$name['a'])?\$name['a']:'test'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a?='test'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php if(!empty(\$name['a'])) echo 'test'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a?:'test'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo !empty(\$name['a'])?\$name['a']:'test'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a?\$name.b:'no'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo !empty(\$name['a'])?\$name['b']:'no'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a==\$name.b?='test'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php if(\$name['a']==\$name['b']) echo 'test'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a==\$name.b?'a':'b'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo \$name['a']==\$name['b']?'a':'b'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a|default='test'==\$name.b?'a':'b'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo (isset(\$name['a']) && (\$name['a'] !== '')?\$name['a']:'test')==\$name['b']?'a':'b'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name.a|trim==\$name.b?='eq'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php if(trim(\$name['a'])==\$name['b']) echo 'eq'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{:ltrim(rtrim(\$name.a))}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo ltrim(rtrim(\$name['a'])); ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{~echo(trim(\$name.a))}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo(trim(\$name['a'])); ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{++\$name.a}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo ++\$name['a']; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{/*\$name*/}
|
|
||||||
EOF;
|
|
||||||
$data = '';
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$0a}
|
|
||||||
EOF;
|
|
||||||
$data = '{$0a}';
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testVarFunction()
|
public function testAssign()
|
||||||
{
|
{
|
||||||
$template = new Template();
|
$reflectProperty = new \ReflectionProperty(get_class($this->template), 'data');
|
||||||
|
$reflectProperty->setAccessible(true);
|
||||||
|
|
||||||
$content = <<<EOF
|
$this->template->assign('version', 'ThinkPHP3.2');
|
||||||
{\$name.a.b|default='test'}
|
$data = $reflectProperty->getValue($this->template);
|
||||||
EOF;
|
$this->assertEquals('ThinkPHP3.2', $data['version']);
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo (isset(\$name['a']['b']) && (\$name['a']['b'] !== '')?\$name['a']['b']:'test'); ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
$this->template->assign(['name' => 'Gao', 'version' => 'ThinkPHP5']);
|
||||||
$this->assertEquals($data, $content);
|
$data = $reflectProperty->getValue($this->template);
|
||||||
|
$this->assertEquals('Gao', $data['name']);
|
||||||
$content = <<<EOF
|
$this->assertEquals('ThinkPHP5', $data['version']);
|
||||||
{\$create_time|date="y-m-d",###}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo date("y-m-d",\$create_time); ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$name}
|
|
||||||
{\$name|trim|substr=0,3}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo \$name; ?>
|
|
||||||
<?php echo substr(trim(\$name),0,3); ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testVarIdentify()
|
public function testGet()
|
||||||
{
|
{
|
||||||
$config['tpl_begin'] = '<#';
|
$this->template = new Template();
|
||||||
$config['tpl_end'] = '#>';
|
$data = [
|
||||||
$config['tpl_var_identify'] = '';
|
'project' => 'ThinkPHP',
|
||||||
$template = new Template($config);
|
'version' => [
|
||||||
|
'ThinkPHP5' => ['Think5.0', 'Think5.1']
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$this->template->assign($data);
|
||||||
|
|
||||||
$content = <<<EOF
|
$this->assertSame($data, $this->template->get());
|
||||||
<#\$info.a??'test'#>
|
$this->assertSame('ThinkPHP', $this->template->get('project'));
|
||||||
EOF;
|
$this->assertSame(['Think5.0', 'Think5.1'], $this->template->get('version.ThinkPHP5'));
|
||||||
$data = <<<EOF
|
$this->assertNull($this->template->get('version.ThinkPHP3.2'));
|
||||||
<?php echo ((is_array(\$info)?\$info['a']:\$info->a)) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
<#\$info.a?='test'#>
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php if((is_array(\$info)?\$info['a']:\$info->a)) echo 'test'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
<#\$info.a==\$info.b?='test'#>
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php if((is_array(\$info)?\$info['a']:\$info->a)==(is_array(\$info)?\$info['b']:\$info->b)) echo 'test'; ?>
|
|
||||||
EOF;
|
|
||||||
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$content = <<<EOF
|
|
||||||
<#\$info.a|default='test'?'yes':'no'#>
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo ((is_array(\$info)?\$info['a']:\$info->a) ?: 'test')?'yes':'no'; ?>
|
|
||||||
EOF;
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
|
|
||||||
$template2 = new Template();
|
|
||||||
$template2->tpl_var_identify = 'obj';
|
|
||||||
$content = <<<EOF
|
|
||||||
{\$info2.b|trim?'yes':'no'}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo trim(\$info2->b)?'yes':'no'; ?>
|
|
||||||
EOF;
|
|
||||||
$template2->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testThinkVar()
|
/**
|
||||||
|
* @dataProvider provideTestParseWithVar
|
||||||
|
*/
|
||||||
|
public function testParseWithVar($content, $expected)
|
||||||
|
{
|
||||||
|
$this->template = new Template();
|
||||||
|
|
||||||
|
$this->template->parse($content);
|
||||||
|
$this->assertEquals($expected, $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideTestParseWithVarFunction
|
||||||
|
*/
|
||||||
|
public function testParseWithVarFunction($content, $expected)
|
||||||
|
{
|
||||||
|
$this->template = new Template();
|
||||||
|
|
||||||
|
$this->template->parse($content);
|
||||||
|
$this->assertEquals($expected, $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideTestParseWithVarIdentify
|
||||||
|
*/
|
||||||
|
public function testParseWithVarIdentify($content, $expected, $config)
|
||||||
|
{
|
||||||
|
$this->template = new Template($config);
|
||||||
|
|
||||||
|
$this->template->parse($content);
|
||||||
|
$this->assertEquals($expected, $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideTestParseWithThinkVar
|
||||||
|
*/
|
||||||
|
public function testParseWithThinkVar($content, $expected)
|
||||||
{
|
{
|
||||||
$config['tpl_begin'] = '{';
|
$config['tpl_begin'] = '{';
|
||||||
$config['tpl_end'] = '}';
|
$config['tpl_end'] = '}';
|
||||||
$template = new Template($config);
|
$this->template = new Template($config);
|
||||||
|
|
||||||
$_SERVER['SERVER_NAME'] = 'server_name';
|
$_SERVER['SERVER_NAME'] = 'server_name';
|
||||||
$_GET['action'] = 'action';
|
$_GET['action'] = 'action';
|
||||||
$_POST['action'] = 'action';
|
$_POST['action'] = 'action';
|
||||||
$_COOKIE['name'] = 'name';
|
$_COOKIE['name'] = 'name';
|
||||||
$_SESSION['action'] = ['name' => 'name'];
|
$_SESSION['action'] = ['name' => 'name'];
|
||||||
define('SITE_NAME', 'site_name');
|
|
||||||
|
|
||||||
$content = <<<EOF
|
$this->template->parse($content);
|
||||||
{\$Think.SERVER.SERVER_NAME}<br/>
|
$this->assertEquals($expected, $content);
|
||||||
{\$Think.GET.action}<br/>
|
|
||||||
{\$Think.POST.action}<br/>
|
|
||||||
{\$Think.COOKIE.action}<br/>
|
|
||||||
{\$Think.COOKIE.action.name}<br/>
|
|
||||||
{\$Think.SESSION.action}<br/>
|
|
||||||
{\$Think.SESSION.action.name}<br/>
|
|
||||||
{\$Think.ENV.OS}<br/>
|
|
||||||
{\$Think.REQUEST.action}<br/>
|
|
||||||
{\$Think.CONST.SITE_NAME}<br/>
|
|
||||||
{\$Think.LANG.action}<br/>
|
|
||||||
{\$Think.CONFIG.action.name}<br/>
|
|
||||||
{\$Think.NOW}<br/>
|
|
||||||
{\$Think.VERSION}<br/>
|
|
||||||
{\$Think.LDELIM}<br/>
|
|
||||||
{\$Think.RDELIM}<br/>
|
|
||||||
{\$Think.SITE_NAME}<br/>
|
|
||||||
{\$Think.SITE.URL}
|
|
||||||
EOF;
|
|
||||||
$data = <<<EOF
|
|
||||||
<?php echo \\think\\Request::instance()->server('SERVER_NAME'); ?><br/>
|
|
||||||
<?php echo \\think\\Request::instance()->get('action'); ?><br/>
|
|
||||||
<?php echo \\think\\Request::instance()->post('action'); ?><br/>
|
|
||||||
<?php echo \\think\\Cookie::get('action'); ?><br/>
|
|
||||||
<?php echo \\think\\Cookie::get('action.name'); ?><br/>
|
|
||||||
<?php echo \\think\\Session::get('action'); ?><br/>
|
|
||||||
<?php echo \\think\\Session::get('action.name'); ?><br/>
|
|
||||||
<?php echo \\think\\Request::instance()->env('OS'); ?><br/>
|
|
||||||
<?php echo \\think\\Request::instance()->request('action'); ?><br/>
|
|
||||||
<?php echo SITE_NAME; ?><br/>
|
|
||||||
<?php echo \\think\\Lang::get('action'); ?><br/>
|
|
||||||
<?php echo \\think\\Config::get('action.name'); ?><br/>
|
|
||||||
<?php echo date('Y-m-d g:i a',time()); ?><br/>
|
|
||||||
<?php echo THINK_VERSION; ?><br/>
|
|
||||||
<?php echo '{'; ?><br/>
|
|
||||||
<?php echo '}'; ?><br/>
|
|
||||||
<?php echo SITE_NAME; ?><br/>
|
|
||||||
<?php echo ''; ?>
|
|
||||||
EOF;
|
|
||||||
$template->parse($content);
|
|
||||||
$this->assertEquals($data, $content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testFetch()
|
/**
|
||||||
|
* @expectedException \think\exception\TemplateNotFoundException
|
||||||
|
*/
|
||||||
|
public function testFetchWithEmptyTemplate()
|
||||||
{
|
{
|
||||||
$template = new Template();
|
$this->template = new Template();
|
||||||
$template->assign('name', 'name');
|
|
||||||
|
$this->template->fetch('Foo');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideTestFetchWithNoCache
|
||||||
|
*/
|
||||||
|
public function testFetchWithNoCache($data, $expected)
|
||||||
|
{
|
||||||
|
$this->template = new Template();
|
||||||
|
|
||||||
|
$this->template->fetch($data['template'], $data['vars'], $data['config']);
|
||||||
|
|
||||||
|
$this->expectOutputString($expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testFetchWithCache()
|
||||||
|
{
|
||||||
|
$this->template = new Template();
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'name' => 'value'
|
||||||
|
];
|
||||||
$config = [
|
$config = [
|
||||||
'strip_space' => true,
|
'cache_id' => 'TEST_FETCH_WITH_CACHE',
|
||||||
'view_path' => dirname(__FILE__) . DS,
|
|
||||||
'cache_id' => '__CACHE_ID__',
|
|
||||||
'display_cache' => true,
|
'display_cache' => true,
|
||||||
];
|
];
|
||||||
$data = ['name' => 'value'];
|
|
||||||
$template->layout('layout')->fetch('display', $data, $config);
|
$this->template->fetch(APP_PATH . 'views' . DS .'display.html', $data, $config);
|
||||||
|
|
||||||
$this->expectOutputString('value');
|
$this->expectOutputString('value');
|
||||||
|
$this->assertEquals('value', Cache::get($config['cache_id']));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDisplay()
|
public function testDisplay()
|
||||||
{
|
{
|
||||||
$config['view_path'] = dirname(__FILE__) . DS;
|
$config = [
|
||||||
$config['view_suffix'] = '.html';
|
'view_path' => APP_PATH . DS . 'views' . DS,
|
||||||
$config['layout_on'] = true;
|
'view_suffix' => '.html',
|
||||||
$config['layout_name'] = 'layout';
|
'layout_on' => true,
|
||||||
$template = new Template($config);
|
'layout_name' => 'layout'
|
||||||
$files = ['extend' => 'extend', 'include' => 'include'];
|
];
|
||||||
$template->assign('files', $files);
|
|
||||||
$template->assign('user', ['name' => 'name', 'account' => 100]);
|
$this->template = new Template($config);
|
||||||
$template->assign('message', 'message');
|
|
||||||
$template->assign('info', ['value' => 'value']);
|
$this->template->assign('files', ['extend' => 'extend', 'include' => 'include']);
|
||||||
|
$this->template->assign('user', ['name' => 'name', 'account' => 100]);
|
||||||
|
$this->template->assign('message', 'message');
|
||||||
|
$this->template->assign('info', ['value' => 'value']);
|
||||||
|
|
||||||
$content = <<<EOF
|
$content = <<<EOF
|
||||||
{extend name="\$files.extend" /}
|
{extend name="\$files.extend" /}
|
||||||
@@ -356,7 +185,7 @@ main
|
|||||||
{/block}
|
{/block}
|
||||||
{/block}
|
{/block}
|
||||||
EOF;
|
EOF;
|
||||||
$content2 = <<<EOF
|
$expected = <<<EOF
|
||||||
<nav>
|
<nav>
|
||||||
header
|
header
|
||||||
<div id="wrap">
|
<div id="wrap">
|
||||||
@@ -382,33 +211,205 @@ value:
|
|||||||
php code</div>
|
php code</div>
|
||||||
</nav>
|
</nav>
|
||||||
EOF;
|
EOF;
|
||||||
$template->display($content);
|
$this->template->display($content);
|
||||||
$this->expectOutputString($content2);
|
$this->expectOutputString($expected);
|
||||||
// $template->parse($content);
|
|
||||||
// var_dump($content);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testVarAssign()
|
/**
|
||||||
|
* @dataProvider provideTestLayout
|
||||||
|
*/
|
||||||
|
public function testLayout($data, $expected)
|
||||||
{
|
{
|
||||||
$template = new Template();
|
$this->template = new Template();
|
||||||
$template->assign('name', 'value');
|
|
||||||
$value = $template->get('name');
|
$this->template->layout($data['name'], $data['replace']);
|
||||||
$this->assertEquals('value', $value);
|
|
||||||
|
$this->assertSame($expected['layout_on'], $this->template->config('layout_on'));
|
||||||
|
$this->assertSame($expected['layout_name'], $this->template->config('layout_name'));
|
||||||
|
$this->assertSame($expected['layout_item'], $this->template->config('layout_item'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testVarGet()
|
public function testParseAttr()
|
||||||
{
|
{
|
||||||
$template = new Template();
|
$attributes = $this->template->parseAttr("<name version='ThinkPHP' name=\"Gao\"></name>");
|
||||||
$data = ['a' => 'a', 'b' => 'b'];
|
$this->assertSame(['version' => 'ThinkPHP', 'name' => 'Gao'], $attributes);
|
||||||
$template->assign($data);
|
|
||||||
$this->assertEquals($data, $template->get());
|
$attributes = $this->template->parseAttr("<name version='ThinkPHP' name=\"Gao\">TestCase</name>", 'version');
|
||||||
|
$this->assertSame('ThinkPHP', $attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testIsCache()
|
public function testIsCache()
|
||||||
{
|
{
|
||||||
$template = new Template(['cache_id' => '__CACHE_ID__', 'display_cache' => true]);
|
$this->template = new Template();
|
||||||
$this->assertTrue($template->isCache('__CACHE_ID__'));
|
$config = [
|
||||||
$template->display_cache = false;
|
'cache_id' => rand(0, 10000) . rand(0, 10000) . time(),
|
||||||
$this->assertTrue(!$template->isCache('__CACHE_ID__'));
|
'display_cache' => true
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertFalse($this->template->isCache($config['cache_id']));
|
||||||
|
|
||||||
|
$this->template->fetch(APP_PATH . 'views' . DS .'display.html', [], $config);
|
||||||
|
$this->assertTrue($this->template->isCache($config['cache_id']));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideTestParseWithVar()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
["{\$name.a.b}", "<?php echo \$name['a']['b']; ?>"],
|
||||||
|
["{\$name.a??'test'}", "<?php echo isset(\$name['a'])?\$name['a']:'test'; ?>"],
|
||||||
|
["{\$name.a?='test'}", "<?php if(!empty(\$name['a'])) echo 'test'; ?>"],
|
||||||
|
["{\$name.a?:'test'}", "<?php echo !empty(\$name['a'])?\$name['a']:'test'; ?>"],
|
||||||
|
["{\$name.a?\$name.b:'no'}", "<?php echo !empty(\$name['a'])?\$name['b']:'no'; ?>"],
|
||||||
|
["{\$name.a==\$name.b?='test'}", "<?php if(\$name['a']==\$name['b']) echo 'test'; ?>"],
|
||||||
|
["{\$name.a==\$name.b?'a':'b'}", "<?php echo \$name['a']==\$name['b']?'a':'b'; ?>"],
|
||||||
|
["{\$name.a|default='test'==\$name.b?'a':'b'}", "<?php echo (isset(\$name['a']) && (\$name['a'] !== '')?\$name['a']:'test')==\$name['b']?'a':'b'; ?>"],
|
||||||
|
["{\$name.a|trim==\$name.b?='eq'}", "<?php if(trim(\$name['a'])==\$name['b']) echo 'eq'; ?>"],
|
||||||
|
["{:ltrim(rtrim(\$name.a))}", "<?php echo ltrim(rtrim(\$name['a'])); ?>"],
|
||||||
|
["{~echo(trim(\$name.a))}", "<?php echo(trim(\$name['a'])); ?>"],
|
||||||
|
["{++\$name.a}", "<?php echo ++\$name['a']; ?>"],
|
||||||
|
["{/*\$name*/}", ""],
|
||||||
|
["{\$0a}", "{\$0a}"]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideTestParseWithVarFunction()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
["{\$name.a.b|default='test'}", "<?php echo (isset(\$name['a']['b']) && (\$name['a']['b'] !== '')?\$name['a']['b']:'test'); ?>"],
|
||||||
|
["{\$create_time|date=\"y-m-d\",###}", "<?php echo date(\"y-m-d\",\$create_time); ?>"],
|
||||||
|
["{\$name}\n{\$name|trim|substr=0,3}", "<?php echo \$name; ?>\n<?php echo substr(trim(\$name),0,3); ?>"]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideTestParseWithVarIdentify()
|
||||||
|
{
|
||||||
|
$config['tpl_begin'] = '<#';
|
||||||
|
$config['tpl_end'] = '#>';
|
||||||
|
$config['tpl_var_identify'] = '';
|
||||||
|
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
"<#\$info.a??'test'#>",
|
||||||
|
"<?php echo ((is_array(\$info)?\$info['a']:\$info->a)) ? (is_array(\$info)?\$info['a']:\$info->a) : 'test'; ?>",
|
||||||
|
$config
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"<#\$info.a?='test'#>",
|
||||||
|
"<?php if((is_array(\$info)?\$info['a']:\$info->a)) echo 'test'; ?>",
|
||||||
|
$config
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"<#\$info.a==\$info.b?='test'#>",
|
||||||
|
"<?php if((is_array(\$info)?\$info['a']:\$info->a)==(is_array(\$info)?\$info['b']:\$info->b)) echo 'test'; ?>",
|
||||||
|
$config
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"<#\$info.a|default='test'?'yes':'no'#>",
|
||||||
|
"<?php echo ((is_array(\$info)?\$info['a']:\$info->a) ?: 'test')?'yes':'no'; ?>",
|
||||||
|
$config
|
||||||
|
],
|
||||||
|
[
|
||||||
|
"{\$info2.b|trim?'yes':'no'}",
|
||||||
|
"<?php echo trim(\$info2->b)?'yes':'no'; ?>",
|
||||||
|
array_merge(['tpl_var_identify' => 'obj'])
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideTestParseWithThinkVar()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
["{\$Think.SERVER.SERVER_NAME}<br/>", "<?php echo \\think\\Request::instance()->server('SERVER_NAME'); ?><br/>"],
|
||||||
|
["{\$Think.GET.action}<br/>", "<?php echo \\think\\Request::instance()->get('action'); ?><br/>"],
|
||||||
|
["{\$Think.POST.action}<br/>", "<?php echo \\think\\Request::instance()->post('action'); ?><br/>"],
|
||||||
|
["{\$Think.COOKIE.action}<br/>", "<?php echo \\think\\Cookie::get('action'); ?><br/>"],
|
||||||
|
["{\$Think.COOKIE.action.name}<br/>", "<?php echo \\think\\Cookie::get('action.name'); ?><br/>"],
|
||||||
|
["{\$Think.SESSION.action}<br/>", "<?php echo \\think\\Session::get('action'); ?><br/>"],
|
||||||
|
["{\$Think.SESSION.action.name}<br/>", "<?php echo \\think\\Session::get('action.name'); ?><br/>"],
|
||||||
|
["{\$Think.ENV.OS}<br/>", "<?php echo \\think\\Request::instance()->env('OS'); ?><br/>"],
|
||||||
|
["{\$Think.REQUEST.action}<br/>", "<?php echo \\think\\Request::instance()->request('action'); ?><br/>"],
|
||||||
|
["{\$Think.CONST.THINK_VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
|
||||||
|
["{\$Think.LANG.action}<br/>", "<?php echo \\think\\Lang::get('action'); ?><br/>"],
|
||||||
|
["{\$Think.CONFIG.action.name}<br/>", "<?php echo \\think\\Config::get('action.name'); ?><br/>"],
|
||||||
|
["{\$Think.NOW}<br/>", "<?php echo date('Y-m-d g:i a',time()); ?><br/>"],
|
||||||
|
["{\$Think.VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
|
||||||
|
["{\$Think.LDELIM}<br/>", "<?php echo '{'; ?><br/>"],
|
||||||
|
["{\$Think.RDELIM}<br/>", "<?php echo '}'; ?><br/>"],
|
||||||
|
["{\$Think.THINK_VERSION}<br/>", "<?php echo THINK_VERSION; ?><br/>"],
|
||||||
|
["{\$Think.SITE.URL}", "<?php echo ''; ?>"]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideTestFetchWithNoCache()
|
||||||
|
{
|
||||||
|
$provideData = [];
|
||||||
|
|
||||||
|
$this->template = [
|
||||||
|
'template' => APP_PATH . 'views' . DS .'display.html',
|
||||||
|
'vars' => [],
|
||||||
|
'config' => []
|
||||||
|
];
|
||||||
|
$expected = 'default';
|
||||||
|
$provideData[] = [$this->template, $expected];
|
||||||
|
|
||||||
|
$this->template = [
|
||||||
|
'template' => APP_PATH . 'views' . DS .'display.html',
|
||||||
|
'vars' => ['name' => 'ThinkPHP5'],
|
||||||
|
'config' => []
|
||||||
|
];
|
||||||
|
$expected = 'ThinkPHP5';
|
||||||
|
$provideData[] = [$this->template, $expected];
|
||||||
|
|
||||||
|
$this->template = [
|
||||||
|
'template' => 'views@display',
|
||||||
|
'vars' => [],
|
||||||
|
'config' => [
|
||||||
|
'view_suffix' => 'html'
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$expected = 'default';
|
||||||
|
$provideData[] = [$this->template, $expected];
|
||||||
|
|
||||||
|
$this->template = [
|
||||||
|
'template' => 'views@/display',
|
||||||
|
'vars' => ['name' => 'ThinkPHP5'],
|
||||||
|
'config' => [
|
||||||
|
'view_suffix' => 'phtml'
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$expected = 'ThinkPHP5';
|
||||||
|
$provideData[] = [$this->template, $expected];
|
||||||
|
|
||||||
|
$this->template = [
|
||||||
|
'template' => 'display',
|
||||||
|
'vars' => ['name' => 'ThinkPHP5'],
|
||||||
|
'config' => [
|
||||||
|
'view_suffix' => 'html',
|
||||||
|
'view_base' => APP_PATH . 'views' . DS
|
||||||
|
]
|
||||||
|
];
|
||||||
|
$expected = 'ThinkPHP5';
|
||||||
|
$provideData[] = [$this->template, $expected];
|
||||||
|
|
||||||
|
return $provideData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideTestLayout()
|
||||||
|
{
|
||||||
|
$provideData = [];
|
||||||
|
|
||||||
|
$data = ['name' => false, 'replace' => ''];
|
||||||
|
$expected = ['layout_on' => false, 'layout_name' => 'layout', 'layout_item' => '{__CONTENT__}'];
|
||||||
|
$provideData[] = [$data, $expected];
|
||||||
|
|
||||||
|
$data = ['name' => null, 'replace' => ''];
|
||||||
|
$expected = ['layout_on' => true, 'layout_name' => 'layout', 'layout_item' => '{__CONTENT__}'];
|
||||||
|
$provideData[] = [$data, $expected];
|
||||||
|
|
||||||
|
$data = ['name' => 'ThinkName', 'replace' => 'ThinkReplace'];
|
||||||
|
$expected = ['layout_on' => true, 'layout_name' => 'ThinkName', 'layout_item' => 'ThinkReplace'];
|
||||||
|
$provideData[] = [$data, $expected];
|
||||||
|
|
||||||
|
return $provideData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user