From 1c0a73a1c17e0749f7cdc5f39d5311c570dfbd0a Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 7 Apr 2023 21:21:35 +0300 Subject: [PATCH] Use guest identity instead of nulls --- src/CurrentUser.php | 6 +++--- src/UserAuth.php | 6 +----- tests/UserAuthTest.php | 5 +++-- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/CurrentUser.php b/src/CurrentUser.php index 4efc4a1..b85a7ed 100644 --- a/src/CurrentUser.php +++ b/src/CurrentUser.php @@ -9,10 +9,10 @@ use Yiisoft\Auth\IdentityInterface; use Yiisoft\Auth\IdentityRepositoryInterface; use Yiisoft\Session\SessionInterface; -use Yiisoft\User\Event\AfterLogout; use Yiisoft\User\Event\AfterLogin; -use Yiisoft\User\Event\BeforeLogout; +use Yiisoft\User\Event\AfterLogout; use Yiisoft\User\Event\BeforeLogin; +use Yiisoft\User\Event\BeforeLogout; use Yiisoft\User\Guest\GuestIdentity; use Yiisoft\User\Guest\GuestIdentityFactory; use Yiisoft\User\Guest\GuestIdentityFactoryInterface; @@ -307,7 +307,7 @@ private function afterLogout(IdentityInterface $identity): void private function switchIdentity(IdentityInterface $identity): void { $this->identity = $identity; - $this->saveId($identity instanceof GuestIdentityInterface ? null : $identity->getId()); + $this->saveId($identity->getId()); } private function getSavedId(): ?string diff --git a/src/UserAuth.php b/src/UserAuth.php index a2cf9a7..6fe4c89 100644 --- a/src/UserAuth.php +++ b/src/UserAuth.php @@ -26,12 +26,8 @@ public function __construct(CurrentUser $currentUser, ResponseFactoryInterface $ $this->responseFactory = $responseFactory; } - public function authenticate(ServerRequestInterface $request): ?IdentityInterface + public function authenticate(ServerRequestInterface $request): IdentityInterface { - if ($this->currentUser->isGuest()) { - return null; - } - return $this->currentUser->getIdentity(); } diff --git a/tests/UserAuthTest.php b/tests/UserAuthTest.php index 11e3e18..c8deaf8 100644 --- a/tests/UserAuthTest.php +++ b/tests/UserAuthTest.php @@ -10,10 +10,11 @@ use PHPUnit\Framework\TestCase; use Yiisoft\Http\Status; use Yiisoft\Test\Support\EventDispatcher\SimpleEventDispatcher; +use Yiisoft\User\CurrentUser; +use Yiisoft\User\Guest\GuestIdentityInterface; use Yiisoft\User\Tests\Support\MockArraySessionStorage; use Yiisoft\User\Tests\Support\MockIdentity; use Yiisoft\User\Tests\Support\MockIdentityRepository; -use Yiisoft\User\CurrentUser; use Yiisoft\User\UserAuth; final class UserAuthTest extends TestCase @@ -33,7 +34,7 @@ public function testIdentityNotAuthenticated(): void $user = $this->createCurrentUser(); $result = (new UserAuth($user, new ResponseFactory()))->authenticate(new ServerRequest()); - $this->assertNull($result); + $this->assertInstanceOf(GuestIdentityInterface::class, $result); } public function testChallengeIsCorrect(): void