Skip to content

Commit d71e123

Browse files
committed
fix tests
1 parent f35cb3c commit d71e123

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

tests/Executor/ValidationWithCacheTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
66
use GraphQL\Deferred;
77
use GraphQL\GraphQL;
8+
use GraphQL\Language\AST\DocumentNode;
89
use GraphQL\Tests\Executor\TestClasses\Cat;
910
use GraphQL\Tests\Executor\TestClasses\Dog;
1011
use GraphQL\Tests\PsrValidationCacheAdapter;
@@ -16,13 +17,32 @@
1617
use Symfony\Component\Cache\Adapter\ArrayAdapter;
1718
use Symfony\Component\Cache\Psr16Cache;
1819

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+
1939
final class ValidationWithCacheTest extends TestCase
2040
{
2141
use ArraySubsetAsserts;
2242

2343
public function testIsValidationCachedWithAdapter(): void
2444
{
25-
$cache = new PsrValidationCacheAdapter(new Psr16Cache(new ArrayAdapter()));
45+
$cache = new SpyValidationCacheAdapter(new Psr16Cache(new ArrayAdapter()));
2646
$petType = new InterfaceType([
2747
'name' => 'Pet',
2848
'fields' => [
@@ -78,11 +98,14 @@ public function testIsValidationCachedWithAdapter(): void
7898
}
7999
}';
80100

101+
// make the same call twice in a row. We'll then inspect the cache object to count calls
81102
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
84103
$result = GraphQL::executeQuery( $schema, $query, null, null, null, null, null, null, $cache)->toArray();
85104

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+
86109
$expected = [
87110
'data' => [
88111
'pets' => [

tests/PsrValidationCacheAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Psr\SimpleCache\CacheInterface;
1010
use Symfony\Component\String\Exception\InvalidArgumentException;
1111

12-
final class PsrValidationCacheAdapter implements ValidationCache
12+
class PsrValidationCacheAdapter implements ValidationCache
1313
{
1414
private const KEY_PREFIX = 'gql_validation_';
1515
private int $ttl;

0 commit comments

Comments
 (0)