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\DependencyInjection\Scope;
16 use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
17 use Symfony\Component\EventDispatcher\Event;
18 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20 class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
22 protected function createEventDispatcher()
24 $container = new Container();
26 return new ContainerAwareEventDispatcher($container);
29 public function testAddAListenerService()
33 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
36 ->expects($this->once())
41 $container = new Container();
42 $container->set('service.listener', $service);
44 $dispatcher = new ContainerAwareEventDispatcher($container);
45 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
47 $dispatcher->dispatch('onEvent', $event);
50 public function testAddASubscriberService()
54 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
57 ->expects($this->once())
63 ->expects($this->once())
64 ->method('onEventWithPriority')
69 ->expects($this->once())
70 ->method('onEventNested')
74 $container = new Container();
75 $container->set('service.subscriber', $service);
77 $dispatcher = new ContainerAwareEventDispatcher($container);
78 $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
80 $dispatcher->dispatch('onEvent', $event);
81 $dispatcher->dispatch('onEventWithPriority', $event);
82 $dispatcher->dispatch('onEventNested', $event);
85 public function testPreventDuplicateListenerService()
89 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
92 ->expects($this->once())
97 $container = new Container();
98 $container->set('service.listener', $service);
100 $dispatcher = new ContainerAwareEventDispatcher($container);
101 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
102 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
104 $dispatcher->dispatch('onEvent', $event);
108 * @expectedException \InvalidArgumentException
111 public function testTriggerAListenerServiceOutOfScope()
113 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
115 $scope = new Scope('scope');
116 $container = new Container();
117 $container->addScope($scope);
118 $container->enterScope('scope');
120 $container->set('service.listener', $service, 'scope');
122 $dispatcher = new ContainerAwareEventDispatcher($container);
123 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
125 $container->leaveScope('scope');
126 $dispatcher->dispatch('onEvent');
132 public function testReEnteringAScope()
134 $event = new Event();
136 $service1 = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
139 ->expects($this->exactly(2))
144 $scope = new Scope('scope');
145 $container = new Container();
146 $container->addScope($scope);
147 $container->enterScope('scope');
149 $container->set('service.listener', $service1, 'scope');
151 $dispatcher = new ContainerAwareEventDispatcher($container);
152 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
153 $dispatcher->dispatch('onEvent', $event);
155 $service2 = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
158 ->expects($this->once())
163 $container->enterScope('scope');
164 $container->set('service.listener', $service2, 'scope');
166 $dispatcher->dispatch('onEvent', $event);
168 $container->leaveScope('scope');
170 $dispatcher->dispatch('onEvent');
173 public function testHasListenersOnLazyLoad()
175 $event = new Event();
177 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
179 $container = new Container();
180 $container->set('service.listener', $service);
182 $dispatcher = new ContainerAwareEventDispatcher($container);
183 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
185 $event->setDispatcher($dispatcher);
186 $event->setName('onEvent');
189 ->expects($this->once())
194 $this->assertTrue($dispatcher->hasListeners());
196 if ($dispatcher->hasListeners('onEvent')) {
197 $dispatcher->dispatch('onEvent');
201 public function testGetListenersOnLazyLoad()
203 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
205 $container = new Container();
206 $container->set('service.listener', $service);
208 $dispatcher = new ContainerAwareEventDispatcher($container);
209 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
211 $listeners = $dispatcher->getListeners();
213 $this->assertTrue(isset($listeners['onEvent']));
215 $this->assertCount(1, $dispatcher->getListeners('onEvent'));
218 public function testRemoveAfterDispatch()
220 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
222 $container = new Container();
223 $container->set('service.listener', $service);
225 $dispatcher = new ContainerAwareEventDispatcher($container);
226 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
228 $dispatcher->dispatch('onEvent', new Event());
229 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
230 $this->assertFalse($dispatcher->hasListeners('onEvent'));
233 public function testRemoveBeforeDispatch()
235 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
237 $container = new Container();
238 $container->set('service.listener', $service);
240 $dispatcher = new ContainerAwareEventDispatcher($container);
241 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
243 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
244 $this->assertFalse($dispatcher->hasListeners('onEvent'));
250 public function onEvent(Event $e)
255 class SubscriberService implements EventSubscriberInterface
257 public static function getSubscribedEvents()
260 'onEvent' => 'onEvent',
261 'onEventWithPriority' => array('onEventWithPriority', 10),
262 'onEventNested' => array(array('onEventNested')),
266 public function onEvent(Event $e)
270 public function onEventWithPriority(Event $e)
274 public function onEventNested(Event $e)