@@ -316,4 +316,68 @@ public function testMiddlewareInjectsServiceIntoDICViaCustomContainerInstance()
316316
317317 $ this ->assertEquals ($ service , $ container ->get (AuthorizationService::class));
318318 }
319+
320+ public function testRequireAuthorizationCheckCallableReturnsTrue (): void
321+ {
322+ $ this ->expectException (AuthorizationRequiredException::class);
323+
324+ $ service = $ this ->createMock (AuthorizationServiceInterface::class);
325+ $ service ->expects ($ this ->once ())
326+ ->method ('authorizationChecked ' )
327+ ->willReturn (false );
328+
329+ $ request = (new ServerRequest ())->withAttribute ('identity ' , ['id ' => 1 ]);
330+ $ handler = new TestRequestHandler ();
331+
332+ $ middleware = new AuthorizationMiddleware ($ service , [
333+ 'requireAuthorizationCheck ' => function (ServerRequestInterface $ request ): bool {
334+ return true ;
335+ },
336+ 'identityDecorator ' => IdentityDecorator::class,
337+ ]);
338+ $ middleware ->process ($ request , $ handler );
339+ }
340+
341+ public function testRequireAuthorizationCheckCallableReturnsFalse (): void
342+ {
343+ $ service = $ this ->createMock (AuthorizationServiceInterface::class);
344+ $ service ->expects ($ this ->never ())
345+ ->method ('authorizationChecked ' );
346+
347+ $ request = (new ServerRequest ())->withAttribute ('identity ' , ['id ' => 1 ]);
348+ $ handler = new TestRequestHandler ();
349+
350+ $ middleware = new AuthorizationMiddleware ($ service , [
351+ 'requireAuthorizationCheck ' => function (ServerRequestInterface $ request ): bool {
352+ return false ;
353+ },
354+ 'identityDecorator ' => IdentityDecorator::class,
355+ ]);
356+ $ result = $ middleware ->process ($ request , $ handler );
357+
358+ $ this ->assertInstanceOf (ResponseInterface::class, $ result );
359+ }
360+
361+ public function testRequireAuthorizationCheckCallableWithRouteBasedLogic (): void
362+ {
363+ $ service = $ this ->createMock (AuthorizationServiceInterface::class);
364+ $ service ->expects ($ this ->never ())
365+ ->method ('authorizationChecked ' );
366+
367+ $ request = ServerRequestFactory::fromGlobals (['REQUEST_URI ' => '/admin/queue ' ]);
368+ $ handler = new TestRequestHandler ();
369+
370+ $ middleware = new AuthorizationMiddleware ($ service , [
371+ 'requireAuthorizationCheck ' => function (ServerRequestInterface $ request ): bool {
372+ // Skip authorization check for admin/queue routes
373+ $ path = $ request ->getUri ()->getPath ();
374+
375+ return !str_contains ($ path , '/admin/queue ' );
376+ },
377+ 'identityDecorator ' => IdentityDecorator::class,
378+ ]);
379+ $ result = $ middleware ->process ($ request , $ handler );
380+
381+ $ this ->assertInstanceOf (ResponseInterface::class, $ result );
382+ }
319383}
0 commit comments