|
5 | 5 | use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
6 | 6 | use GraphQL\Deferred;
|
7 | 7 | use GraphQL\GraphQL;
|
| 8 | +use GraphQL\Language\AST\DocumentNode; |
8 | 9 | use GraphQL\Tests\Executor\TestClasses\Cat;
|
9 | 10 | use GraphQL\Tests\Executor\TestClasses\Dog;
|
10 | 11 | use GraphQL\Tests\PsrValidationCacheAdapter;
|
|
16 | 17 | use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
17 | 18 | use Symfony\Component\Cache\Psr16Cache;
|
18 | 19 |
|
| 20 | +final class SpyValidationCacheAdapter extends PsrValidationCacheAdapter |
| 21 | +{ |
| 22 | + public int $isValidatedCalls = 0; |
| 23 | + public int $markValidatedCalls = 0; |
| 24 | + |
| 25 | + public function isValidated(Schema $schema, DocumentNode $ast): bool |
| 26 | + { |
| 27 | + $this->isValidatedCalls++; |
| 28 | + return parent::isValidated($schema, $ast); |
| 29 | + } |
| 30 | + |
| 31 | + public function markValidated(Schema $schema, DocumentNode $ast): void |
| 32 | + { |
| 33 | + $this->markValidatedCalls++; |
| 34 | + parent::markValidated($schema, $ast); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
19 | 39 | final class ValidationWithCacheTest extends TestCase
|
20 | 40 | {
|
21 | 41 | use ArraySubsetAsserts;
|
22 | 42 |
|
23 | 43 | public function testIsValidationCachedWithAdapter(): void
|
24 | 44 | {
|
25 |
| - $cache = new PsrValidationCacheAdapter(new Psr16Cache(new ArrayAdapter())); |
| 45 | + $cache = new SpyValidationCacheAdapter(new Psr16Cache(new ArrayAdapter())); |
26 | 46 | $petType = new InterfaceType([
|
27 | 47 | 'name' => 'Pet',
|
28 | 48 | 'fields' => [
|
@@ -78,11 +98,14 @@ public function testIsValidationCachedWithAdapter(): void
|
78 | 98 | }
|
79 | 99 | }';
|
80 | 100 |
|
| 101 | + // make the same call twice in a row. We'll then inspect the cache object to count calls |
81 | 102 | GraphQL::executeQuery( $schema, $query, null, null, null, null, null, null, $cache)->toArray();
|
82 |
| - |
83 |
| - // TODO: use a spy or something to prove that the validation only happens once |
84 | 103 | $result = GraphQL::executeQuery( $schema, $query, null, null, null, null, null, null, $cache)->toArray();
|
85 | 104 |
|
| 105 | + // ✅ Assert that validation only happened once |
| 106 | + self::assertEquals(2, $cache->isValidatedCalls, 'Should check cache twice'); |
| 107 | + self::assertEquals(1, $cache->markValidatedCalls, 'Should mark as validated once'); |
| 108 | + |
86 | 109 | $expected = [
|
87 | 110 | 'data' => [
|
88 | 111 | 'pets' => [
|
|
0 commit comments