From d985ad009932cf4b2b284705677f00b027e0e3fd Mon Sep 17 00:00:00 2001 From: Phillip Davis Date: Tue, 14 Jul 2026 21:15:48 +0930 Subject: [PATCH] test: add testListenersOrder This tests the change in #162 that makes sure not to sort listeners that are of equal priority, but to preserve the order in which they were created. Signed-off-by: Phillip Davis --- tests/Event/WildcardEmitterTest.php | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/Event/WildcardEmitterTest.php b/tests/Event/WildcardEmitterTest.php index 5fd5377..b9fba0e 100644 --- a/tests/Event/WildcardEmitterTest.php +++ b/tests/Event/WildcardEmitterTest.php @@ -4,6 +4,7 @@ namespace Sabre\Event; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Depends; class WildcardEmitterTest extends \PHPUnit\Framework\TestCase @@ -26,6 +27,46 @@ public function testListeners(): void self::assertEquals([$callback2, $callback1], $ee->listeners('foo')); } + public function aMethod(int $number): int + { + return $number * 2; + } + + public function bMethod(int $number): int + { + return $number * 4; + } + + /** + * @return array> + */ + public static function listenersOrderDataProvider(): array + { + // Regardless of the alphabetical order of the method names, + // when the event priority is the same, the order of the + // event listeners remains in the order specified and is not + // "accidentally" sorted. + return [ + ['aMethod', 'bMethod'], + ['bMethod', 'aMethod'], + ]; + } + + #[DataProvider('listenersOrderDataProvider')] + public function testListenersOrder(string $methodName1, string $methodName2): void + { + $ee = new WildcardEmitter(); + + $event1 = [$this, $methodName1]; + $event2 = [$this, $methodName2]; + // @phpstan-ignore argument.type + $ee->on('bar', $event1, 200); + // @phpstan-ignore argument.type + $ee->on('bar', $event2, 200); + + self::assertEquals([$event1, $event2], $ee->listeners('bar')); + } + public function testWildcardListeners(): void { $ee = new WildcardEmitter();