-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathCacherTest.php
More file actions
357 lines (278 loc) · 10.2 KB
/
CacherTest.php
File metadata and controls
357 lines (278 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
namespace Tests\API;
use Facades\Tests\Factories\EntryFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\API\AbstractCacher;
use Statamic\Events\EntrySaved;
use Statamic\Events\Event;
use Statamic\Facades;
use Statamic\Facades\Token;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
class CacherTest extends TestCase
{
use PreventSavingStacheItemsToDisk;
private $collection;
public function setUp(): void
{
parent::setUp();
Facades\Config::set('statamic.api.enabled', true);
Facades\Config::set('statamic.api.resources.collections', true);
$this->collection = Facades\Collection::make('articles')->save();
}
protected function makeEntry($slug)
{
return EntryFactory::id($slug)->slug($slug)->collection($this->collection)->make();
}
#[Test]
public function it_caches_endpoint_using_default_cacher()
{
$this->makeEntry('apple')->save();
$this->get($endpoint = '/api/collections/articles/entries')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hash = md5($endpoint);
$cacheKey = "api-cache:$hash";
$this->assertTrue(Cache::has($cacheKey));
$this->assertEquals([$cacheKey], Cache::get('api-cache:tracked-responses'));
// manually manipulate whats in the cache so we can be sure it uses it.
Cache::put($cacheKey, [
'content' => json_encode(['foo' => 'bar']),
'status' => 200,
'headers' => ['content-type' => ['application/json']],
], 10);
$this->get($endpoint)
->assertOk()
->assertJson(['foo' => 'bar']);
}
#[Test]
#[DataProvider('bypassCacheProvider')]
public function it_bypasses_cache_when_using_a_valid_token($endpoint, $headers)
{
optional(Token::find('test-token'))->delete(); // garbage collection
Token::make('test-token', TestTokenHandler::class)->save();
$this->makeEntry('apple')->save();
$this->get($endpoint, $headers)
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hash = md5($endpoint);
$this->assertFalse(Cache::has("api-cache:$hash"));
$this->assertNull(Cache::get('api-cache:tracked-responses'));
}
#[Test]
#[DataProvider('bypassCacheProvider')]
public function it_doesnt_bypass_cache_when_using_an_invalid_token($endpoint, $headers)
{
// No token should exist, but do garbage collection.
// There may be a leftover token from a previous test.
optional(Token::find('test-token'))->delete();
$this->makeEntry('apple')->save();
$this->get($endpoint, $headers)
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hash = md5($endpoint);
$this->assertTrue(Cache::has($cacheKey = "api-cache:$hash"));
$this->assertEquals([$cacheKey], Cache::get('api-cache:tracked-responses'));
}
public static function bypassCacheProvider()
{
$endpoint = '/api/collections/articles/entries';
return [
[$endpoint.'?token=test-token', []],
[$endpoint, ['X-Statamic-Token' => 'test-token']],
];
}
#[Test]
#[DefineEnvironment('setCustomExpiry')]
public function it_caches_endpoint_using_configured_expiry()
{
$this->makeEntry('apple')->save();
$endpoint = '/api/collections/articles/entries';
$hash = md5($endpoint);
$cacheKey = "api-cache:$hash";
Carbon::setTestNow(now());
$this->get($endpoint)->assertOk();
$this->assertTrue(Cache::has($cacheKey));
Carbon::setTestNow(now()->addMinutes(14));
$this->assertFalse(Cache::has($cacheKey));
}
#[Test]
public function it_caches_endpoint_with_query_params()
{
$this->makeEntry('apple')->save();
$this->get($endpoint = '/api/collections/articles/entries?query=params')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hash = md5($endpoint);
$cacheKey = "api-cache:$hash";
$this->assertTrue(Cache::has($cacheKey));
$this->assertEquals([$cacheKey], Cache::get('api-cache:tracked-responses'));
}
#[Test]
public function it_caches_multiple_endpoints()
{
$this->makeEntry('apple')->save();
$this->get($endpointOne = '/api/collections/articles/entries?query=one')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$this->get($endpointTwo = '/api/collections/articles/entries?query=two')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hashOne = md5($endpointOne);
$hashTwo = md5($endpointTwo);
$this->assertTrue(Cache::has("api-cache:$hashOne"));
$this->assertTrue(Cache::has("api-cache:$hashTwo"));
$cachedResponses = [
"api-cache:$hashOne",
"api-cache:$hashTwo",
];
$this->assertEquals($cachedResponses, Cache::get('api-cache:tracked-responses'));
}
#[Test]
public function it_busts_whole_cache_when_content_is_saved()
{
$entry = $this->makeEntry('apple');
$entry->save();
$this->get($endpointOne = '/api/collections/articles/entries?query=one')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$this->get($endpointTwo = '/api/collections/articles/entries?query=two')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hashOne = md5($endpointOne);
$hashTwo = md5($endpointTwo);
$this->assertTrue(Cache::has("api-cache:$hashOne"));
$this->assertTrue(Cache::has("api-cache:$hashTwo"));
$this->assertCount(2, Cache::get('api-cache:tracked-responses'));
$entry->save();
$this->assertFalse(Cache::has("api-cache:$hashOne"));
$this->assertFalse(Cache::has("api-cache:$hashTwo"));
$this->assertFalse(Cache::has('api-cache:tracked-responses'));
}
#[Test]
public function it_busts_whole_cache_when_unrelated_content_is_saved()
{
$this->makeEntry('apple')->save();
$this->get($endpointOne = '/api/collections/articles/entries?query=one')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$this->get($endpointTwo = '/api/collections/articles/entries?query=two')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hashOne = md5($endpointOne);
$hashTwo = md5($endpointTwo);
$this->assertTrue(Cache::has("api-cache:$hashOne"));
$this->assertTrue(Cache::has("api-cache:$hashTwo"));
$this->assertCount(2, Cache::get('api-cache:tracked-responses'));
Facades\Form::make('contact')->save();
$this->assertFalse(Cache::has("api-cache:$hashOne"));
$this->assertFalse(Cache::has("api-cache:$hashTwo"));
$this->assertFalse(Cache::has('api-cache:tracked-responses'));
}
#[Test]
public function it_can_disable_default_cacher_by_setting_false_on_parent_cache_config()
{
Facades\Config::set('statamic.api.cache', false);
$this->makeEntry('apple')->save();
$this->get($endpoint = '/api/collections/articles/entries')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hash = md5($endpoint);
$cacheKey = "api-cache:$hash";
$this->assertFalse(Cache::has($cacheKey));
$this->assertFalse(Cache::has('api-cache:tracked-responses'));
}
#[Test]
public function it_can_disable_default_cacher_by_setting_false_on_child_class_config()
{
Facades\Config::set('statamic.api.cache.class', false);
$this->makeEntry('apple')->save();
$this->get($endpoint = '/api/collections/articles/entries')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hash = md5($endpoint);
$cacheKey = "api-cache:$hash";
$this->assertFalse(Cache::has($cacheKey));
$this->assertFalse(Cache::has('api-cache:tracked-responses'));
}
#[Test]
public function it_can_use_custom_cacher()
{
Facades\Config::set('statamic.api.cache.class', CustomCacher::class);
$entry = $this->makeEntry('apple');
$entry->save();
$this->get($endpoint = '/api/collections/articles/entries')
->assertOk()
->assertJson(['data' => [
['id' => 'apple'],
]]);
$hash = md5($endpoint);
$cacheKey = "api-cache:$hash";
$this->assertFalse(Cache::has($cacheKey));
$this->assertFalse(Cache::has('api-cache:tracked-responses'));
$this->assertTrue(Cache::has('custom-cache'));
Facades\Form::make('contact')->save();
$this->assertTrue(Cache::has('custom-cache'));
$entry->save();
$this->assertFalse(Cache::has('custom-cache'));
}
protected function setCustomExpiry($app)
{
$app->config->set('statamic.api.cache.expiry', 13);
}
}
class CustomCacher extends AbstractCacher
{
public function get(Request $request)
{
return Cache::get('custom-cache');
}
public function put(Request $request, JsonResponse $response)
{
return Cache::put('custom-cache', $response, 1);
}
public function handleInvalidationEvent(Event $event)
{
if (! $event instanceof EntrySaved) {
return;
}
Cache::forget('custom-cache');
}
}
class TestTokenHandler
{
public function handle($token, $request, $next)
{
return $next($request);
}
}