Symfony 3.4:如何在树枝模板中模拟app.request

时间:2018-07-30 22:20:47

标签: request mocking twig phpunit symfony-3.4

假设我有以下方法

  public function prepareEmail($dataModel)
    {
        return $this->container->get('templating')->render(
            'MyBundle:Emails:email-document.html.twig',
             [
                 'data' => $dataModel,
             ]
        )
    }

我想测试树枝模板中存在的非常复杂的逻辑。

在我的phpunit测试工具包中,我没有嘲笑TwigEngine服务,因此我正在使用完全渲染的模板。

在测试中,我做这样的事情

public function testPrepareEmail()
{
    $template = $this->manager->prepareEmail($testData);

    static::assertContains('some part of template', $template);
}

一切看起来不错,但是我将以下字符串添加到了我的模板之一

{{ app.request.getSchemeAndHttpHost() }}

现在我出错了

Twig_Error_Runtime : Impossible to invoke a method ("getSchemeAndHttpHost") on a null variable.

这是非常合理的,问题是在我的情况下如何模拟Symfony Request对象?

0 个答案:

没有答案