55namespace Codeception \Module \Symfony ;
66
77use Symfony \Component \Security \Core \Authorization \Voter \AuthenticatedVoter ;
8+ use Symfony \Component \Security \Core \Encoder \UserPasswordEncoderInterface ;
89use Symfony \Component \Security \Core \Security ;
910use Symfony \Component \Security \Core \User \UserInterface ;
1011use function sprintf ;
@@ -21,8 +22,7 @@ trait SecurityAssertionsTrait
2122 */
2223 public function dontSeeAuthentication (): void
2324 {
24- /** @var Security $security */
25- $ security = $ this ->grabService ('security.helper ' );
25+ $ security = $ this ->grabSecurityService ();
2626
2727 $ this ->assertFalse (
2828 $ security ->isGranted (AuthenticatedVoter::IS_AUTHENTICATED_FULLY ),
@@ -40,8 +40,7 @@ public function dontSeeAuthentication(): void
4040 */
4141 public function dontSeeRememberedAuthentication (): void
4242 {
43- /** @var Security $security */
44- $ security = $ this ->grabService ('security.helper ' );
43+ $ security = $ this ->grabSecurityService ();
4544
4645 $ hasRememberMeCookie = $ this ->client ->getCookieJar ()->get ('REMEMBERME ' );
4746 $ hasRememberMeRole = $ security ->isGranted (AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED );
@@ -63,8 +62,7 @@ public function dontSeeRememberedAuthentication(): void
6362 */
6463 public function seeAuthentication (): void
6564 {
66- /** @var Security $security */
67- $ security = $ this ->grabService ('security.helper ' );
65+ $ security = $ this ->grabSecurityService ();
6866
6967 $ user = $ security ->getUser ();
7068
@@ -88,8 +86,7 @@ public function seeAuthentication(): void
8886 */
8987 public function seeRememberedAuthentication (): void
9088 {
91- /** @var Security $security */
92- $ security = $ this ->grabService ('security.helper ' );
89+ $ security = $ this ->grabSecurityService ();
9390
9491 $ user = $ security ->getUser ();
9592
@@ -119,8 +116,7 @@ public function seeRememberedAuthentication(): void
119116 */
120117 public function seeUserHasRole (string $ role ): void
121118 {
122- /** @var Security $security */
123- $ security = $ this ->grabService ('security.helper ' );
119+ $ security = $ this ->grabSecurityService ();
124120
125121 $ user = $ security ->getUser ();
126122
@@ -172,15 +168,24 @@ public function seeUserHasRoles(array $roles): void
172168 public function seeUserPasswordDoesNotNeedRehash (UserInterface $ user = null ): void
173169 {
174170 if ($ user === null ) {
175- /** @var Security $security */
176- $ security = $ this ->grabService ('security.helper ' );
171+ $ security = $ this ->grabSecurityService ();
177172 $ user = $ security ->getUser ();
178173 if ($ user === null ) {
179174 $ this ->fail ('No user found to validate ' );
180175 }
181176 }
182- $ encoder = $ this ->grabService ( ' security.user_password_encoder.generic ' );
177+ $ hasher = $ this ->grabPasswordHasherService ( );
183178
184- $ this ->assertFalse ($ encoder ->needsRehash ($ user ), 'User password needs rehash ' );
179+ $ this ->assertFalse ($ hasher ->needsRehash ($ user ), 'User password needs rehash ' );
180+ }
181+
182+ protected function grabSecurityService (): Security
183+ {
184+ return $ this ->grabService ('security.helper ' );
185+ }
186+
187+ protected function grabPasswordHasherService (): UserPasswordEncoderInterface
188+ {
189+ return $ this ->grabService ('security.user_password_encoder.generic ' );
185190 }
186191}
0 commit comments