用 phpunit 测试树枝

testing twig with phpunit

我想用 Twig 和 PHPUnit(都安装了 composer,添加了 slim/views)实现了一些单元测试。但是当我尝试测试模板生成时,它会在 {{ baseUrl() }} 处阻塞。但是当我在没有 phpunit 的导航器上测试模板时,模板工作正常。

这是错误信息:

Twig_Error_Runtime: An exception has been thrown during the rendering of a template 
("Undefined index: REQUEST_METHOD") in "application/General.twig" at line 21.

this article所述,为 slim 框架编写测试用例并不容易。您可以如下模拟 SLIM 环境:

// Prepare a mock environment
        Environment::mock(array_merge(array(
            'REQUEST_METHOD' => $method,
            'PATH_INFO' => $path,
            'SERVER_NAME' => 'slim-test.dev',
        ), $options));
        $app = new \Slim\Slim();
        $this->app = $app;
        $this->request = $app->request();
        $this->response = $app->response();

this gist

中的完整代码示例

希望对您有所帮助