diff --git a/composer.json b/composer.json index d9ddbbcf..0363fe31 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 1d045f07..cdb206cc 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -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 * @@ -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 ]; diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index 4c3bc192..73a440dc 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -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; @@ -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 { @@ -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; } }