单元测试智能模板



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

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

待测试的模板

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

预期输出

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

测试用例类

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);
    }
}

最新更新