【问题标题】:Unit Testing Smarty Templates单元测试 Smarty 模板
【发布时间】:2011-06-27 05:36:12
【问题描述】:

我使用 Smarty 模板,我只是想知道是否有任何类型的测试机制可供我使用。不同模板文件的数量正在增加,复杂性也在增加。理想情况下,我很想测试最终输出的 HTML,以确保 Smarty 中使用的模板/条件/变量按预期工作。有没有办法做到这一点?

【问题讨论】:

  • 这个问题不是专门针对 smarty 的,而是关于程序输出测试的。如果输出符合要求,则执行控制器操作和测试(使用您喜欢的任何工具)。

标签: php unit-testing testing smarty


【解决方案1】:

您可以使用 Smarty 的fetch() 功能。下面是一个松散的示例/伪代码。

要测试的模板

{* foo.tpl *}
<html>
    <head></head>
    <body>{$hi}</body>
</html>

预期输出

<!-- foo.html -->
<html>
    <head></head>
    <body>Hello World!</body>
</html>

TestCase 类

class FooTemplateTestCase extends TestCase {

    protected $_view;

    public function setup(){
        $this->_view = new Smarty();
        // setup smarty options, caching, etc
    }

    public function test(){
        $this->_view->assign('hi', 'Hello World!');

        $output = $this->_view->fetch('foo.tpl');
        $expected_output = file_get_contents('foo.html');

        $this->assertEquals($expected_output, $output);
    }

}

【讨论】:

    猜你喜欢
    • 2013-10-01
    • 2010-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2015-11-10
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多