Skip to content

Commit

Permalink
[FrameworkBundle] Fix an issue with ContainerAwareEventDispatcher whe…
Browse files Browse the repository at this point in the history
…n re-entering a scope
  • Loading branch information
vicb committed Apr 20, 2011
1 parent 1df8b2e commit cc89d4b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class ContainerAwareEventDispatcher extends EventDispatcher
*/
private $listenerIds = array();

/**
* The services registered as listeners
* @var array
*/
private $listeners = array();

/**
* Constructor.
*
Expand Down Expand Up @@ -79,7 +85,12 @@ public function dispatch($eventName, Event $event = null)
{
if (isset($this->listenerIds[$eventName])) {
foreach ($this->listenerIds[$eventName] as $serviceId => $priority) {
$this->addListener($eventName, $this->container->get($serviceId), $priority);
if (isset($this->listeners[$eventName][$serviceId])) {
$this->removeListener($eventName, $this->listeners[$eventName][$serviceId]);
}
$listener = $this->container->get($serviceId);
$this->listeners[$eventName][$serviceId] = $listener;
$this->addListener($eventName, $listener, $priority);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,58 @@ 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');
$dispatcher->dispatch('onEvent');
}

public function testReEnteringAScope()
{
$event = new Event();

$service1 = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');

$service1
->expects($this->exactly(2))
->method('onEvent')
->with($event)
;

$scope = new Scope('scope');
$container = new Container();
$container->addScope($scope);
$container->enterScope('scope');

$container->set('service.listener', $service1, 'scope');

$dispatcher = new ContainerAwareEventDispatcher($container);
$dispatcher->addListenerService('onEvent', 'service.listener');
$dispatcher->dispatch('onEvent', $event);

$service2 = $this->getMock('Symfony\Bundle\FrameworkBundle\Tests\Service');

$service2
->expects($this->once())
->method('onEvent')
->with($event)
;

$container->enterScope('scope');
$container->set('service.listener', $service2, 'scope');

$dispatcher->dispatch('onEvent', $event);

$container->leaveScope('scope');

$dispatcher->dispatch('onEvent');
}
}

Expand Down

0 comments on commit cc89d4b

Please sign in to comment.