From f7581be261b1c85021cb8cc768a4d203115c1047 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sun, 12 Apr 2026 16:05:30 +0100 Subject: [PATCH] add withoutCache() to mock client --- src/Http/Faking/MockClient.php | 27 +++++++++++++++++++++++++++ tests/Unit/MockClientTest.php | 10 ++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/Http/Faking/MockClient.php b/src/Http/Faking/MockClient.php index 7250deca..e65a5fe6 100644 --- a/src/Http/Faking/MockClient.php +++ b/src/Http/Faking/MockClient.php @@ -58,6 +58,13 @@ class MockClient */ protected static ?MockClient $globalMockClient = null; + /** + * When true, the response cache plugin will not read or write the cache + * for requests using this mock client (for example, so fixture files are + * re-read from disk on each request during tests). + */ + protected bool $withoutResponseCache = false; + /** * Constructor * @@ -404,6 +411,26 @@ public function findResponseByRequestUrl(string $url, ?int $index = null): ?Resp return null; } + /** + * Disable the response cache while this mock client is in use. + * + * @return $this + */ + public function withoutCache(): static + { + $this->withoutResponseCache = true; + + return $this; + } + + /** + * Whether the response cache plugin should bypass caching. + */ + public function shouldBypassResponseCache(): bool + { + return $this->withoutResponseCache; + } + /** * Register a global mock client * diff --git a/tests/Unit/MockClientTest.php b/tests/Unit/MockClientTest.php index f542bee0..b998583f 100644 --- a/tests/Unit/MockClientTest.php +++ b/tests/Unit/MockClientTest.php @@ -261,3 +261,13 @@ $response = connector()->send(new UserRequest, $mockClient); $response->throw(); }); + +test('mock client can opt out of response cache integration', function () { + $client = new MockClient([]); + + expect($client->shouldBypassResponseCache())->toBeFalse(); + + $client->withoutCache(); + + expect($client->shouldBypassResponseCache())->toBeTrue(); +});