diff --git a/src/Illuminate/Auth/GuardHelpers.php b/src/Illuminate/Auth/GuardHelpers.php index 9a682c3dce1c..291c65d461af 100644 --- a/src/Illuminate/Auth/GuardHelpers.php +++ b/src/Illuminate/Auth/GuardHelpers.php @@ -40,6 +40,16 @@ public function authenticate() throw new AuthenticationException; } + /** + * Determine if the guard has the current user without triggering side effects. + * + * @return bool + */ + public function hasUser() + { + return ! is_null($this->user); + } + /** * Determine if the current user is authenticated. * diff --git a/tests/Auth/AuthGuardTest.php b/tests/Auth/AuthGuardTest.php index 85bf17b30b36..f353da868dda 100755 --- a/tests/Auth/AuthGuardTest.php +++ b/tests/Auth/AuthGuardTest.php @@ -178,6 +178,22 @@ public function testAuthenticateThrowsWhenUserIsNull() $guard->authenticate(); } + public function testHasUserReturnsFalseWhenUserIsNotNull() + { + $user = m::mock('Illuminate\Contracts\Auth\Authenticatable'); + $guard = $this->getGuard()->setUser($user); + + $this->assertTrue($guard->hasUser()); + } + + public function testHasUserReturnsFalseWhenUserIsNull() + { + $guard = $this->getGuard(); + $guard->getSession()->shouldNotReceive('get'); + + $this->assertFalse($guard->hasUser()); + } + public function testIsAuthedReturnsTrueWhenUserIsNotNull() { $user = m::mock('Illuminate\Contracts\Auth\Authenticatable');