66
77use Illuminate \Auth \GuardHelpers ;
88use Illuminate \Contracts \Auth \Authenticatable ;
9+ use Illuminate \Contracts \Auth \Factory as Auth ;
910
1011trait InteractsWithAuthentication
1112{
13+ /**
14+ * Set the given user object to the current or specified Guard.
15+ */
16+ public function amActingAs (Authenticatable $ user , string $ guardName = null ): void
17+ {
18+ if (isset ($ user ->wasRecentlyCreated ) && $ user ->wasRecentlyCreated ) {
19+ $ user ->wasRecentlyCreated = false ;
20+ }
21+
22+ $ this ->getAuth ()->guard ($ guardName )->setUser ($ user );
23+
24+ $ this ->getAuth ()->shouldUse ($ guardName );
25+ }
26+
1227 /**
1328 * Set the currently logged in user for the application.
1429 * Unlike 'amActingAs', this method does update the session, fire the login events
@@ -41,20 +56,6 @@ public function amLoggedAs($user, string $guardName = null): void
4156 );
4257 }
4358
44- /**
45- * Set the given user object to the current or specified Guard.
46- */
47- public function amActingAs (Authenticatable $ user , string $ guardName = null ): void
48- {
49- if (isset ($ user ->wasRecentlyCreated ) && $ user ->wasRecentlyCreated ) {
50- $ user ->wasRecentlyCreated = false ;
51- }
52-
53- $ this ->getAuth ()->guard ($ guardName )->setUser ($ user );
54-
55- $ this ->getAuth ()->shouldUse ($ guardName );
56- }
57-
5859 /**
5960 * Assert that the user is authenticated as the given user.
6061 */
@@ -104,19 +105,19 @@ public function dontSeeAuthentication(string $guardName = null): void
104105 }
105106
106107 /**
107- * Checks that a user is authenticated .
108+ * Logout user.
108109 */
109- public function seeAuthentication ( string $ guardName = null ): void
110+ public function logout ( ): void
110111 {
111- $ this ->assertTrue ( $ this -> isAuthenticated ( $ guardName ), ' The user is not authenticated ' );
112+ $ this ->getAuth ()-> logout ( );
112113 }
113114
114115 /**
115- * Logout user.
116+ * Checks that a user is authenticated .
116117 */
117- public function logout ( ): void
118+ public function seeAuthentication ( string $ guardName = null ): void
118119 {
119- $ this ->getAuth ()-> logout ( );
120+ $ this ->assertTrue ( $ this -> isAuthenticated ( $ guardName ), ' The user is not authenticated ' );
120121 }
121122
122123 /**
@@ -140,4 +141,12 @@ protected function isAuthenticated(?string $guardName): bool
140141 {
141142 return $ this ->getAuth ()->guard ($ guardName )->check ();
142143 }
144+
145+ /**
146+ * @return \Illuminate\Auth\AuthManager|\Illuminate\Contracts\Auth\StatefulGuard
147+ */
148+ protected function getAuth (): ?Auth
149+ {
150+ return $ this ->app ['auth ' ] ?? null ;
151+ }
143152}
0 commit comments