单元测试 Smarty 模板

Spa*_*pta 4 php testing unit-testing smarty

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

Pau*_*lRe 5

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

待测试模板

{* foo.tpl *}
<html>
    <head></head>
    <body>{$hi}</body>
</html>
Run Code Online (Sandbox Code Playgroud)

预期输出

<!-- foo.html -->
<html>
    <head></head>
    <body>Hello World!</body>
</html>
Run Code Online (Sandbox Code Playgroud)

测试用例类

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

}
Run Code Online (Sandbox Code Playgroud)