为 Symfony EventListener 编写 UnitTest [英] Write UnitTest for Symfony EventListener

查看:14
本文介绍了为 Symfony EventListener 编写 UnitTest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有一个 SymfonyBundle,它实际上不仅仅是一个自定义 EventDispatcher 和一个 EventListener.

I have a SymfonyBundle here, which is really not more then a custom EventDispatcher, and an EventListener.

我将如何对这段代码进行单元测试?

How would I go about unit testing this code ?

我知道如何为控制器等创建功能测试,但我不确定如何为这个调度程序和侦听器编写测试..

I know how to create functional tests for controllers etc, but am not really sure how to go about writing tests for this dispatcher and listener..

有人能指出我正确的方向吗,谷歌似乎在这方面对我没有多大帮助.

Can someone point me in the right direction pls, google doesnt seem to help me out much on this one .

提前致谢.

山姆·J

04/04/2014:这个是捆绑我试图为

我已经设置了测试来检查两个服务(调度程序和侦听器)是否都已加载,但现在我需要测试 FUNCTIONALITY .. 例如,检查在我的调度程序上触发的事件是否在内核上的 REAL 调度程序上触发.终止.

I already have tests set up to check if both services (dispatcher and listener) are loaded, but now i need to test the FUNCTIONALITY .. eg check if an event fired on MY dispatcher, is fired on the REAL dispatcher on kernel.terinate.

EDIT 所以我疯狂地假设我只需要触发我的事件,然后以某种方式到达 kernel.terminate,然后检查我的事件是否被真正的调度员,但如何..

EDIT so i am bnasically assuming that i just need to fire my event, then somehow get to kernel.terminate, and check if my event was then fired by the real dispatcher, but how ..

推荐答案

您可以通过模拟所有必要的东西来对您的侦听器进行单元测试,例如,从我的项目中:

You can unit test your listener by mocking up all the necessary stuff it needs to work, for example, from a project of mine:

class UploadListenerTest extends PHPUnit_Framework_TestCase
{
    public function testOnUpload()
    {
        $session = new Session(new MockArraySessionStorage());
        $file = new File(__FILE__);

        $event = new PostPersistEvent($file, new EmptyResponse, new Request(), "", []);

        $listener = new UploadListener($session);
        $listener->onUpload($event);

        $tempFiles = $session->get('_temp_files');

        $this->assertCount(1, $tempFiles);
        $this->assertEquals($tempFiles[0], $file->getFilename());

        $otherFile = new File(__FILE__);

        $event = new PostPersistEvent($otherFile, new EmptyResponse, new Request(), "", []);

        $listener->onUpload($event);

        $tempFiles = $session->get('_temp_files');

        $this->assertCount(2, $tempFiles);
        $this->assertEquals($tempFiles[0], $file->getFilename());
        $this->assertEquals($tempFiles[0], $otherFile->getFilename());
    }
} 

如您所见,我正在创建事件侦听器所需的每个对象,以便对其行为进行单元测试.

As you can see, I am creating every object my Event Listener needs in order to unit test it's behaviour.

您也可以采用功能性方式.启动 Symfony 内核并为您的事件触发创建条件,然后在事件触发后测试您需要具备的预期条件:

You can also go the functional way. Boot the Symfony kernel and create the conditions for your event to trigger, and then test the expected conditions you need to have after the event is triggered:

public function testUploadNoFilesNoAjaxLoggedUser()
{
    $this->loginUser($this->getDummyUser());

    $response = $this->requestRoute(self::UPLOAD_ROUTE, "POST");

    $this->assertResponseRedirect("panel_index", $response);
}

如您所见,我首先记录用户,然后对上传表单发出实际请求.之后我断言我的响应应该是重定向到主面板.Symfony 在后台触发了该事件,并且该事件返回了我需要断言的 RedirectResponse.

As you can see, I'm first logging the user and then doing an actual request to the upload form. After that I assert that my response should be a redirection to the main panel. Symfony is triggering the event under the hood, and this event is returning the RedirectResponse I need to assert.

我的建议是您尝试同时编写单元测试和功能测试,这将使您的应用程序处于更稳定的状态.

My recommendation is that you try to write both unit and functional tests, it will leave your application in a more stable state.

编辑

在此 PR 中添加了有关如何测试事件调度程序本身的特定问题的答案:

Added answer to specific question on how to test an event dispatcher itself in this PR:

https://github.com/whitewhidow/AsyncDispatcherBundle/pull/1/files

这篇关于为 Symfony EventListener 编写 UnitTest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆