Commit 3f60ead
committed
feature symfony#61110 [FrameworkBundle] Prepare session in functionnal tests (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 symfony#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()File tree
3 files changed
+46
-8
lines changed- src/Symfony/Bundle/FrameworkBundle
- Tests/Functional
3 files changed
+46
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| 21 | + | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
| |||
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
66 | 96 | | |
67 | 97 | | |
68 | 98 | | |
| |||
116 | 146 | | |
117 | 147 | | |
118 | 148 | | |
119 | | - | |
| 149 | + | |
120 | 150 | | |
121 | 151 | | |
122 | 152 | | |
123 | | - | |
124 | 153 | | |
125 | 154 | | |
126 | 155 | | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | 156 | | |
134 | 157 | | |
135 | 158 | | |
| |||
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
48 | 62 | | |
49 | 63 | | |
50 | 64 | | |
| |||
0 commit comments