Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Prepare session in functional tests#61110

Merged
fabpot merged 1 commit into
symfony:7.4from
cyve:get-session-in-tests
Sep 13, 2025
Merged

[FrameworkBundle] Prepare session in functional tests#61110
fabpot merged 1 commit into
symfony:7.4from
cyve:get-session-in-tests

Conversation

@cyve
Copy link
Copy Markdown
Contributor

@cyve cyve commented Jul 13, 2025

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.

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 :

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 😄

Comment thread src/Symfony/Bundle/FrameworkBundle/Tests/Functional/SessionTest.php
@cyve cyve force-pushed the get-session-in-tests branch from c470c19 to 989654b Compare July 16, 2025 07:01
@cyve cyve force-pushed the get-session-in-tests branch 2 times, most recently from d8011a5 to b4a8647 Compare July 22, 2025 19:57
Comment thread src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md Outdated
Comment thread src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php Outdated
Comment thread src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php Outdated
Comment thread src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php Outdated
Comment thread src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php Outdated
@cyve cyve force-pushed the get-session-in-tests branch from b4a8647 to 4be5343 Compare July 23, 2025 19:59
@doMynation
Copy link
Copy Markdown

doMynation commented Sep 3, 2025

Bumping this, as that would IMO be a great addition to the framework. Preparing the request with some session data before the request is emitted can be very useful to mock previous user interactions in multi-step operations.

@cyve
Copy link
Copy Markdown
Contributor Author

cyve commented Sep 6, 2025

I added a PR in the documentation : symfony/symfony-docs#21352

@cyve cyve force-pushed the get-session-in-tests branch 2 times, most recently from b2e1cc1 to a4ebe76 Compare September 10, 2025 18:15
Comment on lines +149 to +150
$session = $this->getSession();
if (!$session instanceof SessionInterface) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$session = $this->getSession();
if (!$session instanceof SessionInterface) {
if (!$session = $this->getSession()) {

$session->set('foo', 'bar');
$session->save();

// prove remembered name from programmatically prepared session
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// prove remembered name from programmatically prepared session
// prove remembered name from programatically prepared session

* Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()`
* Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait`
* Add `framework.type_info.aliases` option
* Add `KernelBrowser::getSession()` method
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Add `KernelBrowser::getSession()` method
* Add `KernelBrowser::getSession()`

@fabpot fabpot force-pushed the get-session-in-tests branch from a4ebe76 to a8f045f Compare September 13, 2025 06:26
@fabpot
Copy link
Copy Markdown
Member

fabpot commented Sep 13, 2025

Thank you @cyve.

@fabpot fabpot merged commit 3f60ead into symfony:7.4 Sep 13, 2025
5 of 12 checks passed
This was referenced Oct 27, 2025
@javiereguiluz javiereguiluz changed the title [FrameworkBundle] Prepare session in functionnal tests [FrameworkBundle] Prepare session in functional tests Feb 17, 2026
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Feb 17, 2026
…yve)

This PR was merged into the 7.4 branch.

Discussion
----------

[FrameworkBundle] Prepare session in functional tests

Allow to setup session before performing a request in PHPUnit functionnal tests

Code PR:
* symfony/symfony#61110

Commits
-------

4c96d19 [FrameworkBundle] Add KernelBrowser::getSession()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Access Session from the KernelBrowser

8 participants