diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index da8b8d71..2eb0a711 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: php: [7.3, 7.4, 8.0] - symfony: [4.4, 5.2] + symfony: [4.4, 5.3] steps: - name: Checkout code @@ -31,8 +31,8 @@ jobs: path: framework-tests ref: 4.4 - - name: Checkout Symfony 5.2 Sample - if: matrix.symfony == 5.2 + - name: Checkout Symfony 5.3 Sample + if: matrix.symfony == 5.3 uses: actions/checkout@v2 with: repository: Codeception/symfony-module-tests diff --git a/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php b/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php index 8ae73d00..674c3153 100644 --- a/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SecurityAssertionsTrait.php @@ -4,6 +4,7 @@ namespace Codeception\Module\Symfony; +use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use Symfony\Component\Security\Core\Security; @@ -64,9 +65,7 @@ public function seeAuthentication(): void { $security = $this->grabSecurityService(); - $user = $security->getUser(); - - if ($user === null) { + if (!$user = $security->getUser()) { $this->fail('There is no user in session'); } @@ -88,9 +87,7 @@ public function seeRememberedAuthentication(): void { $security = $this->grabSecurityService(); - $user = $security->getUser(); - - if ($user === null) { + if ($security->getUser() === null) { $this->fail('There is no user in session'); } @@ -118,17 +115,19 @@ public function seeUserHasRole(string $role): void { $security = $this->grabSecurityService(); - $user = $security->getUser(); - - if ($user === null) { + if (!$user = $security->getUser()) { $this->fail('There is no user in session'); } + $userIdentifier = method_exists($user, 'getUserIdentifier') ? + $user->getUserIdentifier() : + $user->getUsername(); + $this->assertTrue( $security->isGranted($role), sprintf( 'User %s has no role %s', - $user->getUsername(), + $userIdentifier, $role ) ); @@ -169,8 +168,7 @@ public function seeUserPasswordDoesNotNeedRehash(UserInterface $user = null): vo { if ($user === null) { $security = $this->grabSecurityService(); - $user = $security->getUser(); - if ($user === null) { + if (!$user = $security->getUser()) { $this->fail('No user found to validate'); } } @@ -184,8 +182,17 @@ protected function grabSecurityService(): Security return $this->grabService('security.helper'); } - protected function grabPasswordHasherService(): UserPasswordEncoderInterface + /** + * @return UserPasswordHasherInterface|UserPasswordEncoderInterface + */ + protected function grabPasswordHasherService() { - return $this->grabService('security.user_password_encoder.generic'); + $hasher = $this->getService('security.password_hasher') ?: $this->getService('security.password_encoder'); + + if ($hasher === null) { + $this->fail('Password hasher service could not be found.'); + } + + return $hasher; } } \ No newline at end of file diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index 92a60fe8..f562fad3 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -34,7 +34,7 @@ trait SessionAssertionsTrait */ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', $firewallContext = null): void { - $session = $this->grabSessionService(); + $session = $this->getCurrentSession(); if ($this->config['guard']) { $token = new PostAuthenticationGuardToken($user, $firewallName, $user->getRoles()); @@ -68,7 +68,7 @@ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', */ public function dontSeeInSession(string $attribute, $value = null): void { - $session = $this->grabSessionService(); + $session = $this->getCurrentSession(); if (null === $value) { if ($session->has($attribute)) { @@ -94,7 +94,7 @@ public function logout(): void $tokenStorage->setToken(); } - $session = $this->grabSessionService(); + $session = $this->getCurrentSession(); $sessionName = $session->getName(); $session->invalidate(); @@ -126,7 +126,7 @@ public function logout(): void */ public function seeInSession(string $attribute, $value = null): void { - $session = $this->grabSessionService(); + $session = $this->getCurrentSession(); if (!$session->has($attribute)) { $this->fail("No session attribute with name '{$attribute}'"); @@ -164,7 +164,7 @@ protected function getTokenStorage(): ?TokenStorageInterface return $this->getService('security.token_storage'); } - protected function grabSessionService(): SessionInterface + protected function getCurrentSession(): SessionInterface { return $this->grabService('session'); }