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;
22 class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
24 protected function createEventDispatcher()
26 $container = new Container();
28 return new ContainerAwareEventDispatcher($container);
31 public function testAddAListenerService()
35 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
38 ->expects($this->once())
43 $container = new Container();
44 $container->set('service.listener', $service);
46 $dispatcher = new ContainerAwareEventDispatcher($container);
47 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
49 $dispatcher->dispatch('onEvent', $event);
52 public function testAddASubscriberService()
56 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
59 ->expects($this->once())
65 ->expects($this->once())
66 ->method('onEventWithPriority')
71 ->expects($this->once())
72 ->method('onEventNested')
76 $container = new Container();
77 $container->set('service.subscriber', $service);
79 $dispatcher = new ContainerAwareEventDispatcher($container);
80 $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
82 $dispatcher->dispatch('onEvent', $event);
83 $dispatcher->dispatch('onEventWithPriority', $event);
84 $dispatcher->dispatch('onEventNested', $event);
87 public function testPreventDuplicateListenerService()
91 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
94 ->expects($this->once())
99 $container = new Container();
100 $container->set('service.listener', $service);
102 $dispatcher = new ContainerAwareEventDispatcher($container);
103 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
104 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
106 $dispatcher->dispatch('onEvent', $event);
109 public function testHasListenersOnLazyLoad()
111 $event = new Event();
113 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
115 $container = new Container();
116 $container->set('service.listener', $service);
118 $dispatcher = new ContainerAwareEventDispatcher($container);
119 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
122 ->expects($this->once())
127 $this->assertTrue($dispatcher->hasListeners());
129 if ($dispatcher->hasListeners('onEvent')) {
130 $dispatcher->dispatch('onEvent');
134 public function testGetListenersOnLazyLoad()
136 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
138 $container = new Container();
139 $container->set('service.listener', $service);
141 $dispatcher = new ContainerAwareEventDispatcher($container);
142 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
144 $listeners = $dispatcher->getListeners();
146 $this->assertArrayHasKey('onEvent', $listeners);
148 $this->assertCount(1, $dispatcher->getListeners('onEvent'));
151 public function testRemoveAfterDispatch()
153 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
155 $container = new Container();
156 $container->set('service.listener', $service);
158 $dispatcher = new ContainerAwareEventDispatcher($container);
159 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
161 $dispatcher->dispatch('onEvent', new Event());
162 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
163 $this->assertFalse($dispatcher->hasListeners('onEvent'));
166 public function testRemoveBeforeDispatch()
168 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
170 $container = new Container();
171 $container->set('service.listener', $service);
173 $dispatcher = new ContainerAwareEventDispatcher($container);
174 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
176 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
177 $this->assertFalse($dispatcher->hasListeners('onEvent'));
183 public function onEvent(Event $e)
188 class SubscriberService implements EventSubscriberInterface
190 public static function getSubscribedEvents()
193 'onEvent' => 'onEvent',
194 'onEventWithPriority' => array('onEventWithPriority', 10),
195 'onEventNested' => array(array('onEventNested')),
199 public function onEvent(Event $e)
203 public function onEventWithPriority(Event $e)
207 public function onEventNested(Event $e)