Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
cyve wants to merge1 commit intosymfony:7.4
base:7.4
Choose a base branch
Loading
fromcyve:get-session-in-tests

Conversation

cyve
Copy link
Contributor

QA
Branch?7.4
Bug fix?no
New feature?yes
Deprecations?no
IssuesFix#46023
LicenseMIT

Hello,

I propose to add agetSession() method to theKernelBrowser 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 usegetSession() in theloginUser() method. Then, the session could be preset either before or after the login, without risking to be overwritten.

publicfunctiontestForm(){$client =self::createClient();$client->loginUser(newInMemoryUser('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 callingget.

Could you add it to the test ?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Sure, done.

@cyvecyveforce-pushed theget-session-in-tests branch fromc470c19 to989654bCompareJuly 16, 2025 07:01
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@VincentLangletVincentLangletVincentLanglet left review comments

@alamiraultalamiraultalamirault approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
7.4
Development

Successfully merging this pull request may close these issues.

Access Session from the KernelBrowser
4 participants
@cyve@VincentLanglet@alamirault@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp