Skip to content

Symfony 6 support #157

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
May 15, 2022
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
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
"php": "^8.0",
"ext-json": "*",
"codeception/lib-innerbrowser": "^3.1.1",
"codeception/codeception": "^5.0.0-RC1"
"codeception/codeception": "^5.0.0-RC3"
},
"require-dev": {
"codeception/module-asserts": "^3.0",
"codeception/module-doctrine2": "^3.0",
"doctrine/orm": "^2.10",
"symfony/form": "^4.4 | ^5.0",
"symfony/framework-bundle": "^4.4 | ^5.0",
"symfony/http-kernel": "^4.4 | ^5.0",
"symfony/mailer": "^4.4 | ^5.0",
"symfony/routing": "^4.4 | ^5.0",
"symfony/security-bundle": "^4.4 | ^5.0",
"symfony/twig-bundle": "^4.4 | ^5.0",
"vlucas/phpdotenv": "^4.2 | ^5.3"
"symfony/form": "^4.4 | ^5.0 | ^6.0",
"symfony/framework-bundle": "^4.4 | ^5.0 | ^6.0",
"symfony/http-kernel": "^4.4 | ^5.0 | ^6.0",
"symfony/mailer": "^4.4 | ^5.0 | ^6.0",
"symfony/routing": "^4.4 | ^5.0 | ^6.0",
"symfony/security-bundle": "^4.4 | ^5.0 | ^6.0",
"symfony/twig-bundle": "^4.4 | ^5.0 | ^6.0",
"vlucas/phpdotenv": "^4.2 | ^5.4"
},
"suggest": {
"codeception/module-asserts": "Include traditional PHPUnit assertions in your tests",
Expand Down
3 changes: 3 additions & 0 deletions src/Codeception/Module/Symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
* * debug: true - Turn on/off debug mode
* * 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)
* * rebootable_client: 'true' - Reboot client's kernel before each request
* * guard: 'false' - Enable custom authentication system with guard (only for 4.x and 5.x versions of the symfony)
* * authenticator: 'false' - Reboot client's kernel before each request (only for 6.x versions of the symfony)
*
* #### Example (`functional.suite.yml`) - Symfony 4 Directory Structure
*
Expand Down Expand Up @@ -160,6 +162,7 @@ class Symfony extends Framework implements DoctrineProvider, PartedModule
'cache_router' => false,
'em_service' => 'doctrine.orm.entity_manager',
'rebootable_client' => true,
'authenticator' => false,
'guard' => false
];

Expand Down
37 changes: 33 additions & 4 deletions src/Codeception/Module/Symfony/SessionAssertionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
use Symfony\Component\Security\Http\Logout\LogoutUrlGenerator;
use function is_int;
use function serialize;
Expand Down Expand Up @@ -37,12 +38,22 @@ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main',
{
$session = $this->getCurrentSession();

if ($this->config['guard']) {
$token = new PostAuthenticationGuardToken($user, $firewallName, $user->getRoles());
if ($this->getSymfonyMajorVersion() < 6) {
if ($this->config['guard']) {
$token = new PostAuthenticationGuardToken($user, $firewallName, $user->getRoles());
} else {
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
}
} else {
$token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
if ($this->config['authenticator']) {
$token = new PostAuthenticationToken($user, $firewallName, $user->getRoles());
} else {
$token = new UsernamePasswordToken($user, $firewallName, $user->getRoles());
}
}

$this->getTokenStorage()->setToken($token);

if ($firewallContext) {
$session->set('_security_' . $firewallContext, serialize($token));
} else {
Expand Down Expand Up @@ -199,6 +210,24 @@ protected function getLogoutUrlGenerator(): ?LogoutUrlGenerator

protected function getCurrentSession(): SessionInterface
{
return $this->grabService('session');
$container = $this->_getContainer();

if ($this->getSymfonyMajorVersion() < 6) {
return $container->get('session');
}

if ($container->has('session')) {
return $container->get('session');
}

$session = $container->get('session.factory')->createSession();
$container->set('session', $session);

return $session;
}

protected function getSymfonyMajorVersion(): int
{
return $this->kernel::MAJOR_VERSION;
}
}