]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/event-dispatcher/Symfony/Component/EventDispatcher/Tests/ContainerAwareEventDispatcherTest.php
71f3ad05215ecdf15117050fef95b6d3e2e75ea6
[github/wallabag/wallabag.git] / vendor / symfony / event-dispatcher / Symfony / Component / EventDispatcher / 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 \PHPUnit_Framework_TestCase
21 {
22 protected function setUp()
23 {
24 if (!class_exists('Symfony\Component\DependencyInjection\Container')) {
25 $this->markTestSkipped('The "DependencyInjection" component is not available');
26 }
27 }
28
29 public function testAddAListenerService()
30 {
31 $event = new Event();
32
33 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
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->getMock('Symfony\Component\EventDispatcher\Tests\SubscriberService');
55
56 $service
57 ->expects($this->once())
58 ->method('onEvent')
59 ->with($event)
60 ;
61
62 $container = new Container();
63 $container->set('service.subscriber', $service);
64
65 $dispatcher = new ContainerAwareEventDispatcher($container);
66 $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
67
68 $dispatcher->dispatch('onEvent', $event);
69 }
70
71 public function testPreventDuplicateListenerService()
72 {
73 $event = new Event();
74
75 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
76
77 $service
78 ->expects($this->once())
79 ->method('onEvent')
80 ->with($event)
81 ;
82
83 $container = new Container();
84 $container->set('service.listener', $service);
85
86 $dispatcher = new ContainerAwareEventDispatcher($container);
87 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
88 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
89
90 $dispatcher->dispatch('onEvent', $event);
91 }
92
93 /**
94 * @expectedException \InvalidArgumentException
95 */
96 public function testTriggerAListenerServiceOutOfScope()
97 {
98 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
99
100 $scope = new Scope('scope');
101 $container = new Container();
102 $container->addScope($scope);
103 $container->enterScope('scope');
104
105 $container->set('service.listener', $service, 'scope');
106
107 $dispatcher = new ContainerAwareEventDispatcher($container);
108 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
109
110 $container->leaveScope('scope');
111 $dispatcher->dispatch('onEvent');
112 }
113
114 public function testReEnteringAScope()
115 {
116 $event = new Event();
117
118 $service1 = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
119
120 $service1
121 ->expects($this->exactly(2))
122 ->method('onEvent')
123 ->with($event)
124 ;
125
126 $scope = new Scope('scope');
127 $container = new Container();
128 $container->addScope($scope);
129 $container->enterScope('scope');
130
131 $container->set('service.listener', $service1, 'scope');
132
133 $dispatcher = new ContainerAwareEventDispatcher($container);
134 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
135 $dispatcher->dispatch('onEvent', $event);
136
137 $service2 = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
138
139 $service2
140 ->expects($this->once())
141 ->method('onEvent')
142 ->with($event)
143 ;
144
145 $container->enterScope('scope');
146 $container->set('service.listener', $service2, 'scope');
147
148 $dispatcher->dispatch('onEvent', $event);
149
150 $container->leaveScope('scope');
151
152 $dispatcher->dispatch('onEvent');
153 }
154
155 public function testHasListenersOnLazyLoad()
156 {
157 $event = new Event();
158
159 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
160
161 $container = new Container();
162 $container->set('service.listener', $service);
163
164 $dispatcher = new ContainerAwareEventDispatcher($container);
165 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
166
167 $event->setDispatcher($dispatcher);
168 $event->setName('onEvent');
169
170 $service
171 ->expects($this->once())
172 ->method('onEvent')
173 ->with($event)
174 ;
175
176 $this->assertTrue($dispatcher->hasListeners());
177
178 if ($dispatcher->hasListeners('onEvent')) {
179 $dispatcher->dispatch('onEvent');
180 }
181 }
182
183 public function testGetListenersOnLazyLoad()
184 {
185 $event = new Event();
186
187 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
188
189 $container = new Container();
190 $container->set('service.listener', $service);
191
192 $dispatcher = new ContainerAwareEventDispatcher($container);
193 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
194
195 $listeners = $dispatcher->getListeners();
196
197 $this->assertTrue(isset($listeners['onEvent']));
198
199 $this->assertCount(1, $dispatcher->getListeners('onEvent'));
200 }
201
202 public function testRemoveAfterDispatch()
203 {
204 $event = new Event();
205
206 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
207
208 $container = new Container();
209 $container->set('service.listener', $service);
210
211 $dispatcher = new ContainerAwareEventDispatcher($container);
212 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
213
214 $dispatcher->dispatch('onEvent', new Event());
215 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
216 $this->assertFalse($dispatcher->hasListeners('onEvent'));
217 }
218
219 public function testRemoveBeforeDispatch()
220 {
221 $event = new Event();
222
223 $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
224
225 $container = new Container();
226 $container->set('service.listener', $service);
227
228 $dispatcher = new ContainerAwareEventDispatcher($container);
229 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
230
231 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
232 $this->assertFalse($dispatcher->hasListeners('onEvent'));
233 }
234 }
235
236 class Service
237 {
238 public function onEvent(Event $e)
239 {
240 }
241 }
242
243 class SubscriberService implements EventSubscriberInterface
244 {
245 public static function getSubscribedEvents()
246 {
247 return array(
248 'onEvent' => 'onEvent',
249 'onEvent' => array('onEvent', 10),
250 'onEvent' => array('onEvent'),
251 );
252 }
253
254 public function onEvent(Event $e)
255 {
256 }
257 }