You are here

public function ContainerAwareEventDispatcherTest::testDispatchByPriority in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/EventDispatcher/ContainerAwareEventDispatcherTest.php \Drupal\Tests\Component\EventDispatcher\ContainerAwareEventDispatcherTest::testDispatchByPriority()

File

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

Class

ContainerAwareEventDispatcherTest
Unit tests for the ContainerAwareEventDispatcher.

Namespace

Drupal\Tests\Component\EventDispatcher

Code

public function testDispatchByPriority() {
  $invoked = [];
  $listener1 = function () use (&$invoked) {
    $invoked[] = '1';
  };
  $listener2 = function () use (&$invoked) {
    $invoked[] = '2';
  };
  $listener3 = function () use (&$invoked) {
    $invoked[] = '3';
  };
  $this->dispatcher
    ->addListener('pre.foo', $listener1, -10);
  $this->dispatcher
    ->addListener('pre.foo', $listener2);
  $this->dispatcher
    ->addListener('pre.foo', $listener3, 10);
  $this->dispatcher
    ->dispatch(new Event(), self::PREFOO);
  $this
    ->assertEquals([
    '3',
    '2',
    '1',
  ], $invoked);
}