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