Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Component / EventDispatcher / ContainerAwareEventDispatcherTest.php
1 <?php
2 // @codingStandardsIgnoreFile
3
4 namespace Drupal\Tests\Component\EventDispatcher;
5
6 use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
7 use Symfony\Component\DependencyInjection\Container;
8 use Symfony\Component\DependencyInjection\ContainerBuilder;
9 use Symfony\Component\EventDispatcher\Tests\CallableClass;
10 use Symfony\Component\EventDispatcher\Tests\TestEventListener;
11 use Symfony\Component\EventDispatcher\Tests\ContainerAwareEventDispatcherTest as SymfonyContainerAwareEventDispatcherTest;
12
13 /**
14  * Unit tests for the ContainerAwareEventDispatcher.
15  *
16  * NOTE: 98% of this code is a literal copy of Symfony's emerging
17  * CompiledEventDispatcherTest.
18  *
19  * This file does NOT follow Drupal coding standards, so as to simplify future
20  * synchronizations.
21  *
22  * @see https://github.com/symfony/symfony/pull/12521
23  *
24  * @group EventDispatcher
25  */
26 class ContainerAwareEventDispatcherTest extends SymfonyContainerAwareEventDispatcherTest
27 {
28     protected function createEventDispatcher()
29     {
30         $container = new Container();
31
32         return new ContainerAwareEventDispatcher($container);
33     }
34
35     public function testGetListenersWithCallables()
36     {
37         // When passing in callables exclusively as listeners into the event
38         // dispatcher constructor, the event dispatcher must not attempt to
39         // resolve any services.
40         $container = $this->getMock('Symfony\Component\DependencyInjection\IntrospectableContainerInterface');
41         $container->expects($this->never())->method($this->anything());
42
43         $firstListener = new CallableClass();
44         $secondListener = function () {};
45         $thirdListener = array(new TestEventListener(), 'preFoo');
46         $listeners = array(
47             'test_event' => array(
48                 0 => array(
49                     array('callable' => $firstListener),
50                     array('callable' => $secondListener),
51                     array('callable' => $thirdListener),
52                 ),
53             ),
54         );
55
56         $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
57         $actualListeners = $dispatcher->getListeners();
58
59         $expectedListeners = array(
60             'test_event' => array(
61                 $firstListener,
62                 $secondListener,
63                 $thirdListener,
64             ),
65         );
66
67         $this->assertSame($expectedListeners, $actualListeners);
68     }
69
70     public function testDispatchWithCallables()
71     {
72         // When passing in callables exclusively as listeners into the event
73         // dispatcher constructor, the event dispatcher must not attempt to
74         // resolve any services.
75         $container = $this->getMock('Symfony\Component\DependencyInjection\IntrospectableContainerInterface');
76         $container->expects($this->never())->method($this->anything());
77
78         $firstListener = new CallableClass();
79         $secondListener = function () {};
80         $thirdListener = array(new TestEventListener(), 'preFoo');
81         $listeners = array(
82             'test_event' => array(
83                 0 => array(
84                     array('callable' => $firstListener),
85                     array('callable' => $secondListener),
86                     array('callable' => $thirdListener),
87                 ),
88             ),
89         );
90
91         $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
92         $dispatcher->dispatch('test_event');
93
94         $this->assertTrue($thirdListener[0]->preFooInvoked);
95     }
96
97     public function testGetListenersWithServices()
98     {
99         $container = new ContainerBuilder();
100         $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
101
102         $listeners = array(
103             'test_event' => array(
104                 0 => array(
105                     array('service' => array('listener_service', 'preFoo')),
106                 ),
107             ),
108         );
109
110         $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
111         $actualListeners = $dispatcher->getListeners();
112
113         $listenerService = $container->get('listener_service');
114         $expectedListeners = array(
115             'test_event' => array(
116                 array($listenerService, 'preFoo'),
117             ),
118         );
119
120         $this->assertSame($expectedListeners, $actualListeners);
121     }
122
123     public function testDispatchWithServices()
124     {
125         $container = new ContainerBuilder();
126         $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
127
128         $listeners = array(
129             'test_event' => array(
130                 0 => array(
131                     array('service' => array('listener_service', 'preFoo')),
132                 ),
133             ),
134         );
135
136         $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
137
138         $dispatcher->dispatch('test_event');
139
140         $listenerService = $container->get('listener_service');
141         $this->assertTrue($listenerService->preFooInvoked);
142     }
143
144     public function testRemoveService()
145     {
146         $container = new ContainerBuilder();
147         $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
148         $container->register('other_listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
149
150         $listeners = array(
151             'test_event' => array(
152                 0 => array(
153                     array('service' => array('listener_service', 'preFoo')),
154                     array('service' => array('other_listener_service', 'preFoo')),
155                 ),
156             ),
157         );
158
159         $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
160
161         $listenerService = $container->get('listener_service');
162         $dispatcher->removeListener('test_event', array($listenerService, 'preFoo'));
163
164         // Ensure that other service was not initialized during removal of the
165         // listener service.
166         $this->assertFalse($container->initialized('other_listener_service'));
167
168         $dispatcher->dispatch('test_event');
169
170         $this->assertFalse($listenerService->preFooInvoked);
171         $otherService = $container->get('other_listener_service');
172         $this->assertTrue($otherService->preFooInvoked);
173     }
174
175     public function testGetListenerPriorityWithServices()
176     {
177         $container = new ContainerBuilder();
178         $container->register('listener_service', TestEventListener::class);
179
180         $listeners = array(
181             'test_event' => array(
182                 5 => array(
183                     array('service' => array('listener_service', 'preFoo')),
184                 ),
185             ),
186         );
187
188         $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
189         $listenerService = $container->get('listener_service');
190         $actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']);
191
192         $this->assertSame(5, $actualPriority);
193     }
194
195  }