function ContainerAwareEventDispatcherTest::testDispatch

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php \Drupal\Tests\Component\EventDispatcher\ContainerAwareEventDispatcherTest::testDispatch()
  2. 8.9.x core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php \Drupal\Tests\Component\EventDispatcher\ContainerAwareEventDispatcherTest::testDispatch()
  3. 11.x core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php \Drupal\Tests\Component\EventDispatcher\ContainerAwareEventDispatcherTest::testDispatch()

File

core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php, line 309

Class

ContainerAwareEventDispatcherTest
Unit tests for the ContainerAwareEventDispatcher.

Namespace

Drupal\Tests\Component\EventDispatcher

Code

public function testDispatch() : void {
  $this->dispatcher
    ->addListener('pre.foo', [
    $this->listener,
    'preFoo',
  ]);
  $this->dispatcher
    ->addListener('post.foo', [
    $this->listener,
    'postFoo',
  ]);
  $this->dispatcher
    ->dispatch(new Event(), self::PRE_FOO);
  $this->assertTrue($this->listener->preFooInvoked);
  $this->assertFalse($this->listener->postFooInvoked);
  $this->assertInstanceOf(Event::class, $this->dispatcher
    ->dispatch(new Event(), 'no_event'));
  $this->assertInstanceOf(Event::class, $this->dispatcher
    ->dispatch(new Event(), self::PRE_FOO));
  // Any kind of object can be dispatched, not only instances of Event.
  $this->assertInstanceOf(\stdClass::class, $this->dispatcher
    ->dispatch(new \stdClass(), self::PRE_FOO));
  $event = new Event();
  $return = $this->dispatcher
    ->dispatch($event, self::PRE_FOO);
  $this->assertSame($event, $return);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.