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 \PHPUnit_Framework_TestCase
22 protected function setUp()
24 if (!class_exists('Symfony\Component\DependencyInjection\Container')) {
25 $this->markTestSkipped('The "DependencyInjection" component is not available');
29 public function testAddAListenerService()
33 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
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->getMock('Symfony\Component\EventDispatcher\Tests\SubscriberService');
57 ->expects($this->once())
62 $container = new Container();
63 $container->set('service.subscriber', $service);
65 $dispatcher = new ContainerAwareEventDispatcher($container);
66 $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
68 $dispatcher->dispatch('onEvent', $event);
71 public function testPreventDuplicateListenerService()
75 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
78 ->expects($this->once())
83 $container = new Container();
84 $container->set('service.listener', $service);
86 $dispatcher = new ContainerAwareEventDispatcher($container);
87 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
88 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
90 $dispatcher->dispatch('onEvent', $event);
94 * @expectedException \InvalidArgumentException
96 public function testTriggerAListenerServiceOutOfScope()
98 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
100 $scope = new Scope('scope');
101 $container = new Container();
102 $container->addScope($scope);
103 $container->enterScope('scope');
105 $container->set('service.listener', $service, 'scope');
107 $dispatcher = new ContainerAwareEventDispatcher($container);
108 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
110 $container->leaveScope('scope');
111 $dispatcher->dispatch('onEvent');
114 public function testReEnteringAScope()
116 $event = new Event();
118 $service1 = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
121 ->expects($this->exactly(2))
126 $scope = new Scope('scope');
127 $container = new Container();
128 $container->addScope($scope);
129 $container->enterScope('scope');
131 $container->set('service.listener', $service1, 'scope');
133 $dispatcher = new ContainerAwareEventDispatcher($container);
134 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
135 $dispatcher->dispatch('onEvent', $event);
137 $service2 = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
140 ->expects($this->once())
145 $container->enterScope('scope');
146 $container->set('service.listener', $service2, 'scope');
148 $dispatcher->dispatch('onEvent', $event);
150 $container->leaveScope('scope');
152 $dispatcher->dispatch('onEvent');
155 public function testHasListenersOnLazyLoad()
157 $event = new Event();
159 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
161 $container = new Container();
162 $container->set('service.listener', $service);
164 $dispatcher = new ContainerAwareEventDispatcher($container);
165 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
167 $event->setDispatcher($dispatcher);
168 $event->setName('onEvent');
171 ->expects($this->once())
176 $this->assertTrue($dispatcher->hasListeners());
178 if ($dispatcher->hasListeners('onEvent')) {
179 $dispatcher->dispatch('onEvent');
183 public function testGetListenersOnLazyLoad()
185 $event = new Event();
187 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
189 $container = new Container();
190 $container->set('service.listener', $service);
192 $dispatcher = new ContainerAwareEventDispatcher($container);
193 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
195 $listeners = $dispatcher->getListeners();
197 $this->assertTrue(isset($listeners['onEvent']));
199 $this->assertCount(1, $dispatcher->getListeners('onEvent'));
202 public function testRemoveAfterDispatch()
204 $event = new Event();
206 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
208 $container = new Container();
209 $container->set('service.listener', $service);
211 $dispatcher = new ContainerAwareEventDispatcher($container);
212 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
214 $dispatcher->dispatch('onEvent', new Event());
215 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
216 $this->assertFalse($dispatcher->hasListeners('onEvent'));
219 public function testRemoveBeforeDispatch()
221 $event = new Event();
223 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
225 $container = new Container();
226 $container->set('service.listener', $service);
228 $dispatcher = new ContainerAwareEventDispatcher($container);
229 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
231 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
232 $this->assertFalse($dispatcher->hasListeners('onEvent'));
238 public function onEvent(Event
$e)
243 class SubscriberService
implements EventSubscriberInterface
245 public static function getSubscribedEvents()
248 'onEvent' => 'onEvent',
249 'onEvent' => array('onEvent', 10),
250 'onEvent' => array('onEvent'),
254 public function onEvent(Event
$e)