comparison vendor/symfony/event-dispatcher/Tests/ContainerAwareEventDispatcherTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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\EventDispatcher\ContainerAwareEventDispatcher;
16 use Symfony\Component\EventDispatcher\Event;
17 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
19 class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
20 {
21 protected function createEventDispatcher()
22 {
23 $container = new Container();
24
25 return new ContainerAwareEventDispatcher($container);
26 }
27
28 public function testAddAListenerService()
29 {
30 $event = new Event();
31
32 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
33
34 $service
35 ->expects($this->once())
36 ->method('onEvent')
37 ->with($event)
38 ;
39
40 $container = new Container();
41 $container->set('service.listener', $service);
42
43 $dispatcher = new ContainerAwareEventDispatcher($container);
44 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
45
46 $dispatcher->dispatch('onEvent', $event);
47 }
48
49 public function testAddASubscriberService()
50 {
51 $event = new Event();
52
53 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
54
55 $service
56 ->expects($this->once())
57 ->method('onEvent')
58 ->with($event)
59 ;
60
61 $service
62 ->expects($this->once())
63 ->method('onEventWithPriority')
64 ->with($event)
65 ;
66
67 $service
68 ->expects($this->once())
69 ->method('onEventNested')
70 ->with($event)
71 ;
72
73 $container = new Container();
74 $container->set('service.subscriber', $service);
75
76 $dispatcher = new ContainerAwareEventDispatcher($container);
77 $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
78
79 $dispatcher->dispatch('onEvent', $event);
80 $dispatcher->dispatch('onEventWithPriority', $event);
81 $dispatcher->dispatch('onEventNested', $event);
82 }
83
84 public function testPreventDuplicateListenerService()
85 {
86 $event = new Event();
87
88 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
89
90 $service
91 ->expects($this->once())
92 ->method('onEvent')
93 ->with($event)
94 ;
95
96 $container = new Container();
97 $container->set('service.listener', $service);
98
99 $dispatcher = new ContainerAwareEventDispatcher($container);
100 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
101 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
102
103 $dispatcher->dispatch('onEvent', $event);
104 }
105
106 public function testHasListenersOnLazyLoad()
107 {
108 $event = new Event();
109
110 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
111
112 $container = new Container();
113 $container->set('service.listener', $service);
114
115 $dispatcher = new ContainerAwareEventDispatcher($container);
116 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
117
118 $service
119 ->expects($this->once())
120 ->method('onEvent')
121 ->with($event)
122 ;
123
124 $this->assertTrue($dispatcher->hasListeners());
125
126 if ($dispatcher->hasListeners('onEvent')) {
127 $dispatcher->dispatch('onEvent');
128 }
129 }
130
131 public function testGetListenersOnLazyLoad()
132 {
133 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
134
135 $container = new Container();
136 $container->set('service.listener', $service);
137
138 $dispatcher = new ContainerAwareEventDispatcher($container);
139 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
140
141 $listeners = $dispatcher->getListeners();
142
143 $this->assertTrue(isset($listeners['onEvent']));
144
145 $this->assertCount(1, $dispatcher->getListeners('onEvent'));
146 }
147
148 public function testRemoveAfterDispatch()
149 {
150 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
151
152 $container = new Container();
153 $container->set('service.listener', $service);
154
155 $dispatcher = new ContainerAwareEventDispatcher($container);
156 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
157
158 $dispatcher->dispatch('onEvent', new Event());
159 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
160 $this->assertFalse($dispatcher->hasListeners('onEvent'));
161 }
162
163 public function testRemoveBeforeDispatch()
164 {
165 $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
166
167 $container = new Container();
168 $container->set('service.listener', $service);
169
170 $dispatcher = new ContainerAwareEventDispatcher($container);
171 $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
172
173 $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
174 $this->assertFalse($dispatcher->hasListeners('onEvent'));
175 }
176 }
177
178 class Service
179 {
180 public function onEvent(Event $e)
181 {
182 }
183 }
184
185 class SubscriberService implements EventSubscriberInterface
186 {
187 public static function getSubscribedEvents()
188 {
189 return array(
190 'onEvent' => 'onEvent',
191 'onEventWithPriority' => array('onEventWithPriority', 10),
192 'onEventNested' => array(array('onEventNested')),
193 );
194 }
195
196 public function onEvent(Event $e)
197 {
198 }
199
200 public function onEventWithPriority(Event $e)
201 {
202 }
203
204 public function onEventNested(Event $e)
205 {
206 }
207 }