Skip to content

Commit 22bd132

Browse files
committed
refactor: update DispatcherTest
1 parent c1fa21e commit 22bd132

File tree

1 file changed

+89
-20
lines changed

1 file changed

+89
-20
lines changed

tests/Unit/DispatcherTest.php

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ class DispatcherTest extends TestCase
2828
public function testConstructorRegistersRoutes(): void
2929
{
3030
$routes = [
31-
'test/method' => $this->mockRoute,
31+
$this->mockRoute,
3232
];
3333

34+
$this->mockRoute
35+
->expects($this->once())
36+
->method('getMethods')
37+
->willReturn(['test/method']);
38+
3439
$this->routesFactory
3540
->expects($this->once())
3641
->method('create')
@@ -59,7 +64,12 @@ public function testHandleRequestWithExistingRoute(): void
5964
->with($request, $this->context)
6065
->willReturn($expectedResult);
6166

62-
$routes = ['test/method' => $this->mockRoute];
67+
$this->mockRoute
68+
->expects($this->once())
69+
->method('getMethods')
70+
->willReturn(['test/method']);
71+
72+
$routes = [$this->mockRoute];
6373
$this->routesFactory
6474
->method('create')
6575
->willReturn($routes);
@@ -84,7 +94,13 @@ public function testHandleRequestWithNonExistingRoute(): void
8494
params: [],
8595
);
8696

87-
$routes = ['test/method' => $this->mockRoute];
97+
$routes = [$this->mockRoute];
98+
99+
$this->mockRoute
100+
->expects($this->once())
101+
->method('getMethods')
102+
->willReturn(['test/method']);
103+
88104
$this->routesFactory
89105
->method('create')
90106
->willReturn($routes);
@@ -122,7 +138,12 @@ public function testHandleRequestLogsRequestDetails(): void
122138
->method('handleRequest')
123139
->willReturn($expectedResult);
124140

125-
$routes = ['test/method' => $this->mockRoute];
141+
$this->mockRoute
142+
->expects($this->once())
143+
->method('getMethods')
144+
->willReturn(['test/method']);
145+
146+
$routes = [$this->mockRoute];
126147
$this->routesFactory
127148
->method('create')
128149
->willReturn($routes);
@@ -149,7 +170,12 @@ public function testHandleNotificationWithExistingRoute(): void
149170
->method('handleNotification')
150171
->with($notification, $this->context);
151172

152-
$routes = ['test/notification' => $this->mockRoute];
173+
$this->mockRoute
174+
->expects($this->once())
175+
->method('getMethods')
176+
->willReturn(['test/notification']);
177+
178+
$routes = [$this->mockRoute];
153179
$this->routesFactory
154180
->method('create')
155181
->willReturn($routes);
@@ -171,7 +197,13 @@ public function testHandleNotificationWithNonExistingRoute(): void
171197
params: [],
172198
);
173199

174-
$routes = ['test/notification' => $this->mockRoute];
200+
$routes = [$this->mockRoute];
201+
202+
$this->mockRoute
203+
->expects($this->once())
204+
->method('getMethods')
205+
->willReturn(['test/notification']);
206+
175207
$this->routesFactory
176208
->method('create')
177209
->willReturn($routes);
@@ -200,7 +232,12 @@ public function testHandleNotificationDoesNotCallRouteWhenNotFound(): void
200232
->expects($this->never())
201233
->method('handleNotification');
202234

203-
$routes = ['test/notification' => $this->mockRoute];
235+
$this->mockRoute
236+
->expects($this->once())
237+
->method('getMethods')
238+
->willReturn(['test/notification']);
239+
240+
$routes = [$this->mockRoute];
204241
$this->routesFactory
205242
->method('create')
206243
->willReturn($routes);
@@ -220,7 +257,12 @@ public function testHandleNotificationLogsNotificationDetails(): void
220257
$this->mockRoute
221258
->method('handleNotification');
222259

223-
$routes = ['test/notification' => $this->mockRoute];
260+
$this->mockRoute
261+
->expects($this->once())
262+
->method('getMethods')
263+
->willReturn(['test/notification']);
264+
265+
$routes = [$this->mockRoute];
224266
$this->routesFactory
225267
->method('create')
226268
->willReturn($routes);
@@ -238,17 +280,12 @@ public function testMultipleRoutesRegistration(): void
238280
$route1 = $this->createMock(RouteInterface::class);
239281
$route2 = $this->createMock(RouteInterface::class);
240282

241-
$routes = [
242-
'method/one' => $route1,
243-
'method/two' => $route2,
244-
];
283+
$routes = [$route1, $route2];
245284

246285
$this->routesFactory
247286
->method('create')
248287
->willReturn($routes);
249288

250-
$dispatcher = new Dispatcher($this->logger, $this->routesFactory);
251-
252289
// Test first route
253290
$request1 = new Request('2.0', 1, 'method/one', []);
254291
$result1 = $this->createMock(Result::class);
@@ -259,8 +296,10 @@ public function testMultipleRoutesRegistration(): void
259296
->with($request1, $this->context)
260297
->willReturn($result1);
261298

262-
$actualResult1 = $dispatcher->handleRequest($request1, $this->context);
263-
$this->assertSame($result1, $actualResult1);
299+
$route1
300+
->expects($this->once())
301+
->method('getMethods')
302+
->willReturn(['method/one']);
264303

265304
// Test second route
266305
$request2 = new Request('2.0', 2, 'method/two', []);
@@ -272,6 +311,16 @@ public function testMultipleRoutesRegistration(): void
272311
->with($request2, $this->context)
273312
->willReturn($result2);
274313

314+
$route2
315+
->expects($this->once())
316+
->method('getMethods')
317+
->willReturn(['method/two']);
318+
319+
$dispatcher = new Dispatcher($this->logger, $this->routesFactory);
320+
321+
$actualResult1 = $dispatcher->handleRequest($request1, $this->context);
322+
$this->assertSame($result1, $actualResult1);
323+
275324
$actualResult2 = $dispatcher->handleRequest($request2, $this->context);
276325
$this->assertSame($result2, $actualResult2);
277326
}
@@ -302,7 +351,12 @@ public function testRouteExceptionPropagation(): void
302351
->method('handleRequest')
303352
->willThrowException($customException);
304353

305-
$routes = ['test/method' => $this->mockRoute];
354+
$this->mockRoute
355+
->expects($this->once())
356+
->method('getMethods')
357+
->willReturn(['test/method']);
358+
359+
$routes = [$this->mockRoute];
306360
$this->routesFactory
307361
->method('create')
308362
->willReturn($routes);
@@ -325,7 +379,12 @@ public function testNotificationRouteExceptionHandling(): void
325379
->method('handleNotification')
326380
->willThrowException($customException);
327381

328-
$routes = ['test/notification' => $this->mockRoute];
382+
$this->mockRoute
383+
->expects($this->once())
384+
->method('getMethods')
385+
->willReturn(['test/notification']);
386+
387+
$routes = [$this->mockRoute];
329388
$this->routesFactory
330389
->method('create')
331390
->willReturn($routes);
@@ -370,7 +429,12 @@ public function testHandleRequestWithNullParams(): void
370429
->with($request, $this->context)
371430
->willReturn($expectedResult);
372431

373-
$routes = ['test/method' => $this->mockRoute];
432+
$this->mockRoute
433+
->expects($this->once())
434+
->method('getMethods')
435+
->willReturn(['test/method']);
436+
437+
$routes = [$this->mockRoute];
374438
$this->routesFactory
375439
->method('create')
376440
->willReturn($routes);
@@ -398,7 +462,12 @@ public function testHandleNotificationWithNullParams(): void
398462
->method('handleNotification')
399463
->with($notification, $this->context);
400464

401-
$routes = ['test/notification' => $this->mockRoute];
465+
$this->mockRoute
466+
->expects($this->once())
467+
->method('getMethods')
468+
->willReturn(['test/notification']);
469+
470+
$routes = [$this->mockRoute];
402471
$this->routesFactory
403472
->method('create')
404473
->willReturn($routes);

0 commit comments

Comments
 (0)