【问题标题】:Is it possible to get the rendered view output from a Phalcon controller? From within a unit test?是否可以从 Phalcon 控制器获取渲染视图输出?从单元测试中?
【发布时间】:2020-09-03 20:17:21
【问题描述】:

我正在编写一个控制器测试,如下所示:

class DepositOrderTest extends AbstractUnitTest {
    public function testTestCase(): void {
        $deposit_controller = new DepositController();
        $deposit_controller->setDI($this->di);
        $result = $deposit_controller->indexAction();
        error_log($result);
    }
}

我希望$result 可能是 HTML 输出,但事实并非如此。如何获取此操作的 HTML 输出?有可能吗?

谢谢。

【问题讨论】:

    标签: php phalcon


    【解决方案1】:

    简而言之,您可以使用Phalcon\Mvc\View\Simple 来呈现没有层次结构的视图

    use Phalcon\Mvc\View\Simple;
    
    $view = new Simple();
    
    $view->setViewsDir('../app/views/');
    
    echo $view->render('templates/welcome');
    
    echo $view->render(
        'templates/welcome',
        [
            'email'   => $email,
            'content' => $content,
        ]
    );
    

    或者您可以查看documentation 了解其他/复杂选项

    【讨论】:

    • 我希望能够与控制器功能一起测试,这不可能吗?
    • $view->render('users/edit') 如果您使用的是.phtml 引擎并且您更改了所需的内容,这将返回视图部分views/users/edit.phtml。例如,您可以使用Phalcon\Mvc\View\Simple::registerEngines(array $engines) 注册引擎。我建议看一下文档中的 api,如果它不清楚 github 上的类源
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-29
    相关资源
    最近更新 更多