Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\EventDispatcher\Tests; Chris@0: Chris@0: use Symfony\Component\DependencyInjection\Container; Chris@0: use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; Chris@0: use Symfony\Component\EventDispatcher\Event; Chris@0: use Symfony\Component\EventDispatcher\EventSubscriberInterface; Chris@0: Chris@14: /** Chris@14: * @group legacy Chris@14: */ Chris@0: class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest Chris@0: { Chris@0: protected function createEventDispatcher() Chris@0: { Chris@0: $container = new Container(); Chris@0: Chris@0: return new ContainerAwareEventDispatcher($container); Chris@0: } Chris@0: Chris@0: public function testAddAListenerService() Chris@0: { Chris@0: $event = new Event(); Chris@0: Chris@0: $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock(); Chris@0: Chris@0: $service Chris@0: ->expects($this->once()) Chris@0: ->method('onEvent') Chris@0: ->with($event) Chris@0: ; Chris@0: Chris@0: $container = new Container(); Chris@0: $container->set('service.listener', $service); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container); Chris@17: $dispatcher->addListenerService('onEvent', ['service.listener', 'onEvent']); Chris@0: Chris@0: $dispatcher->dispatch('onEvent', $event); Chris@0: } Chris@0: Chris@0: public function testAddASubscriberService() Chris@0: { Chris@0: $event = new Event(); Chris@0: Chris@0: $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock(); Chris@0: Chris@0: $service Chris@0: ->expects($this->once()) Chris@0: ->method('onEvent') Chris@0: ->with($event) Chris@0: ; Chris@0: Chris@0: $service Chris@0: ->expects($this->once()) Chris@0: ->method('onEventWithPriority') Chris@0: ->with($event) Chris@0: ; Chris@0: Chris@0: $service Chris@0: ->expects($this->once()) Chris@0: ->method('onEventNested') Chris@0: ->with($event) Chris@0: ; Chris@0: Chris@0: $container = new Container(); Chris@0: $container->set('service.subscriber', $service); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container); Chris@0: $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService'); Chris@0: Chris@0: $dispatcher->dispatch('onEvent', $event); Chris@0: $dispatcher->dispatch('onEventWithPriority', $event); Chris@0: $dispatcher->dispatch('onEventNested', $event); Chris@0: } Chris@0: Chris@0: public function testPreventDuplicateListenerService() Chris@0: { Chris@0: $event = new Event(); Chris@0: Chris@0: $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock(); Chris@0: Chris@0: $service Chris@0: ->expects($this->once()) Chris@0: ->method('onEvent') Chris@0: ->with($event) Chris@0: ; Chris@0: Chris@0: $container = new Container(); Chris@0: $container->set('service.listener', $service); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container); Chris@17: $dispatcher->addListenerService('onEvent', ['service.listener', 'onEvent'], 5); Chris@17: $dispatcher->addListenerService('onEvent', ['service.listener', 'onEvent'], 10); Chris@0: Chris@0: $dispatcher->dispatch('onEvent', $event); Chris@0: } Chris@0: Chris@0: public function testHasListenersOnLazyLoad() Chris@0: { Chris@0: $event = new Event(); Chris@0: Chris@0: $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock(); Chris@0: Chris@0: $container = new Container(); Chris@0: $container->set('service.listener', $service); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container); Chris@17: $dispatcher->addListenerService('onEvent', ['service.listener', 'onEvent']); Chris@0: Chris@0: $service Chris@0: ->expects($this->once()) Chris@0: ->method('onEvent') Chris@0: ->with($event) Chris@0: ; Chris@0: Chris@0: $this->assertTrue($dispatcher->hasListeners()); Chris@0: Chris@0: if ($dispatcher->hasListeners('onEvent')) { Chris@0: $dispatcher->dispatch('onEvent'); Chris@0: } Chris@0: } Chris@0: Chris@0: public function testGetListenersOnLazyLoad() Chris@0: { Chris@0: $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock(); Chris@0: Chris@0: $container = new Container(); Chris@0: $container->set('service.listener', $service); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container); Chris@17: $dispatcher->addListenerService('onEvent', ['service.listener', 'onEvent']); Chris@0: Chris@0: $listeners = $dispatcher->getListeners(); Chris@0: Chris@14: $this->assertArrayHasKey('onEvent', $listeners); Chris@0: Chris@0: $this->assertCount(1, $dispatcher->getListeners('onEvent')); Chris@0: } Chris@0: Chris@0: public function testRemoveAfterDispatch() Chris@0: { Chris@0: $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock(); Chris@0: Chris@0: $container = new Container(); Chris@0: $container->set('service.listener', $service); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container); Chris@17: $dispatcher->addListenerService('onEvent', ['service.listener', 'onEvent']); Chris@0: Chris@0: $dispatcher->dispatch('onEvent', new Event()); Chris@17: $dispatcher->removeListener('onEvent', [$container->get('service.listener'), 'onEvent']); Chris@0: $this->assertFalse($dispatcher->hasListeners('onEvent')); Chris@0: } Chris@0: Chris@0: public function testRemoveBeforeDispatch() Chris@0: { Chris@0: $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock(); Chris@0: Chris@0: $container = new Container(); Chris@0: $container->set('service.listener', $service); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container); Chris@17: $dispatcher->addListenerService('onEvent', ['service.listener', 'onEvent']); Chris@0: Chris@17: $dispatcher->removeListener('onEvent', [$container->get('service.listener'), 'onEvent']); Chris@0: $this->assertFalse($dispatcher->hasListeners('onEvent')); Chris@0: } Chris@0: } Chris@0: Chris@0: class Service Chris@0: { Chris@0: public function onEvent(Event $e) Chris@0: { Chris@0: } Chris@0: } Chris@0: Chris@0: class SubscriberService implements EventSubscriberInterface Chris@0: { Chris@0: public static function getSubscribedEvents() Chris@0: { Chris@17: return [ Chris@0: 'onEvent' => 'onEvent', Chris@17: 'onEventWithPriority' => ['onEventWithPriority', 10], Chris@17: 'onEventNested' => [['onEventNested']], Chris@17: ]; Chris@0: } Chris@0: Chris@0: public function onEvent(Event $e) Chris@0: { Chris@0: } Chris@0: Chris@0: public function onEventWithPriority(Event $e) Chris@0: { Chris@0: } Chris@0: Chris@0: public function onEventNested(Event $e) Chris@0: { Chris@0: } Chris@0: }