1111use OCA \User_SAML \Middleware \OnlyLoggedInMiddleware ;
1212use OCP \AppFramework \Controller ;
1313use OCP \AppFramework \Http \RedirectResponse ;
14- use OCP \AppFramework \Utility \IControllerMethodReflector ;
1514use OCP \IURLGenerator ;
1615use OCP \IUserSession ;
1716use Override ;
1817use PHPUnit \Framework \MockObject \MockObject ;
1918
2019class OnlyLoggedInMiddlewareTest extends \Test \TestCase {
2120 protected IURLGenerator &MockObject $ urlGenerator ;
22- private IControllerMethodReflector &MockObject $ reflector ;
2321 private IUserSession &MockObject $ userSession ;
24- private OnlyLoggedInMiddleware $ onlyLoggedInMiddleware ;
22+ private OnlyLoggedInMiddleware & MockObject $ onlyLoggedInMiddleware ;
2523
2624 #[Override]
2725 protected function setUp (): void {
28- $ this ->reflector = $ this ->createMock (IControllerMethodReflector::class);
2926 $ this ->userSession = $ this ->createMock (IUserSession::class);
3027 $ this ->urlGenerator = $ this ->createMock (IURLGenerator::class);
31- $ this ->onlyLoggedInMiddleware = new OnlyLoggedInMiddleware (
32- $ this ->reflector ,
33- $ this ->userSession ,
34- $ this ->urlGenerator
35- );
28+ $ this ->onlyLoggedInMiddleware = $ this ->getMockBuilder (OnlyLoggedInMiddleware::class)
29+ ->setConstructorArgs ([
30+ $ this ->userSession ,
31+ $ this ->urlGenerator ,
32+ ])
33+ ->onlyMethods (['hasAttribute ' ])
34+ ->getMock ();
3635
3736 parent ::setUp ();
3837 }
3938
4039 public function testBeforeControllerWithoutAnnotation (): void {
41- $ this ->reflector
40+ $ controller = $ this ->createMock (Controller::class);
41+ $ this ->onlyLoggedInMiddleware
4242 ->expects ($ this ->once ())
43- ->method ('hasAnnotation ' )
44- ->with (' OnlyUnauthenticatedUsers ' )
43+ ->method ('hasAttribute ' )
44+ ->with ($ controller , ' bar ' )
4545 ->willReturn (false );
4646 $ this ->userSession
4747 ->expects ($ this ->never ())
4848 ->method ('isLoggedIn ' );
4949
50- $ this ->onlyLoggedInMiddleware ->beforeController ($ this -> createMock (Controller::class) , 'bar ' );
50+ $ this ->onlyLoggedInMiddleware ->beforeController ($ controller , 'bar ' );
5151 }
5252
5353 public function testBeforeControllerWithAnnotationAndNotLoggedIn (): void {
54- $ this ->reflector
54+ $ controller = $ this ->createMock (Controller::class);
55+ $ this ->onlyLoggedInMiddleware
5556 ->expects ($ this ->once ())
56- ->method ('hasAnnotation ' )
57- ->with (' OnlyUnauthenticatedUsers ' )
57+ ->method ('hasAttribute ' )
58+ ->with ($ controller , ' bar ' )
5859 ->willReturn (true );
5960 $ this ->userSession
6061 ->expects ($ this ->once ())
6162 ->method ('isLoggedIn ' )
6263 ->willReturn (false );
6364
64- $ this ->onlyLoggedInMiddleware ->beforeController ($ this -> createMock (Controller::class) , 'bar ' );
65+ $ this ->onlyLoggedInMiddleware ->beforeController ($ controller , 'bar ' );
6566 }
6667
6768 public function testBeforeControllerWithAnnotationAndLoggedIn (): void {
68- $ this ->reflector
69+ $ controller = $ this ->createMock (Controller::class);
70+ $ this ->onlyLoggedInMiddleware
6971 ->expects ($ this ->once ())
70- ->method ('hasAnnotation ' )
71- ->with (' OnlyUnauthenticatedUsers ' )
72+ ->method ('hasAttribute ' )
73+ ->with ($ controller , ' bar ' )
7274 ->willReturn (true );
7375 $ this ->userSession
7476 ->expects ($ this ->once ())
@@ -78,7 +80,7 @@ public function testBeforeControllerWithAnnotationAndLoggedIn(): void {
7880 $ this ->expectException (Exception::class);
7981 $ this ->expectExceptionMessage ('User is already logged-in ' );
8082
81- $ this ->onlyLoggedInMiddleware ->beforeController ($ this -> createMock (Controller::class) , 'bar ' );
83+ $ this ->onlyLoggedInMiddleware ->beforeController ($ controller , 'bar ' );
8284 }
8385
8486 public function testAfterExceptionWithNormalException (): void {
0 commit comments