浏览代码

[FrameworkBundle] Add unit tests for ContainerAwareEventDispatcher

Victor Berchet 14 年之前
父节点
当前提交
a4c41179e0

+ 2 - 2
src/Symfony/Bundle/FrameworkBundle/ContainerAwareEventDispatcher.php

@@ -61,9 +61,9 @@ class ContainerAwareEventDispatcher extends EventDispatcher
             throw new \InvalidArgumentException('Expected a string argument');
         }
 
-        foreach ((array) $eventNames as $event) {
+        foreach ((array) $eventNames as $eventName) {
             // Prevent duplicate entries
-            $this->listenerIds[$event][$serviceId] = $priority;
+            $this->listenerIds[$eventName][$serviceId] = $priority;
         }
     }
 

+ 61 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/ContainerAwareEventDispatcherTest.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Tests;
+
+use Symfony\Component\DependencyInjection\Container;
+use Symfony\Bundle\FrameworkBundle\ContainerAwareEventDispatcher;
+use Symfony\Component\EventDispatcher\Event;
+
+class ContainerAwareEventDispatcherTest extends \PHPUnit_Framework_TestCase
+{
+    public function testAddAListenerService()
+    {
+        $event = new Event();
+
+        $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
+
+        $service
+            ->expects($this->once())
+            ->method('onEvent')
+            ->with($event)
+        ;
+
+        $container = new Container();
+        $container->set('service.listener', $service);
+
+        $dispatcher = new ContainerAwareEventDispatcher($container);
+        $dispatcher->addListenerService('onEvent', 'service.listener');
+
+        $dispatcher->dispatch('onEvent', $event);
+    }
+
+    public function testPreventDuplicateListenerService()
+    {
+        $event = new Event();
+
+        $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
+
+        $service
+            ->expects($this->once())
+            ->method('onEvent')
+            ->with($event)
+        ;
+
+        $container = new Container();
+        $container->set('service.listener', $service);
+
+        $dispatcher = new ContainerAwareEventDispatcher($container);
+        $dispatcher->addListenerService('onEvent', 'service.listener', 5);
+        $dispatcher->addListenerService('onEvent', 'service.listener', 10);
+
+        $dispatcher->dispatch('onEvent', $event);
+    }
+
+}
+
+class Service
+{
+    function onEvent(Event $e)
+    {        
+    }
+}