diff --git a/.github/workflows/rector-cs.yml b/.github/workflows/rector-cs.yml new file mode 100644 index 0000000..03b7c75 --- /dev/null +++ b/.github/workflows/rector-cs.yml @@ -0,0 +1,28 @@ +name: Rector + PHP CS Fixer + +on: + pull_request_target: + paths: + - 'config/**' + - 'src/**' + - 'tests/**' + - '.github/workflows/rector-cs.yml' + - 'composer.json' + - 'rector.php' + - '.php-cs-fixer.dist.php' + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + rector: + uses: yiisoft/actions/.github/workflows/rector-cs.yml@master + secrets: + token: ${{ secrets.YIISOFT_GITHUB_TOKEN }} + with: + repository: ${{ github.event.pull_request.head.repo.full_name }} + php: '8.1' diff --git a/.github/workflows/rector.yml b/.github/workflows/rector.yml deleted file mode 100644 index 457772a..0000000 --- a/.github/workflows/rector.yml +++ /dev/null @@ -1,24 +0,0 @@ -on: - pull_request_target: - paths-ignore: - - 'docs/**' - - 'README.md' - - 'CHANGELOG.md' - - '.gitignore' - - '.gitattributes' - - 'infection.json.dist' - - 'psalm.xml' - -name: rector - -jobs: - rector: - uses: yiisoft/actions/.github/workflows/rector.yml@master - secrets: - token: ${{ secrets.YIISOFT_GITHUB_TOKEN }} - with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - os: >- - ['ubuntu-latest'] - php: >- - ['8.3'] diff --git a/.gitignore b/.gitignore index e882061..83d7038 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,7 @@ composer.phar /phpunit.phar /phpunit.xml /.phpunit.cache + +# PHP CS Fixer +/.php-cs-fixer.cache +/.php-cs-fixer.php diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..7571f30 --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,35 @@ +in([ + __DIR__ . '/config', + __DIR__ . '/src', + __DIR__ . '/tests', +]); + +return (new Config()) + ->setRiskyAllowed(true) + ->setParallelConfig(ParallelConfigFactory::detect()) + ->setRules([ + '@PER-CS3.0' => true, + 'no_unused_imports' => true, + 'ordered_class_elements' => true, + 'class_attributes_separation' => ['elements' => ['method' => 'one']], + 'declare_strict_types' => true, + 'native_function_invocation' => true, + 'native_constant_invocation' => true, + 'fully_qualified_strict_types' => [ + 'import_symbols' => true + ], + 'global_namespace_import' => [ + 'import_classes' => true, + 'import_constants' => true, + 'import_functions' => true, + ], + ]) + ->setFinder($finder); diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 1ab379b..0000000 --- a/.styleci.yml +++ /dev/null @@ -1,85 +0,0 @@ -preset: psr12 -risky: true - -version: 8.1 - -finder: - exclude: - - docs - - vendor - -enabled: - - alpha_ordered_traits - - array_indentation - - array_push - - combine_consecutive_issets - - combine_consecutive_unsets - - combine_nested_dirname - - declare_strict_types - - dir_constant - - fully_qualified_strict_types - - function_to_constant - - hash_to_slash_comment - - is_null - - logical_operators - - magic_constant_casing - - magic_method_casing - - method_separation - - modernize_types_casting - - native_function_casing - - native_function_type_declaration_casing - - no_alias_functions - - no_empty_comment - - no_empty_phpdoc - - no_empty_statement - - no_extra_block_blank_lines - - no_short_bool_cast - - no_superfluous_elseif - - no_unneeded_control_parentheses - - no_unneeded_curly_braces - - no_unneeded_final_method - - no_unset_cast - - no_unused_imports - - no_unused_lambda_imports - - no_useless_else - - no_useless_return - - normalize_index_brace - - php_unit_dedicate_assert - - php_unit_dedicate_assert_internal_type - - php_unit_expectation - - php_unit_mock - - php_unit_mock_short_will_return - - php_unit_namespaced - - php_unit_no_expectation_annotation - - phpdoc_no_empty_return - - phpdoc_no_useless_inheritdoc - - phpdoc_order - - phpdoc_property - - phpdoc_scalar - - phpdoc_singular_inheritdoc - - phpdoc_trim - - phpdoc_trim_consecutive_blank_line_separation - - phpdoc_type_to_var - - phpdoc_types - - phpdoc_types_order - - print_to_echo - - regular_callable_call - - return_assignment - - self_accessor - - self_static_accessor - - set_type_to_cast - - short_array_syntax - - short_list_syntax - - simplified_if_return - - single_quote - - standardize_not_equals - - ternary_to_null_coalescing - - trailing_comma_in_multiline_array - - unalign_double_arrow - - unalign_equals - - empty_loop_body_braces - - integer_literal_case - - union_type_without_spaces - -disabled: - - function_declaration diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ff30dc..c551780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Chg #104: Bump minimal PHP version to 8.1 (@vjik) - Enh #104: Explicitly mark readonly properties (@vjik) +- Enh #105: Explicitly import classes and functions in "use" section (@mspirkov) ## 3.2.1 December 17, 2025 diff --git a/composer.json b/composer.json index 2f8e73b..90f51d2 100644 --- a/composer.json +++ b/composer.json @@ -36,6 +36,7 @@ "yiisoft/strings": "^2.0" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.92.5", "jetbrains/phpstorm-attributes": "^1.2", "maglnet/composer-require-checker": "^4.7.1", "nyholm/psr7": "^1.8.2", diff --git a/src/Debug/AuthenticationMethodInterfaceProxy.php b/src/Debug/AuthenticationMethodInterfaceProxy.php index 3e4c027..1c7a8ba 100644 --- a/src/Debug/AuthenticationMethodInterfaceProxy.php +++ b/src/Debug/AuthenticationMethodInterfaceProxy.php @@ -11,9 +11,7 @@ final class AuthenticationMethodInterfaceProxy implements AuthenticationMethodInterface { - public function __construct(private readonly AuthenticationMethodInterface $decorated, private readonly IdentityCollector $collector) - { - } + public function __construct(private readonly AuthenticationMethodInterface $decorated, private readonly IdentityCollector $collector) {} public function authenticate(ServerRequestInterface $request): ?IdentityInterface { diff --git a/src/Debug/IdentityCollector.php b/src/Debug/IdentityCollector.php index 0e65133..4c19d57 100644 --- a/src/Debug/IdentityCollector.php +++ b/src/Debug/IdentityCollector.php @@ -8,6 +8,9 @@ use Yiisoft\Yii\Debug\Collector\CollectorTrait; use Yiisoft\Yii\Debug\Collector\SummaryCollectorInterface; +use function count; +use function is_array; + final class IdentityCollector implements SummaryCollectorInterface { use CollectorTrait; @@ -38,11 +41,6 @@ public function collect(?IdentityInterface $identity): void ]; } - private function reset(): void - { - $this->identities = []; - } - public function getSummary(): array { if (!$this->isActive()) { @@ -54,4 +52,9 @@ public function getSummary(): array 'total' => count($this->identities), ]; } + + private function reset(): void + { + $this->identities = []; + } } diff --git a/src/Handler/AuthenticationFailureHandler.php b/src/Handler/AuthenticationFailureHandler.php index d98da41..31f4143 100644 --- a/src/Handler/AuthenticationFailureHandler.php +++ b/src/Handler/AuthenticationFailureHandler.php @@ -17,8 +17,7 @@ final class AuthenticationFailureHandler implements RequestHandlerInterface { public function __construct( private readonly ResponseFactoryInterface $responseFactory, - ) { - } + ) {} public function handle(ServerRequestInterface $request): ResponseInterface { diff --git a/src/Method/Composite.php b/src/Method/Composite.php index ed48e1c..7cbbf7f 100644 --- a/src/Method/Composite.php +++ b/src/Method/Composite.php @@ -8,6 +8,7 @@ use Psr\Http\Message\ServerRequestInterface; use Yiisoft\Auth\AuthenticationMethodInterface; use Yiisoft\Auth\IdentityInterface; +use RuntimeException; /** * Composite allows multiple authentication methods at the same time. @@ -19,14 +20,13 @@ final class Composite implements AuthenticationMethodInterface */ public function __construct( private readonly array $methods, - ) { - } + ) {} public function authenticate(ServerRequestInterface $request): ?IdentityInterface { foreach ($this->methods as $method) { if (!$method instanceof AuthenticationMethodInterface) { - throw new \RuntimeException('Authentication method must be an instance of ' . AuthenticationMethodInterface::class . '.'); + throw new RuntimeException('Authentication method must be an instance of ' . AuthenticationMethodInterface::class . '.'); } $identity = $method->authenticate($request); diff --git a/src/Method/HttpBasic.php b/src/Method/HttpBasic.php index c286677..03c4851 100644 --- a/src/Method/HttpBasic.php +++ b/src/Method/HttpBasic.php @@ -10,6 +10,7 @@ use Yiisoft\Auth\IdentityInterface; use Yiisoft\Auth\IdentityWithTokenRepositoryInterface; use Yiisoft\Http\Header; +use SensitiveParameter; use function call_user_func; use function count; @@ -37,9 +38,7 @@ final class HttpBasic implements AuthenticationMethodInterface */ private $authenticationCallback; - public function __construct(private IdentityWithTokenRepositoryInterface $identityRepository) - { - } + public function __construct(private IdentityWithTokenRepositoryInterface $identityRepository) {} public function authenticate(ServerRequestInterface $request): ?IdentityInterface { @@ -159,15 +158,15 @@ private function getTokenFromHeaders(ServerRequestInterface $request): ?string return $request->getServerParams()['REDIRECT_HTTP_AUTHORIZATION'] ?? null; } - private function extractCredentialsFromHeader(#[\SensitiveParameter] string $authToken): array + private function extractCredentialsFromHeader(#[SensitiveParameter] string $authToken): array { return array_map( - static fn ($value) => $value === '' ? null : $value, - explode(':', base64_decode(substr($authToken, 6)), 2) + static fn($value) => $value === '' ? null : $value, + explode(':', base64_decode(substr($authToken, 6)), 2), ); } - private function isBasicToken(#[\SensitiveParameter] string $token): bool + private function isBasicToken(#[SensitiveParameter] string $token): bool { return strncasecmp($token, 'basic', 5) === 0; } diff --git a/src/Method/HttpCookie.php b/src/Method/HttpCookie.php index d63225e..14d1a9b 100644 --- a/src/Method/HttpCookie.php +++ b/src/Method/HttpCookie.php @@ -21,11 +21,10 @@ final class HttpCookie implements AuthenticationMethodInterface private ?string $tokenType = null; public function __construct( - private IdentityWithTokenRepositoryInterface $identityRepository - ) { - } + private IdentityWithTokenRepositoryInterface $identityRepository, + ) {} - public function authenticate(ServerRequestInterface $request): IdentityInterface|null + public function authenticate(ServerRequestInterface $request): ?IdentityInterface { $authToken = $this->getAuthenticationToken($request); diff --git a/src/Method/HttpHeader.php b/src/Method/HttpHeader.php index faca60c..fde77ca 100644 --- a/src/Method/HttpHeader.php +++ b/src/Method/HttpHeader.php @@ -23,17 +23,15 @@ class HttpHeader implements AuthenticationMethodInterface { protected string $headerName = 'X-Api-Key'; - private ?string $tokenType = null; /** * @var string A pattern to use to extract the HTTP authentication value. * @psalm-var non-empty-string */ protected string $pattern = '/(.*)/'; + private ?string $tokenType = null; - public function __construct(protected IdentityWithTokenRepositoryInterface $identityRepository) - { - } + public function __construct(protected IdentityWithTokenRepositoryInterface $identityRepository) {} public function authenticate(ServerRequestInterface $request): ?IdentityInterface { diff --git a/src/Method/QueryParameter.php b/src/Method/QueryParameter.php index 819edb9..ff543ad 100644 --- a/src/Method/QueryParameter.php +++ b/src/Method/QueryParameter.php @@ -20,9 +20,7 @@ final class QueryParameter implements AuthenticationMethodInterface private string $parameterName = 'access-token'; private ?string $tokenType = null; - public function __construct(private IdentityWithTokenRepositoryInterface $identityRepository) - { - } + public function __construct(private IdentityWithTokenRepositoryInterface $identityRepository) {} public function authenticate(ServerRequestInterface $request): ?IdentityInterface { diff --git a/src/Middleware/Authentication.php b/src/Middleware/Authentication.php index 5d344df..9f70db1 100644 --- a/src/Middleware/Authentication.php +++ b/src/Middleware/Authentication.php @@ -40,7 +40,7 @@ public function __construct( ?RequestHandlerInterface $authenticationFailureHandler = null, ) { $this->failureHandler = $authenticationFailureHandler ?? new AuthenticationFailureHandler( - $responseFactory + $responseFactory, ); } @@ -51,7 +51,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if ($identity === null && !$this->isOptional($request)) { return $this->authenticationMethod->challenge( - $this->failureHandler->handle($request) + $this->failureHandler->handle($request), ); } diff --git a/tests/AuthenticationFailureHandlerTest.php b/tests/AuthenticationFailureHandlerTest.php index b9d8f61..f4fc23b 100644 --- a/tests/AuthenticationFailureHandlerTest.php +++ b/tests/AuthenticationFailureHandlerTest.php @@ -27,7 +27,7 @@ public function testShouldReturnCorrectErrorInBody(): void $response = $this ->createHandler() ->handle($this->createRequest()); - $this->assertEquals('Your request was made with invalid credentials.', (string)$response->getBody()); + $this->assertEquals('Your request was made with invalid credentials.', (string) $response->getBody()); } private function createHandler(): AuthenticationFailureHandler diff --git a/tests/AuthenticationMiddlewareTest.php b/tests/AuthenticationMiddlewareTest.php index e193dc3..51e21b1 100644 --- a/tests/AuthenticationMiddlewareTest.php +++ b/tests/AuthenticationMiddlewareTest.php @@ -50,7 +50,7 @@ function (ServerRequestInterface $request) use ($identity) { $this->assertEquals($identity, $request->getAttribute(Authentication::class)); return $this->responseFactory->createResponse(); - } + }, ); $auth = new Authentication($this->authenticationMethod, $this->responseFactory); @@ -100,7 +100,7 @@ public function testShouldNotExecuteHandlerAndReturn401OnAuthenticationFailure() ->expects($this->once()) ->method('challenge') ->willReturnCallback( - static fn (ResponseInterface $response) => $response->withHeader($header, $headerValue) + static fn(ResponseInterface $response) => $response->withHeader($header, $headerValue), ); $handler = $this->createMock(RequestHandlerInterface::class); @@ -129,7 +129,7 @@ public function testCustomAuthenticationFailureResponse(): void ->expects($this->once()) ->method('challenge') ->willReturnCallback( - static fn (ResponseInterface $response) => $response->withHeader($header, $headerValue) + static fn(ResponseInterface $response) => $response->withHeader($header, $headerValue), ); $handler = $this->createMock(RequestHandlerInterface::class); @@ -142,19 +142,19 @@ public function testCustomAuthenticationFailureResponse(): void $auth = new Authentication( $this->authenticationMethod, $this->responseFactory, - $this->createAuthenticationFailureHandler($failureResponse) + $this->createAuthenticationFailureHandler($failureResponse), ); $response = $auth->process($request, $handler); $this->assertEquals(401, $response->getStatusCode()); $this->assertEquals($headerValue, $response->getHeaderLine($header)); - $this->assertEquals($failureResponse, (string)$response->getBody()); + $this->assertEquals($failureResponse, (string) $response->getBody()); } public function testImmutability(): void { $original = new Authentication( $this->authenticationMethod, - $this->responseFactory + $this->responseFactory, ); $this->assertNotSame($original, $original->withOptionalPatterns(['test'])); @@ -166,8 +166,7 @@ private function createAuthenticationFailureHandler(string $failureResponse): Re public function __construct( private readonly string $failureResponse, private readonly ResponseFactoryInterface $responseFactory, - ) { - } + ) {} public function handle(ServerRequestInterface $request): ResponseInterface { diff --git a/tests/Method/CompositeTest.php b/tests/Method/CompositeTest.php index 8f077c3..3047343 100644 --- a/tests/Method/CompositeTest.php +++ b/tests/Method/CompositeTest.php @@ -16,6 +16,7 @@ use Yiisoft\Auth\Tests\Stub\FakeIdentityRepository; use Yiisoft\Http\Header; use Yiisoft\Http\Method; +use RuntimeException; final class CompositeTest extends TestCase { @@ -25,10 +26,10 @@ public function testIncorrectArguments(): void 'test', ]); - $this->expectException(\RuntimeException::class); + $this->expectException(RuntimeException::class); $authenticationMethod->authenticate( - $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']) + $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']), ); } @@ -42,14 +43,14 @@ public function testSuccessfulAuthentication(): void ]); $result = $authenticationMethod->authenticate( - $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']) + $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']), ); $this->assertNotNull($result); $this->assertEquals('test-id', $result->getId()); $result = $authenticationMethod->authenticate( - $this->createRequest([], ['access-token' => 'access-token-value']) + $this->createRequest([], ['access-token' => 'access-token-value']), ); $this->assertNotNull($result); @@ -67,14 +68,14 @@ public function testIdentityNotFoundByToken(): void ]); $result = $authenticationMethod->authenticate( - $this->createRequest([], ['access-token' => 'access-token-value']) + $this->createRequest([], ['access-token' => 'access-token-value']), ); $this->assertNull($result); $result = $authenticationMethod->authenticate( - $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']) + $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']), ); $this->assertNotNull($result); @@ -84,7 +85,7 @@ public function testIdentityNotFoundByToken(): void public function testEmptyAuthMethods(): void { $result = (new Composite([]))->authenticate( - $this->createRequest() + $this->createRequest(), ); $this->assertNull($result); @@ -105,7 +106,7 @@ public function testChallengeIsCorrect(): void 'Authorization realm="api"', $authenticationMethod ->challenge($response) - ->getHeaderLine(Header::WWW_AUTHENTICATE) + ->getHeaderLine(Header::WWW_AUTHENTICATE), ); } diff --git a/tests/Method/HttpBasicTest.php b/tests/Method/HttpBasicTest.php index 4a8be12..3b3350b 100644 --- a/tests/Method/HttpBasicTest.php +++ b/tests/Method/HttpBasicTest.php @@ -21,20 +21,20 @@ public function testSuccessfulAuthentication(): void { $identityRepository = new FakeIdentityRepository($this->createIdentity()); $result = (new HttpBasic($identityRepository))->authenticate( - $this->createRequest(['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password']) + $this->createRequest(['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password']), ); $this->assertNotNull($result); $this->assertEquals('test-id', $result->getId()); $this->assertEquals( [ - 'findIdentityByToken' => - [ + 'findIdentityByToken' + => [ 'token' => 'user', 'type' => null, ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } @@ -45,8 +45,8 @@ public function testIdentityNotFoundByToken(): void $this->assertNull( $authenticationMethod->authenticate( - $this->createRequest(['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password']) - ) + $this->createRequest(['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password']), + ), ); } @@ -57,8 +57,8 @@ public function testPassedOnlyPassword(): void $this->assertNull( $authenticationMethod->authenticate( - $this->createRequest(['PHP_AUTH_PW' => 'password']) - ) + $this->createRequest(['PHP_AUTH_PW' => 'password']), + ), ); } @@ -66,10 +66,10 @@ public function testSuccessfulAuthenticationWithAuthCallback(): void { $identityRepository = new FakeIdentityRepository(null); $authenticationMethod = (new HttpBasic($identityRepository)) - ->withAuthenticationCallback(fn (?string $username, ?string $password): ?IdentityInterface => $this->createIdentity($username . ':' . $password)); + ->withAuthenticationCallback(fn(?string $username, ?string $password): ?IdentityInterface => $this->createIdentity($username . ':' . $password)); $result = $authenticationMethod->authenticate( - $this->createRequest(['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password']) + $this->createRequest(['PHP_AUTH_USER' => 'user', 'PHP_AUTH_PW' => 'password']), ); $this->assertNotNull($result); @@ -81,10 +81,10 @@ public function testAuthenticationCallbackWithEmptyUsername(): void { $identityRepository = new FakeIdentityRepository(null); $authenticationMethod = (new HttpBasic($identityRepository)) - ->withAuthenticationCallback(fn (?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); + ->withAuthenticationCallback(fn(?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); $result = $authenticationMethod->authenticate( - $this->createRequest(['PHP_AUTH_PW' => 'password']) + $this->createRequest(['PHP_AUTH_PW' => 'password']), ); $this->assertNotNull($result); @@ -96,10 +96,10 @@ public function testAuthenticationCallbackWithEmptyPassword(): void { $identityRepository = new FakeIdentityRepository(null); $authenticationMethod = (new HttpBasic($identityRepository)) - ->withAuthenticationCallback(fn (?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); + ->withAuthenticationCallback(fn(?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); $result = $authenticationMethod->authenticate( - $this->createRequest(['PHP_AUTH_USER' => 'user']) + $this->createRequest(['PHP_AUTH_USER' => 'user']), ); $this->assertNotNull($result); @@ -117,7 +117,7 @@ public function testChallengeIsCorrect(): void 'Basic realm="api"', $authenticationMethod ->challenge($response) - ->getHeaderLine(Header::WWW_AUTHENTICATE) + ->getHeaderLine(Header::WWW_AUTHENTICATE), ); } @@ -132,7 +132,7 @@ public function testCustomRealm(): void 'Basic realm="gateway"', $authenticationMethod ->challenge($response) - ->getHeaderLine(Header::WWW_AUTHENTICATE) + ->getHeaderLine(Header::WWW_AUTHENTICATE), ); } @@ -143,7 +143,7 @@ public function testInvalidHeaderName(): void $authenticationMethod = new HttpBasic($identityRepository); $result = $authenticationMethod->authenticate( - $this->createRequest([], [Header::AUTHORIZATION => 'Basik:' . $encodeFields]) + $this->createRequest([], [Header::AUTHORIZATION => 'Basik:' . $encodeFields]), ); $this->assertNull($result); @@ -154,10 +154,10 @@ public function testEmptyPasswordInHeader(): void $encodeFields = base64_encode('user'); $identityRepository = new FakeIdentityRepository($this->createIdentity()); $authenticationMethod = (new HttpBasic($identityRepository)) - ->withAuthenticationCallback(fn (?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); + ->withAuthenticationCallback(fn(?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); $result = $authenticationMethod->authenticate( - $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]) + $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]), ); $this->assertNotNull($result); @@ -170,10 +170,10 @@ public function testEmptyUsernameInHeader(): void $encodeFields = base64_encode(':password'); $identityRepository = new FakeIdentityRepository($this->createIdentity()); $authenticationMethod = (new HttpBasic($identityRepository)) - ->withAuthenticationCallback(fn (?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); + ->withAuthenticationCallback(fn(?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); $result = $authenticationMethod->authenticate( - $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]) + $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]), ); $this->assertNotNull($result); @@ -186,10 +186,10 @@ public function testPasswordWithColons(): void $encodeFields = base64_encode(':password:with:colons'); $identityRepository = new FakeIdentityRepository($this->createIdentity()); $authenticationMethod = (new HttpBasic($identityRepository)) - ->withAuthenticationCallback(fn (?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); + ->withAuthenticationCallback(fn(?string $username, ?string $password): ?IdentityInterface => $this->createIdentity(($username ?? 'null') . ':' . ($password ?? 'null'))); $result = $authenticationMethod->authenticate( - $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]) + $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]), ); $this->assertNotNull($result); @@ -202,10 +202,10 @@ public function testSuccessfulAuthenticationWithHeaders(): void $encodeFields = base64_encode('admin:pass'); $identityRepository = new FakeIdentityRepository($this->createIdentity()); $authenticationMethod = (new HttpBasic($identityRepository)) - ->withAuthenticationCallback(fn (string $username, string $password): IdentityInterface => $this->createIdentity($username . ':' . $password)); + ->withAuthenticationCallback(fn(string $username, string $password): IdentityInterface => $this->createIdentity($username . ':' . $password)); $result = $authenticationMethod->authenticate( - $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]) + $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]), ); $this->assertNotNull($result); @@ -217,20 +217,20 @@ public function testSuccessfulAuthenticationWithHeadersContainsOnlyUsername(): v $encodeFields = base64_encode('username'); $identityRepository = new FakeIdentityRepository($this->createIdentity()); $result = (new HttpBasic($identityRepository))->authenticate( - $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]) + $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]), ); $this->assertNotNull($result); $this->assertEquals('test-id', $result->getId()); $this->assertEquals( [ - 'findIdentityByToken' => - [ + 'findIdentityByToken' + => [ 'token' => 'username', 'type' => null, ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } @@ -238,7 +238,7 @@ public function testNotAuthenticationBecauseEmptyParams(): void { $identityRepository = new FakeIdentityRepository($this->createIdentity()); $result = (new HttpBasic($identityRepository))->authenticate( - $this->createRequest() + $this->createRequest(), ); $this->assertNull($result); @@ -249,20 +249,20 @@ public function testSuccessfulAuthenticationWithRedirectParam(): void $encodeFields = base64_encode('admin:pass'); $identityRepository = new FakeIdentityRepository($this->createIdentity()); $result = (new HttpBasic($identityRepository))->authenticate( - $this->createRequest(['REDIRECT_HTTP_AUTHORIZATION' => 'Basic:' . $encodeFields]) + $this->createRequest(['REDIRECT_HTTP_AUTHORIZATION' => 'Basic:' . $encodeFields]), ); $this->assertNotNull($result); $this->assertEquals('test-id', $result->getId()); $this->assertEquals( [ - 'findIdentityByToken' => - [ + 'findIdentityByToken' + => [ 'token' => 'admin', 'type' => null, ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } @@ -271,7 +271,7 @@ public function testImmutability(): void $identityRepository = new FakeIdentityRepository($this->createIdentity()); $original = (new HttpBasic($identityRepository)); $this->assertNotSame($original, $original->withRealm('realm')); - $this->assertNotSame($original, $original->withAuthenticationCallback(static fn () => null)); + $this->assertNotSame($original, $original->withAuthenticationCallback(static fn() => null)); $this->assertNotSame($original, $original->withTokenType('api')); } @@ -283,18 +283,18 @@ public function testWithTokenType(): void (new HttpBasic($identityRepository)) ->withTokenType('another-token-type') ->authenticate( - $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]) + $this->createRequest([], [Header::AUTHORIZATION => 'Basic:' . $encodeFields]), ); $this->assertEquals( [ - 'findIdentityByToken' => - [ + 'findIdentityByToken' + => [ 'token' => 'username', 'type' => 'another-token-type', ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } diff --git a/tests/Method/HttpBearerTest.php b/tests/Method/HttpBearerTest.php index ffb3682..cbab9a8 100644 --- a/tests/Method/HttpBearerTest.php +++ b/tests/Method/HttpBearerTest.php @@ -21,7 +21,7 @@ public function testSuccessfulAuthentication(): void { $identityRepository = new FakeIdentityRepository($this->createIdentity()); $result = (new HttpBearer($identityRepository))->authenticate( - $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']) + $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']), ); $this->assertNotNull($result); @@ -33,7 +33,7 @@ public function testSuccessfulAuthentication(): void 'type' => null, ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } @@ -44,8 +44,8 @@ public function testIdentityNotFoundByToken(): void $this->assertNull( $authenticationMethod->authenticate( - $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']) - ) + $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']), + ), ); } @@ -59,7 +59,7 @@ public function testChallengeIsCorrect(): void 'Authorization realm="api"', $authenticationMethod ->challenge($response) - ->getHeaderLine(Header::WWW_AUTHENTICATE) + ->getHeaderLine(Header::WWW_AUTHENTICATE), ); } @@ -74,7 +74,7 @@ public function testCustomRealm(): void 'Authorization realm="gateway"', $authenticationMethod ->challenge($response) - ->getHeaderLine(Header::WWW_AUTHENTICATE) + ->getHeaderLine(Header::WWW_AUTHENTICATE), ); } @@ -94,7 +94,7 @@ public function testWithTokenType(): void (new HttpBearer($identityRepository)) ->withTokenType('another-token-type') ->authenticate( - $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']) + $this->createRequest([Header::AUTHORIZATION => 'Bearer api-key']), ); $this->assertEquals( @@ -104,7 +104,7 @@ public function testWithTokenType(): void 'type' => 'another-token-type', ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } diff --git a/tests/Method/HttpCookieTest.php b/tests/Method/HttpCookieTest.php index d6de171..1b79007 100644 --- a/tests/Method/HttpCookieTest.php +++ b/tests/Method/HttpCookieTest.php @@ -21,7 +21,7 @@ public function testSuccessfulAuthentication(): void $identity = $this->createIdentity(); $identityRepository = new FakeIdentityRepository($identity); $result = (new HttpCookie($identityRepository))->authenticate( - $this->createRequest(['access-token' => 'access-token-value']) + $this->createRequest(['access-token' => 'access-token-value']), ); $this->assertSame($result, $identity); @@ -32,7 +32,7 @@ public function testSuccessfulAuthentication(): void 'type' => null, ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } @@ -42,7 +42,7 @@ public function testWithoutToken(): void $identityRepository = new FakeIdentityRepository($identity); $result = (new HttpCookie($identityRepository))->authenticate( - $this->createRequest() + $this->createRequest(), ); $this->assertNull($result); @@ -55,8 +55,8 @@ public function testIdentityNotFoundByToken(): void $this->assertNull( $authenticationMethod->authenticate( - $this->createRequest(['access-token' => 'access-token-value']) - ) + $this->createRequest(['access-token' => 'access-token-value']), + ), ); } @@ -76,7 +76,7 @@ public function testCustomTokenParam(): void ->withCookieName('AuthToken'); $result = $authenticationMethod->authenticate( - $this->createRequest(['AuthToken' => 'AccessTokenValue']) + $this->createRequest(['AuthToken' => 'AccessTokenValue']), ); $this->assertNotNull($result); @@ -97,7 +97,7 @@ public function testWithTokenType(): void (new HttpCookie($identityRepository)) ->withTokenType('another-token-type') ->authenticate( - $this->createRequest(['access-token' => 'access-token-value']) + $this->createRequest(['access-token' => 'access-token-value']), ); $this->assertEquals( @@ -107,7 +107,7 @@ public function testWithTokenType(): void 'type' => 'another-token-type', ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } diff --git a/tests/Method/HttpHeaderTest.php b/tests/Method/HttpHeaderTest.php index 407edb5..0519c1c 100644 --- a/tests/Method/HttpHeaderTest.php +++ b/tests/Method/HttpHeaderTest.php @@ -20,7 +20,7 @@ public function testSuccessfulAuthentication(): void { $identityRepository = new FakeIdentityRepository($this->createIdentity()); $result = (new HttpHeader($identityRepository))->authenticate( - $this->createRequest(['X-Api-Key' => 'api-key']) + $this->createRequest(['X-Api-Key' => 'api-key']), ); $this->assertNotNull($result); @@ -34,8 +34,8 @@ public function testIdentityNotFoundByToken(): void $this->assertNull( $authenticationMethod->authenticate( - $this->createRequest(['X-Api-Key' => 'api-key']) - ) + $this->createRequest(['X-Api-Key' => 'api-key']), + ), ); } @@ -49,7 +49,7 @@ public function testChallengeIsCorrect(): void 400, $authenticationMethod ->challenge($response) - ->getStatusCode() + ->getStatusCode(), ); } @@ -60,8 +60,8 @@ public function testEmptyTokenHeader(): void $this->assertNull( $authenticationMethod->authenticate( - $this->createRequest() - ) + $this->createRequest(), + ), ); } @@ -71,7 +71,7 @@ public function testCustomHeaderName(): void $authenticationMethod = (new HttpHeader($identityRepository)) ->withHeaderName('Auth'); $result = $authenticationMethod->authenticate( - $this->createRequest(['Auth' => 'api-key']) + $this->createRequest(['Auth' => 'api-key']), ); $this->assertNotNull($result); @@ -84,20 +84,20 @@ public function testCustomPattern(): void $authenticationMethod = (new HttpHeader($identityRepository)) ->withPattern('/^CustomTokenPrefix\s+(.*?)$/'); $result = $authenticationMethod->authenticate( - $this->createRequest(['X-Api-Key' => 'CustomTokenPrefix api-key']) + $this->createRequest(['X-Api-Key' => 'CustomTokenPrefix api-key']), ); $this->assertNotNull($result); $this->assertEquals('test-id', $result->getId()); $this->assertEquals( [ - 'findIdentityByToken' => - [ + 'findIdentityByToken' + => [ 'token' => 'api-key', 'type' => null, ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } @@ -107,7 +107,7 @@ public function testCustomPatternThatDoesNotMatch(): void $authenticationMethod = (new HttpHeader($identityRepository)) ->withPattern('/^not-match-regexp/'); $result = $authenticationMethod->authenticate( - $this->createRequest(['X-Api-Key' => 'api-key']) + $this->createRequest(['X-Api-Key' => 'api-key']), ); $this->assertNull($result); @@ -128,18 +128,18 @@ public function testWithTokenType(): void (new HttpHeader($identityRepository)) ->withTokenType('another-token-type') ->authenticate( - $this->createRequest(['X-Api-Key' => 'api-key']) + $this->createRequest(['X-Api-Key' => 'api-key']), ); $this->assertEquals( [ - 'findIdentityByToken' => - [ + 'findIdentityByToken' + => [ 'token' => 'api-key', 'type' => 'another-token-type', ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } diff --git a/tests/Method/QueryParameterTest.php b/tests/Method/QueryParameterTest.php index ea760b9..a97af9c 100644 --- a/tests/Method/QueryParameterTest.php +++ b/tests/Method/QueryParameterTest.php @@ -20,7 +20,7 @@ public function testSuccessfulAuthentication(): void { $identityRepository = new FakeIdentityRepository($this->createIdentity()); $result = (new QueryParameter($identityRepository))->authenticate( - $this->createRequest(['access-token' => 'access-token-value']) + $this->createRequest(['access-token' => 'access-token-value']), ); $this->assertNotNull($result); @@ -32,7 +32,7 @@ public function testSuccessfulAuthentication(): void 'type' => null, ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } @@ -43,8 +43,8 @@ public function testIdentityNotFoundByToken(): void $this->assertNull( $authenticationMethod->authenticate( - $this->createRequest(['access-token' => 'access-token-value']) - ) + $this->createRequest(['access-token' => 'access-token-value']), + ), ); } @@ -55,8 +55,8 @@ public function testInvalidTypeToken(): void $this->assertNull( $authenticationMethod->authenticate( - $this->createRequest(['access-token' => 100]) - ) + $this->createRequest(['access-token' => 100]), + ), ); $this->assertEmpty($identityRepository->getCallParams()); @@ -80,7 +80,7 @@ public function testCustomTokenParam(): void ->withParameterName('AuthToken'); $result = $authenticationMethod->authenticate( - $this->createRequest(['AuthToken' => 'AccessTokenValue']) + $this->createRequest(['AuthToken' => 'AccessTokenValue']), ); $this->assertNotNull($result); @@ -101,7 +101,7 @@ public function testWithTokenType(): void (new QueryParameter($identityRepository)) ->withTokenType('another-token-type') ->authenticate( - $this->createRequest(['access-token' => 'access-token-value']) + $this->createRequest(['access-token' => 'access-token-value']), ); $this->assertEquals( @@ -111,7 +111,7 @@ public function testWithTokenType(): void 'type' => 'another-token-type', ], ], - $identityRepository->getCallParams() + $identityRepository->getCallParams(), ); } diff --git a/tests/Stub/FakeIdentity.php b/tests/Stub/FakeIdentity.php index fa01d84..a0660ec 100644 --- a/tests/Stub/FakeIdentity.php +++ b/tests/Stub/FakeIdentity.php @@ -10,8 +10,7 @@ final class FakeIdentity implements IdentityInterface { public function __construct( private readonly ?string $id, - ) { - } + ) {} public function getId(): ?string { diff --git a/tests/Stub/FakeIdentityRepository.php b/tests/Stub/FakeIdentityRepository.php index c2e0afd..98f5789 100644 --- a/tests/Stub/FakeIdentityRepository.php +++ b/tests/Stub/FakeIdentityRepository.php @@ -13,8 +13,7 @@ final class FakeIdentityRepository implements IdentityWithTokenRepositoryInterfa public function __construct( private readonly ?IdentityInterface $returnIdentity, - ) { - } + ) {} public function findIdentity(string $id): ?IdentityInterface {