Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ parameters:
-
identifier: return.type
path: src/Expectations/Pagination.php
-
identifier: return.type
path: src/Expectations/Authentication.php
23 changes: 23 additions & 0 deletions src/Exceptions/MissingDefaultAuthException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Saloon\Lawman\Exceptions;

use RuntimeException;
use Saloon\Contracts\Authenticator;

final class MissingDefaultAuthException extends RuntimeException
{
/**
* @param class-string $class
*/
public static function for(string $class): self
{
return new self(sprintf(
'[%s] does not define a defaultAuth() method returning a concrete authenticator; its return type resolves to the generic [%s] contract.',
$class,
Authenticator::class,
));
}
}
51 changes: 42 additions & 9 deletions src/Expectations/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Pest\Arch\Contracts\ArchExpectation;
use Pest\Expectation;
use Saloon\Contracts\Authenticator;
use Saloon\Contracts\OAuthAuthenticator;
use Saloon\Http\Auth\AccessTokenAuthenticator;
Expand All @@ -14,50 +15,82 @@
use Saloon\Http\Auth\NullAuthenticator;
use Saloon\Http\Auth\QueryAuthenticator;
use Saloon\Http\Auth\TokenAuthenticator;
use Saloon\Lawman\Exceptions\MissingDefaultAuthException;

/**
* Resolve the authenticator a class declares as the return type of its defaultAuth() method.
*/
function lawmanDefaultAuthenticator(mixed $value): string
{
$expectationClassName = lawmanExpectationClassName($value);

$type = (new ReflectionClass($expectationClassName))->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(
Expand Down
2 changes: 1 addition & 1 deletion src/Expectations/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

expect()->extend(
'toHaveCaching',
fn (): ArchExpectation => $this->toUse(HasCaching::class)
fn (): ArchExpectation => $this->toUseTrait(HasCaching::class)
->toImplement(Cacheable::class)
);
2 changes: 1 addition & 1 deletion src/Expectations/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function (): Expectation {

expect()->extend(
'toUseAsyncPagination',
fn (): ArchExpectation => $this->toUse(HasAsyncPagination::class)
fn (): ArchExpectation => $this->toUseTrait(HasAsyncPagination::class)
);

expect()->extend(
Expand Down
2 changes: 1 addition & 1 deletion src/Expectations/RateLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

expect()->extend(
'toHaveRateLimits',
fn (): ArchExpectation => $this->toUse(HasRateLimits::class)
fn (): ArchExpectation => $this->toUseTrait(HasRateLimits::class)
);
12 changes: 6 additions & 6 deletions src/Expectations/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/Expectations/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
expect()->extend(
'toBeSaloonDto',
fn (): ArchExpectation => $this->toImplement(WithResponse::class)
->toUse(HasResponse::class)
->toUseTrait(HasResponse::class)
);
16 changes: 8 additions & 8 deletions src/Expectations/Trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
4 changes: 1 addition & 3 deletions tests/Feature/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading