-
-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathQueryCacheTest.php
More file actions
301 lines (251 loc) · 9.57 KB
/
Copy pathQueryCacheTest.php
File metadata and controls
301 lines (251 loc) · 9.57 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
<?php declare(strict_types=1);
namespace Tests\Integration;
use GraphQL\Language\Parser;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Cache\Events\CacheMissed;
use Illuminate\Cache\Events\KeyWritten;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use Nuwave\Lighthouse\Cache\QueryCache;
use Tests\TestCase;
use Tests\Utils\Queries\Foo;
final class QueryCacheTest extends TestCase
{
public function testEnabledWithDefaults(): void
{
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.query_cache.enable', true);
$config->set('lighthouse.validation_cache.enable', false);
$event = Event::fake();
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$event->assertDispatchedTimes(CacheMissed::class, 1);
$event->assertDispatchedTimes(CacheHit::class, 0);
$event->assertDispatchedTimes(KeyWritten::class, 1);
// second request should be hit
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$event->assertDispatchedTimes(CacheMissed::class, 1);
$event->assertDispatchedTimes(CacheHit::class, 1);
$event->assertDispatchedTimes(KeyWritten::class, 1);
}
public function testStoreModeWithRestrictedSerializableClasses(): void
{
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.query_cache.enable', true);
$config->set('lighthouse.validation_cache.enable', false);
// Laravel 13 lets applications restrict which classes may be unserialized from the cache.
// Any store that serializes values then calls unserialize() with `allowed_classes`,
// turning every disallowed object back into __PHP_Incomplete_Class.
$config->set('cache.stores.array.serialize', true);
$config->set('cache.serializable_classes', []);
$query = /** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL;
// Populates the query cache.
$this->graphQL($query)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
// Reads the cached query back out of the store.
$this->graphQL($query)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
}
public function testDifferentQueriesHasDifferentKeys(): void
{
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.query_cache.enable', true);
$config->set('lighthouse.validation_cache.enable', false);
$event = Event::fake();
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$event->assertDispatchedTimes(CacheMissed::class, 2);
$event->assertDispatchedTimes(CacheHit::class, 0);
$event->assertDispatchedTimes(KeyWritten::class, 2);
}
public function testDisabled(): void
{
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.query_cache.enable', false);
$config->set('lighthouse.validation_cache.enable', false);
$event = Event::fake();
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$event->assertDispatchedTimes(CacheMissed::class, 0);
$event->assertDispatchedTimes(CacheHit::class, 0);
$event->assertDispatchedTimes(KeyWritten::class, 0);
}
public function testModeOPcache(): void
{
$filesystem = Storage::fake();
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.query_cache.enable', true);
$config->set('lighthouse.query_cache.mode', 'opcache');
$config->set('lighthouse.query_cache.opcache_path', $filesystem->path(''));
$expectedFilePath = 'lighthouse-query-ec859ac754fc185143d0daf8dcfa644b5e2271e219b9a8f80c3a6fdfb0ce67d0.php';
$filesystem->assertMissing($expectedFilePath);
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$filesystem->assertExists($expectedFilePath);
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$filesystem->assertExists($expectedFilePath);
}
public function testModeHybrid(): void
{
$filesystem = Storage::fake();
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.query_cache.enable', true);
$config->set('lighthouse.query_cache.mode', 'hybrid');
$config->set('lighthouse.query_cache.opcache_path', $filesystem->path(''));
$event = Event::fake();
$expectedFilePath = 'lighthouse-query-ec859ac754fc185143d0daf8dcfa644b5e2271e219b9a8f80c3a6fdfb0ce67d0.php';
$filesystem->assertMissing($expectedFilePath);
// First request should miss both caches and create both store and file cache
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$filesystem->assertExists($expectedFilePath);
$event->assertDispatchedTimes(CacheMissed::class, 1);
$event->assertDispatchedTimes(CacheHit::class, 0);
$event->assertDispatchedTimes(KeyWritten::class, 1);
// Second request should hit file cache (no store cache events)
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$filesystem->assertExists($expectedFilePath);
// No additional cache events since file cache is used directly
$event->assertDispatchedTimes(CacheMissed::class, 1);
$event->assertDispatchedTimes(CacheHit::class, 0);
$event->assertDispatchedTimes(KeyWritten::class, 1);
}
public function testModeHybridWithPopulatedCacheStoreButMissingFile(): void
{
$filesystem = Storage::fake();
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.query_cache.enable', true);
$config->set('lighthouse.query_cache.mode', 'hybrid');
$config->set('lighthouse.query_cache.opcache_path', $filesystem->path(''));
$query = /** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL;
$queryHash = 'ec859ac754fc185143d0daf8dcfa644b5e2271e219b9a8f80c3a6fdfb0ce67d0';
$expectedFilePath = "lighthouse-query-{$queryHash}.php";
$filesystem->assertMissing($expectedFilePath);
$queryInstance = Parser::parse($query);
Cache::put(
key: "lighthouse:query:{$queryHash}",
value: QueryCache::opcacheFileContents($queryInstance),
);
$event = Event::fake();
$this->graphQL($query)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$filesystem->assertExists($expectedFilePath);
$event->assertDispatchedTimes(CacheMissed::class, 0);
$event->assertDispatchedTimes(CacheHit::class, 1);
$event->assertDispatchedTimes(KeyWritten::class, 0);
$this->graphQL($query)->assertExactJson([
'data' => [
'foo' => Foo::THE_ANSWER,
],
]);
$filesystem->assertExists($expectedFilePath);
$event->assertDispatchedTimes(CacheMissed::class, 0);
$event->assertDispatchedTimes(CacheHit::class, 1);
$event->assertDispatchedTimes(KeyWritten::class, 0);
}
public function testInvalidQueryCacheContents(): void
{
$filesystem = Storage::fake();
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.query_cache.enable', true);
$config->set('lighthouse.query_cache.mode', 'opcache');
$config->set('lighthouse.query_cache.opcache_path', $filesystem->path(''));
$fileName = 'lighthouse-query-ec859ac754fc185143d0daf8dcfa644b5e2271e219b9a8f80c3a6fdfb0ce67d0.php';
$filesystem->put($fileName, '');
$this->expectException(\AssertionError::class);
$this->expectExceptionMessage("The query cache file at {$filesystem->path($fileName)} is expected to return an array.");
$this->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
}
GRAPHQL);
}
}