From 6a30f64c55c0e157cd360e00d1199d26a931dcb8 Mon Sep 17 00:00:00 2001 From: semaz Date: Sun, 15 May 2022 18:43:26 +0300 Subject: [PATCH 1/4] Symfony 6 support (#157) --- composer.json | 18 ++++----- src/Codeception/Module/Symfony.php | 3 ++ .../Module/Symfony/SessionAssertionsTrait.php | 37 +++++++++++++++++-- 3 files changed, 45 insertions(+), 13 deletions(-) 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; } } From a0601ed73de099e98bc50d2f4b5d9fb998714b8e Mon Sep 17 00:00:00 2001 From: TavoNiievez Date: Fri, 27 May 2022 17:09:52 -0500 Subject: [PATCH 2/4] Symfony 6 Support: FormAssertionsTrait --- src/Codeception/Module/Symfony/FormAssertionsTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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.'); } From b8b9379175785abf724ab0c5d7a2e83f5b2a0d8a Mon Sep 17 00:00:00 2001 From: TavoNiievez Date: Fri, 27 May 2022 17:10:12 -0500 Subject: [PATCH 3/4] Symfony 6 Support: SessionAssertionsTrait --- .../Module/Symfony/SessionAssertionsTrait.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php index 73a440dc..e7ff9cbb 100644 --- a/src/Codeception/Module/Symfony/SessionAssertionsTrait.php +++ b/src/Codeception/Module/Symfony/SessionAssertionsTrait.php @@ -82,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)); } } @@ -167,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)); From 83eb81019ad778ff57084c451caeb94df991d4bd Mon Sep 17 00:00:00 2001 From: TavoNiievez Date: Fri, 27 May 2022 17:10:26 -0500 Subject: [PATCH 4/4] Add Symfony 6 tests --- .github/workflows/main.yml | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) 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