Skip to content

Conversation

cyve
Copy link

@cyve cyve commented Sep 6, 2025

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

Code PR: symfony/symfony#61110

@carsonbot carsonbot added this to the 7.4 milestone Sep 6, 2025
@carsonbot carsonbot changed the title [WCM][FrameworkBundle] Prepare session in functionnal tests [FrameworkBundle][WCM] Prepare session in functionnal tests Sep 6, 2025
@cyve cyve force-pushed the get-session-in-test branch from 43b0046 to f3ffaba Compare September 6, 2025 15:26
@javiereguiluz javiereguiluz added the Waiting Code Merge Docs for features pending to be merged label Sep 11, 2025
@carsonbot carsonbot modified the milestones: 7.4, next Sep 11, 2025
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()
@cyve cyve changed the title [FrameworkBundle][WCM] Prepare session in functionnal tests [FrameworkBundle] Prepare session in functionnal tests Sep 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants