Skip to content

Commit 3ee8015

Browse files
authored
Added Symfony 5.3 support (#133)
1 parent 828acaa commit 3ee8015

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
matrix:
1111
php: [7.3, 7.4, 8.0]
12-
symfony: [4.4, 5.2]
12+
symfony: [4.4, 5.3]
1313

1414
steps:
1515
- name: Checkout code
@@ -31,8 +31,8 @@ jobs:
3131
path: framework-tests
3232
ref: 4.4
3333

34-
- name: Checkout Symfony 5.2 Sample
35-
if: matrix.symfony == 5.2
34+
- name: Checkout Symfony 5.3 Sample
35+
if: matrix.symfony == 5.3
3636
uses: actions/checkout@v2
3737
with:
3838
repository: Codeception/symfony-module-tests

src/Codeception/Module/Symfony/SecurityAssertionsTrait.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Codeception\Module\Symfony;
66

7+
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
78
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
89
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
910
use 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
}

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ trait SessionAssertionsTrait
3434
*/
3535
public function amLoggedInAs(UserInterface $user, string $firewallName = 'main', $firewallContext = null): void
3636
{
37-
$session = $this->grabSessionService();
37+
$session = $this->getCurrentSession();
3838

3939
if ($this->config['guard']) {
4040
$token = new PostAuthenticationGuardToken($user, $firewallName, $user->getRoles());
@@ -68,7 +68,7 @@ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main',
6868
*/
6969
public function dontSeeInSession(string $attribute, $value = null): void
7070
{
71-
$session = $this->grabSessionService();
71+
$session = $this->getCurrentSession();
7272

7373
if (null === $value) {
7474
if ($session->has($attribute)) {
@@ -94,7 +94,7 @@ public function logout(): void
9494
$tokenStorage->setToken();
9595
}
9696

97-
$session = $this->grabSessionService();
97+
$session = $this->getCurrentSession();
9898

9999
$sessionName = $session->getName();
100100
$session->invalidate();
@@ -126,7 +126,7 @@ public function logout(): void
126126
*/
127127
public function seeInSession(string $attribute, $value = null): void
128128
{
129-
$session = $this->grabSessionService();
129+
$session = $this->getCurrentSession();
130130

131131
if (!$session->has($attribute)) {
132132
$this->fail("No session attribute with name '{$attribute}'");
@@ -164,7 +164,7 @@ protected function getTokenStorage(): ?TokenStorageInterface
164164
return $this->getService('security.token_storage');
165165
}
166166

167-
protected function grabSessionService(): SessionInterface
167+
protected function getCurrentSession(): SessionInterface
168168
{
169169
return $this->grabService('session');
170170
}

0 commit comments

Comments
 (0)