Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
28 / 28 |
n/a |
0 / 0 |
CRAP | n/a |
0 / 0 |
||
| 1 | <?php |
| 2 | |
| 3 | // ╔════════════════════════════════════════════════════════════╗ |
| 4 | // ║ MIT Licence (#Expat) - https://opensource.org/licenses/MIT ║ |
| 5 | // ║ Copyright 2026 Frederic Poeydomenge <dyno@phexium.com> ║ |
| 6 | // ╚════════════════════════════════════════════════════════════╝ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | use League\Event\EventDispatcher as LeagueEventDispatcher; |
| 11 | use League\Event\PrioritizedListenerRegistry; |
| 12 | use Phexium\Plugin\Dispatcher\Adapter\LeagueDispatcher; |
| 13 | use Phexium\Plugin\Dispatcher\Adapter\LeagueListenerRegistry; |
| 14 | use Tests\Phexium\Fake\Domain\Event\EventListener as FakeEventListener; |
| 15 | |
| 16 | test('it handles subscription and dispatching with an ListenerInterface', function (): void { |
| 17 | $eventName = stdClass::class; |
| 18 | $event = new stdClass(); |
| 19 | |
| 20 | $registry = new PrioritizedListenerRegistry(); |
| 21 | $leagueDispatcher = new LeagueEventDispatcher($registry); |
| 22 | $dispatcher = new LeagueDispatcher($leagueDispatcher); |
| 23 | $listenerRegistry = new LeagueListenerRegistry($registry); |
| 24 | |
| 25 | $listener = new FakeEventListener(); |
| 26 | |
| 27 | $listenerRegistry->subscribeTo($eventName, $listener); |
| 28 | $dispatcher->dispatch($event); |
| 29 | |
| 30 | expect($listener->wasInvokedWith($event))->toBeTrue(); |
| 31 | expect($listener->getInvokeCount())->toBe(1); |
| 32 | }); |
| 33 | |
| 34 | test('it handles subscription and dispatching with a callable', function (): void { |
| 35 | $eventName = stdClass::class; |
| 36 | $event = new stdClass(); |
| 37 | |
| 38 | $registry = new PrioritizedListenerRegistry(); |
| 39 | $leagueDispatcher = new LeagueEventDispatcher($registry); |
| 40 | $dispatcher = new LeagueDispatcher($leagueDispatcher); |
| 41 | $listenerRegistry = new LeagueListenerRegistry($registry); |
| 42 | |
| 43 | $receivedEvent = null; |
| 44 | |
| 45 | $callableListener = function (object $e) use (&$receivedEvent): void { |
| 46 | $receivedEvent = $e; |
| 47 | }; |
| 48 | |
| 49 | $listenerRegistry->subscribeTo($eventName, $callableListener); |
| 50 | $dispatcher->dispatch($event); |
| 51 | |
| 52 | expect($receivedEvent)->toBe($event); |
| 53 | }); |