This repository has no description
1import { IEventPublisher } from '../../../../shared/application/events/IEventPublisher';
2import { IDomainEvent } from '../../../../shared/domain/events/IDomainEvent';
3import { Result, ok, err } from '../../../../shared/core/Result';
4import { EventName } from 'src/shared/infrastructure/events/EventConfig';
5
6export class FakeEventPublisher implements IEventPublisher {
7 private publishedEvents: IDomainEvent[] = [];
8 private shouldFail: boolean = false;
9
10 async publishEvents(events: IDomainEvent[]): Promise<Result<void>> {
11 if (this.shouldFail) {
12 return err(new Error('Event publishing failed'));
13 }
14
15 this.publishedEvents.push(...events);
16 return ok(undefined);
17 }
18
19 getPublishedEvents(): IDomainEvent[] {
20 return [...this.publishedEvents];
21 }
22
23 getPublishedEventsOfType<T extends IDomainEvent>(eventType: EventName): T[] {
24 return this.publishedEvents.filter(
25 (event) => event.eventName === eventType,
26 ) as T[];
27 }
28
29 setShouldFail(shouldFail: boolean): void {
30 this.shouldFail = shouldFail;
31 }
32
33 clear(): void {
34 this.publishedEvents = [];
35 this.shouldFail = false;
36 }
37}