-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathDocumentASTTest.php
More file actions
121 lines (96 loc) · 3.74 KB
/
DocumentASTTest.php
File metadata and controls
121 lines (96 loc) · 3.74 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
<?php declare(strict_types=1);
namespace Tests\Unit\Schema\AST;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\Parser;
use Nuwave\Lighthouse\Exceptions\DefinitionException;
use Nuwave\Lighthouse\Exceptions\SchemaSyntaxErrorException;
use Nuwave\Lighthouse\Schema\AST\DocumentAST;
use Nuwave\Lighthouse\Schema\RootType;
use Tests\TestCase;
use Tests\Utils\Models\User;
final class DocumentASTTest extends TestCase
{
public function testParsesSimpleSchema(): void
{
$schema = /** @lang GraphQL */ <<<'GRAPHQL'
type Query {
foo: Int
}
GRAPHQL;
// calculated as hash('sha256', $schema)
$schemaHash = '774433c158904b98b4f69eddee3424679b99a70736960a189b7d7b5923695bac';
$documentAST = DocumentAST::fromSource($schema);
$this->assertInstanceOf(
ObjectTypeDefinitionNode::class,
$documentAST->types[RootType::QUERY],
);
$this->assertSame($schemaHash, $documentAST->hash);
}
public function testThrowsOnInvalidSchema(): void
{
$this->expectException(SchemaSyntaxErrorException::class);
$this->expectExceptionMessage('Syntax Error: Expected Name, found !, near: ');
DocumentAST::fromSource(/** @lang GraphQL */ <<<'GRAPHQL'
type Mutation {
bar: Int
}
type Query {
foo: Int!!
}
type Foo {
bar: ID
}
GRAPHQL);
}
public function testThrowsOnUnknownModelClasses(): void
{
$this->expectException(DefinitionException::class);
$this->expectExceptionMessage('Failed to find a model class Unknown in namespaces [Tests\Utils\Models, Tests\Utils\ModelsSecondary] referenced in @model on type Query.');
DocumentAST::fromSource(/** @lang GraphQL */ <<<'GRAPHQL'
type Query @model(class: "Unknown") {
foo: Int!
}
GRAPHQL);
}
public function testOverwritesDefinitionWithSameName(): void
{
$documentAST = DocumentAST::fromSource(/** @lang GraphQL */ <<<'GRAPHQL'
type Query {
foo: Int
}
GRAPHQL);
$overwrite = Parser::objectTypeDefinition(/** @lang GraphQL */ <<<'GRAPHQL'
type Query {
bar: Int
}
GRAPHQL);
$documentAST->types[$overwrite->name->value] = $overwrite;
$this->assertSame(
$overwrite,
$documentAST->types[RootType::QUERY],
);
}
public function testBeSerialized(): void
{
$documentAST = DocumentAST::fromSource(/** @lang GraphQL */ <<<'GRAPHQL'
extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])
type Query @model(class: "User") {
foo: Int
}
directive @foo on FIELD
GRAPHQL);
// @phpstan-ignore theCodingMachineSafe.function (Safe\unserialize is not available in thecodingmachine/safe ^1 and ^2)
$reserialized = unserialize(
serialize($documentAST),
);
$this->assertInstanceOf(DocumentAST::class, $reserialized);
$queryType = $reserialized->types[RootType::QUERY];
$this->assertInstanceOf(ObjectTypeDefinitionNode::class, $queryType);
$this->assertArrayHasKey('foo', $reserialized->directives);
$this->assertSame(['Query'], $reserialized->classNameToObjectTypeNames[User::class]);
$this->assertArrayHasKey(0, $reserialized->schemaExtensions);
$schemaExtension = $reserialized->schemaExtensions[0];
$this->assertArrayHasKey(0, $schemaExtension->directives); // @phpstan-ignore method.impossibleType (NodeList not understood by earlier deps)
$this->assertSame($documentAST->hash, $reserialized->hash);
}
}