Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
44 / 44
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
8declare(strict_types=1);
9
10pest()->group('integration');
11
12use Phexium\Plugin\EventBus\Adapter\SyncEventBus;
13use Phexium\Plugin\Logger\Adapter\NullLogger;
14use Tests\Phexium\Fake\Domain\Event\DomainEvent as FakeDomainEvent;
15use Tests\Phexium\Fake\Plugin\Dispatcher\FakeDispatcher;
16use Tests\Phexium\Fake\Plugin\Dispatcher\FakeListenerRegistry;
17use Tests\Phexium\Fake\Plugin\Logger\Logger as FakeLogger;
18
19describe('Publishing', function (): void {
20    it('dispatches events through the event dispatcher', function (): void {
21        $event = new FakeDomainEvent('test-data');
22        $listenerRegistry = new FakeListenerRegistry();
23        $dispatcher = new FakeDispatcher($listenerRegistry);
24        $logger = new NullLogger();
25
26        $eventBus = new SyncEventBus($dispatcher, $listenerRegistry, $logger);
27
28        $result = $eventBus->dispatch($event);
29
30        expect($result)->toBe($event);
31        expect($dispatcher->wasDispatched(FakeDomainEvent::class))->toBeTrue();
32        expect($dispatcher->getDispatchCount())->toBe(1);
33        expect($dispatcher->getLastDispatchedEvent())->toBe($event);
34    });
35
36    it('logs event dispatch with context', function (): void {
37        $event = new FakeDomainEvent('test-data');
38        $listenerRegistry = new FakeListenerRegistry();
39        $dispatcher = new FakeDispatcher($listenerRegistry);
40        $logger = new FakeLogger();
41
42        $eventBus = new SyncEventBus($dispatcher, $listenerRegistry, $logger);
43
44        $eventBus->dispatch($event);
45
46        expect($logger->hasLog('debug', 'EventBus: Dispatching domain event', [
47            'event' => FakeDomainEvent::class,
48            'eventName' => $event->getEventName(),
49            'aggregateId' => $event->getAggregateId()->getValue(),
50        ]))->toBeTrue();
51    });
52});
53
54describe('Subscription', function (): void {
55    it('subscribes listeners to events', function (): void {
56        $listenerRegistry = new FakeListenerRegistry();
57        $dispatcher = new FakeDispatcher($listenerRegistry);
58        $logger = new NullLogger();
59        $eventBus = new SyncEventBus($dispatcher, $listenerRegistry, $logger);
60
61        $listenerCalled = false;
62        $listener = function (object $event) use (&$listenerCalled): void {
63            $listenerCalled = true;
64        };
65
66        $eventBus->subscribe(FakeDomainEvent::class, $listener);
67
68        $event = new FakeDomainEvent();
69        $dispatcher->dispatch($event);
70
71        expect($listenerCalled)->toBeTrue();
72    });
73});