1
0

laravel-event-synthesizer.test.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * Laravel event-dispatch bridge (PHP).
  3. *
  4. * Laravel decouples an event dispatch from its listener(s), linked by the event class:
  5. * `event(new SongLiked($id))` has no static edge to the `handle(SongLiked $e)` that runs it
  6. * (usually a separate `app/Listeners/` file). This bridges each `event(new X(...))` site to every
  7. * listener's `handle` for X, via TWO registration mechanisms: (A) a typed `handle(EventType $e)`
  8. * (auto-discovery, union-split for `A|B`) and (B) the `protected $listen` map in an
  9. * EventServiceProvider (which also covers a listener whose `handle()` is untyped). Queued JOBS
  10. * dispatch via `::dispatch()`/`dispatch()` and their `handle()` takes a service — so only
  11. * `event(new X)` is matched and jobs are excluded.
  12. */
  13. import { describe, it, expect, beforeEach, afterEach } from 'vitest';
  14. import * as fs from 'node:fs';
  15. import * as path from 'node:path';
  16. import * as os from 'node:os';
  17. import { CodeGraph } from '../src';
  18. describe('laravel-event synthesizer', () => {
  19. let dir: string;
  20. beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'laravel-event-')); });
  21. afterEach(() => { fs.rmSync(dir, { recursive: true, force: true }); });
  22. const write = (rel: string, body: string) => {
  23. const p = path.join(dir, rel);
  24. fs.mkdirSync(path.dirname(p), { recursive: true });
  25. fs.writeFileSync(p, body);
  26. };
  27. it('bridges event(new X) to listener handles via typed handles, the $listen map, unions, and fan-out; excludes jobs', async () => {
  28. for (const [name, body] of [
  29. ['SongLiked', 'public int $id; public function __construct(int $id) { $this->id = $id; }'],
  30. ['LibraryChanged', ''],
  31. ['ScanDone', ''],
  32. ['OwnerTest', ''],
  33. ['UserTest', ''],
  34. ] as const) {
  35. write(`app/Events/${name}.php`, `<?php\nnamespace App\\Events;\nclass ${name} {\n ${body}\n}\n`);
  36. }
  37. // (A) typed-handle listener — auto-discovery, no $listen entry needed.
  38. write('app/Listeners/LoveTrack.php', `<?php
  39. namespace App\\Listeners;
  40. use App\\Events\\SongLiked;
  41. class LoveTrack {
  42. public function handle(SongLiked $event): void {}
  43. }
  44. `);
  45. // (B) UNTYPED handle — linkable only through the $listen map.
  46. write('app/Listeners/PruneLibrary.php', `<?php
  47. namespace App\\Listeners;
  48. class PruneLibrary {
  49. public function handle(): void {}
  50. }
  51. `);
  52. // Fan-out: two listeners for ScanDone.
  53. write('app/Listeners/WriteScanLog.php', `<?php
  54. namespace App\\Listeners;
  55. use App\\Events\\ScanDone;
  56. class WriteScanLog {
  57. public function handle(ScanDone $event): void {}
  58. }
  59. `);
  60. write('app/Listeners/DeleteStale.php', `<?php
  61. namespace App\\Listeners;
  62. use App\\Events\\ScanDone;
  63. class DeleteStale {
  64. public function handle(ScanDone $event): void {}
  65. }
  66. `);
  67. // Union-typed handle — one listener, two events.
  68. write('app/Listeners/SendsTestNotification.php', `<?php
  69. namespace App\\Listeners;
  70. use App\\Events\\OwnerTest;
  71. use App\\Events\\UserTest;
  72. class SendsTestNotification {
  73. public function handle(OwnerTest|UserTest $event): void {}
  74. }
  75. `);
  76. // A queued JOB — handle takes a service, dispatched via ::dispatch()/dispatch(). Never an edge.
  77. write('app/Jobs/ProcessAudio.php', `<?php
  78. namespace App\\Jobs;
  79. use App\\Services\\AudioService;
  80. class ProcessAudio implements ShouldQueue {
  81. public function handle(AudioService $svc): void {}
  82. }
  83. `);
  84. // The $listen map — registers the untyped PruneLibrary for LibraryChanged.
  85. write('app/Providers/EventServiceProvider.php', `<?php
  86. namespace App\\Providers;
  87. use App\\Events\\LibraryChanged;
  88. use App\\Listeners\\PruneLibrary;
  89. class EventServiceProvider {
  90. protected $listen = [
  91. LibraryChanged::class => [
  92. PruneLibrary::class,
  93. ],
  94. ];
  95. }
  96. `);
  97. write('app/Services/SongService.php', `<?php
  98. namespace App\\Services;
  99. use App\\Events\\SongLiked;
  100. use App\\Events\\LibraryChanged;
  101. use App\\Events\\ScanDone;
  102. use App\\Events\\OwnerTest;
  103. use App\\Events\\UserTest;
  104. use App\\Jobs\\ProcessAudio;
  105. class SongService {
  106. public function like(int $id): void { event(new SongLiked($id)); }
  107. public function deleteSongs(): void { event(new LibraryChanged()); }
  108. public function scan(): void { event(new ScanDone()); }
  109. public function ownerTest(): void { event(new OwnerTest()); }
  110. public function userTest(): void { event(new UserTest()); }
  111. public function process(): void {
  112. ProcessAudio::dispatch();
  113. dispatch(new ProcessAudio());
  114. }
  115. }
  116. `);
  117. const cg = await CodeGraph.init(dir, { silent: true });
  118. await cg.indexAll();
  119. const db = (cg as any).db.db;
  120. const edges = db
  121. .prepare(
  122. `SELECT s.name source, t.name target, t.file_path tf, json_extract(e.metadata,'$.via') via
  123. FROM edges e JOIN nodes s ON s.id = e.source JOIN nodes t ON t.id = e.target
  124. WHERE json_extract(e.metadata,'$.synthesizedBy') = 'laravel-event'`
  125. )
  126. .all();
  127. const bySrc = (s: string) => edges.filter((r: any) => r.source === s);
  128. const file = (r: any) => /(\w+)\.php$/.exec(r.tf)![1];
  129. expect(edges.length).toBe(6);
  130. expect(edges.every((r: any) => r.target === 'handle')).toBe(true);
  131. // (A) typed handle.
  132. expect(bySrc('like').map((r: any) => [r.via, file(r)])).toEqual([['SongLiked', 'LoveTrack']]);
  133. // (B) untyped handle via the $listen map.
  134. expect(bySrc('deleteSongs').map((r: any) => [r.via, file(r)])).toEqual([['LibraryChanged', 'PruneLibrary']]);
  135. // Fan-out: ScanDone → both listeners.
  136. expect(new Set(bySrc('scan').map(file))).toEqual(new Set(['WriteScanLog', 'DeleteStale']));
  137. // Union split: OwnerTest and UserTest each reach the one listener (separate dispatchers,
  138. // so they aren't deduped to a single source→target edge).
  139. expect(bySrc('ownerTest').map((r: any) => [r.via, file(r)])).toEqual([['OwnerTest', 'SendsTestNotification']]);
  140. expect(bySrc('userTest').map((r: any) => [r.via, file(r)])).toEqual([['UserTest', 'SendsTestNotification']]);
  141. // PRECISION: a queued job (::dispatch / dispatch()) produces nothing.
  142. expect(edges.some((r: any) => r.source === 'process')).toBe(false);
  143. cg.close?.();
  144. });
  145. it('produces no edges in a PHP project with no Laravel events (clean control)', async () => {
  146. write('src/Client.php', `<?php
  147. namespace Acme;
  148. class Client {
  149. public function send(string $url): string { return $url; }
  150. }
  151. `);
  152. const cg = await CodeGraph.init(dir, { silent: true });
  153. await cg.indexAll();
  154. const db = (cg as any).db.db;
  155. const count = db
  156. .prepare(`SELECT count(*) c FROM edges WHERE json_extract(metadata,'$.synthesizedBy') = 'laravel-event'`)
  157. .get();
  158. expect(count.c).toBe(0);
  159. cg.close?.();
  160. });
  161. });