Skip to content

Commit 6a30f64

Browse files
authored
Symfony 6 support (#157)
1 parent 89b4af1 commit 6a30f64

File tree

3 files changed

+45
-13
lines changed

3 files changed

+45
-13
lines changed

composer.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919
"php": "^8.0",
2020
"ext-json": "*",
2121
"codeception/lib-innerbrowser": "^3.1.1",
22-
"codeception/codeception": "^5.0.0-RC1"
22+
"codeception/codeception": "^5.0.0-RC3"
2323
},
2424
"require-dev": {
2525
"codeception/module-asserts": "^3.0",
2626
"codeception/module-doctrine2": "^3.0",
2727
"doctrine/orm": "^2.10",
28-
"symfony/form": "^4.4 | ^5.0",
29-
"symfony/framework-bundle": "^4.4 | ^5.0",
30-
"symfony/http-kernel": "^4.4 | ^5.0",
31-
"symfony/mailer": "^4.4 | ^5.0",
32-
"symfony/routing": "^4.4 | ^5.0",
33-
"symfony/security-bundle": "^4.4 | ^5.0",
34-
"symfony/twig-bundle": "^4.4 | ^5.0",
35-
"vlucas/phpdotenv": "^4.2 | ^5.3"
28+
"symfony/form": "^4.4 | ^5.0 | ^6.0",
29+
"symfony/framework-bundle": "^4.4 | ^5.0 | ^6.0",
30+
"symfony/http-kernel": "^4.4 | ^5.0 | ^6.0",
31+
"symfony/mailer": "^4.4 | ^5.0 | ^6.0",
32+
"symfony/routing": "^4.4 | ^5.0 | ^6.0",
33+
"symfony/security-bundle": "^4.4 | ^5.0 | ^6.0",
34+
"symfony/twig-bundle": "^4.4 | ^5.0 | ^6.0",
35+
"vlucas/phpdotenv": "^4.2 | ^5.4"
3636
},
3737
"suggest": {
3838
"codeception/module-asserts": "Include traditional PHPUnit assertions in your tests",

src/Codeception/Module/Symfony.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
* * debug: true - Turn on/off debug mode
8484
* * cache_router: 'false' - Enable router caching between tests in order to [increase performance](http://lakion.com/blog/how-did-we-speed-up-sylius-behat-suite-with-blackfire)
8585
* * rebootable_client: 'true' - Reboot client's kernel before each request
86+
* * guard: 'false' - Enable custom authentication system with guard (only for 4.x and 5.x versions of the symfony)
87+
* * authenticator: 'false' - Reboot client's kernel before each request (only for 6.x versions of the symfony)
8688
*
8789
* #### Example (`functional.suite.yml`) - Symfony 4 Directory Structure
8890
*
@@ -160,6 +162,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
160162
'cache_router' => false,
161163
'em_service' => 'doctrine.orm.entity_manager',
162164
'rebootable_client' => true,
165+
'authenticator' => false,
163166
'guard' => false
164167
];
165168

src/Codeception/Module/Symfony/SessionAssertionsTrait.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
1111
use Symfony\Component\Security\Core\User\UserInterface;
1212
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
13+
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
1314
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
1415
use function is_int;
1516
use function serialize;
@@ -37,12 +38,22 @@ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main',
3738
{
3839
$session = $this->getCurrentSession();
3940

40-
if ($this->config['guard']) {
41-
$token = new PostAuthenticationGuardToken($user, $firewallName, $user->getRoles());
41+
if ($this->getSymfonyMajorVersion() < 6) {
42+
if ($this->config['guard']) {
43+
$token = new PostAuthenticationGuardToken($user, $firewallName, $user->getRoles());
44+
} else {
45+
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
46+
}
4247
} else {
43-
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
48+
if ($this->config['authenticator']) {
49+
$token = new PostAuthenticationToken($user, $firewallName, $user->getRoles());
50+
} else {
51+
$token = new UsernamePasswordToken($user, $firewallName, $user->getRoles());
52+
}
4453
}
4554

55+
$this->getTokenStorage()->setToken($token);
56+
4657
if ($firewallContext) {
4758
$session->set('_security_' . $firewallContext, serialize($token));
4859
} else {
@@ -199,6 +210,24 @@ protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator
199210

200211
protected function getCurrentSession(): SessionInterface
201212
{
202-
return $this->grabService('session');
213+
$container = $this->_getContainer();
214+
215+
if ($this->getSymfonyMajorVersion() < 6) {
216+
return $container->get('session');
217+
}
218+
219+
if ($container->has('session')) {
220+
return $container->get('session');
221+
}
222+
223+
$session = $container->get('session.factory')->createSession();
224+
$container->set('session', $session);
225+
226+
return $session;
227+
}
228+
229+
protected function getSymfonyMajorVersion(): int
230+
{
231+
return $this->kernel::MAJOR_VERSION;
203232
}
204233
}

0 commit comments

Comments
 (0)