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