12
12
namespace Symfony \Component \Security \Http \Tests \Authenticator ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
- use Symfony \Component \HttpFoundation \Cookie ;
16
15
use Symfony \Component \HttpFoundation \Request ;
17
16
use Symfony \Component \Security \Core \Authentication \Token \RememberMeToken ;
18
17
use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorage ;
19
18
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
20
19
use Symfony \Component \Security \Core \User \User ;
21
20
use Symfony \Component \Security \Http \Authenticator \RememberMeAuthenticator ;
22
- use Symfony \Component \Security \Http \RememberMe \AbstractRememberMeServices ;
23
21
use Symfony \Component \Security \Http \RememberMe \RememberMeServicesInterface ;
24
22
25
23
class RememberMeAuthenticatorTest extends TestCase
@@ -37,8 +35,6 @@ protected function setUp(): void
37
35
'name ' => '_remember_me_cookie ' ,
38
36
]);
39
37
$ this ->request = new Request ();
40
- $ this ->request ->cookies ->set ('_remember_me_cookie ' , $ val = $ this ->generateCookieValue ());
41
- $ this ->request ->attributes ->set (AbstractRememberMeServices::COOKIE_ATTR_NAME , new Cookie ('_remember_me_cookie ' , $ val ));
42
38
}
43
39
44
40
public function testSupportsTokenStorageWithToken ()
@@ -48,39 +44,34 @@ public function testSupportsTokenStorageWithToken()
48
44
$ this ->assertFalse ($ this ->authenticator ->supports ($ this ->request ));
49
45
}
50
46
51
- public function testSupportsRequestWithoutAttribute ()
47
+ /**
48
+ * @dataProvider provideSupportsData
49
+ */
50
+ public function testSupports ($ autoLoginResult , $ support )
52
51
{
53
- $ this ->request -> attributes -> remove (AbstractRememberMeServices:: COOKIE_ATTR_NAME );
52
+ $ this ->rememberMeServices -> expects ( $ this -> once ())-> method ( ' autoLogin ' )-> with ( $ this -> request )-> willReturn ( $ autoLoginResult );
54
53
55
- $ this ->assertNull ( $ this ->authenticator ->supports ($ this ->request ));
54
+ $ this ->assertSame ( $ support , $ this ->authenticator ->supports ($ this ->request ));
56
55
}
57
56
58
- public function testSupportsRequestWithoutCookie ()
57
+ public function provideSupportsData ()
59
58
{
60
- $ this ->request ->cookies ->remove ('_remember_me_cookie ' );
61
-
62
- $ this ->assertFalse ($ this ->authenticator ->supports ($ this ->request ));
63
- }
64
-
65
- public function testSupports ()
66
- {
67
- $ this ->assertNull ($ this ->authenticator ->supports ($ this ->request ));
59
+ yield [null , false ];
60
+ yield [$ this ->createMock (TokenInterface::class), null ];
68
61
}
69
62
70
63
public function testAuthenticate ()
71
64
{
72
- $ this ->rememberMeServices ->expects ($ this ->once ())
73
- ->method ('autoLogin ' )
74
- ->with ($ this ->request )
75
- ->willReturn (new RememberMeToken ($ user = new User ('wouter ' , 'test ' ), 'main ' , 'secret ' ));
76
-
65
+ $ this ->request ->attributes ->set ('_remember_me_token ' , new RememberMeToken ($ user = new User ('wouter ' , 'test ' ), 'main ' , 'secret ' ));
77
66
$ passport = $ this ->authenticator ->authenticate ($ this ->request );
78
67
79
68
$ this ->assertSame ($ user , $ passport ->getUser ());
80
69
}
81
70
82
- private function generateCookieValue ()
71
+ public function testAuthenticateWithoutToken ()
83
72
{
84
- return base64_encode (implode (AbstractRememberMeServices::COOKIE_DELIMITER , ['part1 ' , 'part2 ' ]));
73
+ $ this ->expectException (\LogicException::class);
74
+
75
+ $ this ->authenticator ->authenticate ($ this ->request );
85
76
}
86
77
}
0 commit comments