From 38bcd9d8a6d369eb57b9d4af661da4967511c749 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 1 Jun 2022 10:59:10 +0900 Subject: [PATCH 1/3] test: add folders and move test files --- .../AccessTokenAuthenticatorTest.php | 32 +++++++++---------- .../SessionAuthenticatorTest.php | 22 ++++++------- .../{ => Filters}/ChainFilterTest.php | 18 +++++------ .../{ => Filters}/SessionFilterTest.php | 14 ++++---- .../{ => Filters}/TokenFilterTest.php | 16 +++++----- 5 files changed, 51 insertions(+), 51 deletions(-) rename tests/Authentication/{ => Authenticators}/AccessTokenAuthenticatorTest.php (87%) rename tests/Authentication/{ => Authenticators}/SessionAuthenticatorTest.php (93%) rename tests/Authentication/{ => Filters}/ChainFilterTest.php (79%) rename tests/Authentication/{ => Filters}/SessionFilterTest.php (81%) rename tests/Authentication/{ => Filters}/TokenFilterTest.php (79%) diff --git a/tests/Authentication/AccessTokenAuthenticatorTest.php b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php similarity index 87% rename from tests/Authentication/AccessTokenAuthenticatorTest.php rename to tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php index aa97ab190..c075d88a5 100644 --- a/tests/Authentication/AccessTokenAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php @@ -1,6 +1,6 @@ setProvider(model(UserModel::class)); // @phpstan-ignore-line + $auth->setProvider(\model(UserModel::class)); // @phpstan-ignore-line /** @var AccessTokens $authenticator */ $authenticator = $auth->factory('tokens'); @@ -39,7 +39,7 @@ protected function setUp(): void public function testLogin() { - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $this->auth->login($user); @@ -51,7 +51,7 @@ public function testLogin() public function testLogout() { // this one's a little odd since it's stateless, but roll with it... - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $this->auth->login($user); $this->assertNotNull($this->auth->getUser()); @@ -62,7 +62,7 @@ public function testLogout() public function testLoginByIdNoToken() { - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $this->assertFalse($this->auth->loggedIn()); @@ -75,7 +75,7 @@ public function testLoginByIdNoToken() public function testLoginByIdWithToken() { /** @var User $user */ - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $token = $user->generateAccessToken('foo'); $this->setRequestHeader($token->raw_token); @@ -90,7 +90,7 @@ public function testLoginByIdWithToken() public function testLoginByIdWithMultipleTokens() { /** @var User $user */ - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $token1 = $user->generateAccessToken('foo'); $user->generateAccessToken('bar'); @@ -108,7 +108,7 @@ public function testCheckNoToken() $result = $this->auth->check([]); $this->assertFalse($result->isOK()); - $this->assertSame(lang('Auth.noToken'), $result->reason()); + $this->assertSame(\lang('Auth.noToken'), $result->reason()); } public function testCheckBadToken() @@ -116,15 +116,15 @@ public function testCheckBadToken() $result = $this->auth->check(['token' => 'abc123']); $this->assertFalse($result->isOK()); - $this->assertSame(lang('Auth.badToken'), $result->reason()); + $this->assertSame(\lang('Auth.badToken'), $result->reason()); } public function testCheckOldToken() { /** @var User $user */ - $user = fake(UserModel::class); + $user = \fake(UserModel::class); /** @var UserIdentityModel $identities */ - $identities = model(UserIdentityModel::class); + $identities = \model(UserIdentityModel::class); $token = $user->generateAccessToken('foo'); $token->last_used_at = Time::now()->subYears(1)->subMinutes(1); $identities->save($token); @@ -132,13 +132,13 @@ public function testCheckOldToken() $result = $this->auth->check(['token' => $token->raw_token]); $this->assertFalse($result->isOK()); - $this->assertSame(lang('Auth.oldToken'), $result->reason()); + $this->assertSame(\lang('Auth.oldToken'), $result->reason()); } public function testCheckSuccess() { /** @var User $user */ - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $token = $user->generateAccessToken('foo'); $this->seeInDatabase('auth_identities', [ @@ -165,7 +165,7 @@ public function testAttemptCannotFindUser() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(lang('Auth.badToken'), $result->reason()); + $this->assertSame(\lang('Auth.badToken'), $result->reason()); // A login attempt should have always been recorded $this->seeInDatabase('auth_token_logins', [ @@ -178,7 +178,7 @@ public function testAttemptCannotFindUser() public function testAttemptSuccess() { /** @var User $user */ - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $token = $user->generateAccessToken('foo'); $this->setRequestHeader($token->raw_token); @@ -205,7 +205,7 @@ public function testAttemptSuccess() protected function setRequestHeader(string $token) { - $request = service('request'); + $request = \service('request'); $request->setHeader('Authorization', 'Bearer ' . $token); } } diff --git a/tests/Authentication/SessionAuthenticatorTest.php b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php similarity index 93% rename from tests/Authentication/SessionAuthenticatorTest.php rename to tests/Authentication/Authenticators/SessionAuthenticatorTest.php index 556ccee5b..1850c2ba6 100644 --- a/tests/Authentication/SessionAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php @@ -1,6 +1,6 @@ setProvider(model(UserModel::class)); // @phpstan-ignore-line + $auth->setProvider(\model(UserModel::class)); // @phpstan-ignore-line /** @var Session $authenticator */ $authenticator = $auth->factory('session'); @@ -70,7 +70,7 @@ public function testLoggedInWithRememberCookie() // Insert remember-me token. /** @var RememberModel $rememberModel */ - $rememberModel = model(RememberModel::class); + $rememberModel = \model(RememberModel::class); $selector = 'selector'; $validator = 'validator'; $expires = date('Y-m-d H:i:s', time() + setting('Auth.sessionConfig')['rememberLength']); @@ -114,7 +114,7 @@ public function testLoginWithRemember() ]); // Cookie should have been set - $response = service('response'); + $response = \service('response'); $this->assertNotNull($response->getCookie('remember')); } @@ -134,7 +134,7 @@ public function testLogout() public function testLoginByIdBadUser() { $this->expectException(AuthenticationException::class); - $this->expectExceptionMessage(lang('Auth.invalidUser')); + $this->expectExceptionMessage(\lang('Auth.invalidUser')); $this->auth->loginById(123); } @@ -176,7 +176,7 @@ public function testForgetCurrentUser() public function testForgetAnotherUser() { - fake(RememberModel::class, ['user_id' => $this->user->id]); + \fake(RememberModel::class, ['user_id' => $this->user->id]); $this->seeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); @@ -193,7 +193,7 @@ public function testCheckNoPassword() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(lang('Auth.badAttempt'), $result->reason()); + $this->assertSame(\lang('Auth.badAttempt'), $result->reason()); } public function testCheckCannotFindUser() @@ -205,7 +205,7 @@ public function testCheckCannotFindUser() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(lang('Auth.badAttempt'), $result->reason()); + $this->assertSame(\lang('Auth.badAttempt'), $result->reason()); } public function testCheckBadPassword() @@ -222,7 +222,7 @@ public function testCheckBadPassword() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(lang('Auth.invalidPassword'), $result->reason()); + $this->assertSame(\lang('Auth.invalidPassword'), $result->reason()); } public function testCheckSuccess() @@ -253,7 +253,7 @@ public function testAttemptCannotFindUser() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(lang('Auth.badAttempt'), $result->reason()); + $this->assertSame(\lang('Auth.badAttempt'), $result->reason()); // A login attempt should have always been recorded $this->seeInDatabase('auth_logins', [ @@ -325,7 +325,7 @@ public function testAttemptCaseInsensitive() public function testAttemptUsernameOnly() { /** @var User $user */ - $user = fake(UserModel::class, ['username' => 'foorog']); + $user = \fake(UserModel::class, ['username' => 'foorog']); $user->createEmailIdentity([ 'email' => 'FOO@example.com', 'password' => 'secret123', diff --git a/tests/Authentication/ChainFilterTest.php b/tests/Authentication/Filters/ChainFilterTest.php similarity index 79% rename from tests/Authentication/ChainFilterTest.php rename to tests/Authentication/Filters/ChainFilterTest.php index da7368f06..5599bbea4 100644 --- a/tests/Authentication/ChainFilterTest.php +++ b/tests/Authentication/Filters/ChainFilterTest.php @@ -1,6 +1,6 @@ aliases['chain'] = ChainAuth::class; Factories::injectMock('filters', 'filters', $filterConfig); // Add a test route that we can visit to trigger. - $routes = service('routes'); + $routes = \service('routes'); $routes->group('/', ['filter' => 'chain'], static function ($routes) { $routes->get('protected-route', static function () { echo 'Protected'; @@ -71,8 +71,8 @@ public function testFilterSuccessSeession() $result->assertStatus(200); $result->assertSee('Protected'); - $this->assertSame($this->user->id, auth()->id()); - $this->assertSame($this->user->id, auth()->user()->id); + $this->assertSame($this->user->id, \auth()->id()); + $this->assertSame($this->user->id, \auth()->user()->id); } public function testFilterSuccessTokens() @@ -85,11 +85,11 @@ public function testFilterSuccessTokens() $result->assertStatus(200); $result->assertSee('Protected'); - $this->assertSame($this->user->id, auth()->id()); - $this->assertSame($this->user->id, auth()->user()->id); + $this->assertSame($this->user->id, \auth()->id()); + $this->assertSame($this->user->id, \auth()->user()->id); // User should have the current token set. - $this->assertInstanceOf(AccessToken::class, auth('tokens')->user()->currentAccessToken()); - $this->assertSame($token->id, auth('tokens')->user()->currentAccessToken()->id); + $this->assertInstanceOf(AccessToken::class, \auth('tokens')->user()->currentAccessToken()); + $this->assertSame($token->id, \auth('tokens')->user()->currentAccessToken()->id); } } diff --git a/tests/Authentication/SessionFilterTest.php b/tests/Authentication/Filters/SessionFilterTest.php similarity index 81% rename from tests/Authentication/SessionFilterTest.php rename to tests/Authentication/Filters/SessionFilterTest.php index 202b97150..d0839dbdb 100644 --- a/tests/Authentication/SessionFilterTest.php +++ b/tests/Authentication/Filters/SessionFilterTest.php @@ -1,6 +1,6 @@ aliases['sessionAuth'] = SessionAuth::class; Factories::injectMock('filters', 'filters', $filterConfig); // Add a test route that we can visit to trigger. - $routes = service('routes'); + $routes = \service('routes'); $routes->group('/', ['filter' => 'sessionAuth'], static function ($routes) { $routes->get('protected-route', static function () { echo 'Protected'; @@ -58,7 +58,7 @@ public function testFilterNotAuthorized() public function testFilterSuccess() { - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $_SESSION['user']['id'] = $user->id; $result = $this->withSession(['user' => ['id' => $user->id]]) @@ -67,9 +67,9 @@ public function testFilterSuccess() $result->assertStatus(200); $result->assertSee('Protected'); - $this->assertSame($user->id, auth('session')->id()); - $this->assertSame($user->id, auth('session')->user()->id); + $this->assertSame($user->id, \auth('session')->id()); + $this->assertSame($user->id, \auth('session')->user()->id); // Last Active should have been updated - $this->assertNotEmpty(auth('session')->user()->last_active); + $this->assertNotEmpty(\auth('session')->user()->last_active); } } diff --git a/tests/Authentication/TokenFilterTest.php b/tests/Authentication/Filters/TokenFilterTest.php similarity index 79% rename from tests/Authentication/TokenFilterTest.php rename to tests/Authentication/Filters/TokenFilterTest.php index 7463f83b3..7ae114da1 100644 --- a/tests/Authentication/TokenFilterTest.php +++ b/tests/Authentication/Filters/TokenFilterTest.php @@ -1,6 +1,6 @@ aliases['tokenAuth'] = TokenAuth::class; Factories::injectMock('filters', 'filters', $filterConfig); // Add a test route that we can visit to trigger. - $routes = service('routes'); + $routes = \service('routes'); $routes->group('/', ['filter' => 'tokenAuth'], static function ($routes) { $routes->get('protected-route', static function () { echo 'Protected'; @@ -61,7 +61,7 @@ public function testFilterNotAuthorized() public function testFilterSuccess() { /** @var User $user */ - $user = fake(UserModel::class); + $user = \fake(UserModel::class); $token = $user->generateAccessToken('foo'); $result = $this->withHeaders(['Authorization' => 'Bearer ' . $token->raw_token]) @@ -70,11 +70,11 @@ public function testFilterSuccess() $result->assertStatus(200); $result->assertSee('Protected'); - $this->assertSame($user->id, auth('tokens')->id()); - $this->assertSame($user->id, auth('tokens')->user()->id); + $this->assertSame($user->id, \auth('tokens')->id()); + $this->assertSame($user->id, \auth('tokens')->user()->id); // User should have the current token set. - $this->assertInstanceOf(AccessToken::class, auth('tokens')->user()->currentAccessToken()); - $this->assertSame($token->id, auth('tokens')->user()->currentAccessToken()->id); + $this->assertInstanceOf(AccessToken::class, \auth('tokens')->user()->currentAccessToken()); + $this->assertSame($token->id, \auth('tokens')->user()->currentAccessToken()->id); } } From 930788b4a156247c95b4f0e1c03500c2df6ee82b Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 2 Jun 2022 10:20:48 +0900 Subject: [PATCH 2/3] refactor: remove \ for functions --- .../AccessTokenAuthenticatorTest.php | 30 +++++++++---------- .../SessionAuthenticatorTest.php | 20 ++++++------- .../Filters/ChainFilterTest.php | 16 +++++----- .../Filters/SessionFilterTest.php | 12 ++++---- .../Filters/TokenFilterTest.php | 14 ++++----- 5 files changed, 46 insertions(+), 46 deletions(-) diff --git a/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php index c075d88a5..6bb8afded 100644 --- a/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/AccessTokenAuthenticatorTest.php @@ -28,7 +28,7 @@ protected function setUp(): void $config = new Auth(); $auth = new Authentication($config); - $auth->setProvider(\model(UserModel::class)); // @phpstan-ignore-line + $auth->setProvider(model(UserModel::class)); // @phpstan-ignore-line /** @var AccessTokens $authenticator */ $authenticator = $auth->factory('tokens'); @@ -39,7 +39,7 @@ protected function setUp(): void public function testLogin() { - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $this->auth->login($user); @@ -51,7 +51,7 @@ public function testLogin() public function testLogout() { // this one's a little odd since it's stateless, but roll with it... - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $this->auth->login($user); $this->assertNotNull($this->auth->getUser()); @@ -62,7 +62,7 @@ public function testLogout() public function testLoginByIdNoToken() { - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $this->assertFalse($this->auth->loggedIn()); @@ -75,7 +75,7 @@ public function testLoginByIdNoToken() public function testLoginByIdWithToken() { /** @var User $user */ - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $token = $user->generateAccessToken('foo'); $this->setRequestHeader($token->raw_token); @@ -90,7 +90,7 @@ public function testLoginByIdWithToken() public function testLoginByIdWithMultipleTokens() { /** @var User $user */ - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $token1 = $user->generateAccessToken('foo'); $user->generateAccessToken('bar'); @@ -108,7 +108,7 @@ public function testCheckNoToken() $result = $this->auth->check([]); $this->assertFalse($result->isOK()); - $this->assertSame(\lang('Auth.noToken'), $result->reason()); + $this->assertSame(lang('Auth.noToken'), $result->reason()); } public function testCheckBadToken() @@ -116,15 +116,15 @@ public function testCheckBadToken() $result = $this->auth->check(['token' => 'abc123']); $this->assertFalse($result->isOK()); - $this->assertSame(\lang('Auth.badToken'), $result->reason()); + $this->assertSame(lang('Auth.badToken'), $result->reason()); } public function testCheckOldToken() { /** @var User $user */ - $user = \fake(UserModel::class); + $user = fake(UserModel::class); /** @var UserIdentityModel $identities */ - $identities = \model(UserIdentityModel::class); + $identities = model(UserIdentityModel::class); $token = $user->generateAccessToken('foo'); $token->last_used_at = Time::now()->subYears(1)->subMinutes(1); $identities->save($token); @@ -132,13 +132,13 @@ public function testCheckOldToken() $result = $this->auth->check(['token' => $token->raw_token]); $this->assertFalse($result->isOK()); - $this->assertSame(\lang('Auth.oldToken'), $result->reason()); + $this->assertSame(lang('Auth.oldToken'), $result->reason()); } public function testCheckSuccess() { /** @var User $user */ - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $token = $user->generateAccessToken('foo'); $this->seeInDatabase('auth_identities', [ @@ -165,7 +165,7 @@ public function testAttemptCannotFindUser() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(\lang('Auth.badToken'), $result->reason()); + $this->assertSame(lang('Auth.badToken'), $result->reason()); // A login attempt should have always been recorded $this->seeInDatabase('auth_token_logins', [ @@ -178,7 +178,7 @@ public function testAttemptCannotFindUser() public function testAttemptSuccess() { /** @var User $user */ - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $token = $user->generateAccessToken('foo'); $this->setRequestHeader($token->raw_token); @@ -205,7 +205,7 @@ public function testAttemptSuccess() protected function setRequestHeader(string $token) { - $request = \service('request'); + $request = service('request'); $request->setHeader('Authorization', 'Bearer ' . $token); } } diff --git a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php index 1850c2ba6..ec75ad0ca 100644 --- a/tests/Authentication/Authenticators/SessionAuthenticatorTest.php +++ b/tests/Authentication/Authenticators/SessionAuthenticatorTest.php @@ -34,7 +34,7 @@ protected function setUp(): void $config = new Auth(); $auth = new Authentication($config); - $auth->setProvider(\model(UserModel::class)); // @phpstan-ignore-line + $auth->setProvider(model(UserModel::class)); // @phpstan-ignore-line /** @var Session $authenticator */ $authenticator = $auth->factory('session'); @@ -70,7 +70,7 @@ public function testLoggedInWithRememberCookie() // Insert remember-me token. /** @var RememberModel $rememberModel */ - $rememberModel = \model(RememberModel::class); + $rememberModel = model(RememberModel::class); $selector = 'selector'; $validator = 'validator'; $expires = date('Y-m-d H:i:s', time() + setting('Auth.sessionConfig')['rememberLength']); @@ -114,7 +114,7 @@ public function testLoginWithRemember() ]); // Cookie should have been set - $response = \service('response'); + $response = service('response'); $this->assertNotNull($response->getCookie('remember')); } @@ -134,7 +134,7 @@ public function testLogout() public function testLoginByIdBadUser() { $this->expectException(AuthenticationException::class); - $this->expectExceptionMessage(\lang('Auth.invalidUser')); + $this->expectExceptionMessage(lang('Auth.invalidUser')); $this->auth->loginById(123); } @@ -176,7 +176,7 @@ public function testForgetCurrentUser() public function testForgetAnotherUser() { - \fake(RememberModel::class, ['user_id' => $this->user->id]); + fake(RememberModel::class, ['user_id' => $this->user->id]); $this->seeInDatabase('auth_remember_tokens', ['user_id' => $this->user->id]); @@ -193,7 +193,7 @@ public function testCheckNoPassword() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(\lang('Auth.badAttempt'), $result->reason()); + $this->assertSame(lang('Auth.badAttempt'), $result->reason()); } public function testCheckCannotFindUser() @@ -205,7 +205,7 @@ public function testCheckCannotFindUser() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(\lang('Auth.badAttempt'), $result->reason()); + $this->assertSame(lang('Auth.badAttempt'), $result->reason()); } public function testCheckBadPassword() @@ -222,7 +222,7 @@ public function testCheckBadPassword() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(\lang('Auth.invalidPassword'), $result->reason()); + $this->assertSame(lang('Auth.invalidPassword'), $result->reason()); } public function testCheckSuccess() @@ -253,7 +253,7 @@ public function testAttemptCannotFindUser() $this->assertInstanceOf(Result::class, $result); $this->assertFalse($result->isOK()); - $this->assertSame(\lang('Auth.badAttempt'), $result->reason()); + $this->assertSame(lang('Auth.badAttempt'), $result->reason()); // A login attempt should have always been recorded $this->seeInDatabase('auth_logins', [ @@ -325,7 +325,7 @@ public function testAttemptCaseInsensitive() public function testAttemptUsernameOnly() { /** @var User $user */ - $user = \fake(UserModel::class, ['username' => 'foorog']); + $user = fake(UserModel::class, ['username' => 'foorog']); $user->createEmailIdentity([ 'email' => 'FOO@example.com', 'password' => 'secret123', diff --git a/tests/Authentication/Filters/ChainFilterTest.php b/tests/Authentication/Filters/ChainFilterTest.php index 5599bbea4..515d5093a 100644 --- a/tests/Authentication/Filters/ChainFilterTest.php +++ b/tests/Authentication/Filters/ChainFilterTest.php @@ -32,12 +32,12 @@ protected function setUp(): void $_SESSION = []; // Register our filter - $filterConfig = \config('Filters'); + $filterConfig = config('Filters'); $filterConfig->aliases['chain'] = ChainAuth::class; Factories::injectMock('filters', 'filters', $filterConfig); // Add a test route that we can visit to trigger. - $routes = \service('routes'); + $routes = service('routes'); $routes->group('/', ['filter' => 'chain'], static function ($routes) { $routes->get('protected-route', static function () { echo 'Protected'; @@ -71,8 +71,8 @@ public function testFilterSuccessSeession() $result->assertStatus(200); $result->assertSee('Protected'); - $this->assertSame($this->user->id, \auth()->id()); - $this->assertSame($this->user->id, \auth()->user()->id); + $this->assertSame($this->user->id, auth()->id()); + $this->assertSame($this->user->id, auth()->user()->id); } public function testFilterSuccessTokens() @@ -85,11 +85,11 @@ public function testFilterSuccessTokens() $result->assertStatus(200); $result->assertSee('Protected'); - $this->assertSame($this->user->id, \auth()->id()); - $this->assertSame($this->user->id, \auth()->user()->id); + $this->assertSame($this->user->id, auth()->id()); + $this->assertSame($this->user->id, auth()->user()->id); // User should have the current token set. - $this->assertInstanceOf(AccessToken::class, \auth('tokens')->user()->currentAccessToken()); - $this->assertSame($token->id, \auth('tokens')->user()->currentAccessToken()->id); + $this->assertInstanceOf(AccessToken::class, auth('tokens')->user()->currentAccessToken()); + $this->assertSame($token->id, auth('tokens')->user()->currentAccessToken()->id); } } diff --git a/tests/Authentication/Filters/SessionFilterTest.php b/tests/Authentication/Filters/SessionFilterTest.php index d0839dbdb..2e9b9966a 100644 --- a/tests/Authentication/Filters/SessionFilterTest.php +++ b/tests/Authentication/Filters/SessionFilterTest.php @@ -27,12 +27,12 @@ protected function setUp(): void $_SESSION = []; // Register our filter - $filterConfig = \config('Filters'); + $filterConfig = config('Filters'); $filterConfig->aliases['sessionAuth'] = SessionAuth::class; Factories::injectMock('filters', 'filters', $filterConfig); // Add a test route that we can visit to trigger. - $routes = \service('routes'); + $routes = service('routes'); $routes->group('/', ['filter' => 'sessionAuth'], static function ($routes) { $routes->get('protected-route', static function () { echo 'Protected'; @@ -58,7 +58,7 @@ public function testFilterNotAuthorized() public function testFilterSuccess() { - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $_SESSION['user']['id'] = $user->id; $result = $this->withSession(['user' => ['id' => $user->id]]) @@ -67,9 +67,9 @@ public function testFilterSuccess() $result->assertStatus(200); $result->assertSee('Protected'); - $this->assertSame($user->id, \auth('session')->id()); - $this->assertSame($user->id, \auth('session')->user()->id); + $this->assertSame($user->id, auth('session')->id()); + $this->assertSame($user->id, auth('session')->user()->id); // Last Active should have been updated - $this->assertNotEmpty(\auth('session')->user()->last_active); + $this->assertNotEmpty(auth('session')->user()->last_active); } } diff --git a/tests/Authentication/Filters/TokenFilterTest.php b/tests/Authentication/Filters/TokenFilterTest.php index 7ae114da1..42ef583df 100644 --- a/tests/Authentication/Filters/TokenFilterTest.php +++ b/tests/Authentication/Filters/TokenFilterTest.php @@ -29,12 +29,12 @@ protected function setUp(): void $_SESSION = []; // Register our filter - $filterConfig = \config('Filters'); + $filterConfig = config('Filters'); $filterConfig->aliases['tokenAuth'] = TokenAuth::class; Factories::injectMock('filters', 'filters', $filterConfig); // Add a test route that we can visit to trigger. - $routes = \service('routes'); + $routes = service('routes'); $routes->group('/', ['filter' => 'tokenAuth'], static function ($routes) { $routes->get('protected-route', static function () { echo 'Protected'; @@ -61,7 +61,7 @@ public function testFilterNotAuthorized() public function testFilterSuccess() { /** @var User $user */ - $user = \fake(UserModel::class); + $user = fake(UserModel::class); $token = $user->generateAccessToken('foo'); $result = $this->withHeaders(['Authorization' => 'Bearer ' . $token->raw_token]) @@ -70,11 +70,11 @@ public function testFilterSuccess() $result->assertStatus(200); $result->assertSee('Protected'); - $this->assertSame($user->id, \auth('tokens')->id()); - $this->assertSame($user->id, \auth('tokens')->user()->id); + $this->assertSame($user->id, auth('tokens')->id()); + $this->assertSame($user->id, auth('tokens')->user()->id); // User should have the current token set. - $this->assertInstanceOf(AccessToken::class, \auth('tokens')->user()->currentAccessToken()); - $this->assertSame($token->id, \auth('tokens')->user()->currentAccessToken()->id); + $this->assertInstanceOf(AccessToken::class, auth('tokens')->user()->currentAccessToken()); + $this->assertSame($token->id, auth('tokens')->user()->currentAccessToken()->id); } } From 431d938bed0c51e8ad84982771d8a344ddc54f3e Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 2 Jun 2022 10:22:28 +0900 Subject: [PATCH 3/3] refactor: remove unused `use` --- tests/Authentication/Filters/ChainFilterTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Authentication/Filters/ChainFilterTest.php b/tests/Authentication/Filters/ChainFilterTest.php index 515d5093a..c583152c0 100644 --- a/tests/Authentication/Filters/ChainFilterTest.php +++ b/tests/Authentication/Filters/ChainFilterTest.php @@ -4,7 +4,6 @@ use CodeIgniter\Config\Factories; use CodeIgniter\Shield\Entities\AccessToken; -use CodeIgniter\Shield\Entities\User; use CodeIgniter\Shield\Filters\ChainAuth; use CodeIgniter\Test\DatabaseTestTrait; use CodeIgniter\Test\FeatureTestTrait;