Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
php-version: '8.3'

- name: Install Dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: true
matrix:
os: [ ubuntu-latest, windows-latest ]
php: [ 8.2, 8.3, 8.4 ]
php: [ 8.3, 8.4 ]
stability: [ prefer-lowest, prefer-stable ]

name: P${{ matrix.php }} - ${{ matrix.stability }} - ${{ matrix.os }}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
],
"minimum-stability": "stable",
"require": {
"php": "^8.2",
"saloonphp/saloon": "^3.0 || ^4.0"
"php": "^8.3",
"saloonphp/saloon": "^4.1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.13",
Expand Down
4 changes: 4 additions & 0 deletions src/Http/Middleware/CacheMiddleware.php
Comment thread
Sammyjo20 marked this conversation as resolved.
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 Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Saloon\CachePlugin\Tests\Fixtures\Connectors\TestConnector;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedUserRequest;

$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": []
}
Loading