You are here

class ContainerAwareEventDispatcherTest in Zircon Profile 8.0

Same name in this branch
  1. 8.0 vendor/symfony/event-dispatcher/Tests/ContainerAwareEventDispatcherTest.php \Symfony\Component\EventDispatcher\Tests\ContainerAwareEventDispatcherTest
  2. 8.0 core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php \Drupal\Tests\Component\EventDispatcher\ContainerAwareEventDispatcherTest
Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php \Drupal\Tests\Component\EventDispatcher\ContainerAwareEventDispatcherTest

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.

@group Symfony

Hierarchy

Expanded class hierarchy of ContainerAwareEventDispatcherTest

See also

https://github.com/symfony/symfony/pull/12521

File

core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php, line 30
Contains \Drupal\Tests\Component\EventDispatcher\ContainerAwareEventDispatcherTest.

Namespace

Drupal\Tests\Component\EventDispatcher
View source
class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest {
  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
      ->getMock('Symfony\\Component\\DependencyInjection\\IntrospectableContainerInterface');
    $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
      ->getMock('Symfony\\Component\\DependencyInjection\\IntrospectableContainerInterface');
    $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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractEventDispatcherTest::$dispatcher private property
AbstractEventDispatcherTest::$listener private property
AbstractEventDispatcherTest::postBar constant
AbstractEventDispatcherTest::postFoo constant
AbstractEventDispatcherTest::preBar constant
AbstractEventDispatcherTest::preFoo constant
AbstractEventDispatcherTest::setUp protected function
AbstractEventDispatcherTest::tearDown protected function
AbstractEventDispatcherTest::testAddListener public function
AbstractEventDispatcherTest::testAddSubscriber public function
AbstractEventDispatcherTest::testAddSubscriberWithMultipleListeners public function
AbstractEventDispatcherTest::testAddSubscriberWithPriorities public function
AbstractEventDispatcherTest::testDispatch public function
AbstractEventDispatcherTest::testDispatchByPriority public function
AbstractEventDispatcherTest::testDispatchForClosure public function
AbstractEventDispatcherTest::testEventReceivesTheDispatcherInstanceAsArgument public function
AbstractEventDispatcherTest::testGetAllListenersSortsByPriority public function
AbstractEventDispatcherTest::testGetListenersSortsByPriority public function
AbstractEventDispatcherTest::testGetListenersWhenAddedCallbackListenerIsRemoved public function
AbstractEventDispatcherTest::testHasListenersWhenAddedCallbackListenerIsRemoved public function
AbstractEventDispatcherTest::testHasListenersWithoutEventsReturnsFalseAfterHasListenersWithEventHasBeenCalled public function
AbstractEventDispatcherTest::testInitialState public function
AbstractEventDispatcherTest::testLegacyDispatch public function @group legacy
AbstractEventDispatcherTest::testLegacyEventReceivesTheDispatcherInstance public function @group legacy
AbstractEventDispatcherTest::testRemoveListener public function
AbstractEventDispatcherTest::testRemoveSubscriber public function
AbstractEventDispatcherTest::testRemoveSubscriberWithMultipleListeners public function
AbstractEventDispatcherTest::testRemoveSubscriberWithPriorities public function
AbstractEventDispatcherTest::testStopEventPropagation public function
AbstractEventDispatcherTest::testWorkaroundForPhpBug62976 public function This bug affects:
ContainerAwareEventDispatcherTest::createEventDispatcher protected function Overrides AbstractEventDispatcherTest::createEventDispatcher
ContainerAwareEventDispatcherTest::testDispatchWithCallables public function
ContainerAwareEventDispatcherTest::testDispatchWithServices public function
ContainerAwareEventDispatcherTest::testGetListenersWithCallables public function
ContainerAwareEventDispatcherTest::testGetListenersWithServices public function
ContainerAwareEventDispatcherTest::testRemoveService public function