1
|
|
|
<?php |
2
|
|
|
namespace GraphQL\Tests; |
3
|
|
|
|
4
|
|
|
use GraphQL\Language\Parser; |
5
|
|
|
use GraphQL\Validator\DocumentValidator; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class StarWarsValidationTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
// Star Wars Validation Tests |
11
|
|
|
// Basic Queries |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @it Validates a complex but valid query |
15
|
|
|
*/ |
16
|
|
|
public function testValidatesAComplexButValidQuery() |
17
|
|
|
{ |
18
|
|
|
$query = ' |
19
|
|
|
query NestedQueryWithFragment { |
20
|
|
|
hero { |
21
|
|
|
...NameAndAppearances |
22
|
|
|
friends { |
23
|
|
|
...NameAndAppearances |
24
|
|
|
friends { |
25
|
|
|
...NameAndAppearances |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
fragment NameAndAppearances on Character { |
32
|
|
|
name |
33
|
|
|
appearsIn |
34
|
|
|
} |
35
|
|
|
'; |
36
|
|
|
$errors = $this->validationErrors($query); |
37
|
|
|
$this->assertEquals(true, empty($errors)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @it Notes that non-existent fields are invalid |
42
|
|
|
*/ |
43
|
|
|
public function testThatNonExistentFieldsAreInvalid() |
44
|
|
|
{ |
45
|
|
|
$query = ' |
46
|
|
|
query HeroSpaceshipQuery { |
47
|
|
|
hero { |
48
|
|
|
favoriteSpaceship |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
'; |
52
|
|
|
$errors = $this->validationErrors($query); |
53
|
|
|
$this->assertEquals(false, empty($errors)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @it Requires fields on objects |
58
|
|
|
*/ |
59
|
|
|
public function testRequiresFieldsOnObjects() |
60
|
|
|
{ |
61
|
|
|
$query = ' |
62
|
|
|
query HeroNoFieldsQuery { |
63
|
|
|
hero |
64
|
|
|
} |
65
|
|
|
'; |
66
|
|
|
|
67
|
|
|
$errors = $this->validationErrors($query); |
68
|
|
|
$this->assertEquals(false, empty($errors)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @it Disallows fields on scalars |
73
|
|
|
*/ |
74
|
|
|
public function testDisallowsFieldsOnScalars() |
75
|
|
|
{ |
76
|
|
|
$query = ' |
77
|
|
|
query HeroFieldsOnScalarQuery { |
78
|
|
|
hero { |
79
|
|
|
name { |
80
|
|
|
firstCharacterOfName |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
'; |
85
|
|
|
$errors = $this->validationErrors($query); |
86
|
|
|
$this->assertEquals(false, empty($errors)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @it Disallows object fields on interfaces |
91
|
|
|
*/ |
92
|
|
|
public function testDisallowsObjectFieldsOnInterfaces() |
93
|
|
|
{ |
94
|
|
|
$query = ' |
95
|
|
|
query DroidFieldOnCharacter { |
96
|
|
|
hero { |
97
|
|
|
name |
98
|
|
|
primaryFunction |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
'; |
102
|
|
|
$errors = $this->validationErrors($query); |
103
|
|
|
$this->assertEquals(false, empty($errors)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @it Allows object fields in fragments |
108
|
|
|
*/ |
109
|
|
|
public function testAllowsObjectFieldsInFragments() |
110
|
|
|
{ |
111
|
|
|
$query = ' |
112
|
|
|
query DroidFieldInFragment { |
113
|
|
|
hero { |
114
|
|
|
name |
115
|
|
|
...DroidFields |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
fragment DroidFields on Droid { |
120
|
|
|
primaryFunction |
121
|
|
|
} |
122
|
|
|
'; |
123
|
|
|
$errors = $this->validationErrors($query); |
124
|
|
|
$this->assertEquals(true, empty($errors)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @it Allows object fields in inline fragments |
129
|
|
|
*/ |
130
|
|
|
public function testAllowsObjectFieldsInInlineFragments() |
131
|
|
|
{ |
132
|
|
|
$query = ' |
133
|
|
|
query DroidFieldInFragment { |
134
|
|
|
hero { |
135
|
|
|
name |
136
|
|
|
... on Droid { |
137
|
|
|
primaryFunction |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
'; |
142
|
|
|
$errors = $this->validationErrors($query); |
143
|
|
|
$this->assertEquals(true, empty($errors)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Helper function to test a query and the expected response. |
148
|
|
|
*/ |
149
|
|
|
private function validationErrors($query) |
150
|
|
|
{ |
151
|
|
|
$ast = Parser::parse($query); |
152
|
|
|
return DocumentValidator::validate(StarWarsSchema::build(), $ast); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|