Skip to content

Commit 339cfe7

Browse files
ziadozSammyjo20
andauthored
Add allow stray requests method to config (#522)
* allow stray requests * Code style fixer --------- Co-authored-by: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com>
1 parent b1868db commit 339cfe7

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

src/Config.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ final class Config
4747
*/
4848
private static ?MiddlewarePipeline $globalMiddlewarePipeline = null;
4949

50+
/**
51+
* Whether stray requests should be prevented
52+
*/
53+
private static bool $preventStrayRequests = false;
54+
5055
/**
5156
* Write a custom sender resolver
5257
*/
@@ -86,10 +91,20 @@ public static function clearGlobalMiddleware(): void
8691
*/
8792
public static function preventStrayRequests(): void
8893
{
94+
self::$preventStrayRequests = true;
95+
8996
self::globalMiddleware()->onRequest(static function (PendingRequest $pendingRequest) {
90-
if (! $pendingRequest->hasMockClient()) {
97+
if (self::$preventStrayRequests && ! $pendingRequest->hasMockClient()) {
9198
throw new StrayRequestException;
9299
}
93100
}, order: PipeOrder::LAST);
94101
}
102+
103+
/**
104+
* Allow stray requests without a MockClient.
105+
*/
106+
public static function allowStrayRequests(): void
107+
{
108+
self::$preventStrayRequests = false;
109+
}
95110
}

src/Http/BaseResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class BaseResource
99
/**
1010
* Constructor
1111
*/
12-
public function __construct(readonly protected Connector $connector)
12+
public function __construct(protected readonly Connector $connector)
1313
{
1414
//
1515
}

tests/Fixtures/Authenticators/CustomOAuthAuthenticator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class CustomOAuthAuthenticator extends AccessTokenAuthenticator
1313
* Constructor
1414
*/
1515
public function __construct(
16-
readonly public string $accessToken,
17-
readonly public string $greeting,
18-
readonly public ?string $refreshToken = null,
19-
readonly public ?DateTimeImmutable $expiresAt = null,
16+
public readonly string $accessToken,
17+
public readonly string $greeting,
18+
public readonly ?string $refreshToken = null,
19+
public readonly ?DateTimeImmutable $expiresAt = null,
2020
) {
2121
//
2222
}

tests/Fixtures/Requests/QueryParameterRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class QueryParameterRequest extends Request
2525
/**
2626
* Constructor
2727
*/
28-
public function __construct(readonly public string $endpoint = '/user')
28+
public function __construct(public readonly string $endpoint = '/user')
2929
{
3030
//
3131
}

tests/Unit/ConfigTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,19 @@
8484

8585
Config::clearGlobalMiddleware();
8686
});
87+
88+
test('you can prevent and then allow stray api requests', function () {
89+
Config::preventStrayRequests();
90+
91+
try {
92+
TestConnector::make()->send(new UserRequest);
93+
} catch (StrayRequestException $e) {
94+
expect($e)->toBeInstanceOf(StrayRequestException::class);
95+
}
96+
97+
Config::allowStrayRequests();
98+
99+
TestConnector::make()->send(new UserRequest);
100+
101+
Config::clearGlobalMiddleware();
102+
});

0 commit comments

Comments
 (0)