Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
31 / 31 |
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 | pest()->group('integration'); |
| 11 | |
| 12 | use League\Event\EventDispatcher as LeagueEventDispatcher; |
| 13 | use League\Event\PrioritizedListenerRegistry; |
| 14 | use Phexium\Plugin\Dispatcher\Adapter\LeagueDispatcher; |
| 15 | use Phexium\Plugin\Dispatcher\Adapter\LeagueListenerRegistry; |
| 16 | use Tests\Phexium\Fake\Domain\Event\EventListener as FakeEventListener; |
| 17 | |
| 18 | describe('Event dispatching', function (): void { |
| 19 | it('dispatches event to ListenerInterface subscriber', function (): void { |
| 20 | $eventName = stdClass::class; |
| 21 | $event = new stdClass(); |
| 22 | |
| 23 | $registry = new PrioritizedListenerRegistry(); |
| 24 | $leagueDispatcher = new LeagueEventDispatcher($registry); |
| 25 | $dispatcher = new LeagueDispatcher($leagueDispatcher); |
| 26 | $listenerRegistry = new LeagueListenerRegistry($registry); |
| 27 | |
| 28 | $listener = new FakeEventListener(); |
| 29 | |
| 30 | $listenerRegistry->subscribeTo($eventName, $listener); |
| 31 | $dispatcher->dispatch($event); |
| 32 | |
| 33 | expect($listener->wasInvokedWith($event))->toBeTrue(); |
| 34 | expect($listener->getInvokeCount())->toBe(1); |
| 35 | }); |
| 36 | |
| 37 | it('dispatches event to callable subscriber', function (): void { |
| 38 | $eventName = stdClass::class; |
| 39 | $event = new stdClass(); |
| 40 | |
| 41 | $registry = new PrioritizedListenerRegistry(); |
| 42 | $leagueDispatcher = new LeagueEventDispatcher($registry); |
| 43 | $dispatcher = new LeagueDispatcher($leagueDispatcher); |
| 44 | $listenerRegistry = new LeagueListenerRegistry($registry); |
| 45 | |
| 46 | $receivedEvent = null; |
| 47 | |
| 48 | $callableListener = function (object $e) use (&$receivedEvent): void { |
| 49 | $receivedEvent = $e; |
| 50 | }; |
| 51 | |
| 52 | $listenerRegistry->subscribeTo($eventName, $callableListener); |
| 53 | $dispatcher->dispatch($event); |
| 54 | |
| 55 | expect($receivedEvent)->toBe($event); |
| 56 | }); |
| 57 | }); |