Skip to content

Commit b0a0607

Browse files
authored
Merge pull request #7 from Sammyjo20/feature/v2-overwrite-cacheable-methods
Feature | V2 Allow users to overwrite cacheable methods
2 parents 096fbfe + 573f361 commit b0a0607

File tree

4 files changed

+85
-2
lines changed

4 files changed

+85
-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: 11 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

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

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

tests/Feature/CacheTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedPostRequest;
1313
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedUserRequest;
1414
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedConnectorRequest;
15+
use Saloon\CachePlugin\Tests\Fixtures\Requests\AllowedCachedPostRequest;
1516
use Saloon\CachePlugin\Tests\Fixtures\Requests\CustomKeyCachedUserRequest;
1617
use Saloon\CachePlugin\Tests\Fixtures\Requests\ShortLivedCachedUserRequest;
1718
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedUserRequestWithoutCacheable;
@@ -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 method', 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)