-
-
Notifications
You must be signed in to change notification settings - Fork 624
Expand file tree
/
Copy pathRequestCacheTest.php
More file actions
290 lines (225 loc) · 8.68 KB
/
RequestCacheTest.php
File metadata and controls
290 lines (225 loc) · 8.68 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
<?php
namespace Tests\Feature\GraphQL;
use Facades\Tests\Factories\EntryFactory;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\Form;
use Statamic\Facades\GraphQL;
use Statamic\Facades\Token;
use Statamic\GraphQL\Queries\PingQuery;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;
#[Group('graphql')]
class RequestCacheTest extends TestCase
{
use PreventSavingStacheItemsToDisk;
public function getEnvironmentSetup($app)
{
parent::getEnvironmentSetUp($app);
GraphQL::addQuery(QueryOne::class);
GraphQL::addQuery(QueryTwo::class);
GraphQL::addMiddleware(TrackRequests::class);
}
#[Test]
public function it_caches_a_request()
{
$this->withoutExceptionHandling();
app()->instance('request-tracking', $requests = Collection::make());
Collection::times(2)->each(function () {
$this
->post('/graphql', ['query' => '{one}'])
->assertExactJson(['data' => ['one' => 'one']]);
$this
->post('/graphql', ['query' => '{one, two}'])
->assertExactJson(['data' => ['one' => 'one', 'two' => 'two']]);
$this
->post('/graphql', ['query' => '{one}', 'variables' => ['foo' => 'bar']])
->assertExactJson(['data' => ['one' => 'one']]);
$this
->post('/graphql', ['query' => '{one, two}', 'variables' => ['foo' => 'bar']])
->assertExactJson(['data' => ['one' => 'one', 'two' => 'two']]);
$this
->post('/graphql', ['query' => '{one}', 'variables' => ['foo' => 'baz']])
->assertExactJson(['data' => ['one' => 'one']]);
$this
->post('/graphql', ['query' => '{one, two}', 'variables' => ['foo' => 'baz']])
->assertExactJson(['data' => ['one' => 'one', 'two' => 'two']]);
});
$this->assertCount(6, $requests);
$this->assertEquals([
['query' => '{one}', 'variables' => null],
['query' => '{one, two}', 'variables' => null],
['query' => '{one}', 'variables' => ['foo' => 'bar']],
['query' => '{one, two}', 'variables' => ['foo' => 'bar']],
['query' => '{one}', 'variables' => ['foo' => 'baz']],
['query' => '{one, two}', 'variables' => ['foo' => 'baz']],
], $requests->all());
}
#[Test]
public function it_caches_endpoint_using_configured_expiry()
{
config(['statamic.graphql.cache.expiry' => 13]);
app()->instance('request-tracking', $requests = Collection::make());
// md5('{one}')_md5(json_encode(['foo' => 'bar']))
$key = 'gql-cache:0737a8fd85124abab1445165f4753936_9bb58f26192e4ba00f01e2e7b136bbd8';
Carbon::setTestNow(now());
$this->assertFalse(Cache::has($key));
$this->post('/graphql', ['query' => '{one}', 'variables' => ['foo' => 'bar']]);
$this->assertTrue(Cache::has($key));
Carbon::setTestNow(now()->addMinutes(14));
$this->assertFalse(Cache::has($key));
}
#[Test]
#[DataProvider('bypassCacheProvider')]
public function it_bypasses_cache_when_using_a_valid_token($url, $headers)
{
$this->withoutExceptionHandling();
app()->instance('request-tracking', $requests = Collection::make());
optional(Token::find('test-token'))->delete(); // garbage collection
Token::make('test-token', TestTokenHandler::class)->save();
Collection::times(2)->each(function () use ($url, $headers) {
$this
->post($url, ['query' => '{one}'], $headers)
->assertExactJson(['data' => ['one' => 'one']]);
});
$this->assertCount(2, $requests);
$this->assertEquals([
['query' => '{one}', 'variables' => null],
['query' => '{one}', 'variables' => null],
], $requests->all());
}
#[Test]
#[DataProvider('bypassCacheProvider')]
public function it_doesnt_bypass_cache_when_using_an_invalid_token($url, $headers)
{
$this->withoutExceptionHandling();
app()->instance('request-tracking', $requests = Collection::make());
// No token should exist, but do garbage collection.
// There may be a leftover token from a previous test.
optional(Token::find('test-token'))->delete();
Collection::times(2)->each(function () use ($url, $headers) {
$this
->post($url, ['query' => '{one}'], $headers)
->assertExactJson(['data' => ['one' => 'one']]);
});
$this->assertCount(1, $requests);
$this->assertEquals([
['query' => '{one}', 'variables' => null],
], $requests->all());
}
public static function bypassCacheProvider()
{
return [
['/graphql?token=test-token', []],
['/graphql', ['X-Statamic-Token' => 'test-token']],
];
}
#[Test]
public function it_busts_whole_cache_when_content_is_saved()
{
Cache::forever('completely-unrelated-thing', 'test');
$entry = EntryFactory::collection('test')->create();
app()->instance('request-tracking', $requests = Collection::make());
$this->post('/graphql', ['query' => '{one}']);
$entry->save();
$this->post('/graphql', ['query' => '{one}']);
$this->assertCount(2, $requests);
$this->assertEquals([
['query' => '{one}', 'variables' => null],
['query' => '{one}', 'variables' => null],
], $requests->all());
// Ensure that the solution was not just to clear the entire cache.
$this->assertEquals('test', Cache::get('completely-unrelated-thing'));
}
#[Test]
public function it_ignores_configured_events()
{
$entry = EntryFactory::collection('test')->create();
$form = tap(Form::make('test'))->save();
config(['statamic.graphql.cache.ignored_events' => [
\Statamic\Events\EntrySaved::class,
]]);
app()->instance('request-tracking', $requests = Collection::make());
$this->post('/graphql', ['query' => '{one}']);
$entry->save();
$this->post('/graphql', ['query' => '{one}']);
$this->assertCount(1, $requests);
$this->assertEquals([
['query' => '{one}', 'variables' => null],
], $requests->all());
$form->save();
$this->post('/graphql', ['query' => '{one}']);
$this->assertCount(2, $requests);
$this->assertEquals([
['query' => '{one}', 'variables' => null],
['query' => '{one}', 'variables' => null],
], $requests->all());
}
#[Test]
public function it_caches_response_data_as_primitives()
{
app()->instance('request-tracking', $requests = Collection::make());
$this->post('/graphql', ['query' => '{one}']);
$key = 'gql-cache:'.md5('{one}').'_'.md5(json_encode(null));
$cached = Cache::get($key);
$this->assertIsArray($cached);
$this->assertArrayHasKey('content', $cached);
$this->assertArrayHasKey('status', $cached);
$this->assertArrayHasKey('headers', $cached);
$this->assertEquals(200, $cached['status']);
}
#[Test]
public function it_can_disable_caching()
{
config(['statamic.graphql.cache' => false]);
app()->instance('request-tracking', $requests = Collection::make());
Collection::times(2)->each(function () {
$this
->post('/graphql', ['query' => '{one}'])
->assertExactJson(['data' => ['one' => 'one']]);
});
$this->assertCount(2, $requests);
$this->assertEquals([
['query' => '{one}', 'variables' => null],
['query' => '{one}', 'variables' => null],
], $requests->all());
}
}
class QueryOne extends PingQuery
{
protected $attributes = ['name' => 'one'];
public function resolve()
{
return 'one';
}
}
class QueryTwo extends PingQuery
{
protected $attributes = ['name' => 'two'];
public function resolve()
{
return 'two';
}
}
class TrackRequests
{
public function handle($request, $next)
{
app('request-tracking')[] = [
'query' => $request->input('query'),
'variables' => $request->input('variables'),
];
return $next($request);
}
}
class TestTokenHandler
{
public function handle($token, $request, $next)
{
return $next($request);
}
}