Skip to content

Added Symfony 5.3 support #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 21 additions & 14 deletions src/Codeception/Module/Symfony/SecurityAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

Expand All @@ -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');
}

Expand Down Expand Up @@ -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
)
);
Expand Down Expand Up @@ -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');
}
}
Expand All @@ -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;
}
}
10 changes: 5 additions & 5 deletions src/Codeception/Module/Symfony/SessionAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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)) {
Expand All @@ -94,7 +94,7 @@ public function logout(): void
$tokenStorage->setToken();
}

$session = $this->grabSessionService();
$session = $this->getCurrentSession();

$sessionName = $session->getName();
$session->invalidate();
Expand Down Expand Up @@ -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}'");
Expand Down Expand Up @@ -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');
}
Expand Down