diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0c991a8c..f9ef116b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: php: [8.0, 8.1] - symfony: ["4.4.*", "5.4.*"] + symfony: ["4.4.*", "5.4.*", "6.0.*"] steps: - name: Checkout code @@ -29,7 +29,7 @@ jobs: with: repository: Codeception/symfony-module-tests path: framework-tests - ref: 4.4_codecept5 + ref: "4.4_codecept5" - name: Checkout Symfony 5.4 Sample if: "matrix.symfony == '5.4.*'" @@ -37,7 +37,15 @@ jobs: with: repository: Codeception/symfony-module-tests path: framework-tests - ref: 5.4_codecept5 + ref: "5.4_codecept5" + + - name: Checkout Symfony 6.0 Sample + if: "matrix.symfony == '6.0.*'" + uses: actions/checkout@v2 + with: + repository: Codeception/symfony-module-tests + path: framework-tests + ref: "6.0" - name: Get composer cache directory id: composer-cache 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/FormAssertionsTrait.php b/src/Codeception/Module/Symfony/FormAssertionsTrait.php index ec84212b..3b923059 100644 --- a/src/Codeception/Module/Symfony/FormAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/FormAssertionsTrait.php @@ -50,7 +50,7 @@ public function seeFormErrorMessage(string $field, ?string $message = null): voi { $formCollector = $this->grabFormCollector(__FUNCTION__); - if (!$forms = $formCollector->getData()->getValue('forms')['forms']) { + if (!$forms = $formCollector->getData()->getValue(true)['forms']) { $this->fail('There are no forms on the current page.'); } diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index 4c3bc192..e7ff9cbb 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 { @@ -71,11 +82,12 @@ public function dontSeeInSession(string $attribute, $value = null): void { $session = $this->getCurrentSession(); - if (null === $value) { - if ($session->has($attribute)) { - $this->fail("Session attribute with name '{$attribute}' does exist"); - } - } else { + if ($attributeExists = $session->has($attribute)) { + $this->fail("Session attribute with name '{$attribute}' does exist"); + } + $this->assertFalse($attributeExists); + + if (null !== $value) { $this->assertNotSame($value, $session->get($attribute)); } } @@ -156,9 +168,10 @@ public function seeInSession(string $attribute, $value = null): void { $session = $this->getCurrentSession(); - if (!$session->has($attribute)) { + if (!$attributeExists = $session->has($attribute)) { $this->fail("No session attribute with name '{$attribute}'"); } + $this->assertTrue($attributeExists); if (null !== $value) { $this->assertSame($value, $session->get($attribute)); @@ -199,6 +212,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; } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: