You are here

class ContainerAwareEventDispatcherTest in Zircon Profile 8

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

Hierarchy

Expanded class hierarchy of ContainerAwareEventDispatcherTest

File

vendor/symfony/event-dispatcher/Tests/ContainerAwareEventDispatcherTest.php, line 20

Namespace

Symfony\Component\EventDispatcher\Tests
View source
class ContainerAwareEventDispatcherTest extends AbstractEventDispatcherTest {
  protected function createEventDispatcher() {
    $container = new Container();
    return new ContainerAwareEventDispatcher($container);
  }
  public function testAddAListenerService() {
    $event = new Event();
    $service = $this
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $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
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\SubscriberService');
    $service
      ->expects($this
      ->once())
      ->method('onEvent')
      ->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);
  }
  public function testPreventDuplicateListenerService() {
    $event = new Event();
    $service = $this
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $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
   */
  public function testTriggerAListenerServiceOutOfScope() {
    $service = $this
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $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');
  }
  public function testReEnteringAScope() {
    $event = new Event();
    $service1 = $this
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $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
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $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
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $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
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $container = new Container();
    $container
      ->set('service.listener', $service);
    $dispatcher = new ContainerAwareEventDispatcher($container);
    $dispatcher
      ->addListenerService('onEvent', array(
      'service.listener',
      'onEvent',
    ));
    $listeners = $dispatcher
      ->getListeners();
    $this
      ->assertTrue(isset($listeners['onEvent']));
    $this
      ->assertCount(1, $dispatcher
      ->getListeners('onEvent'));
  }
  public function testRemoveAfterDispatch() {
    $service = $this
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $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
      ->getMock('Symfony\\Component\\EventDispatcher\\Tests\\Service');
    $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'));
  }

}

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::testAddAListenerService public function
ContainerAwareEventDispatcherTest::testAddASubscriberService public function
ContainerAwareEventDispatcherTest::testGetListenersOnLazyLoad public function
ContainerAwareEventDispatcherTest::testHasListenersOnLazyLoad public function
ContainerAwareEventDispatcherTest::testPreventDuplicateListenerService public function
ContainerAwareEventDispatcherTest::testReEnteringAScope public function
ContainerAwareEventDispatcherTest::testRemoveAfterDispatch public function
ContainerAwareEventDispatcherTest::testRemoveBeforeDispatch public function
ContainerAwareEventDispatcherTest::testTriggerAListenerServiceOutOfScope public function @expectedException \InvalidArgumentException