annotate vendor/symfony/event-dispatcher/Tests/ContainerAwareEventDispatcherTest.php @ 19:fa3358dc1485 tip

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