Chris@0: getMockBuilder(ContainerInterface::class)->getMock(); Chris@0: $container->expects($this->never())->method($this->anything()); Chris@0: Chris@0: $firstListener = new CallableClass(); Chris@0: $secondListener = function () {}; Chris@0: $thirdListener = array(new TestEventListener(), 'preFoo'); Chris@0: $listeners = array( Chris@0: 'test_event' => array( Chris@0: 0 => array( Chris@0: array('callable' => $firstListener), Chris@0: array('callable' => $secondListener), Chris@0: array('callable' => $thirdListener), Chris@0: ), Chris@0: ), Chris@0: ); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container, $listeners); Chris@0: $actualListeners = $dispatcher->getListeners(); Chris@0: Chris@0: $expectedListeners = array( Chris@0: 'test_event' => array( Chris@0: $firstListener, Chris@0: $secondListener, Chris@0: $thirdListener, Chris@0: ), Chris@0: ); Chris@0: Chris@0: $this->assertSame($expectedListeners, $actualListeners); Chris@0: } Chris@0: Chris@0: public function testDispatchWithCallables() Chris@0: { Chris@0: // When passing in callables exclusively as listeners into the event Chris@0: // dispatcher constructor, the event dispatcher must not attempt to Chris@0: // resolve any services. Chris@14: $container = $this->getMockBuilder(ContainerInterface::class)->getMock(); Chris@0: $container->expects($this->never())->method($this->anything()); Chris@0: Chris@0: $firstListener = new CallableClass(); Chris@0: $secondListener = function () {}; Chris@0: $thirdListener = array(new TestEventListener(), 'preFoo'); Chris@0: $listeners = array( Chris@0: 'test_event' => array( Chris@0: 0 => array( Chris@0: array('callable' => $firstListener), Chris@0: array('callable' => $secondListener), Chris@0: array('callable' => $thirdListener), Chris@0: ), Chris@0: ), Chris@0: ); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container, $listeners); Chris@0: $dispatcher->dispatch('test_event'); Chris@0: Chris@0: $this->assertTrue($thirdListener[0]->preFooInvoked); Chris@0: } Chris@0: Chris@0: public function testGetListenersWithServices() Chris@0: { Chris@0: $container = new ContainerBuilder(); Chris@0: $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener'); Chris@0: Chris@0: $listeners = array( Chris@0: 'test_event' => array( Chris@0: 0 => array( Chris@0: array('service' => array('listener_service', 'preFoo')), Chris@0: ), Chris@0: ), Chris@0: ); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container, $listeners); Chris@0: $actualListeners = $dispatcher->getListeners(); Chris@0: Chris@0: $listenerService = $container->get('listener_service'); Chris@0: $expectedListeners = array( Chris@0: 'test_event' => array( Chris@0: array($listenerService, 'preFoo'), Chris@0: ), Chris@0: ); Chris@0: Chris@0: $this->assertSame($expectedListeners, $actualListeners); Chris@0: } Chris@0: Chris@0: public function testDispatchWithServices() Chris@0: { Chris@0: $container = new ContainerBuilder(); Chris@0: $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener'); Chris@0: Chris@0: $listeners = array( Chris@0: 'test_event' => array( Chris@0: 0 => array( Chris@0: array('service' => array('listener_service', 'preFoo')), Chris@0: ), Chris@0: ), Chris@0: ); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container, $listeners); Chris@0: Chris@0: $dispatcher->dispatch('test_event'); Chris@0: Chris@0: $listenerService = $container->get('listener_service'); Chris@0: $this->assertTrue($listenerService->preFooInvoked); Chris@0: } Chris@0: Chris@0: public function testRemoveService() Chris@0: { Chris@0: $container = new ContainerBuilder(); Chris@0: $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener'); Chris@0: $container->register('other_listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener'); Chris@0: Chris@0: $listeners = array( Chris@0: 'test_event' => array( Chris@0: 0 => array( Chris@0: array('service' => array('listener_service', 'preFoo')), Chris@0: array('service' => array('other_listener_service', 'preFoo')), Chris@0: ), Chris@0: ), Chris@0: ); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container, $listeners); Chris@0: Chris@0: $listenerService = $container->get('listener_service'); Chris@0: $dispatcher->removeListener('test_event', array($listenerService, 'preFoo')); Chris@0: Chris@0: // Ensure that other service was not initialized during removal of the Chris@0: // listener service. Chris@0: $this->assertFalse($container->initialized('other_listener_service')); Chris@0: Chris@0: $dispatcher->dispatch('test_event'); Chris@0: Chris@0: $this->assertFalse($listenerService->preFooInvoked); Chris@0: $otherService = $container->get('other_listener_service'); Chris@0: $this->assertTrue($otherService->preFooInvoked); Chris@0: } Chris@0: Chris@0: public function testGetListenerPriorityWithServices() Chris@0: { Chris@0: $container = new ContainerBuilder(); Chris@0: $container->register('listener_service', TestEventListener::class); Chris@0: Chris@0: $listeners = array( Chris@0: 'test_event' => array( Chris@0: 5 => array( Chris@0: array('service' => array('listener_service', 'preFoo')), Chris@0: ), Chris@0: ), Chris@0: ); Chris@0: Chris@0: $dispatcher = new ContainerAwareEventDispatcher($container, $listeners); Chris@0: $listenerService = $container->get('listener_service'); Chris@0: $actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']); Chris@0: Chris@0: $this->assertSame(5, $actualPriority); Chris@0: } Chris@0: Chris@14: /** Chris@14: * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead. Chris@14: * @group legacy Chris@14: */ Chris@14: public function testAddAListenerService() { Chris@14: parent::testAddAListenerService(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead. Chris@14: * @group legacy Chris@14: */ Chris@14: public function testPreventDuplicateListenerService() { Chris@14: parent::testPreventDuplicateListenerService(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead. Chris@14: * @group legacy Chris@14: */ Chris@14: public function testAddASubscriberService() { Chris@14: parent::testAddASubscriberService(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead. Chris@14: * @group legacy Chris@14: */ Chris@14: public function testHasListenersOnLazyLoad() { Chris@14: parent::testHasListenersOnLazyLoad(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead. Chris@14: * @group legacy Chris@14: */ Chris@14: public function testGetListenersOnLazyLoad() { Chris@14: parent::testGetListenersOnLazyLoad(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead. Chris@14: * @group legacy Chris@14: */ Chris@14: public function testRemoveAfterDispatch() { Chris@14: parent::testRemoveAfterDispatch(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * @expectedDeprecation The Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher class is deprecated since Symfony 3.3 and will be removed in 4.0. Use EventDispatcher with closure factories instead. Chris@14: * @group legacy Chris@14: */ Chris@14: public function testRemoveBeforeDispatch() { Chris@14: parent::testRemoveBeforeDispatch(); Chris@14: } Chris@0: }