Skip to content
Open
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
27 changes: 27 additions & 0 deletions src/Http/Faking/MockClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/MockClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Loading