[FrameworkBundle] Add unit tests for ContainerAwareEventDispatcher

This commit is contained in:
Victor Berchet 2011-04-18 13:45:15 +02:00
parent b45d117fbf
commit a4c41179e0
2 changed files with 63 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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)
{
}
}