Yaffs site version 1.1
[yaffs-website] / vendor / symfony / event-dispatcher / Tests / ContainerAwareEventDispatcherTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\EventDispatcher\Tests;
13
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;
19
20 class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
21 {
22     protected function createEventDispatcher()
23     {
24         $container = new Container();
25
26         return new ContainerAwareEventDispatcher($container);
27     }
28
29     public function testAddAListenerService()
30     {
31         $event = new Event();
32
33         $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
34
35         $service
36             ->expects($this->once())
37             ->method('onEvent')
38             ->with($event)
39         ;
40
41         $container = new Container();
42         $container->set('service.listener', $service);
43
44         $dispatcher = new ContainerAwareEventDispatcher($container);
45         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
46
47         $dispatcher->dispatch('onEvent', $event);
48     }
49
50     public function testAddASubscriberService()
51     {
52         $event = new Event();
53
54         $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
55
56         $service
57             ->expects($this->once())
58             ->method('onEvent')
59             ->with($event)
60         ;
61
62         $service
63             ->expects($this->once())
64             ->method('onEventWithPriority')
65             ->with($event)
66         ;
67
68         $service
69             ->expects($this->once())
70             ->method('onEventNested')
71             ->with($event)
72         ;
73
74         $container = new Container();
75         $container->set('service.subscriber', $service);
76
77         $dispatcher = new ContainerAwareEventDispatcher($container);
78         $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
79
80         $dispatcher->dispatch('onEvent', $event);
81         $dispatcher->dispatch('onEventWithPriority', $event);
82         $dispatcher->dispatch('onEventNested', $event);
83     }
84
85     public function testPreventDuplicateListenerService()
86     {
87         $event = new Event();
88
89         $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
90
91         $service
92             ->expects($this->once())
93             ->method('onEvent')
94             ->with($event)
95         ;
96
97         $container = new Container();
98         $container->set('service.listener', $service);
99
100         $dispatcher = new ContainerAwareEventDispatcher($container);
101         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
102         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
103
104         $dispatcher->dispatch('onEvent', $event);
105     }
106
107     /**
108      * @expectedException \InvalidArgumentException
109      * @group legacy
110      */
111     public function testTriggerAListenerServiceOutOfScope()
112     {
113         $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
114
115         $scope = new Scope('scope');
116         $container = new Container();
117         $container->addScope($scope);
118         $container->enterScope('scope');
119
120         $container->set('service.listener', $service, 'scope');
121
122         $dispatcher = new ContainerAwareEventDispatcher($container);
123         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
124
125         $container->leaveScope('scope');
126         $dispatcher->dispatch('onEvent');
127     }
128
129     /**
130      * @group legacy
131      */
132     public function testReEnteringAScope()
133     {
134         $event = new Event();
135
136         $service1 = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
137
138         $service1
139             ->expects($this->exactly(2))
140             ->method('onEvent')
141             ->with($event)
142         ;
143
144         $scope = new Scope('scope');
145         $container = new Container();
146         $container->addScope($scope);
147         $container->enterScope('scope');
148
149         $container->set('service.listener', $service1, 'scope');
150
151         $dispatcher = new ContainerAwareEventDispatcher($container);
152         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
153         $dispatcher->dispatch('onEvent', $event);
154
155         $service2 = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
156
157         $service2
158             ->expects($this->once())
159             ->method('onEvent')
160             ->with($event)
161         ;
162
163         $container->enterScope('scope');
164         $container->set('service.listener', $service2, 'scope');
165
166         $dispatcher->dispatch('onEvent', $event);
167
168         $container->leaveScope('scope');
169
170         $dispatcher->dispatch('onEvent');
171     }
172
173     public function testHasListenersOnLazyLoad()
174     {
175         $event = new Event();
176
177         $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
178
179         $container = new Container();
180         $container->set('service.listener', $service);
181
182         $dispatcher = new ContainerAwareEventDispatcher($container);
183         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
184
185         $event->setDispatcher($dispatcher);
186         $event->setName('onEvent');
187
188         $service
189             ->expects($this->once())
190             ->method('onEvent')
191             ->with($event)
192         ;
193
194         $this->assertTrue($dispatcher->hasListeners());
195
196         if ($dispatcher->hasListeners('onEvent')) {
197             $dispatcher->dispatch('onEvent');
198         }
199     }
200
201     public function testGetListenersOnLazyLoad()
202     {
203         $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
204
205         $container = new Container();
206         $container->set('service.listener', $service);
207
208         $dispatcher = new ContainerAwareEventDispatcher($container);
209         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
210
211         $listeners = $dispatcher->getListeners();
212
213         $this->assertTrue(isset($listeners['onEvent']));
214
215         $this->assertCount(1, $dispatcher->getListeners('onEvent'));
216     }
217
218     public function testRemoveAfterDispatch()
219     {
220         $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
221
222         $container = new Container();
223         $container->set('service.listener', $service);
224
225         $dispatcher = new ContainerAwareEventDispatcher($container);
226         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
227
228         $dispatcher->dispatch('onEvent', new Event());
229         $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
230         $this->assertFalse($dispatcher->hasListeners('onEvent'));
231     }
232
233     public function testRemoveBeforeDispatch()
234     {
235         $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
236
237         $container = new Container();
238         $container->set('service.listener', $service);
239
240         $dispatcher = new ContainerAwareEventDispatcher($container);
241         $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
242
243         $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
244         $this->assertFalse($dispatcher->hasListeners('onEvent'));
245     }
246 }
247
248 class Service
249 {
250     public function onEvent(Event $e)
251     {
252     }
253 }
254
255 class SubscriberService implements EventSubscriberInterface
256 {
257     public static function getSubscribedEvents()
258     {
259         return array(
260             'onEvent' => 'onEvent',
261             'onEventWithPriority' => array('onEventWithPriority', 10),
262             'onEventNested' => array(array('onEventNested')),
263         );
264     }
265
266     public function onEvent(Event $e)
267     {
268     }
269
270     public function onEventWithPriority(Event $e)
271     {
272     }
273
274     public function onEventNested(Event $e)
275     {
276     }
277 }