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
4 changes: 4 additions & 0 deletions src/Http/Middleware/CacheMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function __construct(
*/
public function __invoke(PendingRequest $pendingRequest): ?FakeResponse
{
if ($pendingRequest->getMockClient()?->shouldBypassResponseCache() === true) {
return null;
}

$driver = $this->driver;
$cacheKey = hash('sha256', $this->cacheKey ?? CacheKeyHelper::create($pendingRequest));

Expand Down
74 changes: 74 additions & 0 deletions tests/Feature/ResponseCacheMockFixtureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

use League\Flysystem\Filesystem;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Saloon\CachePlugin\Tests\Fixtures\Connectors\TestConnector;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedUserRequest;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;

$filesystem = new Filesystem(new LocalFilesystemAdapter(cachePath()));

beforeEach(function () use ($filesystem): void {
$filesystem->deleteDirectory('/');
});

function cachedUserRequestFixtureAbsolutePath(): string
{
$dir = getcwd().'/tests/Fixtures/Saloon';

if (! is_dir($dir)) {
mkdir($dir, 0755, true);
}

return $dir.'/cached-user-request.json';
}

function writeCachedUserRequestFixture(int $version): void
{
$payload = [
'statusCode' => 200,
'headers' => ['Content-Type' => ['application/json']],
'data' => json_encode(['version' => $version]),
'context' => [],
];

file_put_contents(
cachedUserRequestFixtureAbsolutePath(),
json_encode($payload, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT)
);
}

test('response cache can return a stale body when mocks use fixtures and withoutCache is not used', function (): void {
writeCachedUserRequestFixture(1);

$mockClient = new MockClient([
CachedUserRequest::class => MockResponse::fixture('cached-user-request'),
]);

$connector = TestConnector::make();

expect($connector->send(new CachedUserRequest, $mockClient)->json('version'))->toBe(1);

writeCachedUserRequestFixture(2);

expect($connector->send(new CachedUserRequest, $mockClient)->json('version'))->toBe(1);
});

test('withoutCache on the mock client skips response cache so updated fixture files are used', function (): void {
writeCachedUserRequestFixture(1);

$mockClient = (new MockClient([
CachedUserRequest::class => MockResponse::fixture('cached-user-request'),
]))->withoutCache();

$connector = TestConnector::make();

expect($connector->send(new CachedUserRequest, $mockClient)->json('version'))->toBe(1);

writeCachedUserRequestFixture(2);

expect($connector->send(new CachedUserRequest, $mockClient)->json('version'))->toBe(2);
});
10 changes: 10 additions & 0 deletions tests/Fixtures/Saloon/cached-user-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"statusCode": 200,
"headers": {
"Content-Type": [
"application\/json"
]
},
"data": "{\"version\":2}",
"context": []
}