You are here

public function ContainerAwareEventDispatcherTest::testGetListenersSortsByPriority in Drupal 10

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

File

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

Class

ContainerAwareEventDispatcherTest
Unit tests for the ContainerAwareEventDispatcher.

Namespace

Drupal\Tests\Component\EventDispatcher

Code

public function testGetListenersSortsByPriority() {
  $listener1 = new TestEventListener();
  $listener2 = new TestEventListener();
  $listener3 = new TestEventListener();
  $listener1->name = '1';
  $listener2->name = '2';
  $listener3->name = '3';
  $this->dispatcher
    ->addListener('pre.foo', [
    $listener1,
    'preFoo',
  ], -10);
  $this->dispatcher
    ->addListener('pre.foo', [
    $listener2,
    'preFoo',
  ], 10);
  $this->dispatcher
    ->addListener('pre.foo', [
    $listener3,
    'preFoo',
  ]);
  $expected = [
    [
      $listener2,
      'preFoo',
    ],
    [
      $listener3,
      'preFoo',
    ],
    [
      $listener1,
      'preFoo',
    ],
  ];
  $this
    ->assertSame($expected, $this->dispatcher
    ->getListeners('pre.foo'));
}