123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- <?php
- // @codingStandardsIgnoreFile
- namespace Drupal\Tests\Component\EventDispatcher;
- use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
- use Symfony\Component\DependencyInjection\Container;
- use Symfony\Component\DependencyInjection\ContainerBuilder;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Component\EventDispatcher\Tests\CallableClass;
- use Symfony\Component\EventDispatcher\Tests\ContainerAwareEventDispatcherTest as SymfonyContainerAwareEventDispatcherTest;
- use Symfony\Component\EventDispatcher\Tests\TestEventListener;
- /**
- * Unit tests for the ContainerAwareEventDispatcher.
- *
- * NOTE: 98% of this code is a literal copy of Symfony's emerging
- * CompiledEventDispatcherTest.
- *
- * This file does NOT follow Drupal coding standards, so as to simplify future
- * synchronizations.
- *
- * @see https://github.com/symfony/symfony/pull/12521
- *
- * @group EventDispatcher
- */
- class ContainerAwareEventDispatcherTest extends SymfonyContainerAwareEventDispatcherTest
- {
- protected function createEventDispatcher()
- {
- $container = new Container();
- return new ContainerAwareEventDispatcher($container);
- }
- public function testGetListenersWithCallables()
- {
- // When passing in callables exclusively as listeners into the event
- // dispatcher constructor, the event dispatcher must not attempt to
- // resolve any services.
- $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
- $container->expects($this->never())->method($this->anything());
- $firstListener = new CallableClass();
- $secondListener = function () {};
- $thirdListener = array(new TestEventListener(), 'preFoo');
- $listeners = array(
- 'test_event' => array(
- 0 => array(
- array('callable' => $firstListener),
- array('callable' => $secondListener),
- array('callable' => $thirdListener),
- ),
- ),
- );
- $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
- $actualListeners = $dispatcher->getListeners();
- $expectedListeners = array(
- 'test_event' => array(
- $firstListener,
- $secondListener,
- $thirdListener,
- ),
- );
- $this->assertSame($expectedListeners, $actualListeners);
- }
- public function testDispatchWithCallables()
- {
- // When passing in callables exclusively as listeners into the event
- // dispatcher constructor, the event dispatcher must not attempt to
- // resolve any services.
- $container = $this->getMockBuilder(ContainerInterface::class)->getMock();
- $container->expects($this->never())->method($this->anything());
- $firstListener = new CallableClass();
- $secondListener = function () {};
- $thirdListener = array(new TestEventListener(), 'preFoo');
- $listeners = array(
- 'test_event' => array(
- 0 => array(
- array('callable' => $firstListener),
- array('callable' => $secondListener),
- array('callable' => $thirdListener),
- ),
- ),
- );
- $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
- $dispatcher->dispatch('test_event');
- $this->assertTrue($thirdListener[0]->preFooInvoked);
- }
- public function testGetListenersWithServices()
- {
- $container = new ContainerBuilder();
- $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
- $listeners = array(
- 'test_event' => array(
- 0 => array(
- array('service' => array('listener_service', 'preFoo')),
- ),
- ),
- );
- $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
- $actualListeners = $dispatcher->getListeners();
- $listenerService = $container->get('listener_service');
- $expectedListeners = array(
- 'test_event' => array(
- array($listenerService, 'preFoo'),
- ),
- );
- $this->assertSame($expectedListeners, $actualListeners);
- }
- public function testDispatchWithServices()
- {
- $container = new ContainerBuilder();
- $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
- $listeners = array(
- 'test_event' => array(
- 0 => array(
- array('service' => array('listener_service', 'preFoo')),
- ),
- ),
- );
- $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
- $dispatcher->dispatch('test_event');
- $listenerService = $container->get('listener_service');
- $this->assertTrue($listenerService->preFooInvoked);
- }
- public function testRemoveService()
- {
- $container = new ContainerBuilder();
- $container->register('listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
- $container->register('other_listener_service', 'Symfony\Component\EventDispatcher\Tests\TestEventListener');
- $listeners = array(
- 'test_event' => array(
- 0 => array(
- array('service' => array('listener_service', 'preFoo')),
- array('service' => array('other_listener_service', 'preFoo')),
- ),
- ),
- );
- $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
- $listenerService = $container->get('listener_service');
- $dispatcher->removeListener('test_event', array($listenerService, 'preFoo'));
- // Ensure that other service was not initialized during removal of the
- // listener service.
- $this->assertFalse($container->initialized('other_listener_service'));
- $dispatcher->dispatch('test_event');
- $this->assertFalse($listenerService->preFooInvoked);
- $otherService = $container->get('other_listener_service');
- $this->assertTrue($otherService->preFooInvoked);
- }
- public function testGetListenerPriorityWithServices()
- {
- $container = new ContainerBuilder();
- $container->register('listener_service', TestEventListener::class);
- $listeners = array(
- 'test_event' => array(
- 5 => array(
- array('service' => array('listener_service', 'preFoo')),
- ),
- ),
- );
- $dispatcher = new ContainerAwareEventDispatcher($container, $listeners);
- $listenerService = $container->get('listener_service');
- $actualPriority = $dispatcher->getListenerPriority('test_event', [$listenerService, 'preFoo']);
- $this->assertSame(5, $actualPriority);
- }
- /**
- * @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.
- * @group legacy
- */
- public function testAddAListenerService() {
- parent::testAddAListenerService();
- }
- /**
- * @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.
- * @group legacy
- */
- public function testPreventDuplicateListenerService() {
- parent::testPreventDuplicateListenerService();
- }
- /**
- * @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.
- * @group legacy
- */
- public function testAddASubscriberService() {
- parent::testAddASubscriberService();
- }
- /**
- * @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.
- * @group legacy
- */
- public function testHasListenersOnLazyLoad() {
- parent::testHasListenersOnLazyLoad();
- }
- /**
- * @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.
- * @group legacy
- */
- public function testGetListenersOnLazyLoad() {
- parent::testGetListenersOnLazyLoad();
- }
- /**
- * @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.
- * @group legacy
- */
- public function testRemoveAfterDispatch() {
- parent::testRemoveAfterDispatch();
- }
- /**
- * @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.
- * @group legacy
- */
- public function testRemoveBeforeDispatch() {
- parent::testRemoveBeforeDispatch();
- }
- }
|