-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCacheTest.php
More file actions
488 lines (352 loc) · 16.6 KB
/
CacheTest.php
File metadata and controls
488 lines (352 loc) · 16.6 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
<?php
declare(strict_types=1);
use League\Flysystem\Filesystem;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\CachePlugin\Data\CachedResponse;
use League\Flysystem\Local\LocalFilesystemAdapter;
use Saloon\CachePlugin\Exceptions\HasCachingException;
use Saloon\CachePlugin\Tests\Fixtures\Stores\ArrayCache;
use Saloon\CachePlugin\Tests\Fixtures\Connectors\TestConnector;
use Saloon\CachePlugin\Tests\Fixtures\Connectors\CachedConnector;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedPostRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedUserRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\BodyCacheKeyRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedConnectorRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\AllowedCachedPostRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CustomKeyCachedUserRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\ResponseBasedExpiryRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\ShortLivedCachedUserRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\ResolveCacheExpiryCachedRequest;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedUserRequestWithoutCacheable;
use Saloon\CachePlugin\Tests\Fixtures\Requests\CachedUserRequestOnCachedConnector;
$filesystem = new Filesystem(new LocalFilesystemAdapter(cachePath()));
beforeEach(function () use ($filesystem) {
$filesystem->deleteDirectory('/');
});
test('a request with the HasCaching trait will cache the response with a real request', function () {
$responseA = TestConnector::make()->send(new CachedUserRequest);
$responseBody = [
'name' => 'Sammyjo20',
'actual_name' => 'Sam',
'twitter' => '@carre_sam',
];
expect($responseA->isFaked())->toBeFalse();
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual($responseBody);
// Now send a response without the mock middleware, and it should be cached!
$responseB = TestConnector::make()->send(new CachedUserRequest);
expect($responseB->isFaked())->toBeTrue();
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual($responseBody);
});
test('a request with the HasCaching trait will cache the response', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam'], 201, ['X-Howdy' => 'Yeehaw']),
]);
$responseA = TestConnector::make()->send(new CachedUserRequest, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->isMocked())->toBeTrue();
expect($responseA->status())->toEqual(201);
expect($responseA->json())->toEqual(['name' => 'Sam']);
expect($responseA->header('X-Howdy'))->toEqual('Yeehaw');
// Now send a response without the mock middleware, and it should be cached!
// We'll also pass the mock client in here to ensure it's being ignored
$responseB = TestConnector::make()->send(new CachedUserRequest, $mockClient);
expect($responseB->isFaked())->toBeTrue();
expect($responseB->isCached())->toBeTrue();
expect($responseB->isMocked())->toBeFalse();
expect($responseB->status())->toEqual(201);
expect($responseB->json())->toEqual(['name' => 'Sam']);
expect($responseB->header('X-Howdy'))->toEqual('Yeehaw');
});
test('a request with the HasCaching trait will cache the response with string body', function () {
$mockClient = new MockClient([
MockResponse::make('<p>Hi</p>', 201, ['X-Howdy' => 'Yeehaw']),
]);
$responseA = TestConnector::make()->send(new CachedUserRequest, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->status())->toEqual(201);
expect($responseA->body())->toEqual('<p>Hi</p>');
expect($responseA->header('X-Howdy'))->toEqual('Yeehaw');
// Now send a response without the mock middleware, and it should be cached!
$responseB = TestConnector::make()->send(new CachedUserRequest);
expect($responseB->isFaked())->toBeTrue();
expect($responseB->isCached())->toBeTrue();
expect($responseB->status())->toEqual(201);
expect($responseB->body())->toEqual('<p>Hi</p>');
expect($responseB->header('X-Howdy'))->toEqual('Yeehaw');
});
test('it wont cache on anything other than GET and OPTIONS', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Gareth']),
]);
$responseA = TestConnector::make()->send(new CachedPostRequest, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
$responseB = TestConnector::make()->send(new CachedPostRequest, $mockClient);
expect($responseB->isCached())->toBeFalse();
expect($responseB->json())->toEqual(['name' => 'Gareth']);
});
test('it will cache post requests if you customise the cacheableMethods method', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$responseA = TestConnector::make()->send(new AllowedCachedPostRequest, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
$responseB = TestConnector::make()->send(new AllowedCachedPostRequest, $mockClient);
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual(['name' => 'Sam']);
});
test('a response will not be cached if the response was not 2xx', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam'], 422),
MockResponse::make(['name' => 'Gareth'], 500),
]);
$responseA = TestConnector::make()->send(new CachedUserRequest, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
expect($responseA->status())->toEqual(422);
$responseB = TestConnector::make()->send(new CachedUserRequest, $mockClient);
expect($responseB->isCached())->toBeFalse();
expect($responseB->json())->toEqual(['name' => 'Gareth']);
expect($responseB->status())->toEqual(500);
});
test('a custom cache key can be provided on the response', function () use ($filesystem) {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
TestConnector::make()->send(new CustomKeyCachedUserRequest, $mockClient);
$hash = hash('sha256', 'Howdy!');
expect($filesystem->fileExists($hash))->toBeTrue();
});
test('query parameters are used in the cache key', function () use ($filesystem) {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Sam']),
]);
$requestA = new CachedUserRequest();
$requestA->query()->add('name', 'Sam');
$responseA = TestConnector::make()->send($requestA, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
expect($responseA->status())->toEqual(200);
$requestB = new CachedUserRequest();
$requestB->query()->add('name', 'Sam');
$responseB = TestConnector::make()->send($requestB);
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual(['name' => 'Sam']);
expect($responseB->status())->toEqual(200);
$requestC = new CachedUserRequest();
$responseC = TestConnector::make()->send($requestC, $mockClient);
expect($responseC->isCached())->toBeFalse();
});
test('body can be used in the cache key', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Gareth']),
]);
$requestA = new BodyCacheKeyRequest;
$requestA->body()->set([
'name' => 'Sam',
'expiry' => '10 hours',
]);
$responseA = TestConnector::make()->send($requestA, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
expect($responseA->status())->toEqual(200);
$requestB = new BodyCacheKeyRequest;
$requestB->body()->set([
'name' => 'Sam',
'expiry' => '10 hours',
]);
$responseB = TestConnector::make()->send($requestB, $mockClient);
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual(['name' => 'Sam']);
expect($responseB->status())->toEqual(200);
});
test('you will not receive a cached response if the response has expired', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Michael']),
]);
$connector = new TestConnector;
$requestA = new ShortLivedCachedUserRequest();
$responseA = $connector->send($requestA, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
$requestB = new ShortLivedCachedUserRequest();
$responseB = $connector->send($requestB);
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual(['name' => 'Sam']);
sleep(3);
$requestC = new ShortLivedCachedUserRequest();
$responseC = $connector->send($requestC, $mockClient);
expect($responseC->isCached())->toBeFalse();
expect($responseC->json())->toEqual(['name' => 'Michael']);
});
test('you can define a cache expiry based on a response', function () {
$expectedExpiry = 90;
$mockClient = new MockClient([
MockResponse::make(['expiry' => $expectedExpiry]),
]);
$connector = new TestConnector;
$request = new ResponseBasedExpiryRequest();
$response = $connector->send($request, $mockClient);
$expiry = $request->resolveCacheExpiry($response);
expect($expiry)->toEqual($expectedExpiry);
});
test('you can define a cache on the connector and it returns a cached response', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$connector = new CachedConnector;
$requestA = new CachedConnectorRequest();
$responseA = $connector->send($requestA, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
$requestB = new CachedConnectorRequest();
$responseB = $connector->send($requestB);
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual(['name' => 'Sam']);
});
test('if a request has cache configuration then it will take priority over the connectors', function () use ($filesystem) {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$connector = new CachedConnector;
$requestA = new CachedUserRequestOnCachedConnector();
$responseA = $connector->send($requestA, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
$requestB = new CachedUserRequestOnCachedConnector();
$responseB = $connector->send($requestB);
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual(['name' => 'Sam']);
expect($filesystem->directoryExists('custom'))->toBeTrue();
expect(count($filesystem->listContents('custom')->toArray()))->toEqual(1);
});
test('you can disable the cache', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Michael']),
]);
$connector = new TestConnector;
$requestA = new CachedUserRequest();
$responseA = $connector->send($requestA, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
$requestB = new CachedUserRequest();
$responseB = $connector->send($requestB);
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual(['name' => 'Sam']);
$requestC = new CachedUserRequest();
$requestC->disableCaching();
$responseC = $connector->send($requestC, $mockClient);
expect($responseC->isCached())->toBeFalse();
expect($responseC->json())->toEqual(['name' => 'Michael']);
});
test('cache can be invalidated', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
MockResponse::make(['name' => 'Teo']),
]);
$connector = new TestConnector;
$requestA = new CachedUserRequest();
$responseA = $connector->send($requestA, $mockClient);
expect($responseA->isCached())->toBeFalse();
expect($responseA->json())->toEqual(['name' => 'Sam']);
$requestB = new CachedUserRequest();
$responseB = $connector->send($requestB);
// The response should now be cached...
expect($responseB->isCached())->toBeTrue();
expect($responseB->json())->toEqual(['name' => 'Sam']);
$requestC = new CachedUserRequest();
$requestC->invalidateCache();
$responseC = $connector->send($requestC, $mockClient);
expect($responseC->isCached())->toBeFalse();
expect($responseC->json())->toEqual(['name' => 'Teo']);
// Now just make sure that the new response is cached...
$requestD = new CachedUserRequest();
$responseD = $connector->send($requestD);
expect($responseD->isCached())->toBeTrue();
expect($responseD->json())->toEqual(['name' => 'Teo']);
});
test('it throws an exception if you use the HasCaching trait without the Cacheable interface', function () {
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$connector = new TestConnector;
$request = new CachedUserRequestWithoutCacheable;
$this->expectException(HasCachingException::class);
$this->expectExceptionMessage('Your connector or request must implement Saloon\CachePlugin\Contracts\Cacheable to use the HasCaching plugin');
$connector->send($request, $mockClient);
});
test('driver stores correct DateTimeImmutable expiry', function () {
$cache = new ArrayCache;
$expiry = new DateTimeImmutable('+60 seconds');
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$connector = new TestConnector;
$request = new ResolveCacheExpiryCachedRequest($cache, $expiry);
$connector->send($request, $mockClient);
$raw = $cache->get($connector->getCacheKey($request));
expect($raw)->not->toBeNull();
$cachedResponse = unserialize($raw, ['allowed_classes' => true]);
expect($cachedResponse)->toBeInstanceOf(CachedResponse::class);
expect($cachedResponse->expiresAt)->toBeInstanceOf(DateTimeImmutable::class);
expect($cachedResponse->expiresAt->getTimestamp())->toEqual($expiry->getTimestamp());
});
test('driver converts integer expiry into correct DateTimeImmutable', function () {
$cache = new ArrayCache;
$ttl = 120;
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$connector = new TestConnector;
$before = time();
$request = new ResolveCacheExpiryCachedRequest($cache, $ttl);
$connector->send($request, $mockClient);
$after = time();
$raw = $cache->get($connector->getCacheKey($request));
expect($raw)->not->toBeNull();
$cachedResponse = unserialize($raw, ['allowed_classes' => true]);
expect($cachedResponse)->toBeInstanceOf(CachedResponse::class);
expect($cachedResponse->expiresAt)->toBeInstanceOf(DateTimeImmutable::class);
expect($cachedResponse->expiresAt->getTimestamp())->toBeBetween($before + $ttl, $after + $ttl);
});
test('driver does not store item when expiry is zero', function () {
$cache = new ArrayCache;
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$connector = new TestConnector;
$request = new ResolveCacheExpiryCachedRequest($cache, 0);
$connector->send($request, $mockClient);
expect($cache->get($connector->getCacheKey($request)))->toBeNull();
});
test('driver does not store item when expiry is negative', function () {
$cache = new ArrayCache;
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$connector = new TestConnector;
$request = new ResolveCacheExpiryCachedRequest($cache, -10);
$connector->send($request, $mockClient);
expect($cache->get($connector->getCacheKey($request)))->toBeNull();
});
test('driver does not store item when DateTimeImmutable is in the past', function () {
$cache = new ArrayCache;
$past = new DateTimeImmutable('-60 seconds');
$mockClient = new MockClient([
MockResponse::make(['name' => 'Sam']),
]);
$connector = new TestConnector;
$request = new ResolveCacheExpiryCachedRequest($cache, $past);
$connector->send($request, $mockClient);
expect($cache->get($connector->getCacheKey($request)))->toBeNull();
});