|
1 | 1 | <?php declare(strict_types=1); |
2 | 2 |
|
3 | 3 | use Illuminate\Contracts\Config\Repository as ConfigRepository; |
| 4 | +use Illuminate\Filesystem\FilesystemAdapter; |
| 5 | +use Illuminate\Support\Facades\Cache; |
| 6 | +use Illuminate\Support\Facades\Storage; |
4 | 7 | use Tests\TestCase; |
5 | 8 | use Tests\Utils\Queries\Foo; |
6 | 9 |
|
7 | 10 | final class AutomaticPersistedQueriesTest extends TestCase |
8 | 11 | { |
9 | | - public function testEnabled(): void |
| 12 | + public function testEnabledWithStoreMode(): void |
10 | 13 | { |
11 | 14 | $config = $this->app->make(ConfigRepository::class); |
12 | 15 | $config->set('lighthouse.query_cache.enable', true); |
13 | 16 | $config->set('lighthouse.persisted_queries', true); |
| 17 | + $config->set('lighthouse.query_cache.mode', 'store'); |
14 | 18 |
|
15 | 19 | $query = /** @lang GraphQL */ <<<'GRAPHQL' |
16 | 20 | { |
@@ -68,6 +72,77 @@ public function testEnabled(): void |
68 | 72 | ]); |
69 | 73 | } |
70 | 74 |
|
| 75 | + public function testEnabledWithHybridStore(): void |
| 76 | + { |
| 77 | + $filesystem = Storage::fake(); |
| 78 | + $this->assertInstanceOf(FilesystemAdapter::class, $filesystem); |
| 79 | + |
| 80 | + $config = $this->app->make(ConfigRepository::class); |
| 81 | + $config->set('lighthouse.query_cache.enable', true); |
| 82 | + $config->set('lighthouse.persisted_queries', true); |
| 83 | + $config->set('lighthouse.query_cache.mode', 'hybrid'); |
| 84 | + $config->set('lighthouse.query_cache.opcache_path', $filesystem->path('')); |
| 85 | + |
| 86 | + $query = /** @lang GraphQL */ <<<'GRAPHQL' |
| 87 | + { |
| 88 | + foo |
| 89 | + } |
| 90 | + GRAPHQL; |
| 91 | + |
| 92 | + $sha256 = hash('sha256', $query); |
| 93 | + |
| 94 | + $this->postGraphQL([ |
| 95 | + 'extensions' => [ |
| 96 | + 'persistedQuery' => [ |
| 97 | + 'version' => 1, |
| 98 | + 'sha256Hash' => $sha256, |
| 99 | + ], |
| 100 | + ], |
| 101 | + ])->assertJson([ |
| 102 | + 'errors' => [ |
| 103 | + [ |
| 104 | + 'message' => 'PersistedQueryNotFound', |
| 105 | + 'extensions' => [ |
| 106 | + 'code' => 'PERSISTED_QUERY_NOT_FOUND', |
| 107 | + ], |
| 108 | + ], |
| 109 | + ], |
| 110 | + ]); |
| 111 | + |
| 112 | + // run sending the query |
| 113 | + $this->postGraphQL([ |
| 114 | + 'query' => $query, |
| 115 | + 'extensions' => [ |
| 116 | + 'persistedQuery' => [ |
| 117 | + 'version' => 1, |
| 118 | + 'sha256Hash' => $sha256, |
| 119 | + ], |
| 120 | + ], |
| 121 | + ])->assertExactJson([ |
| 122 | + 'data' => [ |
| 123 | + 'foo' => Foo::THE_ANSWER, |
| 124 | + ], |
| 125 | + ]); |
| 126 | + |
| 127 | + $filesystem->assertExists('lighthouse-query-' . $sha256 . '.php'); |
| 128 | + // Simulate different server by deleting the local file - cache will fall back now |
| 129 | + $filesystem->delete('lighthouse-query-' . $sha256 . '.php'); |
| 130 | + |
| 131 | + // run without query, the query should still be cached |
| 132 | + $this->postGraphQL([ |
| 133 | + 'extensions' => [ |
| 134 | + 'persistedQuery' => [ |
| 135 | + 'version' => 1, |
| 136 | + 'sha256Hash' => $sha256, |
| 137 | + ], |
| 138 | + ], |
| 139 | + ])->assertExactJson([ |
| 140 | + 'data' => [ |
| 141 | + 'foo' => Foo::THE_ANSWER, |
| 142 | + ], |
| 143 | + ]); |
| 144 | + } |
| 145 | + |
71 | 146 | public function testConfigDisabled(): void |
72 | 147 | { |
73 | 148 | $config = $this->app->make(ConfigRepository::class); |
|
0 commit comments