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

查看:67
本文介绍了为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

2014年4月4日 :是捆绑即时通讯尝试创建测试

edit: 04/04/2014: This is the bundle im trying to create tests for

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

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天全站免登陆