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 😄

Comment on lines +49 to +51
$session = $client->getSession();
$session->set('name', 'drak');
$session->save();
Copy link
Contributor

Choose a reason for hiding this comment

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

I would expect the following behavior

        $session = $client->getSession();
        $session->set('name', 'drak');
        $session->save();
        
        $session = $client->getSession();
        $session->set('foo', 'bar');
        $session->save();
        
        $crawler = $client->request('GET', '/session');
        $this->assertStringContainsString('Welcome back drak, nice to meet you.', $crawler->text());

meaning we're not resetting the session when calling get.

Could you add it to the test ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done.

@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 from 989654b to d8011a5 Compare July 19, 2025 08:32
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
4 participants