1515
1616use InvalidArgumentException ;
1717use Mezzio \Authentication \UserInterface ;
18+ use Mezzio \Router \Route ;
1819use Mezzio \Router \RouteResult ;
1920use Mimmi20 \Mezzio \GenericAuthorization \Exception \RuntimeException ;
2021use PHPUnit \Event \NoPreviousThrowableException ;
@@ -193,12 +194,7 @@ public function testProcessWithRouteError(): void
193194
194195 $ user = $ this ->createMock (UserInterface::class);
195196
196- $ routeResult = $ this ->createMock (RouteResult::class);
197- $ routeResult ->expects (self ::once ())
198- ->method ('isFailure ' )
199- ->willReturn (true );
200- $ routeResult ->expects (self ::never ())
201- ->method ('getMatchedRouteName ' );
197+ $ routeResult = RouteResult::fromRouteFailure (null );
202198
203199 assert ($ authorization instanceof AuthorizationInterface);
204200 assert ($ responseFactory instanceof ResponseFactoryInterface);
@@ -255,13 +251,7 @@ public function testProcessWithRouteError2(): void
255251
256252 $ user = $ this ->createMock (UserInterface::class);
257253
258- $ routeResult = $ this ->createMock (RouteResult::class);
259- $ routeResult ->expects (self ::once ())
260- ->method ('isFailure ' )
261- ->willReturn (false );
262- $ routeResult ->expects (self ::once ())
263- ->method ('getMatchedRouteName ' )
264- ->willReturn (false );
254+ $ routeResult = RouteResult::fromRouteFailure (null );
265255
266256 assert ($ authorization instanceof AuthorizationInterface);
267257 assert ($ responseFactory instanceof ResponseFactoryInterface);
@@ -324,17 +314,10 @@ public function testProcessRoleNotGranted(): void
324314 ->method ('getRoles ' )
325315 ->willReturn ([]);
326316
327- $ routeResult = $ this ->createMock (RouteResult::class);
328- $ routeResult ->expects (self ::once ())
329- ->method ('isFailure ' )
330- ->willReturn (false );
331- $ routeResult ->expects (self ::exactly (2 ))
332- ->method ('getMatchedRouteName ' )
333- ->willReturn ($ routeName );
334-
335- assert ($ authorization instanceof AuthorizationInterface);
336- assert ($ responseFactory instanceof ResponseFactoryInterface);
337317 $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
318+
319+ $ routeResult = RouteResult::fromRoute (new Route ('/ ' , $ middleware , name: $ routeName ));
320+
338321 self ::assertInstanceOf (AuthorizationMiddleware::class, $ middleware );
339322
340323 $ request = $ this ->createMock (ServerRequestInterface::class);
@@ -389,23 +372,10 @@ public function testProcessRoleNotGranted2(): void
389372 ->method ('getRoles ' )
390373 ->willReturn ([]);
391374
392- $ routeResult = $ this ->createMock (RouteResult::class);
393- $ routeResult ->expects (self ::once ())
394- ->method ('isFailure ' )
395- ->willReturn (false );
396- $ matcher = self ::exactly (2 );
397- $ routeResult ->expects ($ matcher )
398- ->method ('getMatchedRouteName ' )
399- ->willReturnCallback (
400- static fn (): bool | string => match ($ matcher ->numberOfInvocations ()) {
401- 1 => true ,
402- default => $ routeName ,
403- },
404- );
405-
406- assert ($ authorization instanceof AuthorizationInterface);
407- assert ($ responseFactory instanceof ResponseFactoryInterface);
408375 $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
376+
377+ $ routeResult = RouteResult::fromRoute (new Route ('/ ' , $ middleware , name: $ routeName ));
378+
409379 self ::assertInstanceOf (AuthorizationMiddleware::class, $ middleware );
410380
411381 $ request = $ this ->createMock (ServerRequestInterface::class);
@@ -460,17 +430,10 @@ public function testProcessRoleNotGrantedException(): void
460430 ->method ('getRoles ' )
461431 ->willReturn ([]);
462432
463- $ routeResult = $ this ->createMock (RouteResult::class);
464- $ routeResult ->expects (self ::once ())
465- ->method ('isFailure ' )
466- ->willReturn (false );
467- $ routeResult ->expects (self ::exactly (2 ))
468- ->method ('getMatchedRouteName ' )
469- ->willReturn ($ routeName );
470-
471- assert ($ authorization instanceof AuthorizationInterface);
472- assert ($ responseFactory instanceof ResponseFactoryInterface);
473433 $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
434+
435+ $ routeResult = RouteResult::fromRoute (new Route ('/ ' , $ middleware , name: $ routeName ));
436+
474437 self ::assertInstanceOf (AuthorizationMiddleware::class, $ middleware );
475438
476439 $ request = $ this ->createMock (ServerRequestInterface::class);
@@ -521,13 +484,15 @@ public function testProcessGrantedWithRoles(): void
521484 ->method ('getRoles ' )
522485 ->willReturn ([$ role1 , $ role2 ]);
523486
524- $ routeResult = $ this ->createMock (RouteResult::class);
525- $ routeResult ->expects (self ::once ())
526- ->method ('isFailure ' )
527- ->willReturn (false );
528- $ routeResult ->expects (self ::exactly (2 ))
529- ->method ('getMatchedRouteName ' )
530- ->willReturn ($ routeName );
487+ $ authorization = $ this ->createMock (AuthorizationInterface::class);
488+
489+ $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
490+ $ responseFactory ->expects (self ::never ())
491+ ->method ('createResponse ' );
492+
493+ $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
494+
495+ $ routeResult = RouteResult::fromRoute (new Route ('/ ' , $ middleware , name: $ routeName ));
531496
532497 $ request = $ this ->createMock (ServerRequestInterface::class);
533498 $ matcher = self ::exactly (2 );
@@ -549,8 +514,7 @@ static function (string $name, mixed $default = null) use ($matcher, $user, $rou
549514 },
550515 );
551516
552- $ authorization = $ this ->createMock (AuthorizationInterface::class);
553- $ matcher = self ::exactly (2 );
517+ $ matcher = self ::exactly (2 );
554518 $ authorization ->expects ($ matcher )
555519 ->method ('isGranted ' )
556520 ->willReturnCallback (
@@ -584,13 +548,6 @@ static function (
584548
585549 $ expectedResponse = $ this ->createMock (ResponseInterface::class);
586550
587- $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
588- $ responseFactory ->expects (self ::never ())
589- ->method ('createResponse ' );
590-
591- assert ($ authorization instanceof AuthorizationInterface);
592- assert ($ responseFactory instanceof ResponseFactoryInterface);
593- $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
594551 self ::assertInstanceOf (AuthorizationMiddleware::class, $ middleware );
595552
596553 $ handler = $ this ->createMock (RequestHandlerInterface::class);
@@ -623,13 +580,15 @@ public function testProcessGrantedWithRoles2(): void
623580 ->method ('getRoles ' )
624581 ->willReturn ([$ role1 , $ role2 ]);
625582
626- $ routeResult = $ this ->createMock (RouteResult::class);
627- $ routeResult ->expects (self ::once ())
628- ->method ('isFailure ' )
629- ->willReturn (false );
630- $ routeResult ->expects (self ::exactly (2 ))
631- ->method ('getMatchedRouteName ' )
632- ->willReturn ($ routeName );
583+ $ authorization = $ this ->createMock (AuthorizationInterface::class);
584+
585+ $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
586+ $ responseFactory ->expects (self ::never ())
587+ ->method ('createResponse ' );
588+
589+ $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
590+
591+ $ routeResult = RouteResult::fromRoute (new Route ('/ ' , $ middleware , name: $ routeName ));
633592
634593 $ request = $ this ->createMock (ServerRequestInterface::class);
635594 $ matcher = self ::exactly (2 );
@@ -651,21 +610,13 @@ static function (string $name, mixed $default = null) use ($matcher, $user, $rou
651610 },
652611 );
653612
654- $ authorization = $ this ->createMock (AuthorizationInterface::class);
655613 $ authorization ->expects (self ::once ())
656614 ->method ('isGranted ' )
657615 ->with ($ role1 , $ routeName , null , $ request )
658616 ->willReturn (true );
659617
660618 $ expectedResponse = $ this ->createMock (ResponseInterface::class);
661619
662- $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
663- $ responseFactory ->expects (self ::never ())
664- ->method ('createResponse ' );
665-
666- assert ($ authorization instanceof AuthorizationInterface);
667- assert ($ responseFactory instanceof ResponseFactoryInterface);
668- $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
669620 self ::assertInstanceOf (AuthorizationMiddleware::class, $ middleware );
670621
671622 $ handler = $ this ->createMock (RequestHandlerInterface::class);
@@ -696,13 +647,15 @@ public function testProcessGrantedWithoutRoles(): void
696647 ->method ('getRoles ' )
697648 ->willReturn ([]);
698649
699- $ routeResult = $ this ->createMock (RouteResult::class);
700- $ routeResult ->expects (self ::once ())
701- ->method ('isFailure ' )
702- ->willReturn (false );
703- $ routeResult ->expects (self ::exactly (2 ))
704- ->method ('getMatchedRouteName ' )
705- ->willReturn ($ routeName );
650+ $ authorization = $ this ->createMock (AuthorizationInterface::class);
651+
652+ $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
653+ $ responseFactory ->expects (self ::never ())
654+ ->method ('createResponse ' );
655+
656+ $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
657+
658+ $ routeResult = RouteResult::fromRoute (new Route ('/ ' , $ middleware , name: $ routeName ));
706659
707660 $ request = $ this ->createMock (ServerRequestInterface::class);
708661 $ matcher = self ::exactly (2 );
@@ -724,21 +677,13 @@ static function (string $name, mixed $default = null) use ($matcher, $user, $rou
724677 },
725678 );
726679
727- $ authorization = $ this ->createMock (AuthorizationInterface::class);
728680 $ authorization ->expects (self ::once ())
729681 ->method ('isGranted ' )
730682 ->with (null , $ routeName , null , $ request )
731683 ->willReturn (true );
732684
733685 $ expectedResponse = $ this ->createMock (ResponseInterface::class);
734686
735- $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
736- $ responseFactory ->expects (self ::never ())
737- ->method ('createResponse ' );
738-
739- assert ($ authorization instanceof AuthorizationInterface);
740- assert ($ responseFactory instanceof ResponseFactoryInterface);
741- $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
742687 self ::assertInstanceOf (AuthorizationMiddleware::class, $ middleware );
743688
744689 $ handler = $ this ->createMock (RequestHandlerInterface::class);
@@ -771,13 +716,19 @@ public function testProcessNotGrantedWithRoles(): void
771716 ->method ('getRoles ' )
772717 ->willReturn ([$ role1 , $ role2 ]);
773718
774- $ routeResult = $ this ->createMock (RouteResult::class);
775- $ routeResult ->expects (self ::once ())
776- ->method ('isFailure ' )
777- ->willReturn (false );
778- $ routeResult ->expects (self ::exactly (2 ))
779- ->method ('getMatchedRouteName ' )
780- ->willReturn ($ routeName );
719+ $ expectedResponse = $ this ->createMock (ResponseInterface::class);
720+
721+ $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
722+ $ responseFactory ->expects (self ::once ())
723+ ->method ('createResponse ' )
724+ ->with (403 , '' )
725+ ->willReturn ($ expectedResponse );
726+
727+ $ authorization = $ this ->createMock (AuthorizationInterface::class);
728+
729+ $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
730+
731+ $ routeResult = RouteResult::fromRoute (new Route ('/ ' , $ middleware , name: $ routeName ));
781732
782733 $ request = $ this ->createMock (ServerRequestInterface::class);
783734 $ matcher = self ::exactly (2 );
@@ -799,8 +750,7 @@ static function (string $name, mixed $default = null) use ($matcher, $user, $rou
799750 },
800751 );
801752
802- $ authorization = $ this ->createMock (AuthorizationInterface::class);
803- $ matcher = self ::exactly (2 );
753+ $ matcher = self ::exactly (2 );
804754 $ authorization ->expects ($ matcher )
805755 ->method ('isGranted ' )
806756 ->willReturnCallback (
@@ -829,17 +779,6 @@ static function (
829779 },
830780 );
831781
832- $ expectedResponse = $ this ->createMock (ResponseInterface::class);
833-
834- $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
835- $ responseFactory ->expects (self ::once ())
836- ->method ('createResponse ' )
837- ->with (403 , '' )
838- ->willReturn ($ expectedResponse );
839-
840- assert ($ authorization instanceof AuthorizationInterface);
841- assert ($ responseFactory instanceof ResponseFactoryInterface);
842- $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
843782 self ::assertInstanceOf (AuthorizationMiddleware::class, $ middleware );
844783
845784 $ handler = $ this ->createMock (RequestHandlerInterface::class);
@@ -868,13 +807,19 @@ public function testProcessNotGrantedWithoutRoles(): void
868807 ->method ('getRoles ' )
869808 ->willReturn ([]);
870809
871- $ routeResult = $ this ->createMock (RouteResult::class);
872- $ routeResult ->expects (self ::once ())
873- ->method ('isFailure ' )
874- ->willReturn (false );
875- $ routeResult ->expects (self ::exactly (2 ))
876- ->method ('getMatchedRouteName ' )
877- ->willReturn ($ routeName );
810+ $ expectedResponse = $ this ->createMock (ResponseInterface::class);
811+
812+ $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
813+ $ responseFactory ->expects (self ::once ())
814+ ->method ('createResponse ' )
815+ ->with (403 , '' )
816+ ->willReturn ($ expectedResponse );
817+
818+ $ authorization = $ this ->createMock (AuthorizationInterface::class);
819+
820+ $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
821+
822+ $ routeResult = RouteResult::fromRoute (new Route ('/ ' , $ middleware , name: $ routeName ));
878823
879824 $ request = $ this ->createMock (ServerRequestInterface::class);
880825 $ matcher = self ::exactly (2 );
@@ -896,23 +841,11 @@ static function (string $name, mixed $default = null) use ($matcher, $user, $rou
896841 },
897842 );
898843
899- $ authorization = $ this ->createMock (AuthorizationInterface::class);
900844 $ authorization ->expects (self ::once ())
901845 ->method ('isGranted ' )
902846 ->with (null , $ routeName , null , $ request )
903847 ->willReturn (false );
904848
905- $ expectedResponse = $ this ->createMock (ResponseInterface::class);
906-
907- $ responseFactory = $ this ->createMock (ResponseFactoryInterface::class);
908- $ responseFactory ->expects (self ::once ())
909- ->method ('createResponse ' )
910- ->with (403 , '' )
911- ->willReturn ($ expectedResponse );
912-
913- assert ($ authorization instanceof AuthorizationInterface);
914- assert ($ responseFactory instanceof ResponseFactoryInterface);
915- $ middleware = new AuthorizationMiddleware ($ authorization , $ responseFactory , null );
916849 self ::assertInstanceOf (AuthorizationMiddleware::class, $ middleware );
917850
918851 $ handler = $ this ->createMock (RequestHandlerInterface::class);
0 commit comments