14 use Symfony\Component\DependencyInjection\Container;
15 use Symfony\Component\DependencyInjection\Scope;
24 $container =
new Container();
33 $service = $this->getMockBuilder(
'Symfony\Component\EventDispatcher\Tests\Service')->getMock();
36 ->expects($this->once())
41 $container =
new Container();
42 $container->set(
'service.listener', $service);
45 $dispatcher->addListenerService(
'onEvent', array(
'service.listener',
'onEvent'));
47 $dispatcher->dispatch(
'onEvent', $event);
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);
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);
89 $service = $this->getMockBuilder(
'Symfony\Component\EventDispatcher\Tests\Service')->getMock();
92 ->expects($this->once())
97 $container =
new Container();
98 $container->set(
'service.listener', $service);
101 $dispatcher->addListenerService(
'onEvent', array(
'service.listener',
'onEvent'), 5);
102 $dispatcher->addListenerService(
'onEvent', array(
'service.listener',
'onEvent'), 10);
104 $dispatcher->dispatch(
'onEvent', $event);
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');
123 $dispatcher->addListenerService(
'onEvent', array(
'service.listener',
'onEvent'));
125 $container->leaveScope(
'scope');
126 $dispatcher->dispatch(
'onEvent');
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');
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');
175 $event =
new Event();
177 $service = $this->getMockBuilder(
'Symfony\Component\EventDispatcher\Tests\Service')->getMock();
179 $container =
new Container();
180 $container->set(
'service.listener', $service);
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');
203 $service = $this->getMockBuilder(
'Symfony\Component\EventDispatcher\Tests\Service')->getMock();
205 $container =
new Container();
206 $container->set(
'service.listener', $service);
209 $dispatcher->addListenerService(
'onEvent', array(
'service.listener',
'onEvent'));
211 $listeners = $dispatcher->getListeners();
213 $this->assertArrayHasKey(
'onEvent', $listeners);
215 $this->assertCount(1, $dispatcher->getListeners(
'onEvent'));
220 $service = $this->getMockBuilder(
'Symfony\Component\EventDispatcher\Tests\Service')->getMock();
222 $container =
new Container();
223 $container->set(
'service.listener', $service);
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'));
235 $service = $this->getMockBuilder(
'Symfony\Component\EventDispatcher\Tests\Service')->getMock();
237 $container =
new Container();
238 $container->set(
'service.listener', $service);
241 $dispatcher->addListenerService(
'onEvent', array(
'service.listener',
'onEvent'));
243 $dispatcher->removeListener(
'onEvent', array($container->get(
'service.listener'),
'onEvent'));
244 $this->assertFalse($dispatcher->hasListeners(
'onEvent'));
260 'onEvent' =>
'onEvent',
261 'onEventWithPriority' => array(
'onEventWithPriority', 10),
262 'onEventNested' => array(array(
'onEventNested')),