简体   繁体   English

如何使用phpunit测试symfony2嵌入式控制器?

[英]How to test a symfony2 embedded controller with phpunit?

I am using embedded controllers to generate dynamic content in side templates(eg: a menu) 我正在使用嵌入式控制器在侧面模板(例如菜单)中生成动态内容

Usually, I implement functionnal tests to assert controllers. 通常,我执行功能测试以断言控制器。 So far, functionnal tests are passing and phpunit considers my embedded controllers code-covered 到目前为止,功能测试通过了,phpunit认为我的嵌入式控制器已被代码覆盖

I am wondering how to test the embedded controller with different inputs and evaluate the outputs... Is that Unit Testing right ? 我想知道如何用不同的输入来测试嵌入式控制器并评估输出...那单元测试正确吗?

I know Unit Testing controller is a bad practice, but how can I function test an embedded controller when there is no Request Object ? 我知道单元测试控制器是一种不好的做法,但是当没有请求对象时,如何对嵌入式控制器进行功能测试? The route/url is something that the Twig render() function takes care of. 路线/网址是Twig render()函数负责的事情。

{{ render(controller('AppSuperBundle:Default:generateMenu', {'params': ... } )) }}

An example to illustrate: 一个例子来说明:

class DefaultController extends Controller
{
    public function testAction()
    {
        return $this->render('AppSuperBundle::index.html.twig');
    }

    public function generateMenuAction($route, Array $RouteParams)
    {
        $repo = $this->getDoctrine()->getRepository(...
        //some process to generate params of menu items (eg:locale, url, name...)

        return $this->render('AppSuperBundle::menu.html.twig', array('menuItems' => $menuItemsWithParams));
    }
}

The template index.html.twig 模板index.html.twig

<html>
    <body>
        {% block menu %} 
        {{ render(controller('AppSuperBundle:Default:generateMenu', {'route': app.request.attributes.get('_route'), 'RouteParams': app.request.attributes.get('_route_params')} )) }}  
        {% endblock %}
        {% block content %}
        ...
        {% endblock %}
    </body>
</html>

What are your thoughts on this ? 您对此有何看法?

Your embedded controllers do not exist in a vaccum. 您的嵌入式控制器不存在于真空中。 They are being loaded by templates used in your main controllers. 它们由您的主控制器中使用的模板加载。

I would say that it is sufficient to only check main controllers. 我想说只检查主控制器就足够了。 If you really want to check different output from embedded controllers just test the main controller with appropriate params. 如果您确实要检查嵌入式控制器的不同输出,只需使用适当的参数测试主控制器。 In the end it is the main controller that is pasing different values to your embedded controllers. 最后,是主控制器向嵌入式控制器添加了不同的值。

Since render a view is the response, and you are talking about unit test, And i would highly recommend unit test the controllers, since in some projects the controllers can have a lot of logic there. 因为渲染视图是响应,并且您正在谈论单元测试,所以我强烈建议对控制器进行单元测试,因为在某些项目中,控制器在那里可能有很多逻辑。 i would unit test a controller to its behavour,so it won't throw strange error from the code in the controller. 我将对控制器的行为进行单元测试,因此它不会从控制器中的代码中引发奇怪的错误。 so what i suggest you to do is create a test method for each case in each action, and you will probably need to mock some of the objects the controller is using, here is an example: 因此,我建议您做的是为每个动作中的每种情况创建一个测试方法,您可能需要模拟控制器正在使用的一些对象,下面是一个示例:

public function testIndexAction()
{
    $this->employeeRepository->expects($this->once())->method('findByFilter')->will($this->returnValue($this->employee));
    $this->entityManager->expects($this->once())->method('getRepository')->will(
        $this->returnValue($this->employeeRepository)
    );


    $this->employeeManager->expects($this->once())->method('formatEmployeeData')->will(
        $this->returnValue($this->formattedJson)
    );


    $this->mockContainer($this->object);

    $this->object->indexAction();

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM