-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[FrameworkBundle] Prepare session in functionnal tests #21352
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
Open
cyve
wants to merge
1
commit into
symfony:7.4
Choose a base branch
from
cyve:get-session-in-test
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43b0046
to
f3ffaba
Compare
fabpot
added a commit
to symfony/symfony
that referenced
this pull request
Sep 13, 2025
… (cyve) This PR was merged into the 7.4 branch. Discussion ---------- [FrameworkBundle] Prepare session in functionnal tests | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #46023 | License | MIT | Doc PR | symfony/symfony-docs#21352 | Hello, I propose to add a `getSession()` method to the `KernelBrowser` class to be able to prepare the session before sending a HTTP request in a functionnal test. It could be used to preset CSRF tokens, A/B testing data, user preferences, or any stateful information required for the test. I also propose to use `getSession()` in the `loginUser()` method. Then, the session could be preset either before or after the login, without risking to be overwritten. ```php public function testForm() { $client = self::createClient(); $client->loginUser(new InMemoryUser('admin', null)); $session = $client->getSession(); $session->set('_csrf/form', '123456789'); $session->set('foo', 'bar'); $session->save(); $client->request('POST', '/form', ['form' => ['_token' => '123456789']]); } ``` In this example, the session will contain : ```php array:3 [ "_security_main" => "O:52:"Symfony\Bundle\FrameworkBundle\Test\TestBrowserToken":2:{i:0;s:4:"main";i:1;a:5:{i:0;O:49:"Symfony\Component\Security\Core\User\InMemoryUser":4:{s:59:"\x00Symfony\Component\Security\Core\User\InMemoryUser\x00username";s:5:"admin";s:59:"\x00Symfony\Component\Security\Core\User\InMemoryUser\x00password";N;s:56:"\x00Symfony\Component\Security\Core\User\InMemoryUser\x00roles";a:0:{}s:58:"\x00Symfony\Component\Security\Core\User\InMemoryUser\x00enabled";b:1;}i:1;b:1;i:2;N;i:3;a:0:{}i:4;a:0:{}}}", "_csrf/form" => "123456789", "foo" => "bar" ] ``` Thanks in advance for your feedbacks 😄 Commits ------- a8f045f [FrameworkBundle] Add KernelBrowser::getSession()
symfony-splitter
pushed a commit
to symfony/framework-bundle
that referenced
this pull request
Sep 13, 2025
… (cyve) This PR was merged into the 7.4 branch. Discussion ---------- [FrameworkBundle] Prepare session in functionnal tests | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #46023 | License | MIT | Doc PR | symfony/symfony-docs#21352 | Hello, I propose to add a `getSession()` method to the `KernelBrowser` class to be able to prepare the session before sending a HTTP request in a functionnal test. It could be used to preset CSRF tokens, A/B testing data, user preferences, or any stateful information required for the test. I also propose to use `getSession()` in the `loginUser()` method. Then, the session could be preset either before or after the login, without risking to be overwritten. ```php public function testForm() { $client = self::createClient(); $client->loginUser(new InMemoryUser('admin', null)); $session = $client->getSession(); $session->set('_csrf/form', '123456789'); $session->set('foo', 'bar'); $session->save(); $client->request('POST', '/form', ['form' => ['_token' => '123456789']]); } ``` In this example, the session will contain : ```php array:3 [ "_security_main" => "O:52:"Symfony\Bundle\FrameworkBundle\Test\TestBrowserToken":2:{i:0;s:4:"main";i:1;a:5:{i:0;O:49:"Symfony\Component\Security\Core\User\InMemoryUser":4:{s:59:"\x00Symfony\Component\Security\Core\User\InMemoryUser\x00username";s:5:"admin";s:59:"\x00Symfony\Component\Security\Core\User\InMemoryUser\x00password";N;s:56:"\x00Symfony\Component\Security\Core\User\InMemoryUser\x00roles";a:0:{}s:58:"\x00Symfony\Component\Security\Core\User\InMemoryUser\x00enabled";b:1;}i:1;b:1;i:2;N;i:3;a:0:{}i:4;a:0:{}}}", "_csrf/form" => "123456789", "foo" => "bar" ] ``` Thanks in advance for your feedbacks 😄 Commits ------- a8f045fdbcb [FrameworkBundle] Add KernelBrowser::getSession()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Allow to setup session before performing a request in PHPUnit functionnal tests
Code PR: symfony/symfony#61110