Total Complexity | 96 |
Total Lines | 1437 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like VisitorTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use VisitorTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class VisitorTest extends ValidatorTestCase |
||
31 | { |
||
32 | private function getNodeByPath(DocumentNode $ast, $path) |
||
33 | { |
||
34 | $result = $ast; |
||
35 | foreach ($path as $key) { |
||
36 | $resultArray = $result instanceof NodeList ? iterator_to_array($result) : $result->toArray(); |
||
37 | $this->assertArrayHasKey($key, $resultArray); |
||
38 | $result = $resultArray[$key]; |
||
39 | } |
||
40 | return $result; |
||
41 | } |
||
42 | |||
43 | private function checkVisitorFnArgs($ast, $args, $isEdited = false) |
||
44 | { |
||
45 | /** @var Node $node */ |
||
46 | list($node, $key, $parent, $path, $ancestors) = $args; |
||
47 | |||
48 | $parentArray = $parent && ! is_array($parent) ? ($parent instanceof NodeList ? iterator_to_array($parent) : $parent->toArray()) : $parent; |
||
49 | |||
50 | $this->assertInstanceOf(Node::class, $node); |
||
51 | $this->assertContains($node->kind, array_keys(NodeKind::$classMap)); |
||
52 | |||
53 | $isRoot = $key === null; |
||
54 | if ($isRoot) { |
||
55 | if (! $isEdited) { |
||
56 | $this->assertEquals($ast, $node); |
||
57 | } |
||
58 | $this->assertEquals(null, $parent); |
||
59 | $this->assertEquals([], $path); |
||
60 | $this->assertEquals([], $ancestors); |
||
61 | return; |
||
62 | } |
||
63 | |||
64 | $this->assertContains(gettype($key), ['integer', 'string']); |
||
65 | |||
66 | $this->assertArrayHasKey($key, $parentArray); |
||
67 | |||
68 | $this->assertInternalType('array', $path); |
||
69 | $this->assertEquals($key, $path[count($path) - 1]); |
||
70 | |||
71 | $this->assertInternalType('array', $ancestors); |
||
72 | $this->assertCount(count($path) - 1, $ancestors); |
||
73 | |||
74 | if ($isEdited) { |
||
75 | return; |
||
76 | } |
||
77 | |||
78 | $this->assertEquals($node, $parentArray[$key]); |
||
79 | $this->assertEquals($node, $this->getNodeByPath($ast, $path)); |
||
80 | $ancestorsLength = count($ancestors); |
||
81 | for ($i = 0; $i < $ancestorsLength; ++$i) { |
||
82 | $ancestorPath = array_slice($path, 0, $i); |
||
83 | $this->assertEquals($ancestors[$i], $this->getNodeByPath($ast, $ancestorPath)); |
||
84 | } |
||
85 | } |
||
86 | |||
87 | public function testValidatesPathArgument() |
||
88 | { |
||
89 | $visited = []; |
||
90 | |||
91 | $ast = Parser::parse('{ a }', ['noLocation' => true]); |
||
92 | |||
93 | Visitor::visit($ast, [ |
||
94 | 'enter' => function ($node, $key, $parent, $path) use ($ast, &$visited) { |
||
95 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
96 | $visited[] = ['enter', $path]; |
||
97 | }, |
||
98 | 'leave' => function ($node, $key, $parent, $path) use ($ast, &$visited) { |
||
99 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
100 | $visited[] = ['leave', $path]; |
||
101 | }, |
||
102 | ]); |
||
103 | |||
104 | $expected = [ |
||
105 | ['enter', []], |
||
106 | ['enter', ['definitions', 0]], |
||
107 | ['enter', ['definitions', 0, 'selectionSet']], |
||
108 | ['enter', ['definitions', 0, 'selectionSet', 'selections', 0]], |
||
109 | ['enter', ['definitions', 0, 'selectionSet', 'selections', 0, 'name']], |
||
110 | ['leave', ['definitions', 0, 'selectionSet', 'selections', 0, 'name']], |
||
111 | ['leave', ['definitions', 0, 'selectionSet', 'selections', 0]], |
||
112 | ['leave', ['definitions', 0, 'selectionSet']], |
||
113 | ['leave', ['definitions', 0]], |
||
114 | ['leave', []], |
||
115 | ]; |
||
116 | |||
117 | $this->assertEquals($expected, $visited); |
||
118 | } |
||
119 | |||
120 | public function testAllowsEditingNodeOnEnterAndOnLeave() |
||
121 | { |
||
122 | $ast = Parser::parse('{ a, b, c { a, b, c } }', [ 'noLocation' => true ]); |
||
123 | |||
124 | $selectionSet = null; |
||
125 | $editedAst = Visitor::visit($ast, [ |
||
126 | NodeKind::OPERATION_DEFINITION => [ |
||
127 | 'enter' => function (OperationDefinitionNode $node) use (&$selectionSet, $ast) { |
||
128 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
129 | $selectionSet = $node->selectionSet; |
||
130 | |||
131 | $newNode = clone $node; |
||
132 | $newNode->selectionSet = new SelectionSetNode([ |
||
133 | 'selections' => [], |
||
134 | ]); |
||
135 | $newNode->didEnter = true; |
||
|
|||
136 | return $newNode; |
||
137 | }, |
||
138 | 'leave' => function (OperationDefinitionNode $node) use (&$selectionSet, $ast) { |
||
139 | $this->checkVisitorFnArgs($ast, func_get_args(), true); |
||
140 | $newNode = clone $node; |
||
141 | $newNode->selectionSet = $selectionSet; |
||
142 | $newNode->didLeave = true; |
||
143 | return $newNode; |
||
144 | }, |
||
145 | ], |
||
146 | ]); |
||
147 | |||
148 | $this->assertNotEquals($ast, $editedAst); |
||
149 | |||
150 | $expected = $ast->cloneDeep(); |
||
151 | $expected->definitions[0]->didEnter = true; |
||
152 | $expected->definitions[0]->didLeave = true; |
||
153 | |||
154 | $this->assertEquals($expected, $editedAst); |
||
155 | } |
||
156 | |||
157 | public function testAllowsEditingRootNodeOnEnterAndLeave() |
||
158 | { |
||
159 | $ast = Parser::parse('{ a, b, c { a, b, c } }', [ 'noLocation' => true ]); |
||
160 | $definitions = $ast->definitions; |
||
161 | |||
162 | $editedAst = Visitor::visit($ast, [ |
||
163 | NodeKind::DOCUMENT => [ |
||
164 | 'enter' => function (DocumentNode $node) use ($ast) { |
||
165 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
166 | $tmp = clone $node; |
||
167 | $tmp->definitions = []; |
||
168 | $tmp->didEnter = true; |
||
169 | return $tmp; |
||
170 | }, |
||
171 | 'leave' => function (DocumentNode $node) use ($definitions, $ast) { |
||
172 | $this->checkVisitorFnArgs($ast, func_get_args(), true); |
||
173 | $node->definitions = $definitions; |
||
174 | $node->didLeave = true; |
||
175 | }, |
||
176 | ], |
||
177 | ]); |
||
178 | |||
179 | $this->assertNotEquals($ast, $editedAst); |
||
180 | |||
181 | $tmp = $ast->cloneDeep(); |
||
182 | $tmp->didEnter = true; |
||
183 | $tmp->didLeave = true; |
||
184 | |||
185 | $this->assertEquals($tmp, $editedAst); |
||
186 | } |
||
187 | |||
188 | public function testAllowsForEditingOnEnter() |
||
189 | { |
||
190 | $ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]); |
||
191 | $editedAst = Visitor::visit($ast, [ |
||
192 | 'enter' => function ($node) use ($ast) { |
||
193 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
194 | if ($node instanceof FieldNode && $node->name->value === 'b') { |
||
195 | return Visitor::removeNode(); |
||
196 | } |
||
197 | }, |
||
198 | ]); |
||
199 | |||
200 | $this->assertEquals( |
||
201 | Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]), |
||
202 | $ast |
||
203 | ); |
||
204 | $this->assertEquals( |
||
205 | Parser::parse('{ a, c { a, c } }', ['noLocation' => true]), |
||
206 | $editedAst |
||
207 | ); |
||
208 | } |
||
209 | |||
210 | public function testAllowsForEditingOnLeave() |
||
230 | ); |
||
231 | } |
||
232 | |||
233 | public function testVisitsEditedNode() |
||
234 | { |
||
235 | $addedField = new FieldNode([ |
||
236 | 'name' => new NameNode(['value' => '__typename']), |
||
237 | ]); |
||
238 | |||
239 | $didVisitAddedField = false; |
||
240 | |||
241 | $ast = Parser::parse('{ a { x } }', ['noLocation' => true]); |
||
242 | |||
243 | Visitor::visit($ast, [ |
||
244 | 'enter' => function ($node) use ($addedField, &$didVisitAddedField, $ast) { |
||
245 | $this->checkVisitorFnArgs($ast, func_get_args(), true); |
||
246 | if ($node instanceof FieldNode && $node->name->value === 'a') { |
||
247 | return new FieldNode([ |
||
248 | 'selectionSet' => new SelectionSetNode([ |
||
249 | 'selections' => NodeList::create([$addedField])->merge($node->selectionSet->selections), |
||
250 | ]), |
||
251 | ]); |
||
252 | } |
||
253 | if ($node !== $addedField) { |
||
254 | return; |
||
255 | } |
||
256 | |||
257 | $didVisitAddedField = true; |
||
258 | }, |
||
259 | ]); |
||
260 | |||
261 | $this->assertTrue($didVisitAddedField); |
||
262 | } |
||
263 | |||
264 | public function testAllowsSkippingASubTree() |
||
265 | { |
||
266 | $visited = []; |
||
267 | $ast = Parser::parse('{ a, b { x }, c }', ['noLocation' => true]); |
||
268 | |||
269 | Visitor::visit($ast, [ |
||
270 | 'enter' => function (Node $node) use (&$visited, $ast) { |
||
271 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
272 | $visited[] = ['enter', $node->kind, $node->value ?? null]; |
||
273 | if ($node instanceof FieldNode && $node->name->value === 'b') { |
||
274 | return Visitor::skipNode(); |
||
275 | } |
||
276 | }, |
||
277 | 'leave' => function (Node $node) use (&$visited, $ast) { |
||
278 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
279 | $visited[] = ['leave', $node->kind, $node->value ?? null]; |
||
280 | }, |
||
281 | ]); |
||
282 | |||
283 | $expected = [ |
||
284 | [ 'enter', 'Document', null ], |
||
285 | [ 'enter', 'OperationDefinition', null ], |
||
286 | [ 'enter', 'SelectionSet', null ], |
||
287 | [ 'enter', 'Field', null ], |
||
288 | [ 'enter', 'Name', 'a' ], |
||
289 | [ 'leave', 'Name', 'a' ], |
||
290 | [ 'leave', 'Field', null ], |
||
291 | [ 'enter', 'Field', null ], |
||
292 | [ 'enter', 'Field', null ], |
||
293 | [ 'enter', 'Name', 'c' ], |
||
294 | [ 'leave', 'Name', 'c' ], |
||
295 | [ 'leave', 'Field', null ], |
||
296 | [ 'leave', 'SelectionSet', null ], |
||
297 | [ 'leave', 'OperationDefinition', null ], |
||
298 | [ 'leave', 'Document', null ], |
||
299 | ]; |
||
300 | |||
301 | $this->assertEquals($expected, $visited); |
||
302 | } |
||
303 | |||
304 | public function testAllowsEarlyExitWhileVisiting() |
||
305 | { |
||
306 | $visited = []; |
||
307 | $ast = Parser::parse('{ a, b { x }, c }', ['noLocation' => true]); |
||
308 | |||
309 | Visitor::visit($ast, [ |
||
310 | 'enter' => function (Node $node) use (&$visited, $ast) { |
||
311 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
312 | $visited[] = ['enter', $node->kind, $node->value ?? null]; |
||
313 | if ($node instanceof NameNode && $node->value === 'x') { |
||
314 | return Visitor::stop(); |
||
315 | } |
||
316 | }, |
||
317 | 'leave' => function (Node $node) use (&$visited, $ast) { |
||
318 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
319 | $visited[] = ['leave', $node->kind, $node->value ?? null]; |
||
320 | }, |
||
321 | ]); |
||
322 | |||
323 | $expected = [ |
||
324 | [ 'enter', 'Document', null ], |
||
325 | [ 'enter', 'OperationDefinition', null ], |
||
326 | [ 'enter', 'SelectionSet', null ], |
||
327 | [ 'enter', 'Field', null ], |
||
328 | [ 'enter', 'Name', 'a' ], |
||
329 | [ 'leave', 'Name', 'a' ], |
||
330 | [ 'leave', 'Field', null ], |
||
331 | [ 'enter', 'Field', null ], |
||
332 | [ 'enter', 'Name', 'b' ], |
||
333 | [ 'leave', 'Name', 'b' ], |
||
334 | [ 'enter', 'SelectionSet', null ], |
||
335 | [ 'enter', 'Field', null ], |
||
336 | [ 'enter', 'Name', 'x' ], |
||
337 | ]; |
||
338 | |||
339 | $this->assertEquals($expected, $visited); |
||
340 | } |
||
341 | |||
342 | public function testAllowsEarlyExitWhileLeaving() |
||
343 | { |
||
344 | $visited = []; |
||
345 | |||
346 | $ast = Parser::parse('{ a, b { x }, c }', ['noLocation' => true]); |
||
347 | Visitor::visit($ast, [ |
||
348 | 'enter' => function ($node) use (&$visited, $ast) { |
||
349 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
350 | $visited[] = ['enter', $node->kind, $node->value ?? null]; |
||
351 | }, |
||
352 | 'leave' => function ($node) use (&$visited, $ast) { |
||
353 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
354 | $visited[] = ['leave', $node->kind, $node->value ?? null]; |
||
355 | |||
356 | if ($node->kind === NodeKind::NAME && $node->value === 'x') { |
||
357 | return Visitor::stop(); |
||
358 | } |
||
359 | }, |
||
360 | ]); |
||
361 | |||
362 | $this->assertEquals($visited, [ |
||
363 | [ 'enter', 'Document', null ], |
||
364 | [ 'enter', 'OperationDefinition', null ], |
||
365 | [ 'enter', 'SelectionSet', null ], |
||
366 | [ 'enter', 'Field', null ], |
||
367 | [ 'enter', 'Name', 'a' ], |
||
368 | [ 'leave', 'Name', 'a' ], |
||
369 | [ 'leave', 'Field', null ], |
||
370 | [ 'enter', 'Field', null ], |
||
371 | [ 'enter', 'Name', 'b' ], |
||
372 | [ 'leave', 'Name', 'b' ], |
||
373 | [ 'enter', 'SelectionSet', null ], |
||
374 | [ 'enter', 'Field', null ], |
||
375 | [ 'enter', 'Name', 'x' ], |
||
376 | [ 'leave', 'Name', 'x' ], |
||
377 | ]); |
||
378 | } |
||
379 | |||
380 | public function testAllowsANamedFunctionsVisitorAPI() |
||
381 | { |
||
382 | $visited = []; |
||
383 | $ast = Parser::parse('{ a, b { x }, c }', ['noLocation' => true]); |
||
384 | |||
385 | Visitor::visit($ast, [ |
||
386 | NodeKind::NAME => function (NameNode $node) use (&$visited, $ast) { |
||
387 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
388 | $visited[] = ['enter', $node->kind, $node->value]; |
||
389 | }, |
||
390 | NodeKind::SELECTION_SET => [ |
||
391 | 'enter' => function (SelectionSetNode $node) use (&$visited, $ast) { |
||
392 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
393 | $visited[] = ['enter', $node->kind, null]; |
||
394 | }, |
||
395 | 'leave' => function (SelectionSetNode $node) use (&$visited, $ast) { |
||
396 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
397 | $visited[] = ['leave', $node->kind, null]; |
||
398 | }, |
||
399 | ], |
||
400 | ]); |
||
401 | |||
402 | $expected = [ |
||
403 | [ 'enter', 'SelectionSet', null ], |
||
404 | [ 'enter', 'Name', 'a' ], |
||
405 | [ 'enter', 'Name', 'b' ], |
||
406 | [ 'enter', 'SelectionSet', null ], |
||
407 | [ 'enter', 'Name', 'x' ], |
||
408 | [ 'leave', 'SelectionSet', null ], |
||
409 | [ 'enter', 'Name', 'c' ], |
||
410 | [ 'leave', 'SelectionSet', null ], |
||
411 | ]; |
||
412 | |||
413 | $this->assertEquals($expected, $visited); |
||
414 | } |
||
415 | |||
416 | public function testExperimentalVisitsVariablesDefinedInFragments() |
||
417 | { |
||
418 | $ast = Parser::parse( |
||
419 | 'fragment a($v: Boolean = false) on t { f }', |
||
420 | [ |
||
421 | 'noLocation' => true, |
||
422 | 'experimentalFragmentVariables' => true, |
||
423 | ] |
||
424 | ); |
||
425 | $visited = []; |
||
426 | |||
427 | Visitor::visit($ast, [ |
||
428 | 'enter' => function ($node) use (&$visited, $ast) { |
||
429 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
430 | $visited[] = ['enter', $node->kind, $node->value ?? null]; |
||
431 | }, |
||
432 | 'leave' => function ($node) use (&$visited, $ast) { |
||
433 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
434 | $visited[] = ['leave', $node->kind, $node->value ?? null]; |
||
435 | }, |
||
436 | ]); |
||
437 | |||
438 | $expected = [ |
||
439 | ['enter', 'Document', null], |
||
440 | ['enter', 'FragmentDefinition', null], |
||
441 | ['enter', 'Name', 'a'], |
||
442 | ['leave', 'Name', 'a'], |
||
443 | ['enter', 'VariableDefinition', null], |
||
444 | ['enter', 'Variable', null], |
||
445 | ['enter', 'Name', 'v'], |
||
446 | ['leave', 'Name', 'v'], |
||
447 | ['leave', 'Variable', null], |
||
448 | ['enter', 'NamedType', null], |
||
449 | ['enter', 'Name', 'Boolean'], |
||
450 | ['leave', 'Name', 'Boolean'], |
||
451 | ['leave', 'NamedType', null], |
||
452 | ['enter', 'BooleanValue', false], |
||
453 | ['leave', 'BooleanValue', false], |
||
454 | ['leave', 'VariableDefinition', null], |
||
455 | ['enter', 'NamedType', null], |
||
456 | ['enter', 'Name', 't'], |
||
457 | ['leave', 'Name', 't'], |
||
458 | ['leave', 'NamedType', null], |
||
459 | ['enter', 'SelectionSet', null], |
||
460 | ['enter', 'Field', null], |
||
461 | ['enter', 'Name', 'f'], |
||
462 | ['leave', 'Name', 'f'], |
||
463 | ['leave', 'Field', null], |
||
464 | ['leave', 'SelectionSet', null], |
||
465 | ['leave', 'FragmentDefinition', null], |
||
466 | ['leave', 'Document', null], |
||
467 | ]; |
||
468 | |||
469 | $this->assertEquals($expected, $visited); |
||
470 | } |
||
471 | |||
472 | public function testVisitsKitchenSink() |
||
805 | } |
||
806 | |||
807 | /** |
||
808 | * Describe: visitInParallel |
||
809 | * Note: nearly identical to the above test of the same test but using visitInParallel. |
||
810 | */ |
||
811 | public function testAllowsSkippingSubTree() |
||
812 | { |
||
813 | $visited = []; |
||
814 | |||
815 | $ast = Parser::parse('{ a, b { x }, c }'); |
||
816 | Visitor::visit($ast, Visitor::visitInParallel([ |
||
817 | [ |
||
818 | 'enter' => function ($node) use (&$visited, $ast) { |
||
819 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
820 | $visited[] = [ 'enter', $node->kind, $node->value ?? null]; |
||
821 | |||
822 | if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { |
||
823 | return Visitor::skipNode(); |
||
824 | } |
||
825 | }, |
||
826 | |||
827 | 'leave' => function ($node) use (&$visited, $ast) { |
||
828 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
829 | $visited[] = ['leave', $node->kind, $node->value ?? null]; |
||
830 | }, |
||
831 | ], |
||
832 | ])); |
||
833 | |||
834 | $this->assertEquals([ |
||
835 | [ 'enter', 'Document', null ], |
||
836 | [ 'enter', 'OperationDefinition', null ], |
||
837 | [ 'enter', 'SelectionSet', null ], |
||
838 | [ 'enter', 'Field', null ], |
||
839 | [ 'enter', 'Name', 'a' ], |
||
840 | [ 'leave', 'Name', 'a' ], |
||
841 | [ 'leave', 'Field', null ], |
||
842 | [ 'enter', 'Field', null ], |
||
843 | [ 'enter', 'Field', null ], |
||
844 | [ 'enter', 'Name', 'c' ], |
||
845 | [ 'leave', 'Name', 'c' ], |
||
846 | [ 'leave', 'Field', null ], |
||
847 | [ 'leave', 'SelectionSet', null ], |
||
848 | [ 'leave', 'OperationDefinition', null ], |
||
849 | [ 'leave', 'Document', null ], |
||
850 | ], $visited); |
||
851 | } |
||
852 | |||
853 | public function testAllowsSkippingDifferentSubTrees() |
||
854 | { |
||
855 | $visited = []; |
||
856 | |||
857 | $ast = Parser::parse('{ a { x }, b { y} }'); |
||
858 | Visitor::visit($ast, Visitor::visitInParallel([ |
||
859 | [ |
||
860 | 'enter' => function ($node) use (&$visited, $ast) { |
||
861 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
862 | $visited[] = ['no-a', 'enter', $node->kind, $node->value ?? null]; |
||
863 | if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'a') { |
||
864 | return Visitor::skipNode(); |
||
865 | } |
||
866 | }, |
||
867 | 'leave' => function ($node) use (&$visited, $ast) { |
||
868 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
869 | $visited[] = [ 'no-a', 'leave', $node->kind, $node->value ?? null ]; |
||
870 | }, |
||
871 | ], |
||
872 | [ |
||
873 | 'enter' => function ($node) use (&$visited, $ast) { |
||
874 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
875 | $visited[] = ['no-b', 'enter', $node->kind, $node->value ?? null]; |
||
876 | if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { |
||
877 | return Visitor::skipNode(); |
||
878 | } |
||
879 | }, |
||
880 | 'leave' => function ($node) use (&$visited, $ast) { |
||
881 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
882 | $visited[] = ['no-b', 'leave', $node->kind, $node->value ?? null]; |
||
883 | }, |
||
884 | ], |
||
885 | ])); |
||
886 | |||
887 | $this->assertEquals([ |
||
888 | [ 'no-a', 'enter', 'Document', null ], |
||
889 | [ 'no-b', 'enter', 'Document', null ], |
||
890 | [ 'no-a', 'enter', 'OperationDefinition', null ], |
||
891 | [ 'no-b', 'enter', 'OperationDefinition', null ], |
||
892 | [ 'no-a', 'enter', 'SelectionSet', null ], |
||
893 | [ 'no-b', 'enter', 'SelectionSet', null ], |
||
894 | [ 'no-a', 'enter', 'Field', null ], |
||
895 | [ 'no-b', 'enter', 'Field', null ], |
||
896 | [ 'no-b', 'enter', 'Name', 'a' ], |
||
897 | [ 'no-b', 'leave', 'Name', 'a' ], |
||
898 | [ 'no-b', 'enter', 'SelectionSet', null ], |
||
899 | [ 'no-b', 'enter', 'Field', null ], |
||
900 | [ 'no-b', 'enter', 'Name', 'x' ], |
||
901 | [ 'no-b', 'leave', 'Name', 'x' ], |
||
902 | [ 'no-b', 'leave', 'Field', null ], |
||
903 | [ 'no-b', 'leave', 'SelectionSet', null ], |
||
904 | [ 'no-b', 'leave', 'Field', null ], |
||
905 | [ 'no-a', 'enter', 'Field', null ], |
||
906 | [ 'no-b', 'enter', 'Field', null ], |
||
907 | [ 'no-a', 'enter', 'Name', 'b' ], |
||
908 | [ 'no-a', 'leave', 'Name', 'b' ], |
||
909 | [ 'no-a', 'enter', 'SelectionSet', null ], |
||
910 | [ 'no-a', 'enter', 'Field', null ], |
||
911 | [ 'no-a', 'enter', 'Name', 'y' ], |
||
912 | [ 'no-a', 'leave', 'Name', 'y' ], |
||
913 | [ 'no-a', 'leave', 'Field', null ], |
||
914 | [ 'no-a', 'leave', 'SelectionSet', null ], |
||
915 | [ 'no-a', 'leave', 'Field', null ], |
||
916 | [ 'no-a', 'leave', 'SelectionSet', null ], |
||
917 | [ 'no-b', 'leave', 'SelectionSet', null ], |
||
918 | [ 'no-a', 'leave', 'OperationDefinition', null ], |
||
919 | [ 'no-b', 'leave', 'OperationDefinition', null ], |
||
920 | [ 'no-a', 'leave', 'Document', null ], |
||
921 | [ 'no-b', 'leave', 'Document', null ], |
||
922 | ], $visited); |
||
923 | } |
||
924 | |||
925 | public function testAllowsEarlyExitWhileVisiting2() |
||
926 | { |
||
927 | $visited = []; |
||
928 | |||
929 | $ast = Parser::parse('{ a, b { x }, c }'); |
||
930 | Visitor::visit($ast, Visitor::visitInParallel([ [ |
||
931 | 'enter' => function ($node) use (&$visited, $ast) { |
||
932 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
933 | $value = $node->value ?? null; |
||
934 | $visited[] = ['enter', $node->kind, $value]; |
||
935 | if ($node->kind === 'Name' && $value === 'x') { |
||
936 | return Visitor::stop(); |
||
937 | } |
||
938 | }, |
||
939 | 'leave' => function ($node) use (&$visited, $ast) { |
||
940 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
941 | $visited[] = ['leave', $node->kind, $node->value ?? null]; |
||
942 | }, |
||
943 | ], |
||
944 | ])); |
||
945 | |||
946 | $this->assertEquals([ |
||
947 | [ 'enter', 'Document', null ], |
||
948 | [ 'enter', 'OperationDefinition', null ], |
||
949 | [ 'enter', 'SelectionSet', null ], |
||
950 | [ 'enter', 'Field', null ], |
||
951 | [ 'enter', 'Name', 'a' ], |
||
952 | [ 'leave', 'Name', 'a' ], |
||
953 | [ 'leave', 'Field', null ], |
||
954 | [ 'enter', 'Field', null ], |
||
955 | [ 'enter', 'Name', 'b' ], |
||
956 | [ 'leave', 'Name', 'b' ], |
||
957 | [ 'enter', 'SelectionSet', null ], |
||
958 | [ 'enter', 'Field', null ], |
||
959 | [ 'enter', 'Name', 'x' ], |
||
960 | ], $visited); |
||
961 | } |
||
962 | |||
963 | public function testAllowsEarlyExitFromDifferentPoints() |
||
964 | { |
||
965 | $visited = []; |
||
966 | |||
967 | $ast = Parser::parse('{ a { y }, b { x } }'); |
||
968 | Visitor::visit($ast, Visitor::visitInParallel([ |
||
969 | [ |
||
970 | 'enter' => function ($node) use (&$visited, $ast) { |
||
971 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
972 | $value = $node->value ?? null; |
||
973 | $visited[] = ['break-a', 'enter', $node->kind, $value]; |
||
974 | if ($node->kind === 'Name' && $value === 'a') { |
||
975 | return Visitor::stop(); |
||
976 | } |
||
977 | }, |
||
978 | 'leave' => function ($node) use (&$visited, $ast) { |
||
979 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
980 | $visited[] = [ 'break-a', 'leave', $node->kind, $node->value ?? null ]; |
||
981 | }, |
||
982 | ], |
||
983 | [ |
||
984 | 'enter' => function ($node) use (&$visited, $ast) { |
||
985 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
986 | $value = $node->value ?? null; |
||
987 | $visited[] = ['break-b', 'enter', $node->kind, $value]; |
||
988 | if ($node->kind === 'Name' && $value === 'b') { |
||
989 | return Visitor::stop(); |
||
990 | } |
||
991 | }, |
||
992 | 'leave' => function ($node) use (&$visited, $ast) { |
||
993 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
994 | $visited[] = ['break-b', 'leave', $node->kind, $node->value ?? null]; |
||
995 | }, |
||
996 | ], |
||
997 | ])); |
||
998 | |||
999 | $this->assertEquals([ |
||
1000 | [ 'break-a', 'enter', 'Document', null ], |
||
1001 | [ 'break-b', 'enter', 'Document', null ], |
||
1002 | [ 'break-a', 'enter', 'OperationDefinition', null ], |
||
1003 | [ 'break-b', 'enter', 'OperationDefinition', null ], |
||
1004 | [ 'break-a', 'enter', 'SelectionSet', null ], |
||
1005 | [ 'break-b', 'enter', 'SelectionSet', null ], |
||
1006 | [ 'break-a', 'enter', 'Field', null ], |
||
1007 | [ 'break-b', 'enter', 'Field', null ], |
||
1008 | [ 'break-a', 'enter', 'Name', 'a' ], |
||
1009 | [ 'break-b', 'enter', 'Name', 'a' ], |
||
1010 | [ 'break-b', 'leave', 'Name', 'a' ], |
||
1011 | [ 'break-b', 'enter', 'SelectionSet', null ], |
||
1012 | [ 'break-b', 'enter', 'Field', null ], |
||
1013 | [ 'break-b', 'enter', 'Name', 'y' ], |
||
1014 | [ 'break-b', 'leave', 'Name', 'y' ], |
||
1015 | [ 'break-b', 'leave', 'Field', null ], |
||
1016 | [ 'break-b', 'leave', 'SelectionSet', null ], |
||
1017 | [ 'break-b', 'leave', 'Field', null ], |
||
1018 | [ 'break-b', 'enter', 'Field', null ], |
||
1019 | [ 'break-b', 'enter', 'Name', 'b' ], |
||
1020 | ], $visited); |
||
1021 | } |
||
1022 | |||
1023 | public function testAllowsEarlyExitWhileLeaving2() |
||
1024 | { |
||
1025 | $visited = []; |
||
1026 | |||
1027 | $ast = Parser::parse('{ a, b { x }, c }'); |
||
1028 | Visitor::visit($ast, Visitor::visitInParallel([ [ |
||
1029 | 'enter' => function ($node) use (&$visited, $ast) { |
||
1030 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1031 | $visited[] = ['enter', $node->kind, $node->value ?? null]; |
||
1032 | }, |
||
1033 | 'leave' => function ($node) use (&$visited, $ast) { |
||
1034 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1035 | $value = $node->value ?? null; |
||
1036 | $visited[] = ['leave', $node->kind, $value]; |
||
1037 | if ($node->kind === 'Name' && $value === 'x') { |
||
1038 | return Visitor::stop(); |
||
1039 | } |
||
1040 | }, |
||
1041 | ], |
||
1042 | ])); |
||
1043 | |||
1044 | $this->assertEquals([ |
||
1045 | [ 'enter', 'Document', null ], |
||
1046 | [ 'enter', 'OperationDefinition', null ], |
||
1047 | [ 'enter', 'SelectionSet', null ], |
||
1048 | [ 'enter', 'Field', null ], |
||
1049 | [ 'enter', 'Name', 'a' ], |
||
1050 | [ 'leave', 'Name', 'a' ], |
||
1051 | [ 'leave', 'Field', null ], |
||
1052 | [ 'enter', 'Field', null ], |
||
1053 | [ 'enter', 'Name', 'b' ], |
||
1054 | [ 'leave', 'Name', 'b' ], |
||
1055 | [ 'enter', 'SelectionSet', null ], |
||
1056 | [ 'enter', 'Field', null ], |
||
1057 | [ 'enter', 'Name', 'x' ], |
||
1058 | [ 'leave', 'Name', 'x' ], |
||
1059 | ], $visited); |
||
1060 | } |
||
1061 | |||
1062 | public function testAllowsEarlyExitFromLeavingDifferentPoints() |
||
1063 | { |
||
1064 | $visited = []; |
||
1065 | |||
1066 | $ast = Parser::parse('{ a { y }, b { x } }'); |
||
1067 | Visitor::visit($ast, Visitor::visitInParallel([ |
||
1068 | [ |
||
1069 | 'enter' => function ($node) use (&$visited, $ast) { |
||
1070 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1071 | $visited[] = ['break-a', 'enter', $node->kind, $node->value ?? null]; |
||
1072 | }, |
||
1073 | 'leave' => function ($node) use (&$visited, $ast) { |
||
1074 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1075 | $visited[] = ['break-a', 'leave', $node->kind, $node->value ?? null]; |
||
1076 | if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'a') { |
||
1077 | return Visitor::stop(); |
||
1078 | } |
||
1079 | }, |
||
1080 | ], |
||
1081 | [ |
||
1082 | 'enter' => function ($node) use (&$visited, $ast) { |
||
1083 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1084 | $visited[] = ['break-b', 'enter', $node->kind, $node->value ?? null]; |
||
1085 | }, |
||
1086 | 'leave' => function ($node) use (&$visited, $ast) { |
||
1087 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1088 | $visited[] = ['break-b', 'leave', $node->kind, $node->value ?? null]; |
||
1089 | if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { |
||
1090 | return Visitor::stop(); |
||
1091 | } |
||
1092 | }, |
||
1093 | ], |
||
1094 | ])); |
||
1095 | |||
1096 | $this->assertEquals([ |
||
1097 | [ 'break-a', 'enter', 'Document', null ], |
||
1098 | [ 'break-b', 'enter', 'Document', null ], |
||
1099 | [ 'break-a', 'enter', 'OperationDefinition', null ], |
||
1100 | [ 'break-b', 'enter', 'OperationDefinition', null ], |
||
1101 | [ 'break-a', 'enter', 'SelectionSet', null ], |
||
1102 | [ 'break-b', 'enter', 'SelectionSet', null ], |
||
1103 | [ 'break-a', 'enter', 'Field', null ], |
||
1104 | [ 'break-b', 'enter', 'Field', null ], |
||
1105 | [ 'break-a', 'enter', 'Name', 'a' ], |
||
1106 | [ 'break-b', 'enter', 'Name', 'a' ], |
||
1107 | [ 'break-a', 'leave', 'Name', 'a' ], |
||
1108 | [ 'break-b', 'leave', 'Name', 'a' ], |
||
1109 | [ 'break-a', 'enter', 'SelectionSet', null ], |
||
1110 | [ 'break-b', 'enter', 'SelectionSet', null ], |
||
1111 | [ 'break-a', 'enter', 'Field', null ], |
||
1112 | [ 'break-b', 'enter', 'Field', null ], |
||
1113 | [ 'break-a', 'enter', 'Name', 'y' ], |
||
1114 | [ 'break-b', 'enter', 'Name', 'y' ], |
||
1115 | [ 'break-a', 'leave', 'Name', 'y' ], |
||
1116 | [ 'break-b', 'leave', 'Name', 'y' ], |
||
1117 | [ 'break-a', 'leave', 'Field', null ], |
||
1118 | [ 'break-b', 'leave', 'Field', null ], |
||
1119 | [ 'break-a', 'leave', 'SelectionSet', null ], |
||
1120 | [ 'break-b', 'leave', 'SelectionSet', null ], |
||
1121 | [ 'break-a', 'leave', 'Field', null ], |
||
1122 | [ 'break-b', 'leave', 'Field', null ], |
||
1123 | [ 'break-b', 'enter', 'Field', null ], |
||
1124 | [ 'break-b', 'enter', 'Name', 'b' ], |
||
1125 | [ 'break-b', 'leave', 'Name', 'b' ], |
||
1126 | [ 'break-b', 'enter', 'SelectionSet', null ], |
||
1127 | [ 'break-b', 'enter', 'Field', null ], |
||
1128 | [ 'break-b', 'enter', 'Name', 'x' ], |
||
1129 | [ 'break-b', 'leave', 'Name', 'x' ], |
||
1130 | [ 'break-b', 'leave', 'Field', null ], |
||
1131 | [ 'break-b', 'leave', 'SelectionSet', null ], |
||
1132 | [ 'break-b', 'leave', 'Field', null ], |
||
1133 | ], $visited); |
||
1134 | } |
||
1135 | |||
1136 | public function testAllowsForEditingOnEnter2() |
||
1198 | } |
||
1199 | |||
1200 | public function testAllowsForEditingOnLeave2() |
||
1201 | { |
||
1202 | $visited = []; |
||
1203 | |||
1204 | $ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]); |
||
1205 | $editedAst = Visitor::visit($ast, Visitor::visitInParallel([ |
||
1206 | [ |
||
1207 | 'leave' => function ($node) use (&$visited, $ast) { |
||
1208 | $this->checkVisitorFnArgs($ast, func_get_args(), true); |
||
1209 | if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { |
||
1210 | return Visitor::removeNode(); |
||
1211 | } |
||
1212 | }, |
||
1213 | ], |
||
1214 | [ |
||
1215 | 'enter' => function ($node) use (&$visited, $ast) { |
||
1216 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1217 | $visited[] = ['enter', $node->kind, $node->value ?? null]; |
||
1218 | }, |
||
1219 | 'leave' => function ($node) use (&$visited, $ast) { |
||
1220 | $this->checkVisitorFnArgs($ast, func_get_args(), true); |
||
1221 | $visited[] = ['leave', $node->kind, $node->value ?? null]; |
||
1222 | }, |
||
1223 | ], |
||
1224 | ])); |
||
1225 | |||
1226 | $this->assertEquals( |
||
1227 | Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]), |
||
1228 | $ast |
||
1229 | ); |
||
1230 | |||
1231 | $this->assertEquals( |
||
1232 | Parser::parse('{ a, c { a, c } }', ['noLocation' => true]), |
||
1233 | $editedAst |
||
1234 | ); |
||
1235 | |||
1236 | $this->assertEquals([ |
||
1237 | ['enter', 'Document', null], |
||
1238 | ['enter', 'OperationDefinition', null], |
||
1239 | ['enter', 'SelectionSet', null], |
||
1240 | ['enter', 'Field', null], |
||
1241 | ['enter', 'Name', 'a'], |
||
1242 | ['leave', 'Name', 'a'], |
||
1243 | ['leave', 'Field', null], |
||
1244 | ['enter', 'Field', null], |
||
1245 | ['enter', 'Name', 'b'], |
||
1246 | ['leave', 'Name', 'b'], |
||
1247 | ['enter', 'Field', null], |
||
1248 | ['enter', 'Name', 'c'], |
||
1249 | ['leave', 'Name', 'c'], |
||
1250 | ['enter', 'SelectionSet', null], |
||
1251 | ['enter', 'Field', null], |
||
1252 | ['enter', 'Name', 'a'], |
||
1253 | ['leave', 'Name', 'a'], |
||
1254 | ['leave', 'Field', null], |
||
1255 | ['enter', 'Field', null], |
||
1256 | ['enter', 'Name', 'b'], |
||
1257 | ['leave', 'Name', 'b'], |
||
1258 | ['enter', 'Field', null], |
||
1259 | ['enter', 'Name', 'c'], |
||
1260 | ['leave', 'Name', 'c'], |
||
1261 | ['leave', 'Field', null], |
||
1262 | ['leave', 'SelectionSet', null], |
||
1263 | ['leave', 'Field', null], |
||
1264 | ['leave', 'SelectionSet', null], |
||
1265 | ['leave', 'OperationDefinition', null], |
||
1266 | ['leave', 'Document', null], |
||
1267 | ], $visited); |
||
1268 | } |
||
1269 | |||
1270 | |||
1271 | /** |
||
1272 | * Describe: visitWithTypeInfo |
||
1273 | */ |
||
1274 | public function testMaintainsTypeInfoDuringVisit() |
||
1275 | { |
||
1276 | $visited = []; |
||
1277 | |||
1278 | $typeInfo = new TypeInfo(ValidatorTestCase::getTestSchema()); |
||
1279 | |||
1280 | $ast = Parser::parse('{ human(id: 4) { name, pets { ... { name } }, unknown } }'); |
||
1281 | Visitor::visit($ast, Visitor::visitWithTypeInfo($typeInfo, [ |
||
1282 | 'enter' => function ($node) use ($typeInfo, &$visited, $ast) { |
||
1283 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1284 | $parentType = $typeInfo->getParentType(); |
||
1285 | $type = $typeInfo->getType(); |
||
1286 | $inputType = $typeInfo->getInputType(); |
||
1287 | $visited[] = [ |
||
1288 | 'enter', |
||
1289 | $node->kind, |
||
1290 | $node->kind === 'Name' ? $node->value : null, |
||
1291 | $parentType ? (string) $parentType : null, |
||
1292 | $type ? (string) $type : null, |
||
1293 | $inputType ? (string) $inputType : null, |
||
1294 | ]; |
||
1295 | }, |
||
1296 | 'leave' => function ($node) use ($typeInfo, &$visited, $ast) { |
||
1297 | $this->checkVisitorFnArgs($ast, func_get_args()); |
||
1298 | $parentType = $typeInfo->getParentType(); |
||
1299 | $type = $typeInfo->getType(); |
||
1300 | $inputType = $typeInfo->getInputType(); |
||
1301 | $visited[] = [ |
||
1302 | 'leave', |
||
1303 | $node->kind, |
||
1304 | $node->kind === 'Name' ? $node->value : null, |
||
1305 | $parentType ? (string) $parentType : null, |
||
1306 | $type ? (string) $type : null, |
||
1307 | $inputType ? (string) $inputType : null, |
||
1308 | ]; |
||
1309 | }, |
||
1310 | ])); |
||
1311 | |||
1312 | $this->assertEquals([ |
||
1313 | ['enter', 'Document', null, null, null, null], |
||
1314 | ['enter', 'OperationDefinition', null, null, 'QueryRoot', null], |
||
1315 | ['enter', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], |
||
1316 | ['enter', 'Field', null, 'QueryRoot', 'Human', null], |
||
1317 | ['enter', 'Name', 'human', 'QueryRoot', 'Human', null], |
||
1318 | ['leave', 'Name', 'human', 'QueryRoot', 'Human', null], |
||
1319 | ['enter', 'Argument', null, 'QueryRoot', 'Human', 'ID'], |
||
1320 | ['enter', 'Name', 'id', 'QueryRoot', 'Human', 'ID'], |
||
1321 | ['leave', 'Name', 'id', 'QueryRoot', 'Human', 'ID'], |
||
1322 | ['enter', 'IntValue', null, 'QueryRoot', 'Human', 'ID'], |
||
1323 | ['leave', 'IntValue', null, 'QueryRoot', 'Human', 'ID'], |
||
1324 | ['leave', 'Argument', null, 'QueryRoot', 'Human', 'ID'], |
||
1325 | ['enter', 'SelectionSet', null, 'Human', 'Human', null], |
||
1326 | ['enter', 'Field', null, 'Human', 'String', null], |
||
1327 | ['enter', 'Name', 'name', 'Human', 'String', null], |
||
1328 | ['leave', 'Name', 'name', 'Human', 'String', null], |
||
1329 | ['leave', 'Field', null, 'Human', 'String', null], |
||
1330 | ['enter', 'Field', null, 'Human', '[Pet]', null], |
||
1331 | ['enter', 'Name', 'pets', 'Human', '[Pet]', null], |
||
1332 | ['leave', 'Name', 'pets', 'Human', '[Pet]', null], |
||
1333 | ['enter', 'SelectionSet', null, 'Pet', '[Pet]', null], |
||
1334 | ['enter', 'InlineFragment', null, 'Pet', 'Pet', null], |
||
1335 | ['enter', 'SelectionSet', null, 'Pet', 'Pet', null], |
||
1336 | ['enter', 'Field', null, 'Pet', 'String', null], |
||
1337 | ['enter', 'Name', 'name', 'Pet', 'String', null], |
||
1338 | ['leave', 'Name', 'name', 'Pet', 'String', null], |
||
1339 | ['leave', 'Field', null, 'Pet', 'String', null], |
||
1340 | ['leave', 'SelectionSet', null, 'Pet', 'Pet', null], |
||
1341 | ['leave', 'InlineFragment', null, 'Pet', 'Pet', null], |
||
1342 | ['leave', 'SelectionSet', null, 'Pet', '[Pet]', null], |
||
1343 | ['leave', 'Field', null, 'Human', '[Pet]', null], |
||
1344 | ['enter', 'Field', null, 'Human', null, null], |
||
1345 | ['enter', 'Name', 'unknown', 'Human', null, null], |
||
1346 | ['leave', 'Name', 'unknown', 'Human', null, null], |
||
1347 | ['leave', 'Field', null, 'Human', null, null], |
||
1348 | ['leave', 'SelectionSet', null, 'Human', 'Human', null], |
||
1349 | ['leave', 'Field', null, 'QueryRoot', 'Human', null], |
||
1350 | ['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], |
||
1351 | ['leave', 'OperationDefinition', null, null, 'QueryRoot', null], |
||
1352 | ['leave', 'Document', null, null, null, null], |
||
1353 | ], $visited); |
||
1354 | } |
||
1355 | |||
1356 | public function testMaintainsTypeInfoDuringEdit() |
||
1467 | } |
||
1468 | } |
||
1469 |