4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\EventDispatcher\Tests;
14 use Symfony\Component\DependencyInjection\Container;
15 use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
16 use Symfony\Component\EventDispatcher\Event;
17 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19 class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
21 protected function createEventDispatcher()
23 $container = new Container();
25 return new ContainerAwareEventDispatcher($container);
28 public function testAddAListenerService()
32 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
35 ->expects($this->once())
40 $container = new Container();
41 $container->set('service.listener', $service);
43 $dispatcher = new ContainerAwareEventDispatcher($container);
44 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
46 $dispatcher->dispatch('onEvent', $event);
49 public function testAddASubscriberService()
53 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
56 ->expects($this->once())
62 ->expects($this->once())
63 ->method('onEventWithPriority')
68 ->expects($this->once())
69 ->method('onEventNested')
73 $container = new Container();
74 $container->set('service.subscriber', $service);
76 $dispatcher = new ContainerAwareEventDispatcher($container);
77 $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
79 $dispatcher->dispatch('onEvent', $event);
80 $dispatcher->dispatch('onEventWithPriority', $event);
81 $dispatcher->dispatch('onEventNested', $event);
84 public function testPreventDuplicateListenerService()
88 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
91 ->expects($this->once())
96 $container = new Container();
97 $container->set('service.listener', $service);
99 $dispatcher = new ContainerAwareEventDispatcher($container);
100 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
101 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
103 $dispatcher->dispatch('onEvent', $event);
106 public function testHasListenersOnLazyLoad()
108 $event = new Event();
110 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
112 $container = new Container();
113 $container->set('service.listener', $service);
115 $dispatcher = new ContainerAwareEventDispatcher($container);
116 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
119 ->expects($this->once())
124 $this->assertTrue($dispatcher->hasListeners());
126 if ($dispatcher->hasListeners('onEvent')) {
127 $dispatcher->dispatch('onEvent');
131 public function testGetListenersOnLazyLoad()
133 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
135 $container = new Container();
136 $container->set('service.listener', $service);
138 $dispatcher = new ContainerAwareEventDispatcher($container);
139 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
141 $listeners = $dispatcher->getListeners();
143 $this->assertTrue(isset($listeners['onEvent']));
145 $this->assertCount(1, $dispatcher->getListeners('onEvent'));
148 public function testRemoveAfterDispatch()
150 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
152 $container = new Container();
153 $container->set('service.listener', $service);
155 $dispatcher = new ContainerAwareEventDispatcher($container);
156 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
158 $dispatcher->dispatch('onEvent', new Event());
159 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
160 $this->assertFalse($dispatcher->hasListeners('onEvent'));
163 public function testRemoveBeforeDispatch()
165 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
167 $container = new Container();
168 $container->set('service.listener', $service);
170 $dispatcher = new ContainerAwareEventDispatcher($container);
171 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
173 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
174 $this->assertFalse($dispatcher->hasListeners('onEvent'));
180 public function onEvent(Event $e)
185 class SubscriberService implements EventSubscriberInterface
187 public static function getSubscribedEvents()
190 'onEvent' => 'onEvent',
191 'onEventWithPriority' => array('onEventWithPriority', 10),
192 'onEventNested' => array(array('onEventNested')),
196 public function onEvent(Event $e)
200 public function onEventWithPriority(Event $e)
204 public function onEventNested(Event $e)