From 14b288421c42fa38f23b24484ec8928ba2080ea8 Mon Sep 17 00:00:00 2001 From: JonPurvis Date: Sun, 12 Apr 2026 16:15:42 +0100 Subject: [PATCH 1/5] respect MockClient::shouldBypassResponseCache() --- src/Http/Middleware/CacheMiddleware.php | 4 + .../Feature/ResponseCacheMockFixtureTest.php | 74 +++++++++++++++++++ .../Fixtures/Saloon/cached-user-request.json | 10 +++ 3 files changed, 88 insertions(+) create mode 100644 tests/Feature/ResponseCacheMockFixtureTest.php create mode 100644 tests/Fixtures/Saloon/cached-user-request.json diff --git a/src/Http/Middleware/CacheMiddleware.php b/src/Http/Middleware/CacheMiddleware.php index 228695f..32ee523 100644 --- a/src/Http/Middleware/CacheMiddleware.php +++ b/src/Http/Middleware/CacheMiddleware.php @@ -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)); diff --git a/tests/Feature/ResponseCacheMockFixtureTest.php b/tests/Feature/ResponseCacheMockFixtureTest.php new file mode 100644 index 0000000..6c40b8e --- /dev/null +++ b/tests/Feature/ResponseCacheMockFixtureTest.php @@ -0,0 +1,74 @@ +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); +}); diff --git a/tests/Fixtures/Saloon/cached-user-request.json b/tests/Fixtures/Saloon/cached-user-request.json new file mode 100644 index 0000000..0cc2f18 --- /dev/null +++ b/tests/Fixtures/Saloon/cached-user-request.json @@ -0,0 +1,10 @@ +{ + "statusCode": 200, + "headers": { + "Content-Type": [ + "application\/json" + ] + }, + "data": "{\"version\":2}", + "context": [] +} \ No newline at end of file From e8e08ff7ed5641a0ce996b8155011161531a6f63 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Fri, 18 Sep 2026 21:34:52 +0100 Subject: [PATCH 2/5] Updated minimum Composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e3ae38b..9783a3a 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "minimum-stability": "stable", "require": { "php": "^8.2", - "saloonphp/saloon": "^3.0 || ^4.0" + "saloonphp/saloon": "^4.1.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.13", From c6f1f2516cc1b4009348baca9d59b71319e58ecd Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Fri, 18 Sep 2026 21:36:15 +0100 Subject: [PATCH 3/5] Removed PHP 8.2 --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 1fdc01c..8a0299c 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 }} From ecf8b5899a08f41d559161d1cf66337990ef300b Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Fri, 18 Sep 2026 21:37:36 +0100 Subject: [PATCH 4/5] Updated minimum PHP 8.3 --- .github/workflows/php-cs-fixer.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-cs-fixer.yml index 00ec14d..a107261 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-cs-fixer.yml @@ -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: | diff --git a/composer.json b/composer.json index 9783a3a..a238dd9 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "minimum-stability": "stable", "require": { - "php": "^8.2", + "php": "^8.3", "saloonphp/saloon": "^4.1.0" }, "require-dev": { From 105d59fc32f6ba2ad793b2ea7c8b10509c690577 Mon Sep 17 00:00:00 2001 From: Sammyjo20 <29132017+Sammyjo20@users.noreply.github.com> Date: Fri, 18 Sep 2026 21:39:26 +0100 Subject: [PATCH 5/5] Updated code style --- tests/Feature/ResponseCacheMockFixtureTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/ResponseCacheMockFixtureTest.php b/tests/Feature/ResponseCacheMockFixtureTest.php index 6c40b8e..4bc32da 100644 --- a/tests/Feature/ResponseCacheMockFixtureTest.php +++ b/tests/Feature/ResponseCacheMockFixtureTest.php @@ -3,11 +3,11 @@ 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; -use Saloon\Http\Faking\MockClient; -use Saloon\Http\Faking\MockResponse; $filesystem = new Filesystem(new LocalFilesystemAdapter(cachePath()));