1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 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->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: 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: $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
186:
187: $container = new Container();
188: $container->set('service.listener', $service);
189:
190: $dispatcher = new ContainerAwareEventDispatcher($container);
191: $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
192:
193: $listeners = $dispatcher->getListeners();
194:
195: $this->assertTrue(isset($listeners['onEvent']));
196:
197: $this->assertCount(1, $dispatcher->getListeners('onEvent'));
198: }
199:
200: public function testRemoveAfterDispatch()
201: {
202: $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
203:
204: $container = new Container();
205: $container->set('service.listener', $service);
206:
207: $dispatcher = new ContainerAwareEventDispatcher($container);
208: $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
209:
210: $dispatcher->dispatch('onEvent', new Event());
211: $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
212: $this->assertFalse($dispatcher->hasListeners('onEvent'));
213: }
214:
215: public function testRemoveBeforeDispatch()
216: {
217: $service = $this->getMock('Symfony\Component\EventDispatcher\Tests\Service');
218:
219: $container = new Container();
220: $container->set('service.listener', $service);
221:
222: $dispatcher = new ContainerAwareEventDispatcher($container);
223: $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
224:
225: $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
226: $this->assertFalse($dispatcher->hasListeners('onEvent'));
227: }
228: }
229:
230: class Service
231: {
232: public function onEvent(Event $e)
233: {
234: }
235: }
236:
237: class SubscriberService implements EventSubscriberInterface
238: {
239: public static function getSubscribedEvents()
240: {
241: return array(
242: 'onEvent' => array('onEvent'),
243: );
244: }
245:
246: public function onEvent(Event $e)
247: {
248: }
249: }
250: