Skip to content

Commit 7a297a8

Browse files
committed
Allow users to overwrite cacheable methods
1 parent 096fbfe commit 7a297a8

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"minimum-stability": "stable",
1919
"require": {
2020
"php": "^8.1",
21-
"sammyjo20/saloon": "2.0.0-beta3"
21+
"sammyjo20/saloon": "2.0.0-beta5"
2222
},
2323
"require-dev": {
2424
"friendsofphp/php-cs-fixer": "^3.13",

src/Traits/HasCaching.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function bootHasCaching(PendingRequest $pendingRequest): void
4646
return;
4747
}
4848

49-
if (! in_array($pendingRequest->getMethod(), [Method::GET, Method::OPTIONS], true)) {
49+
if (! in_array($pendingRequest->getMethod(), $this->getCacheableMethods(), true)) {
5050
return;
5151
}
5252

@@ -63,6 +63,7 @@ public function bootHasCaching(PendingRequest $pendingRequest): void
6363

6464
$pendingRequest->middleware()->onRequest(
6565
callable: new CacheMiddleware($cacheDriver, $cacheExpiryInSeconds, $this->cacheKey($pendingRequest), $this->invalidateCache),
66+
name: 'cacheMiddleware',
6667
);
6768
}
6869

@@ -112,4 +113,14 @@ public function invalidateCache(): static
112113

113114
return $this;
114115
}
116+
117+
/**
118+
* Define the cacheable methods that can be used
119+
*
120+
* @return array<\Saloon\Enums\Method>
121+
*/
122+
protected function getCacheableMethods(): array
123+
{
124+
return [Method::GET, Method::OPTIONS];
125+
}
115126
}

tests/Feature/CacheTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
declare(strict_types=1);
44

55
use League\Flysystem\Filesystem;
6+
use Saloon\CachePlugin\Tests\Fixtures\Requests\AllowedCachedPostRequest;
67
use Saloon\Http\Faking\MockClient;
78
use Saloon\Http\Faking\MockResponse;
89
use League\Flysystem\Local\LocalFilesystemAdapter;
@@ -109,6 +110,22 @@
109110
expect($responseB->json())->toEqual(['name' => 'Gareth']);
110111
});
111112

113+
test('it will cache post requests if you customise the cacheableMethods property', function () {
114+
$mockClient = new MockClient([
115+
MockResponse::make(['name' => 'Sam']),
116+
]);
117+
118+
$responseA = TestConnector::make()->send(new AllowedCachedPostRequest, $mockClient);
119+
120+
expect($responseA->isCached())->toBeFalse();
121+
expect($responseA->json())->toEqual(['name' => 'Sam']);
122+
123+
$responseB = TestConnector::make()->send(new AllowedCachedPostRequest, $mockClient);
124+
125+
expect($responseB->isCached())->toBeTrue();
126+
expect($responseB->json())->toEqual(['name' => 'Sam']);
127+
});
128+
112129
test('a response will not be cached if the response was not 2xx', function () {
113130
$mockClient = new MockClient([
114131
MockResponse::make(['name' => 'Sam'], 422),
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Saloon\CachePlugin\Tests\Fixtures\Requests;
6+
7+
use Saloon\Enums\Method;
8+
use Saloon\Http\Request;
9+
use League\Flysystem\Filesystem;
10+
use Saloon\Contracts\Body\HasBody;
11+
use Saloon\Traits\Body\HasJsonBody;
12+
use Saloon\CachePlugin\Contracts\Driver;
13+
use Saloon\CachePlugin\Traits\HasCaching;
14+
use Saloon\CachePlugin\Contracts\Cacheable;
15+
use Saloon\CachePlugin\Drivers\FlysystemDriver;
16+
use League\Flysystem\Local\LocalFilesystemAdapter;
17+
18+
class AllowedCachedPostRequest extends Request implements Cacheable, HasBody
19+
{
20+
use HasCaching;
21+
use HasJsonBody;
22+
23+
protected Method $method = Method::POST;
24+
25+
public function resolveEndpoint(): string
26+
{
27+
return '/data';
28+
}
29+
30+
public function defaultData(): array
31+
{
32+
return [
33+
'name' => 'Sammy',
34+
];
35+
}
36+
37+
public function resolveCacheDriver(): Driver
38+
{
39+
return new FlysystemDriver(new Filesystem(new LocalFilesystemAdapter(cachePath())));
40+
}
41+
42+
public function cacheExpiryInSeconds(): int
43+
{
44+
return 60;
45+
}
46+
47+
/**
48+
* Define the cacheable methods that can be used
49+
*
50+
* @return array<\Saloon\Enums\Method>
51+
*/
52+
protected function getCacheableMethods(): array
53+
{
54+
return [Method::GET, Method::OPTIONS, Method::POST];
55+
}
56+
}

0 commit comments

Comments
 (0)