12
12
namespace Symfony \Component \Security \Core \Tests \Authentication ;
13
13
14
14
use PHPUnit \Framework \TestCase ;
15
+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
15
16
use Symfony \Component \Security \Core \Authentication \AuthenticationProviderManager ;
17
+ use Symfony \Component \Security \Core \Authentication \Provider \AuthenticationProviderInterface ;
16
18
use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
17
19
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
18
20
use Symfony \Component \Security \Core \AuthenticationEvents ;
@@ -35,7 +37,7 @@ public function testAuthenticateWithProvidersWithIncorrectInterface()
35
37
$ this ->expectException (\InvalidArgumentException::class);
36
38
(new AuthenticationProviderManager ([
37
39
new \stdClass (),
38
- ]))->authenticate ($ this ->getMockBuilder (TokenInterface::class)-> getMock ( ));
40
+ ]))->authenticate ($ this ->createMock (TokenInterface::class));
39
41
}
40
42
41
43
public function testAuthenticateWhenNoProviderSupportsToken ()
@@ -45,7 +47,7 @@ public function testAuthenticateWhenNoProviderSupportsToken()
45
47
]);
46
48
47
49
try {
48
- $ manager ->authenticate ($ token = $ this ->getMockBuilder (TokenInterface::class)-> getMock ( ));
50
+ $ manager ->authenticate ($ token = $ this ->createMock (TokenInterface::class));
49
51
$ this ->fail ();
50
52
} catch (ProviderNotFoundException $ e ) {
51
53
$ this ->assertSame ($ token , $ e ->getToken ());
@@ -54,18 +56,18 @@ public function testAuthenticateWhenNoProviderSupportsToken()
54
56
55
57
public function testAuthenticateWhenProviderReturnsAccountStatusException ()
56
58
{
57
- $ secondAuthenticationProvider = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Core \ Authentication \ Provider \ AuthenticationProviderInterface::class)-> getMock ( );
59
+ $ secondAuthenticationProvider = $ this ->createMock ( AuthenticationProviderInterface::class);
58
60
59
61
$ manager = new AuthenticationProviderManager ([
60
- $ this ->getAuthenticationProvider (true , null , ' Symfony\Component\Security\Core\Exception\ AccountStatusException' ),
62
+ $ this ->getAuthenticationProvider (true , null , AccountStatusException::class ),
61
63
$ secondAuthenticationProvider ,
62
64
]);
63
65
64
66
// AccountStatusException stops authentication
65
67
$ secondAuthenticationProvider ->expects ($ this ->never ())->method ('supports ' );
66
68
67
69
try {
68
- $ manager ->authenticate ($ token = $ this ->getMockBuilder (TokenInterface::class)-> getMock ( ));
70
+ $ manager ->authenticate ($ token = $ this ->createMock (TokenInterface::class));
69
71
$ this ->fail ();
70
72
} catch (AccountStatusException $ e ) {
71
73
$ this ->assertSame ($ token , $ e ->getToken ());
@@ -75,11 +77,11 @@ public function testAuthenticateWhenProviderReturnsAccountStatusException()
75
77
public function testAuthenticateWhenProviderReturnsAuthenticationException ()
76
78
{
77
79
$ manager = new AuthenticationProviderManager ([
78
- $ this ->getAuthenticationProvider (true , null , ' Symfony\Component\Security\Core\Exception\ AuthenticationException' ),
80
+ $ this ->getAuthenticationProvider (true , null , AuthenticationException::class ),
79
81
]);
80
82
81
83
try {
82
- $ manager ->authenticate ($ token = $ this ->getMockBuilder (TokenInterface::class)-> getMock ( ));
84
+ $ manager ->authenticate ($ token = $ this ->createMock (TokenInterface::class));
83
85
$ this ->fail ();
84
86
} catch (AuthenticationException $ e ) {
85
87
$ this ->assertSame ($ token , $ e ->getToken ());
@@ -89,27 +91,27 @@ public function testAuthenticateWhenProviderReturnsAuthenticationException()
89
91
public function testAuthenticateWhenOneReturnsAuthenticationExceptionButNotAll ()
90
92
{
91
93
$ manager = new AuthenticationProviderManager ([
92
- $ this ->getAuthenticationProvider (true , null , ' Symfony\Component\Security\Core\Exception\ AuthenticationException' ),
93
- $ this ->getAuthenticationProvider (true , $ expected = $ this ->getMockBuilder (TokenInterface::class)-> getMock ( )),
94
+ $ this ->getAuthenticationProvider (true , null , AuthenticationException::class ),
95
+ $ this ->getAuthenticationProvider (true , $ expected = $ this ->createMock (TokenInterface::class)),
94
96
]);
95
97
96
- $ token = $ manager ->authenticate ($ this ->getMockBuilder (TokenInterface::class)-> getMock ( ));
98
+ $ token = $ manager ->authenticate ($ this ->createMock (TokenInterface::class));
97
99
$ this ->assertSame ($ expected , $ token );
98
100
}
99
101
100
102
public function testAuthenticateReturnsTokenOfTheFirstMatchingProvider ()
101
103
{
102
- $ second = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Core \ Authentication \ Provider \ AuthenticationProviderInterface::class)-> getMock ( );
104
+ $ second = $ this ->createMock ( AuthenticationProviderInterface::class);
103
105
$ second
104
106
->expects ($ this ->never ())
105
107
->method ('supports ' )
106
108
;
107
109
$ manager = new AuthenticationProviderManager ([
108
- $ this ->getAuthenticationProvider (true , $ expected = $ this ->getMockBuilder (TokenInterface::class)-> getMock ( )),
110
+ $ this ->getAuthenticationProvider (true , $ expected = $ this ->createMock (TokenInterface::class)),
109
111
$ second ,
110
112
]);
111
113
112
- $ token = $ manager ->authenticate ($ this ->getMockBuilder (TokenInterface::class)-> getMock ( ));
114
+ $ token = $ manager ->authenticate ($ this ->createMock (TokenInterface::class));
113
115
$ this ->assertSame ($ expected , $ token );
114
116
}
115
117
@@ -119,25 +121,25 @@ public function testEraseCredentialFlag()
119
121
$ this ->getAuthenticationProvider (true , $ token = new UsernamePasswordToken ('foo ' , 'bar ' , 'key ' )),
120
122
]);
121
123
122
- $ token = $ manager ->authenticate ($ this ->getMockBuilder (TokenInterface::class)-> getMock ( ));
124
+ $ token = $ manager ->authenticate ($ this ->createMock (TokenInterface::class));
123
125
$ this ->assertEquals ('' , $ token ->getCredentials ());
124
126
125
127
$ manager = new AuthenticationProviderManager ([
126
128
$ this ->getAuthenticationProvider (true , $ token = new UsernamePasswordToken ('foo ' , 'bar ' , 'key ' )),
127
129
], false );
128
130
129
- $ token = $ manager ->authenticate ($ this ->getMockBuilder (TokenInterface::class)-> getMock ( ));
131
+ $ token = $ manager ->authenticate ($ this ->createMock (TokenInterface::class));
130
132
$ this ->assertEquals ('bar ' , $ token ->getCredentials ());
131
133
}
132
134
133
135
public function testAuthenticateDispatchesAuthenticationFailureEvent ()
134
136
{
135
137
$ token = new UsernamePasswordToken ('foo ' , 'bar ' , 'key ' );
136
- $ provider = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Core \ Authentication \ Provider \ AuthenticationProviderInterface::class)-> getMock ( );
138
+ $ provider = $ this ->createMock ( AuthenticationProviderInterface::class);
137
139
$ provider ->expects ($ this ->once ())->method ('supports ' )->willReturn (true );
138
140
$ provider ->expects ($ this ->once ())->method ('authenticate ' )->willThrowException ($ exception = new AuthenticationException ());
139
141
140
- $ dispatcher = $ this ->getMockBuilder (\ Symfony \ Component \ EventDispatcher \ EventDispatcherInterface::class)-> getMock ( );
142
+ $ dispatcher = $ this ->createMock ( EventDispatcherInterface::class);
141
143
$ dispatcher
142
144
->expects ($ this ->once ())
143
145
->method ('dispatch ' )
@@ -158,11 +160,11 @@ public function testAuthenticateDispatchesAuthenticationSuccessEvent()
158
160
{
159
161
$ token = new UsernamePasswordToken ('foo ' , 'bar ' , 'key ' );
160
162
161
- $ provider = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Core \ Authentication \ Provider \ AuthenticationProviderInterface::class)-> getMock ( );
163
+ $ provider = $ this ->createMock ( AuthenticationProviderInterface::class);
162
164
$ provider ->expects ($ this ->once ())->method ('supports ' )->willReturn (true );
163
165
$ provider ->expects ($ this ->once ())->method ('authenticate ' )->willReturn ($ token );
164
166
165
- $ dispatcher = $ this ->getMockBuilder (\ Symfony \ Component \ EventDispatcher \ EventDispatcherInterface::class)-> getMock ( );
167
+ $ dispatcher = $ this ->createMock ( EventDispatcherInterface::class);
166
168
$ dispatcher
167
169
->expects ($ this ->once ())
168
170
->method ('dispatch ' )
@@ -176,7 +178,7 @@ public function testAuthenticateDispatchesAuthenticationSuccessEvent()
176
178
177
179
protected function getAuthenticationProvider ($ supports , $ token = null , $ exception = null )
178
180
{
179
- $ provider = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Core \ Authentication \ Provider \ AuthenticationProviderInterface::class)-> getMock ( );
181
+ $ provider = $ this ->createMock ( AuthenticationProviderInterface::class);
180
182
$ provider ->expects ($ this ->once ())
181
183
->method ('supports ' )
182
184
->willReturn ($ supports )
0 commit comments