44
55namespace Codeception \Module \Symfony ;
66
7+ use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasherInterface ;
78use Symfony \Component \Security \Core \Authorization \Voter \AuthenticatedVoter ;
89use Symfony \Component \Security \Core \Encoder \UserPasswordEncoderInterface ;
910use Symfony \Component \Security \Core \Security ;
@@ -64,9 +65,7 @@ public function seeAuthentication(): void
6465 {
6566 $ security = $ this ->grabSecurityService ();
6667
67- $ user = $ security ->getUser ();
68-
69- if ($ user === null ) {
68+ if (!$ user = $ security ->getUser ()) {
7069 $ this ->fail ('There is no user in session ' );
7170 }
7271
@@ -88,9 +87,7 @@ public function seeRememberedAuthentication(): void
8887 {
8988 $ security = $ this ->grabSecurityService ();
9089
91- $ user = $ security ->getUser ();
92-
93- if ($ user === null ) {
90+ if ($ security ->getUser () === null ) {
9491 $ this ->fail ('There is no user in session ' );
9592 }
9693
@@ -118,17 +115,19 @@ public function seeUserHasRole(string $role): void
118115 {
119116 $ security = $ this ->grabSecurityService ();
120117
121- $ user = $ security ->getUser ();
122-
123- if ($ user === null ) {
118+ if (!$ user = $ security ->getUser ()) {
124119 $ this ->fail ('There is no user in session ' );
125120 }
126121
122+ $ userIdentifier = method_exists ($ user , 'getUserIdentifier ' ) ?
123+ $ user ->getUserIdentifier () :
124+ $ user ->getUsername ();
125+
127126 $ this ->assertTrue (
128127 $ security ->isGranted ($ role ),
129128 sprintf (
130129 'User %s has no role %s ' ,
131- $ user -> getUsername () ,
130+ $ userIdentifier ,
132131 $ role
133132 )
134133 );
@@ -169,8 +168,7 @@ public function seeUserPasswordDoesNotNeedRehash(UserInterface $user = null): vo
169168 {
170169 if ($ user === null ) {
171170 $ security = $ this ->grabSecurityService ();
172- $ user = $ security ->getUser ();
173- if ($ user === null ) {
171+ if (!$ user = $ security ->getUser ()) {
174172 $ this ->fail ('No user found to validate ' );
175173 }
176174 }
@@ -184,8 +182,17 @@ protected function grabSecurityService(): Security
184182 return $ this ->grabService ('security.helper ' );
185183 }
186184
187- protected function grabPasswordHasherService (): UserPasswordEncoderInterface
185+ /**
186+ * @return UserPasswordHasherInterface|UserPasswordEncoderInterface
187+ */
188+ protected function grabPasswordHasherService ()
188189 {
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 ;
190197 }
191198}
0 commit comments