@@ -29,37 +29,17 @@ trait SessionAssertionsTrait
2929 * ]);
3030 * $I->amLoggedInAs($user);
3131 * ```
32- *
33- * @param UserInterface $user
34- * @param string $firewallName
35- * @param null $firewallContext
3632 */
37- public function amLoggedInAs (UserInterface $ user , string $ firewallName = 'main ' , $ firewallContext = null ): void
33+ public function amLoggedInAs (UserInterface $ user , string $ firewallName = 'main ' , string $ firewallContext = null ): void
3834 {
3935 $ session = $ this ->getCurrentSession ();
36+ $ roles = $ user ->getRoles ();
4037
41- if ($ this ->getSymfonyMajorVersion () < 6 ) {
42- if ($ this ->config ['guard ' ]) {
43- $ token = new PostAuthenticationGuardToken ($ user , $ firewallName , $ user ->getRoles ());
44- } else {
45- $ token = new UsernamePasswordToken ($ user , null , $ firewallName , $ user ->getRoles ());
46- }
47- } else {
48- if ($ this ->config ['authenticator ' ]) {
49- $ token = new PostAuthenticationToken ($ user , $ firewallName , $ user ->getRoles ());
50- } else {
51- $ token = new UsernamePasswordToken ($ user , $ firewallName , $ user ->getRoles ());
52- }
53- }
54-
38+ $ token = $ this ->createAuthenticationToken ($ user , $ firewallName , $ roles );
5539 $ this ->getTokenStorage ()->setToken ($ token );
5640
57- if ($ firewallContext ) {
58- $ session ->set ('_security_ ' . $ firewallContext , serialize ($ token ));
59- } else {
60- $ session ->set ('_security_ ' . $ firewallName , serialize ($ token ));
61- }
62-
41+ $ sessionKey = $ firewallContext ? "_security_ {$ firewallContext }" : "_security_ {$ firewallName }" ;
42+ $ session ->set ($ sessionKey , serialize ($ token ));
6343 $ session ->save ();
6444
6545 $ cookie = new Cookie ($ session ->getName (), $ session ->getId ());
@@ -74,16 +54,13 @@ public function amLoggedInAs(UserInterface $user, string $firewallName = 'main',
7454 * $I->dontSeeInSession('attribute');
7555 * $I->dontSeeInSession('attribute', 'value');
7656 * ```
77- *
7857 */
7958 public function dontSeeInSession (string $ attribute , mixed $ value = null ): void
8059 {
8160 $ session = $ this ->getCurrentSession ();
8261
83- if ($ attributeExists = $ session ->has ($ attribute )) {
84- $ this ->fail ("Session attribute with name ' {$ attribute }' does exist " );
85- }
86- $ this ->assertFalse ($ attributeExists );
62+ $ attributeExists = $ session ->has ($ attribute );
63+ $ this ->assertFalse ($ attributeExists , "Session attribute ' {$ attribute }' exists. " );
8764
8865 if (null !== $ value ) {
8966 $ this ->assertNotSame ($ value , $ session ->get ($ attribute ));
@@ -98,8 +75,7 @@ public function dontSeeInSession(string $attribute, mixed $value = null): void
9875 */
9976 public function goToLogoutPath (): void
10077 {
101- $ logoutUrlGenerator = $ this ->getLogoutUrlGenerator ();
102- $ logoutPath = $ logoutUrlGenerator ->getLogoutPath ();
78+ $ logoutPath = $ this ->getLogoutUrlGenerator ()->getLogoutPath ();
10379 $ this ->amOnPage ($ logoutPath );
10480 }
10581
@@ -132,17 +108,14 @@ public function logoutProgrammatically(): void
132108 }
133109
134110 $ session = $ this ->getCurrentSession ();
135-
136111 $ sessionName = $ session ->getName ();
137112 $ session ->invalidate ();
138113
139114 $ cookieJar = $ this ->client ->getCookieJar ();
115+ $ cookiesToExpire = ['MOCKSESSID ' , 'REMEMBERME ' , $ sessionName ];
140116 foreach ($ cookieJar ->all () as $ cookie ) {
141117 $ cookieName = $ cookie ->getName ();
142- if ($ cookieName === 'MOCKSESSID ' ||
143- $ cookieName === 'REMEMBERME ' ||
144- $ cookieName === $ sessionName
145- ) {
118+ if (in_array ($ cookieName , $ cookiesToExpire , true )) {
146119 $ cookieJar ->expire ($ cookieName );
147120 }
148121 }
@@ -163,10 +136,8 @@ public function seeInSession(string $attribute, mixed $value = null): void
163136 {
164137 $ session = $ this ->getCurrentSession ();
165138
166- if (!$ attributeExists = $ session ->has ($ attribute )) {
167- $ this ->fail ("No session attribute with name ' {$ attribute }' " );
168- }
169- $ this ->assertTrue ($ attributeExists );
139+ $ attributeExists = $ session ->has ($ attribute );
140+ $ this ->assertTrue ($ attributeExists , "No session attribute with name ' {$ attribute }' " );
170141
171142 if (null !== $ value ) {
172143 $ this ->assertSame ($ value , $ session ->get ($ attribute ));
@@ -181,8 +152,6 @@ public function seeInSession(string $attribute, mixed $value = null): void
181152 * $I->seeSessionHasValues(['key1', 'key2']);
182153 * $I->seeSessionHasValues(['key1' => 'value1', 'key2' => 'value2']);
183154 * ```
184- *
185- * @param array $bindings
186155 */
187156 public function seeSessionHasValues (array $ bindings ): void
188157 {
@@ -227,4 +196,20 @@ protected function getSymfonyMajorVersion(): int
227196 {
228197 return $ this ->kernel ::MAJOR_VERSION ;
229198 }
199+
200+ /**
201+ * @return UsernamePasswordToken|PostAuthenticationGuardToken|PostAuthenticationToken
202+ */
203+ protected function createAuthenticationToken (UserInterface $ user , string $ firewallName , array $ roles )
204+ {
205+ if ($ this ->getSymfonyMajorVersion () < 6 ) {
206+ return $ this ->config ['guard ' ]
207+ ? new PostAuthenticationGuardToken ($ user , $ firewallName , $ roles )
208+ : new UsernamePasswordToken ($ user , null , $ firewallName , $ roles );
209+ }
210+
211+ return $ this ->config ['authenticator ' ]
212+ ? new PostAuthenticationToken ($ user , $ firewallName , $ roles )
213+ : new UsernamePasswordToken ($ user , $ firewallName , $ roles );
214+ }
230215}
0 commit comments