Skip to content

[FrameworkBundle] Prepare session in functionnal tests #61110

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
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

cyve
Copy link
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

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 😄

@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
@cyve cyve force-pushed the get-session-in-tests branch from b4a8647 to 4be5343 Compare July 23, 2025 19:59
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
5 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy