12
12
namespace Symfony \Component \Security \Guard \Tests \Provider ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \Security \Core \Exception \AuthenticationException ;
16
+ use Symfony \Component \Security \Core \Exception \AuthenticationExpiredException ;
15
17
use Symfony \Component \Security \Core \Exception \BadCredentialsException ;
18
+ use Symfony \Component \Security \Core \User \UserCheckerInterface ;
16
19
use Symfony \Component \Security \Core \User \UserInterface ;
20
+ use Symfony \Component \Security \Core \User \UserProviderInterface ;
17
21
use Symfony \Component \Security \Guard \AuthenticatorInterface ;
18
22
use Symfony \Component \Security \Guard \Provider \GuardAuthenticationProvider ;
19
23
use Symfony \Component \Security \Guard \Token \GuardTokenInterface ;
@@ -33,9 +37,9 @@ public function testAuthenticate()
33
37
{
34
38
$ providerKey = 'my_cool_firewall ' ;
35
39
36
- $ authenticatorA = $ this ->getMockBuilder (AuthenticatorInterface::class)-> getMock ( );
37
- $ authenticatorB = $ this ->getMockBuilder (AuthenticatorInterface::class)-> getMock ( );
38
- $ authenticatorC = $ this ->getMockBuilder (AuthenticatorInterface::class)-> getMock ( );
40
+ $ authenticatorA = $ this ->createMock (AuthenticatorInterface::class);
41
+ $ authenticatorB = $ this ->createMock (AuthenticatorInterface::class);
42
+ $ authenticatorC = $ this ->createMock (AuthenticatorInterface::class);
39
43
$ authenticators = [$ authenticatorA , $ authenticatorB , $ authenticatorC ];
40
44
41
45
// called 2 times - for authenticator A and B (stops on B because of match)
@@ -58,7 +62,7 @@ public function testAuthenticate()
58
62
$ authenticatorC ->expects ($ this ->never ())
59
63
->method ('getUser ' );
60
64
61
- $ mockedUser = $ this ->getMockBuilder (UserInterface::class)-> getMock ( );
65
+ $ mockedUser = $ this ->createMock (UserInterface::class);
62
66
$ authenticatorB ->expects ($ this ->once ())
63
67
->method ('getUser ' )
64
68
->with ($ enteredCredentials , $ this ->userProvider )
@@ -69,7 +73,7 @@ public function testAuthenticate()
69
73
->with ($ enteredCredentials , $ mockedUser )
70
74
// authentication works!
71
75
->willReturn (true );
72
- $ authedToken = $ this ->getMockBuilder (GuardTokenInterface::class)-> getMock ( );
76
+ $ authedToken = $ this ->createMock (GuardTokenInterface::class);
73
77
$ authenticatorB ->expects ($ this ->once ())
74
78
->method ('createAuthenticatedToken ' )
75
79
->with ($ mockedUser , $ providerKey )
@@ -121,12 +125,12 @@ public function testCheckCredentialsReturningFalseFailsAuthentication()
121
125
122
126
public function testGuardWithNoLongerAuthenticatedTriggersLogout ()
123
127
{
124
- $ this ->expectException (\ Symfony \ Component \ Security \ Core \ Exception \ AuthenticationExpiredException::class);
128
+ $ this ->expectException (AuthenticationExpiredException::class);
125
129
$ providerKey = 'my_firewall_abc ' ;
126
130
127
131
// create a token and mark it as NOT authenticated anymore
128
132
// this mimics what would happen if a user "changed" between request
129
- $ mockedUser = $ this ->getMockBuilder (UserInterface::class)-> getMock ( );
133
+ $ mockedUser = $ this ->createMock (UserInterface::class);
130
134
$ token = new PostAuthenticationGuardToken ($ mockedUser , $ providerKey , ['ROLE_USER ' ]);
131
135
$ token ->setAuthenticated (false );
132
136
@@ -136,11 +140,11 @@ public function testGuardWithNoLongerAuthenticatedTriggersLogout()
136
140
137
141
public function testSupportsChecksGuardAuthenticatorsTokenOrigin ()
138
142
{
139
- $ authenticatorA = $ this ->getMockBuilder (AuthenticatorInterface::class)-> getMock ( );
140
- $ authenticatorB = $ this ->getMockBuilder (AuthenticatorInterface::class)-> getMock ( );
143
+ $ authenticatorA = $ this ->createMock (AuthenticatorInterface::class);
144
+ $ authenticatorB = $ this ->createMock (AuthenticatorInterface::class);
141
145
$ authenticators = [$ authenticatorA , $ authenticatorB ];
142
146
143
- $ mockedUser = $ this ->getMockBuilder (UserInterface::class)-> getMock ( );
147
+ $ mockedUser = $ this ->createMock (UserInterface::class);
144
148
$ provider = new GuardAuthenticationProvider ($ authenticators , $ this ->userProvider , 'first_firewall ' , $ this ->userChecker );
145
149
146
150
$ token = new PreAuthenticationGuardToken ($ mockedUser , 'first_firewall_1 ' );
@@ -154,12 +158,12 @@ public function testSupportsChecksGuardAuthenticatorsTokenOrigin()
154
158
155
159
public function testAuthenticateFailsOnNonOriginatingToken ()
156
160
{
157
- $ this ->expectException (\ Symfony \ Component \ Security \ Core \ Exception \ AuthenticationException::class);
161
+ $ this ->expectException (AuthenticationException::class);
158
162
$ this ->expectExceptionMessageMatches ('/second_firewall_0/ ' );
159
- $ authenticatorA = $ this ->getMockBuilder (AuthenticatorInterface::class)-> getMock ( );
163
+ $ authenticatorA = $ this ->createMock (AuthenticatorInterface::class);
160
164
$ authenticators = [$ authenticatorA ];
161
165
162
- $ mockedUser = $ this ->getMockBuilder (UserInterface::class)-> getMock ( );
166
+ $ mockedUser = $ this ->createMock (UserInterface::class);
163
167
$ provider = new GuardAuthenticationProvider ($ authenticators , $ this ->userProvider , 'first_firewall ' , $ this ->userChecker );
164
168
165
169
$ token = new PreAuthenticationGuardToken ($ mockedUser , 'second_firewall_0 ' );
@@ -168,11 +172,9 @@ public function testAuthenticateFailsOnNonOriginatingToken()
168
172
169
173
protected function setUp (): void
170
174
{
171
- $ this ->userProvider = $ this ->getMockBuilder (\Symfony \Component \Security \Core \User \UserProviderInterface::class)->getMock ();
172
- $ this ->userChecker = $ this ->getMockBuilder (\Symfony \Component \Security \Core \User \UserCheckerInterface::class)->getMock ();
173
- $ this ->preAuthenticationToken = $ this ->getMockBuilder (PreAuthenticationGuardToken::class)
174
- ->disableOriginalConstructor ()
175
- ->getMock ();
175
+ $ this ->userProvider = $ this ->createMock (UserProviderInterface::class);
176
+ $ this ->userChecker = $ this ->createMock (UserCheckerInterface::class);
177
+ $ this ->preAuthenticationToken = $ this ->createMock (PreAuthenticationGuardToken::class);
176
178
}
177
179
178
180
protected function tearDown (): void
0 commit comments