From d136fb64203ac6a78b050d1acf4ff0ca422cd976 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:10:02 +0000 Subject: [PATCH 1/2] feat: Better trait and authenticator expectations --- phpstan.neon.dist | 3 ++ .../MissingDefaultAuthException.php | 23 +++++++++ src/Expectations/Authentication.php | 51 +++++++++++++++---- src/Expectations/Trait.php | 16 +++--- tests/Feature/AuthenticationTest.php | 4 +- 5 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 src/Exceptions/MissingDefaultAuthException.php diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 7ed3fc5..1b4aebf 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -12,3 +12,6 @@ parameters: - identifier: return.type path: src/Expectations/Pagination.php + - + identifier: return.type + path: src/Expectations/Authentication.php diff --git a/src/Exceptions/MissingDefaultAuthException.php b/src/Exceptions/MissingDefaultAuthException.php new file mode 100644 index 0000000..7fca934 --- /dev/null +++ b/src/Exceptions/MissingDefaultAuthException.php @@ -0,0 +1,23 @@ +getMethod('defaultAuth')->getReturnType(); + + if (! $type instanceof ReflectionNamedType) { + throw new RuntimeException('defaultAuth() must declare a single named return type.'); + } + + $authenticator = $type->getName(); + + if ($authenticator === Authenticator::class) { + throw MissingDefaultAuthException::for($expectationClassName); + } + + return $authenticator; +} expect()->extend( 'toUseTokenAuthentication', - fn (): ArchExpectation => $this->toUse(TokenAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(TokenAuthenticator::class) ); expect()->extend( 'toUseBasicAuthentication', - fn (): ArchExpectation => $this->toUse(BasicAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(BasicAuthenticator::class) ); expect()->extend( 'toUseCertificateAuthentication', - fn (): ArchExpectation => $this->toUse(CertificateAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(CertificateAuthenticator::class) ); expect()->extend( 'toUseHeaderAuthentication', - fn (): ArchExpectation => $this->toUse(HeaderAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(HeaderAuthenticator::class) ); expect()->extend( 'toUseQueryAuthentication', - fn (): ArchExpectation => $this->toUse(QueryAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(QueryAuthenticator::class) ); expect()->extend( 'toUseDigestAuthentication', - fn (): ArchExpectation => $this->toUse(DigestAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(DigestAuthenticator::class) ); expect()->extend( 'toUseMultipleAuthenticators', - fn (): ArchExpectation => $this->toUse(MultiAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(MultiAuthenticator::class) ); expect()->extend( 'toUseAccessTokenAuthentication', - fn (): ArchExpectation => $this->toUse(AccessTokenAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(AccessTokenAuthenticator::class) ); expect()->extend( 'toUseNullAuthentication', - fn (): ArchExpectation => $this->toUse(NullAuthenticator::class) + fn (): Expectation => $this->and(lawmanDefaultAuthenticator($this->value)) + ->toEqual(NullAuthenticator::class) ); expect()->extend( diff --git a/src/Expectations/Trait.php b/src/Expectations/Trait.php index 7def5dd..36defc9 100644 --- a/src/Expectations/Trait.php +++ b/src/Expectations/Trait.php @@ -14,40 +14,40 @@ expect()->extend( 'toUseAcceptsJsonTrait', - fn (): ArchExpectation => $this->toUse(AcceptsJson::class) + fn (): ArchExpectation => $this->toUseTrait(AcceptsJson::class) ); expect()->extend( 'toUseAlwaysThrowOnErrorsTrait', - fn (): ArchExpectation => $this->toUse(AlwaysThrowOnErrors::class) + fn (): ArchExpectation => $this->toUseTrait(AlwaysThrowOnErrors::class) ); expect()->extend( 'toUseTimeoutTrait', - fn (): ArchExpectation => $this->toUse(HasTimeout::class) + fn (): ArchExpectation => $this->toUseTrait(HasTimeout::class) ); expect()->extend( 'toUseAuthorisationCodeGrantTrait', - fn (): ArchExpectation => $this->toUse(AuthorizationCodeGrant::class) + fn (): ArchExpectation => $this->toUseTrait(AuthorizationCodeGrant::class) ); expect()->extend( 'toUseClientCredentialsGrantTrait', - fn (): ArchExpectation => $this->toUse(ClientCredentialsGrant::class) + fn (): ArchExpectation => $this->toUseTrait(ClientCredentialsGrant::class) ); expect()->extend( 'toUseClientCredentialsBasicAuthGrantTrait', - fn (): ArchExpectation => $this->toUse(ClientCredentialsBasicAuthGrant::class) + fn (): ArchExpectation => $this->toUseTrait(ClientCredentialsBasicAuthGrant::class) ); expect()->extend( 'toUseRequiresAuthTrait', - fn (): ArchExpectation => $this->toUse(RequiresAuth::class) + fn (): ArchExpectation => $this->toUseTrait(RequiresAuth::class) ); expect()->extend( 'toUseApiVersionTrait', - fn (): ArchExpectation => $this->toUse(HasApiVersion::class) + fn (): ArchExpectation => $this->toUseTrait(HasApiVersion::class) ); diff --git a/tests/Feature/AuthenticationTest.php b/tests/Feature/AuthenticationTest.php index 786010c..218e9f3 100644 --- a/tests/Feature/AuthenticationTest.php +++ b/tests/Feature/AuthenticationTest.php @@ -35,9 +35,7 @@ it('checks that a class uses multiple authentication methods', function (): void { expect(ToUseMultipleAuthenticators::class) - ->toUseMultipleAuthenticators() - ->toUseCertificateAuthentication() - ->toUseTokenAuthentication(); + ->toUseMultipleAuthenticators(); }); it('checks that a class uses digest authentication', function (): void { From d73b8408bdfcb8d40d87c62338805c905e5f23a7 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:14:22 +0000 Subject: [PATCH 2/2] feat: Update all toUse to toUseTrait when checking traits --- src/Expectations/Cache.php | 2 +- src/Expectations/Pagination.php | 2 +- src/Expectations/RateLimit.php | 2 +- src/Expectations/Request.php | 12 ++++++------ src/Expectations/Response.php | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Expectations/Cache.php b/src/Expectations/Cache.php index 4d69b6d..2a4a2dd 100644 --- a/src/Expectations/Cache.php +++ b/src/Expectations/Cache.php @@ -8,6 +8,6 @@ expect()->extend( 'toHaveCaching', - fn (): ArchExpectation => $this->toUse(HasCaching::class) + fn (): ArchExpectation => $this->toUseTrait(HasCaching::class) ->toImplement(Cacheable::class) ); diff --git a/src/Expectations/Pagination.php b/src/Expectations/Pagination.php index 52c4df2..494fb95 100644 --- a/src/Expectations/Pagination.php +++ b/src/Expectations/Pagination.php @@ -145,7 +145,7 @@ function (): Expectation { expect()->extend( 'toUseAsyncPagination', - fn (): ArchExpectation => $this->toUse(HasAsyncPagination::class) + fn (): ArchExpectation => $this->toUseTrait(HasAsyncPagination::class) ); expect()->extend( diff --git a/src/Expectations/RateLimit.php b/src/Expectations/RateLimit.php index 089cb21..bbafe6d 100644 --- a/src/Expectations/RateLimit.php +++ b/src/Expectations/RateLimit.php @@ -7,5 +7,5 @@ expect()->extend( 'toHaveRateLimits', - fn (): ArchExpectation => $this->toUse(HasRateLimits::class) + fn (): ArchExpectation => $this->toUseTrait(HasRateLimits::class) ); diff --git a/src/Expectations/Request.php b/src/Expectations/Request.php index 04b4920..39e623e 100644 --- a/src/Expectations/Request.php +++ b/src/Expectations/Request.php @@ -150,37 +150,37 @@ function () { expect()->extend( 'toHaveJsonBody', fn (): ArchExpectation => $this->toImplement(HasBody::class) - ->toUse(HasJsonBody::class) + ->toUseTrait(HasJsonBody::class) ); expect()->extend( 'toHaveMultipartBody', fn (): ArchExpectation => $this->toImplement(HasBody::class) - ->toUse(HasMultipartBody::class) + ->toUseTrait(HasMultipartBody::class) ); expect()->extend( 'toHaveXmlBody', fn (): ArchExpectation => $this->toImplement(HasBody::class) - ->toUse(HasXmlBody::class) + ->toUseTrait(HasXmlBody::class) ); expect()->extend( 'toHaveFormBody', fn (): ArchExpectation => $this->toImplement(HasBody::class) - ->toUse(HasFormBody::class) + ->toUseTrait(HasFormBody::class) ); expect()->extend( 'toHaveStringBody', fn (): ArchExpectation => $this->toImplement(HasBody::class) - ->toUse(HasStringBody::class) + ->toUseTrait(HasStringBody::class) ); expect()->extend( 'toHaveStreamBody', fn (): ArchExpectation => $this->toImplement(HasBody::class) - ->toUse(HasStreamBody::class) + ->toUseTrait(HasStreamBody::class) ); expect()->extend( diff --git a/src/Expectations/Response.php b/src/Expectations/Response.php index 4c39471..32a186d 100644 --- a/src/Expectations/Response.php +++ b/src/Expectations/Response.php @@ -15,5 +15,5 @@ expect()->extend( 'toBeSaloonDto', fn (): ArchExpectation => $this->toImplement(WithResponse::class) - ->toUse(HasResponse::class) + ->toUseTrait(HasResponse::class) );