diff --git a/src/Config.php b/src/Config.php index d7fb9c90..f0d7f4a7 100644 --- a/src/Config.php +++ b/src/Config.php @@ -47,6 +47,11 @@ final class Config */ private static ?MiddlewarePipeline $globalMiddlewarePipeline = null; + /** + * Whether stray requests should be prevented + */ + private static bool $preventStrayRequests = false; + /** * Write a custom sender resolver */ @@ -86,10 +91,20 @@ public static function clearGlobalMiddleware(): void */ public static function preventStrayRequests(): void { + self::$preventStrayRequests = true; + self::globalMiddleware()->onRequest(static function (PendingRequest $pendingRequest) { - if (! $pendingRequest->hasMockClient()) { + if (self::$preventStrayRequests && ! $pendingRequest->hasMockClient()) { throw new StrayRequestException; } }, order: PipeOrder::LAST); } + + /** + * Allow stray requests without a MockClient. + */ + public static function allowStrayRequests(): void + { + self::$preventStrayRequests = false; + } } diff --git a/src/Http/BaseResource.php b/src/Http/BaseResource.php index 3188bebf..b37441e6 100644 --- a/src/Http/BaseResource.php +++ b/src/Http/BaseResource.php @@ -9,7 +9,7 @@ class BaseResource /** * Constructor */ - public function __construct(readonly protected Connector $connector) + public function __construct(protected readonly Connector $connector) { // } diff --git a/tests/Fixtures/Authenticators/CustomOAuthAuthenticator.php b/tests/Fixtures/Authenticators/CustomOAuthAuthenticator.php index 8ea17b3b..b56237f6 100644 --- a/tests/Fixtures/Authenticators/CustomOAuthAuthenticator.php +++ b/tests/Fixtures/Authenticators/CustomOAuthAuthenticator.php @@ -13,10 +13,10 @@ class CustomOAuthAuthenticator extends AccessTokenAuthenticator * Constructor */ public function __construct( - readonly public string $accessToken, - readonly public string $greeting, - readonly public ?string $refreshToken = null, - readonly public ?DateTimeImmutable $expiresAt = null, + public readonly string $accessToken, + public readonly string $greeting, + public readonly ?string $refreshToken = null, + public readonly ?DateTimeImmutable $expiresAt = null, ) { // } diff --git a/tests/Fixtures/Requests/QueryParameterRequest.php b/tests/Fixtures/Requests/QueryParameterRequest.php index b7c8e5fd..9f4c863e 100644 --- a/tests/Fixtures/Requests/QueryParameterRequest.php +++ b/tests/Fixtures/Requests/QueryParameterRequest.php @@ -25,7 +25,7 @@ class QueryParameterRequest extends Request /** * Constructor */ - public function __construct(readonly public string $endpoint = '/user') + public function __construct(public readonly string $endpoint = '/user') { // } diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php index 60049178..ee0f22db 100644 --- a/tests/Unit/ConfigTest.php +++ b/tests/Unit/ConfigTest.php @@ -84,3 +84,19 @@ Config::clearGlobalMiddleware(); }); + +test('you can prevent and then allow stray api requests', function () { + Config::preventStrayRequests(); + + try { + TestConnector::make()->send(new UserRequest); + } catch (StrayRequestException $e) { + expect($e)->toBeInstanceOf(StrayRequestException::class); + } + + Config::allowStrayRequests(); + + TestConnector::make()->send(new UserRequest); + + Config::clearGlobalMiddleware(); +});