1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Tests\Validator; |
6
|
|
|
|
7
|
|
|
use GraphQL\Error\FormattedError; |
8
|
|
|
use GraphQL\Language\SourceLocation; |
9
|
|
|
use GraphQL\Utils\BuildSchema; |
10
|
|
|
use GraphQL\Validator\Rules\KnownDirectives; |
11
|
|
|
use GraphQL\Validator\Rules\LoneSchemaDefinition; |
12
|
|
|
|
13
|
|
|
class LoneSchemaDefinitionTest extends ValidatorTestCase |
14
|
|
|
{ |
15
|
|
|
private function expectSDLErrors($sdlString, $schema = null, $errors = []) |
16
|
|
|
{ |
17
|
|
|
return $this->expectSDLErrorsFromRule(new LoneSchemaDefinition(), $sdlString, $schema, $errors); |
|
|
|
|
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private function schemaDefinitionNotAlone($line, $column) |
21
|
|
|
{ |
22
|
|
|
return FormattedError::create( |
|
|
|
|
23
|
|
|
LoneSchemaDefinition::schemaDefinitionNotAloneMessage(), |
24
|
|
|
[new SourceLocation($line, $column)] |
25
|
|
|
); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function canNotDefineSchemaWithinExtension($line, $column) |
29
|
|
|
{ |
30
|
|
|
return FormattedError::create( |
|
|
|
|
31
|
|
|
LoneSchemaDefinition::canNotDefineSchemaWithinExtensionMessage(), |
32
|
|
|
[new SourceLocation($line, $column)] |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Validate: Schema definition should be alone |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @see it('no schema') |
40
|
|
|
*/ |
41
|
|
|
public function testNoSchema() |
42
|
|
|
{ |
43
|
|
|
$this->expectSDLErrors( |
44
|
|
|
' |
45
|
|
|
type Query { |
46
|
|
|
foo: String |
47
|
|
|
} |
48
|
|
|
', |
49
|
|
|
null, |
50
|
|
|
[] |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @see it('one schema definition') |
56
|
|
|
*/ |
57
|
|
|
public function testOneSchemaDefinition() |
58
|
|
|
{ |
59
|
|
|
$this->expectSDLErrors( |
60
|
|
|
' |
61
|
|
|
schema { |
62
|
|
|
query: Foo |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
type Foo { |
66
|
|
|
foo: String |
67
|
|
|
} |
68
|
|
|
', |
69
|
|
|
null, |
70
|
|
|
[] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see it('multiple schema definitions') |
76
|
|
|
*/ |
77
|
|
|
public function testMultipleSchemaDefinitions() |
78
|
|
|
{ |
79
|
|
|
$this->expectSDLErrors( |
80
|
|
|
' |
81
|
|
|
schema { |
82
|
|
|
query: Foo |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
type Foo { |
86
|
|
|
foo: String |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
schema { |
90
|
|
|
mutation: Foo |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
schema { |
94
|
|
|
subscription: Foo |
95
|
|
|
} |
96
|
|
|
', |
97
|
|
|
null, |
98
|
|
|
[ |
99
|
|
|
$this->schemaDefinitionNotAlone(10, 7), |
100
|
|
|
$this->schemaDefinitionNotAlone(14, 7), |
101
|
|
|
] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @see it('define schema in schema extension') |
107
|
|
|
*/ |
108
|
|
|
public function testDefineSchemaInSchemaExtension() |
109
|
|
|
{ |
110
|
|
|
$schema = BuildSchema::build(' |
111
|
|
|
type Foo { |
112
|
|
|
foo: String |
113
|
|
|
} |
114
|
|
|
'); |
115
|
|
|
|
116
|
|
|
$this->expectSDLErrors( |
117
|
|
|
' |
118
|
|
|
schema { |
119
|
|
|
query: Foo |
120
|
|
|
} |
121
|
|
|
', |
122
|
|
|
$schema, |
123
|
|
|
[] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @see it('redefine schema in schema extension') |
129
|
|
|
*/ |
130
|
|
|
public function testRedefineSchemaInSchemaExtension() |
131
|
|
|
{ |
132
|
|
|
$schema = BuildSchema::build(' |
133
|
|
|
schema { |
134
|
|
|
query: Foo |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
type Foo { |
138
|
|
|
foo: String |
139
|
|
|
}'); |
140
|
|
|
|
141
|
|
|
$this->expectSDLErrors( |
142
|
|
|
' |
143
|
|
|
schema { |
144
|
|
|
mutation: Foo |
145
|
|
|
} |
146
|
|
|
', |
147
|
|
|
$schema, |
148
|
|
|
[$this->canNotDefineSchemaWithinExtension(2, 17)] |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @see it('redefine implicit schema in schema extension') |
154
|
|
|
*/ |
155
|
|
|
public function testRedefineImplicitSchemaInSchemaExtension() |
156
|
|
|
{ |
157
|
|
|
$schema = BuildSchema::build(' |
158
|
|
|
type Query { |
159
|
|
|
fooField: Foo |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
type Foo { |
163
|
|
|
foo: String |
164
|
|
|
} |
165
|
|
|
'); |
166
|
|
|
|
167
|
|
|
$this->expectSDLErrors( |
168
|
|
|
' |
169
|
|
|
schema { |
170
|
|
|
mutation: Foo |
171
|
|
|
} |
172
|
|
|
', |
173
|
|
|
$schema, |
174
|
|
|
[$this->canNotDefineSchemaWithinExtension(2, 17)] |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @see it('extend schema in schema extension') |
180
|
|
|
*/ |
181
|
|
|
public function testExtendSchemaInSchemaExtension() |
182
|
|
|
{ |
183
|
|
|
$schema = BuildSchema::build(' |
184
|
|
|
type Query { |
185
|
|
|
fooField: Foo |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
type Foo { |
189
|
|
|
foo: String |
190
|
|
|
} |
191
|
|
|
'); |
192
|
|
|
|
193
|
|
|
$this->expectSDLErrors( |
194
|
|
|
' |
195
|
|
|
extend schema { |
196
|
|
|
mutation: Foo |
197
|
|
|
} |
198
|
|
|
', |
199
|
|
|
$schema, |
200
|
|
|
[] |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.