123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\EventDispatcher\Tests;
- use Symfony\Component\DependencyInjection\Container;
- use Symfony\Component\DependencyInjection\Scope;
- use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher;
- use Symfony\Component\EventDispatcher\Event;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest
- {
- protected function createEventDispatcher()
- {
- $container = new Container();
- return new ContainerAwareEventDispatcher($container);
- }
- public function testAddAListenerService()
- {
- $event = new Event();
- $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $service
- ->expects($this->once())
- ->method('onEvent')
- ->with($event)
- ;
- $container = new Container();
- $container->set('service.listener', $service);
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
- $dispatcher->dispatch('onEvent', $event);
- }
- public function testAddASubscriberService()
- {
- $event = new Event();
- $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\SubscriberService')->getMock();
- $service
- ->expects($this->once())
- ->method('onEvent')
- ->with($event)
- ;
- $service
- ->expects($this->once())
- ->method('onEventWithPriority')
- ->with($event)
- ;
- $service
- ->expects($this->once())
- ->method('onEventNested')
- ->with($event)
- ;
- $container = new Container();
- $container->set('service.subscriber', $service);
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addSubscriberService('service.subscriber', 'Symfony\Component\EventDispatcher\Tests\SubscriberService');
- $dispatcher->dispatch('onEvent', $event);
- $dispatcher->dispatch('onEventWithPriority', $event);
- $dispatcher->dispatch('onEventNested', $event);
- }
- public function testPreventDuplicateListenerService()
- {
- $event = new Event();
- $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $service
- ->expects($this->once())
- ->method('onEvent')
- ->with($event)
- ;
- $container = new Container();
- $container->set('service.listener', $service);
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 5);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'), 10);
- $dispatcher->dispatch('onEvent', $event);
- }
- /**
- * @expectedException \InvalidArgumentException
- * @group legacy
- */
- public function testTriggerAListenerServiceOutOfScope()
- {
- $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $scope = new Scope('scope');
- $container = new Container();
- $container->addScope($scope);
- $container->enterScope('scope');
- $container->set('service.listener', $service, 'scope');
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
- $container->leaveScope('scope');
- $dispatcher->dispatch('onEvent');
- }
- /**
- * @group legacy
- */
- public function testReEnteringAScope()
- {
- $event = new Event();
- $service1 = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $service1
- ->expects($this->exactly(2))
- ->method('onEvent')
- ->with($event)
- ;
- $scope = new Scope('scope');
- $container = new Container();
- $container->addScope($scope);
- $container->enterScope('scope');
- $container->set('service.listener', $service1, 'scope');
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
- $dispatcher->dispatch('onEvent', $event);
- $service2 = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $service2
- ->expects($this->once())
- ->method('onEvent')
- ->with($event)
- ;
- $container->enterScope('scope');
- $container->set('service.listener', $service2, 'scope');
- $dispatcher->dispatch('onEvent', $event);
- $container->leaveScope('scope');
- $dispatcher->dispatch('onEvent');
- }
- public function testHasListenersOnLazyLoad()
- {
- $event = new Event();
- $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $container = new Container();
- $container->set('service.listener', $service);
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
- $event->setDispatcher($dispatcher);
- $event->setName('onEvent');
- $service
- ->expects($this->once())
- ->method('onEvent')
- ->with($event)
- ;
- $this->assertTrue($dispatcher->hasListeners());
- if ($dispatcher->hasListeners('onEvent')) {
- $dispatcher->dispatch('onEvent');
- }
- }
- public function testGetListenersOnLazyLoad()
- {
- $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $container = new Container();
- $container->set('service.listener', $service);
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
- $listeners = $dispatcher->getListeners();
- $this->assertArrayHasKey('onEvent', $listeners);
- $this->assertCount(1, $dispatcher->getListeners('onEvent'));
- }
- public function testRemoveAfterDispatch()
- {
- $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $container = new Container();
- $container->set('service.listener', $service);
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
- $dispatcher->dispatch('onEvent', new Event());
- $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
- $this->assertFalse($dispatcher->hasListeners('onEvent'));
- }
- public function testRemoveBeforeDispatch()
- {
- $service = $this->getMockBuilder('Symfony\Component\EventDispatcher\Tests\Service')->getMock();
- $container = new Container();
- $container->set('service.listener', $service);
- $dispatcher = new ContainerAwareEventDispatcher($container);
- $dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));
- $dispatcher->removeListener('onEvent', array($container->get('service.listener'), 'onEvent'));
- $this->assertFalse($dispatcher->hasListeners('onEvent'));
- }
- }
- class Service
- {
- public function onEvent(Event $e)
- {
- }
- }
- class SubscriberService implements EventSubscriberInterface
- {
- public static function getSubscribedEvents()
- {
- return array(
- 'onEvent' => 'onEvent',
- 'onEventWithPriority' => array('onEventWithPriority', 10),
- 'onEventNested' => array(array('onEventNested')),
- );
- }
- public function onEvent(Event $e)
- {
- }
- public function onEventWithPriority(Event $e)
- {
- }
- public function onEventNested(Event $e)
- {
- }
- }
|