Pārlūkot izejas kodu

Merge remote branch 'vicb/service-event-tests'

* vicb/service-event-tests:
  [FrameworkBundle] Added a test for listener services not available in the current scope
  [FrameworkBundle] Add unit tests for ContainerAwareEventDispatcher
  [FrameworkBundle] Initialize the listenerId property in the ContainerAwareEventDispatcher class
Fabien Potencier 14 gadi atpakaļ
vecāks
revīzija
477d81f9b1

+ 5 - 3
src/Symfony/Bundle/FrameworkBundle/ContainerAwareEventDispatcher.php

@@ -34,7 +34,7 @@ class ContainerAwareEventDispatcher extends EventDispatcher
      * The service IDs of the event listeners and subscribers
      * @var array
      */
-    private $listenerIds;
+    private $listenerIds = array();
 
     /**
      * Constructor.
@@ -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;
         }
     }
 
@@ -72,6 +72,8 @@ class ContainerAwareEventDispatcher extends EventDispatcher
      *
      * Lazily loads listeners for this event from the dependency injection
      * container.
+     *
+     * @throws \InvalidArgumentException if the service is not defined
      */
     public function dispatch($eventName, Event $event = null)
     {

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

@@ -0,0 +1,82 @@
+<?php
+
+namespace Symfony\Bundle\FrameworkBundle\Tests;
+
+use Symfony\Component\DependencyInjection\Container;
+use Symfony\Bundle\FrameworkBundle\ContainerAwareEventDispatcher;
+use Symfony\Component\EventDispatcher\Event;
+use Symfony\Component\DependencyInjection\Scope;
+
+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);
+    }
+
+    /**
+     * @expectedException \InvalidArgumentException
+     */
+    public function testTriggerAListenerServiceOutOfScope()
+    {
+        $service = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');
+
+        $scope = new Scope('scope');
+
+        $container = new Container();
+        $container->addScope($scope);
+        $container->enterScope('scope');
+        $container->set('service.listener', $service, 'scope');
+
+        $dispatcher = new ContainerAwareEventDispatcher($container);
+        $dispatcher->addListenerService('onEvent', 'service.listener');
+
+        $container->leaveScope('scope');
+        $dispatcher->dispatch('onEvent');        
+    }
+}
+
+class Service
+{
+    function onEvent(Event $e)
+    {        
+    }
+}