|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQL\Tests\Validator; |
|
6
|
|
|
|
|
7
|
|
|
use GraphQL\Validator\Rules\UniqueDirectivesPerLocation; |
|
8
|
|
|
|
|
9
|
|
|
class UniqueDirectivesPerLocationTest extends ValidatorTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
private function expectSDLErrors($sdlString, $schema = null, $errors = []) |
|
12
|
|
|
{ |
|
13
|
|
|
$this->expectSDLErrorsFromRule(new UniqueDirectivesPerLocation(), $sdlString, $schema, $errors); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @see it('no directives') |
|
18
|
|
|
*/ |
|
19
|
|
|
public function testNoDirectives() : void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->expectPassesRule( |
|
22
|
|
|
new UniqueDirectivesPerLocation(), |
|
23
|
|
|
' |
|
24
|
|
|
fragment Test on Type { |
|
25
|
|
|
field |
|
26
|
|
|
} |
|
27
|
|
|
' |
|
28
|
|
|
); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @see it('unique directives in different locations') |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testUniqueDirectivesInDifferentLocations() : void |
|
35
|
|
|
{ |
|
36
|
|
|
$this->expectPassesRule( |
|
37
|
|
|
new UniqueDirectivesPerLocation(), |
|
38
|
|
|
' |
|
39
|
|
|
fragment Test on Type @directiveA { |
|
40
|
|
|
field @directiveB |
|
41
|
|
|
} |
|
42
|
|
|
' |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @see it('unique directives in same locations') |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testUniqueDirectivesInSameLocations() : void |
|
50
|
|
|
{ |
|
51
|
|
|
$this->expectPassesRule( |
|
52
|
|
|
new UniqueDirectivesPerLocation(), |
|
53
|
|
|
' |
|
54
|
|
|
fragment Test on Type @directiveA @directiveB { |
|
55
|
|
|
field @directiveA @directiveB |
|
56
|
|
|
} |
|
57
|
|
|
' |
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @see it('same directives in different locations') |
|
63
|
|
|
*/ |
|
64
|
|
|
public function testSameDirectivesInDifferentLocations() : void |
|
65
|
|
|
{ |
|
66
|
|
|
$this->expectPassesRule( |
|
67
|
|
|
new UniqueDirectivesPerLocation(), |
|
68
|
|
|
' |
|
69
|
|
|
fragment Test on Type @directiveA { |
|
70
|
|
|
field @directiveA |
|
71
|
|
|
} |
|
72
|
|
|
' |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @see it('same directives in similar locations') |
|
78
|
|
|
*/ |
|
79
|
|
|
public function testSameDirectivesInSimilarLocations() : void |
|
80
|
|
|
{ |
|
81
|
|
|
$this->expectPassesRule( |
|
82
|
|
|
new UniqueDirectivesPerLocation(), |
|
83
|
|
|
' |
|
84
|
|
|
fragment Test on Type { |
|
85
|
|
|
field @directive |
|
86
|
|
|
field @directive |
|
87
|
|
|
} |
|
88
|
|
|
' |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @see it('duplicate directives in one location') |
|
94
|
|
|
*/ |
|
95
|
|
|
public function testDuplicateDirectivesInOneLocation() : void |
|
96
|
|
|
{ |
|
97
|
|
|
$this->expectFailsRule( |
|
98
|
|
|
new UniqueDirectivesPerLocation(), |
|
99
|
|
|
' |
|
100
|
|
|
fragment Test on Type { |
|
101
|
|
|
field @directive @directive |
|
102
|
|
|
} |
|
103
|
|
|
', |
|
104
|
|
|
[$this->duplicateDirective('directive', 3, 15, 3, 26)] |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function duplicateDirective($directiveName, $l1, $c1, $l2, $c2) |
|
109
|
|
|
{ |
|
110
|
|
|
return [ |
|
111
|
|
|
'message' => UniqueDirectivesPerLocation::duplicateDirectiveMessage($directiveName), |
|
112
|
|
|
'locations' => [ |
|
113
|
|
|
['line' => $l1, 'column' => $c1], |
|
114
|
|
|
['line' => $l2, 'column' => $c2], |
|
115
|
|
|
], |
|
116
|
|
|
]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @see it('many duplicate directives in one location') |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testManyDuplicateDirectivesInOneLocation() : void |
|
123
|
|
|
{ |
|
124
|
|
|
$this->expectFailsRule( |
|
125
|
|
|
new UniqueDirectivesPerLocation(), |
|
126
|
|
|
' |
|
127
|
|
|
fragment Test on Type { |
|
128
|
|
|
field @directive @directive @directive |
|
129
|
|
|
} |
|
130
|
|
|
', |
|
131
|
|
|
[ |
|
132
|
|
|
$this->duplicateDirective('directive', 3, 15, 3, 26), |
|
133
|
|
|
$this->duplicateDirective('directive', 3, 15, 3, 37), |
|
134
|
|
|
] |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @see it('different duplicate directives in one location') |
|
140
|
|
|
*/ |
|
141
|
|
|
public function testDifferentDuplicateDirectivesInOneLocation() : void |
|
142
|
|
|
{ |
|
143
|
|
|
$this->expectFailsRule( |
|
144
|
|
|
new UniqueDirectivesPerLocation(), |
|
145
|
|
|
' |
|
146
|
|
|
fragment Test on Type { |
|
147
|
|
|
field @directiveA @directiveB @directiveA @directiveB |
|
148
|
|
|
} |
|
149
|
|
|
', |
|
150
|
|
|
[ |
|
151
|
|
|
$this->duplicateDirective('directiveA', 3, 15, 3, 39), |
|
152
|
|
|
$this->duplicateDirective('directiveB', 3, 27, 3, 51), |
|
153
|
|
|
] |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @see it('duplicate directives in many locations') |
|
159
|
|
|
*/ |
|
160
|
|
|
public function testDuplicateDirectivesInManyLocations() : void |
|
161
|
|
|
{ |
|
162
|
|
|
$this->expectFailsRule( |
|
163
|
|
|
new UniqueDirectivesPerLocation(), |
|
164
|
|
|
' |
|
165
|
|
|
fragment Test on Type @directive @directive { |
|
166
|
|
|
field @directive @directive |
|
167
|
|
|
} |
|
168
|
|
|
', |
|
169
|
|
|
[ |
|
170
|
|
|
$this->duplicateDirective('directive', 2, 29, 2, 40), |
|
171
|
|
|
$this->duplicateDirective('directive', 3, 15, 3, 26), |
|
172
|
|
|
] |
|
173
|
|
|
); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @see it('duplicate directives on SDL definitions') |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testDuplicateDirectivesOnSDLDefinitions() |
|
180
|
|
|
{ |
|
181
|
|
|
$this->expectSDLErrors( |
|
182
|
|
|
' |
|
183
|
|
|
schema @directive @directive { query: Dummy } |
|
184
|
|
|
extend schema @directive @directive |
|
185
|
|
|
|
|
186
|
|
|
scalar TestScalar @directive @directive |
|
187
|
|
|
extend scalar TestScalar @directive @directive |
|
188
|
|
|
|
|
189
|
|
|
type TestObject @directive @directive |
|
190
|
|
|
extend type TestObject @directive @directive |
|
191
|
|
|
|
|
192
|
|
|
interface TestInterface @directive @directive |
|
193
|
|
|
extend interface TestInterface @directive @directive |
|
194
|
|
|
|
|
195
|
|
|
union TestUnion @directive @directive |
|
196
|
|
|
extend union TestUnion @directive @directive |
|
197
|
|
|
|
|
198
|
|
|
input TestInput @directive @directive |
|
199
|
|
|
extend input TestInput @directive @directive |
|
200
|
|
|
', |
|
201
|
|
|
null, |
|
202
|
|
|
[ |
|
203
|
|
|
$this->duplicateDirective('directive', 2, 14, 2, 25), |
|
204
|
|
|
$this->duplicateDirective('directive', 3, 21, 3, 32), |
|
205
|
|
|
$this->duplicateDirective('directive', 5, 25, 5, 36), |
|
206
|
|
|
$this->duplicateDirective('directive', 6, 32, 6, 43), |
|
207
|
|
|
$this->duplicateDirective('directive', 8, 23, 8, 34), |
|
208
|
|
|
$this->duplicateDirective('directive', 9, 30, 9, 41), |
|
209
|
|
|
$this->duplicateDirective('directive', 11, 31, 11, 42), |
|
210
|
|
|
$this->duplicateDirective('directive', 12, 38, 12, 49), |
|
211
|
|
|
$this->duplicateDirective('directive', 14, 23, 14, 34), |
|
212
|
|
|
$this->duplicateDirective('directive', 15, 30, 15, 41), |
|
213
|
|
|
$this->duplicateDirective('directive', 17, 23, 17, 34), |
|
214
|
|
|
$this->duplicateDirective('directive', 18, 30, 18, 41), |
|
215
|
|
|
] |
|
216
|
|
|
); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|