4
4
5
5
namespace Codeception \Module \Symfony ;
6
6
7
+ use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasherInterface ;
7
8
use Symfony \Component \Security \Core \Authorization \Voter \AuthenticatedVoter ;
8
9
use Symfony \Component \Security \Core \Encoder \UserPasswordEncoderInterface ;
9
10
use Symfony \Component \Security \Core \Security ;
@@ -64,9 +65,7 @@ public function seeAuthentication(): void
64
65
{
65
66
$ security = $ this ->grabSecurityService ();
66
67
67
- $ user = $ security ->getUser ();
68
-
69
- if ($ user === null ) {
68
+ if (!$ user = $ security ->getUser ()) {
70
69
$ this ->fail ('There is no user in session ' );
71
70
}
72
71
@@ -88,9 +87,7 @@ public function seeRememberedAuthentication(): void
88
87
{
89
88
$ security = $ this ->grabSecurityService ();
90
89
91
- $ user = $ security ->getUser ();
92
-
93
- if ($ user === null ) {
90
+ if ($ security ->getUser () === null ) {
94
91
$ this ->fail ('There is no user in session ' );
95
92
}
96
93
@@ -118,17 +115,19 @@ public function seeUserHasRole(string $role): void
118
115
{
119
116
$ security = $ this ->grabSecurityService ();
120
117
121
- $ user = $ security ->getUser ();
122
-
123
- if ($ user === null ) {
118
+ if (!$ user = $ security ->getUser ()) {
124
119
$ this ->fail ('There is no user in session ' );
125
120
}
126
121
122
+ $ userIdentifier = method_exists ($ user , 'getUserIdentifier ' ) ?
123
+ $ user ->getUserIdentifier () :
124
+ $ user ->getUsername ();
125
+
127
126
$ this ->assertTrue (
128
127
$ security ->isGranted ($ role ),
129
128
sprintf (
130
129
'User %s has no role %s ' ,
131
- $ user -> getUsername () ,
130
+ $ userIdentifier ,
132
131
$ role
133
132
)
134
133
);
@@ -169,8 +168,7 @@ public function seeUserPasswordDoesNotNeedRehash(UserInterface $user = null): vo
169
168
{
170
169
if ($ user === null ) {
171
170
$ security = $ this ->grabSecurityService ();
172
- $ user = $ security ->getUser ();
173
- if ($ user === null ) {
171
+ if (!$ user = $ security ->getUser ()) {
174
172
$ this ->fail ('No user found to validate ' );
175
173
}
176
174
}
@@ -184,8 +182,17 @@ protected function grabSecurityService(): Security
184
182
return $ this ->grabService ('security.helper ' );
185
183
}
186
184
187
- protected function grabPasswordHasherService (): UserPasswordEncoderInterface
185
+ /**
186
+ * @return UserPasswordHasherInterface|UserPasswordEncoderInterface
187
+ */
188
+ protected function grabPasswordHasherService ()
188
189
{
189
- return $ this ->grabService ('security.user_password_encoder.generic ' );
190
+ $ hasher = $ this ->getService ('security.password_hasher ' ) ?: $ this ->getService ('security.password_encoder ' );
191
+
192
+ if ($ hasher === null ) {
193
+ $ this ->fail ('Password hasher service could not be found. ' );
194
+ }
195
+
196
+ return $ hasher ;
190
197
}
191
198
}
0 commit comments