2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2018-08-13 11:36:03 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-04-09 10:36:53 +03:00
|
|
|
namespace GraphQL\Tests\Language;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\DocumentNode;
|
|
|
|
use GraphQL\Language\AST\FieldNode;
|
|
|
|
use GraphQL\Language\AST\NameNode;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\AST\Node;
|
2016-11-19 13:31:47 +03:00
|
|
|
use GraphQL\Language\AST\NodeKind;
|
2017-07-21 18:29:59 +03:00
|
|
|
use GraphQL\Language\AST\NodeList;
|
2016-11-19 02:12:18 +03:00
|
|
|
use GraphQL\Language\AST\OperationDefinitionNode;
|
|
|
|
use GraphQL\Language\AST\SelectionSetNode;
|
2016-04-09 10:36:53 +03:00
|
|
|
use GraphQL\Language\Parser;
|
2016-04-30 17:39:06 +03:00
|
|
|
use GraphQL\Language\Printer;
|
2016-04-09 10:36:53 +03:00
|
|
|
use GraphQL\Language\Visitor;
|
2018-07-29 18:43:10 +03:00
|
|
|
use GraphQL\Tests\Validator\ValidatorTestCase;
|
2016-04-30 17:39:06 +03:00
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Utils\TypeInfo;
|
2018-08-13 11:36:03 +03:00
|
|
|
use function array_keys;
|
|
|
|
use function array_slice;
|
|
|
|
use function count;
|
|
|
|
use function file_get_contents;
|
|
|
|
use function func_get_args;
|
|
|
|
use function gettype;
|
|
|
|
use function is_array;
|
|
|
|
use function iterator_to_array;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class VisitorTest extends ValidatorTestCase
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-02-16 00:44:17 +03:00
|
|
|
private function getNodeByPath(DocumentNode $ast, $path)
|
|
|
|
{
|
|
|
|
$result = $ast;
|
|
|
|
foreach ($path as $key) {
|
|
|
|
$resultArray = $result instanceof NodeList ? iterator_to_array($result) : $result->toArray();
|
|
|
|
$this->assertArrayHasKey($key, $resultArray);
|
|
|
|
$result = $resultArray[$key];
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkVisitorFnArgs($ast, $args, $isEdited = false)
|
|
|
|
{
|
|
|
|
/** @var Node $node */
|
|
|
|
list($node, $key, $parent, $path, $ancestors) = $args;
|
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
$parentArray = $parent && ! is_array($parent) ? ($parent instanceof NodeList ? iterator_to_array($parent) : $parent->toArray()) : $parent;
|
2018-02-16 00:44:17 +03:00
|
|
|
|
|
|
|
$this->assertInstanceOf(Node::class, $node);
|
|
|
|
$this->assertContains($node->kind, array_keys(NodeKind::$classMap));
|
|
|
|
|
|
|
|
$isRoot = $key === null;
|
|
|
|
if ($isRoot) {
|
2018-08-13 11:36:03 +03:00
|
|
|
if (! $isEdited) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->assertEquals($ast, $node);
|
|
|
|
}
|
|
|
|
$this->assertEquals(null, $parent);
|
|
|
|
$this->assertEquals([], $path);
|
|
|
|
$this->assertEquals([], $ancestors);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertContains(gettype($key), ['integer', 'string']);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey($key, $parentArray);
|
|
|
|
|
|
|
|
$this->assertInternalType('array', $path);
|
|
|
|
$this->assertEquals($key, $path[count($path) - 1]);
|
|
|
|
|
|
|
|
$this->assertInternalType('array', $ancestors);
|
|
|
|
$this->assertCount(count($path) - 1, $ancestors);
|
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
if ($isEdited) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($node, $parentArray[$key]);
|
|
|
|
$this->assertEquals($node, $this->getNodeByPath($ast, $path));
|
|
|
|
$ancestorsLength = count($ancestors);
|
|
|
|
for ($i = 0; $i < $ancestorsLength; ++$i) {
|
|
|
|
$ancestorPath = array_slice($path, 0, $i);
|
|
|
|
$this->assertEquals($ancestors[$i], $this->getNodeByPath($ast, $ancestorPath));
|
2018-02-16 00:44:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testValidatesPathArgument() : void
|
2018-02-16 00:44:17 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
$ast = Parser::parse('{ a }', ['noLocation' => true]);
|
|
|
|
|
|
|
|
Visitor::visit($ast, [
|
|
|
|
'enter' => function ($node, $key, $parent, $path) use ($ast, &$visited) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
|
|
|
$visited[] = ['enter', $path];
|
|
|
|
},
|
|
|
|
'leave' => function ($node, $key, $parent, $path) use ($ast, &$visited) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
|
|
|
$visited[] = ['leave', $path];
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
['enter', []],
|
|
|
|
['enter', ['definitions', 0]],
|
|
|
|
['enter', ['definitions', 0, 'selectionSet']],
|
|
|
|
['enter', ['definitions', 0, 'selectionSet', 'selections', 0]],
|
|
|
|
['enter', ['definitions', 0, 'selectionSet', 'selections', 0, 'name']],
|
|
|
|
['leave', ['definitions', 0, 'selectionSet', 'selections', 0, 'name']],
|
|
|
|
['leave', ['definitions', 0, 'selectionSet', 'selections', 0]],
|
|
|
|
['leave', ['definitions', 0, 'selectionSet']],
|
|
|
|
['leave', ['definitions', 0]],
|
|
|
|
['leave', []],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsEditingNodeOnEnterAndOnLeave() : void
|
2016-04-24 14:35:14 +03:00
|
|
|
{
|
|
|
|
$ast = Parser::parse('{ a, b, c { a, b, c } }', [ 'noLocation' => true ]);
|
|
|
|
|
|
|
|
$selectionSet = null;
|
2018-08-13 11:36:03 +03:00
|
|
|
$editedAst = Visitor::visit($ast, [
|
2016-11-19 13:31:47 +03:00
|
|
|
NodeKind::OPERATION_DEFINITION => [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function (OperationDefinitionNode $node) use (&$selectionSet, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2016-04-24 14:35:14 +03:00
|
|
|
$selectionSet = $node->selectionSet;
|
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
$newNode = clone $node;
|
2016-11-19 02:12:18 +03:00
|
|
|
$newNode->selectionSet = new SelectionSetNode([
|
2018-08-13 11:36:03 +03:00
|
|
|
'selections' => [],
|
2016-04-24 14:35:14 +03:00
|
|
|
]);
|
2018-08-13 11:36:03 +03:00
|
|
|
$newNode->didEnter = true;
|
2016-04-24 14:35:14 +03:00
|
|
|
return $newNode;
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function (OperationDefinitionNode $node) use (&$selectionSet, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2018-08-13 11:36:03 +03:00
|
|
|
$newNode = clone $node;
|
2016-04-24 14:35:14 +03:00
|
|
|
$newNode->selectionSet = $selectionSet;
|
2018-08-13 11:36:03 +03:00
|
|
|
$newNode->didLeave = true;
|
2016-04-24 14:35:14 +03:00
|
|
|
return $newNode;
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
|
|
|
],
|
2016-04-24 14:35:14 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertNotEquals($ast, $editedAst);
|
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
$expected = $ast->cloneDeep();
|
2016-04-24 14:35:14 +03:00
|
|
|
$expected->definitions[0]->didEnter = true;
|
|
|
|
$expected->definitions[0]->didLeave = true;
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $editedAst);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsEditingRootNodeOnEnterAndLeave() : void
|
2016-04-24 14:35:14 +03:00
|
|
|
{
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse('{ a, b, c { a, b, c } }', [ 'noLocation' => true ]);
|
2016-04-24 14:35:14 +03:00
|
|
|
$definitions = $ast->definitions;
|
|
|
|
|
|
|
|
$editedAst = Visitor::visit($ast, [
|
2016-11-19 13:31:47 +03:00
|
|
|
NodeKind::DOCUMENT => [
|
2018-02-16 00:44:17 +03:00
|
|
|
'enter' => function (DocumentNode $node) use ($ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$tmp = clone $node;
|
2016-04-24 14:35:14 +03:00
|
|
|
$tmp->definitions = [];
|
2018-08-13 11:36:03 +03:00
|
|
|
$tmp->didEnter = true;
|
2016-04-24 14:35:14 +03:00
|
|
|
return $tmp;
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function (DocumentNode $node) use ($definitions, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2016-04-24 14:35:14 +03:00
|
|
|
$node->definitions = $definitions;
|
2018-08-13 11:36:03 +03:00
|
|
|
$node->didLeave = true;
|
|
|
|
},
|
|
|
|
],
|
2016-04-24 14:35:14 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertNotEquals($ast, $editedAst);
|
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
$tmp = $ast->cloneDeep();
|
2016-04-24 14:35:14 +03:00
|
|
|
$tmp->didEnter = true;
|
|
|
|
$tmp->didLeave = true;
|
|
|
|
|
|
|
|
$this->assertEquals($tmp, $editedAst);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsForEditingOnEnter() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]);
|
2015-07-15 20:05:46 +03:00
|
|
|
$editedAst = Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use ($ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2016-11-19 02:12:18 +03:00
|
|
|
if ($node instanceof FieldNode && $node->name->value === 'b') {
|
2015-07-15 20:05:46 +03:00
|
|
|
return Visitor::removeNode();
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]),
|
|
|
|
$ast
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
Parser::parse('{ a, c { a, c } }', ['noLocation' => true]),
|
|
|
|
$editedAst
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsForEditingOnLeave() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]);
|
2015-07-15 20:05:46 +03:00
|
|
|
$editedAst = Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use ($ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2016-11-19 02:12:18 +03:00
|
|
|
if ($node instanceof FieldNode && $node->name->value === 'b') {
|
2015-07-15 20:05:46 +03:00
|
|
|
return Visitor::removeNode();
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]),
|
|
|
|
$ast
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
Parser::parse('{ a, c { a, c } }', ['noLocation' => true]),
|
|
|
|
$editedAst
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testVisitsEditedNode() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-08-13 11:36:03 +03:00
|
|
|
$addedField = new FieldNode([
|
|
|
|
'name' => new NameNode(['value' => '__typename']),
|
|
|
|
]);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
$didVisitAddedField = false;
|
|
|
|
|
2018-02-16 00:44:17 +03:00
|
|
|
$ast = Parser::parse('{ a { x } }', ['noLocation' => true]);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use ($addedField, &$didVisitAddedField, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2016-11-19 02:12:18 +03:00
|
|
|
if ($node instanceof FieldNode && $node->name->value === 'a') {
|
|
|
|
return new FieldNode([
|
2018-08-13 11:36:03 +03:00
|
|
|
'selectionSet' => new SelectionSetNode([
|
|
|
|
'selections' => NodeList::create([$addedField])->merge($node->selectionSet->selections),
|
|
|
|
]),
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
if ($node !== $addedField) {
|
|
|
|
return;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
|
|
|
|
$didVisitAddedField = true;
|
|
|
|
},
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertTrue($didVisitAddedField);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSkippingASubTree() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse('{ a, b { x }, c }', ['noLocation' => true]);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function (Node $node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $node->value ?? null];
|
2016-11-19 02:12:18 +03:00
|
|
|
if ($node instanceof FieldNode && $node->name->value === 'b') {
|
2015-07-15 20:05:46 +03:00
|
|
|
return Visitor::skipNode();
|
|
|
|
}
|
|
|
|
},
|
2018-02-16 00:44:17 +03:00
|
|
|
'leave' => function (Node $node) use (&$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $node->value ?? null];
|
|
|
|
},
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[ 'enter', 'Document', null ],
|
|
|
|
[ 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Field', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'c' ],
|
|
|
|
[ 'leave', 'Name', 'c' ],
|
|
|
|
[ 'leave', 'Field', null ],
|
|
|
|
[ 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'leave', 'OperationDefinition', null ],
|
2018-08-13 11:36:03 +03:00
|
|
|
[ 'leave', 'Document', null ],
|
2015-07-15 20:05:46 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsEarlyExitWhileVisiting() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse('{ a, b { x }, c }', ['noLocation' => true]);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function (Node $node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $node->value ?? null];
|
2016-11-19 02:12:18 +03:00
|
|
|
if ($node instanceof NameNode && $node->value === 'x') {
|
2015-07-15 20:05:46 +03:00
|
|
|
return Visitor::stop();
|
|
|
|
}
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function (Node $node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $node->value ?? null];
|
|
|
|
},
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[ 'enter', 'Document', null ],
|
|
|
|
[ 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Field', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'b' ],
|
|
|
|
[ 'leave', 'Name', 'b' ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
2018-08-13 11:36:03 +03:00
|
|
|
[ 'enter', 'Name', 'x' ],
|
2015-07-15 20:05:46 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsEarlyExitWhileLeaving() : void
|
2016-04-24 14:35:14 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
2018-02-16 00:44:17 +03:00
|
|
|
$ast = Parser::parse('{ a, b { x }, c }', ['noLocation' => true]);
|
2016-04-24 14:35:14 +03:00
|
|
|
Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $node->value ?? null];
|
2016-04-24 14:35:14 +03:00
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $node->value ?? null];
|
2016-04-24 14:35:14 +03:00
|
|
|
|
2016-11-19 13:31:47 +03:00
|
|
|
if ($node->kind === NodeKind::NAME && $node->value === 'x') {
|
2016-04-24 14:35:14 +03:00
|
|
|
return Visitor::stop();
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2016-04-24 14:35:14 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals($visited, [
|
|
|
|
[ 'enter', 'Document', null ],
|
|
|
|
[ 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Field', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'b' ],
|
|
|
|
[ 'leave', 'Name', 'b' ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'x' ],
|
2018-08-13 11:36:03 +03:00
|
|
|
[ 'leave', 'Name', 'x' ],
|
2016-04-24 14:35:14 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsANamedFunctionsVisitorAPI() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse('{ a, b { x }, c }', ['noLocation' => true]);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
NodeKind::NAME => function (NameNode $node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2015-07-15 20:05:46 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $node->value];
|
|
|
|
},
|
2016-11-19 13:31:47 +03:00
|
|
|
NodeKind::SELECTION_SET => [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function (SelectionSetNode $node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2015-07-15 20:05:46 +03:00
|
|
|
$visited[] = ['enter', $node->kind, null];
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function (SelectionSetNode $node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2015-07-15 20:05:46 +03:00
|
|
|
$visited[] = ['leave', $node->kind, null];
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
|
|
|
],
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Name', 'a' ],
|
|
|
|
[ 'enter', 'Name', 'b' ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Name', 'x' ],
|
|
|
|
[ 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Name', 'c' ],
|
|
|
|
[ 'leave', 'SelectionSet', null ],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testExperimentalVisitsVariablesDefinedInFragments() : void
|
2018-02-15 15:37:45 +03:00
|
|
|
{
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse(
|
2018-02-15 15:37:45 +03:00
|
|
|
'fragment a($v: Boolean = false) on t { f }',
|
2018-02-16 00:44:17 +03:00
|
|
|
[
|
|
|
|
'noLocation' => true,
|
|
|
|
'experimentalFragmentVariables' => true,
|
|
|
|
]
|
2018-02-15 15:37:45 +03:00
|
|
|
);
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $node->value ?? null];
|
2018-02-15 15:37:45 +03:00
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $node->value ?? null];
|
2018-02-15 15:37:45 +03:00
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
['enter', 'Document', null],
|
|
|
|
['enter', 'FragmentDefinition', null],
|
|
|
|
['enter', 'Name', 'a'],
|
|
|
|
['leave', 'Name', 'a'],
|
|
|
|
['enter', 'VariableDefinition', null],
|
|
|
|
['enter', 'Variable', null],
|
|
|
|
['enter', 'Name', 'v'],
|
|
|
|
['leave', 'Name', 'v'],
|
|
|
|
['leave', 'Variable', null],
|
|
|
|
['enter', 'NamedType', null],
|
|
|
|
['enter', 'Name', 'Boolean'],
|
|
|
|
['leave', 'Name', 'Boolean'],
|
|
|
|
['leave', 'NamedType', null],
|
|
|
|
['enter', 'BooleanValue', false],
|
|
|
|
['leave', 'BooleanValue', false],
|
|
|
|
['leave', 'VariableDefinition', null],
|
|
|
|
['enter', 'NamedType', null],
|
|
|
|
['enter', 'Name', 't'],
|
|
|
|
['leave', 'Name', 't'],
|
|
|
|
['leave', 'NamedType', null],
|
|
|
|
['enter', 'SelectionSet', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'f'],
|
|
|
|
['leave', 'Name', 'f'],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['leave', 'SelectionSet', null],
|
|
|
|
['leave', 'FragmentDefinition', null],
|
|
|
|
['leave', 'Document', null],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testVisitsKitchenSink() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql');
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse($kitchenSink);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
$visited = [];
|
|
|
|
Visitor::visit($ast, [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function (Node $node, $key, $parent) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$r = ['enter', $node->kind, $key, $parent instanceof Node ? $parent->kind : null];
|
2015-07-15 20:05:46 +03:00
|
|
|
$visited[] = $r;
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function (Node $node, $key, $parent) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$r = ['leave', $node->kind, $key, $parent instanceof Node ? $parent->kind : null];
|
2015-07-15 20:05:46 +03:00
|
|
|
$visited[] = $r;
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
[ 'enter', 'Document', null, null ],
|
|
|
|
[ 'enter', 'OperationDefinition', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'OperationDefinition' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'OperationDefinition' ],
|
|
|
|
[ 'enter', 'VariableDefinition', 0, null ],
|
|
|
|
[ 'enter', 'Variable', 'variable', 'VariableDefinition' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Variable', 'variable', 'VariableDefinition' ],
|
2015-08-16 23:53:11 +03:00
|
|
|
[ 'enter', 'NamedType', 'type', 'VariableDefinition' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'NamedType', 'type', 'VariableDefinition' ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'leave', 'VariableDefinition', 0, null ],
|
|
|
|
[ 'enter', 'VariableDefinition', 1, null ],
|
|
|
|
[ 'enter', 'Variable', 'variable', 'VariableDefinition' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Variable', 'variable', 'VariableDefinition' ],
|
2015-08-16 23:53:11 +03:00
|
|
|
[ 'enter', 'NamedType', 'type', 'VariableDefinition' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'NamedType', 'type', 'VariableDefinition' ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'enter', 'EnumValue', 'defaultValue', 'VariableDefinition' ],
|
|
|
|
[ 'leave', 'EnumValue', 'defaultValue', 'VariableDefinition' ],
|
|
|
|
[ 'leave', 'VariableDefinition', 1, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'alias', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'alias', 'Field' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
2015-08-16 23:53:11 +03:00
|
|
|
[ 'enter', 'ListValue', 'value', 'Argument' ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'enter', 'IntValue', 0, null ],
|
|
|
|
[ 'leave', 'IntValue', 0, null ],
|
|
|
|
[ 'enter', 'IntValue', 1, null ],
|
|
|
|
[ 'leave', 'IntValue', 1, null ],
|
2015-08-16 23:53:11 +03:00
|
|
|
[ 'leave', 'ListValue', 'value', 'Argument' ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'leave', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'InlineFragment', 1, null ],
|
2015-08-16 23:53:11 +03:00
|
|
|
[ 'enter', 'NamedType', 'typeCondition', 'InlineFragment' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'NamedType', 'typeCondition', 'InlineFragment' ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'enter', 'Directive', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Directive' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Directive' ],
|
|
|
|
[ 'leave', 'Directive', 0, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Field', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'alias', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'alias', 'Field' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'IntValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'IntValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Argument', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 1, null ],
|
|
|
|
[ 'enter', 'Directive', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Directive' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Directive' ],
|
2015-08-16 23:53:11 +03:00
|
|
|
[ 'enter', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'Variable', 'value', 'Argument' ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
2015-08-16 23:53:11 +03:00
|
|
|
[ 'leave', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 0, null ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'leave', 'Directive', 0, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'FragmentSpread', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'FragmentSpread' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'FragmentSpread' ],
|
|
|
|
[ 'leave', 'FragmentSpread', 1, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 1, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
|
|
|
|
[ 'leave', 'InlineFragment', 1, null ],
|
2016-04-24 14:35:14 +03:00
|
|
|
[ 'enter', 'InlineFragment', 2, null ],
|
|
|
|
[ 'enter', 'Directive', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Directive' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Directive' ],
|
|
|
|
[ 'enter', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 0, null ],
|
|
|
|
[ 'leave', 'Directive', 0, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
|
|
|
|
[ 'leave', 'InlineFragment', 2, null ],
|
|
|
|
[ 'enter', 'InlineFragment', 3, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'InlineFragment' ],
|
|
|
|
[ 'leave', 'InlineFragment', 3, null ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
|
|
|
|
[ 'leave', 'OperationDefinition', 0, null ],
|
|
|
|
[ 'enter', 'OperationDefinition', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'OperationDefinition' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'OperationDefinition' ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'IntValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'IntValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Directive', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Directive' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Directive' ],
|
|
|
|
[ 'leave', 'Directive', 0, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
|
|
|
|
[ 'leave', 'OperationDefinition', 1, null ],
|
2016-04-24 14:35:14 +03:00
|
|
|
[ 'enter', 'OperationDefinition', 2, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'OperationDefinition' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'OperationDefinition' ],
|
|
|
|
[ 'enter', 'VariableDefinition', 0, null ],
|
|
|
|
[ 'enter', 'Variable', 'variable', 'VariableDefinition' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Variable', 'variable', 'VariableDefinition' ],
|
|
|
|
[ 'enter', 'NamedType', 'type', 'VariableDefinition' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'NamedType', 'type', 'VariableDefinition' ],
|
|
|
|
[ 'leave', 'VariableDefinition', 0, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Field', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 1, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
|
|
|
|
[ 'leave', 'OperationDefinition', 2, null ],
|
|
|
|
[ 'enter', 'FragmentDefinition', 3, null ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'enter', 'Name', 'name', 'FragmentDefinition' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'FragmentDefinition' ],
|
2015-08-16 23:53:11 +03:00
|
|
|
[ 'enter', 'NamedType', 'typeCondition', 'FragmentDefinition' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'NamedType' ],
|
|
|
|
[ 'leave', 'NamedType', 'typeCondition', 'FragmentDefinition' ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'FragmentDefinition' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Argument', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Variable' ],
|
|
|
|
[ 'leave', 'Variable', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 1, null ],
|
|
|
|
[ 'enter', 'Argument', 2, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'ObjectValue', 'value', 'Argument' ],
|
|
|
|
[ 'enter', 'ObjectField', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'ObjectField' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'ObjectField' ],
|
|
|
|
[ 'enter', 'StringValue', 'value', 'ObjectField' ],
|
|
|
|
[ 'leave', 'StringValue', 'value', 'ObjectField' ],
|
|
|
|
[ 'leave', 'ObjectField', 0, null ],
|
2018-02-08 16:58:08 +03:00
|
|
|
[ 'enter', 'ObjectField', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'ObjectField' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'ObjectField' ],
|
|
|
|
[ 'enter', 'StringValue', 'value', 'ObjectField' ],
|
|
|
|
[ 'leave', 'StringValue', 'value', 'ObjectField' ],
|
|
|
|
[ 'leave', 'ObjectField', 1, null ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'leave', 'ObjectValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 2, null ],
|
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'FragmentDefinition' ],
|
2016-04-24 14:35:14 +03:00
|
|
|
[ 'leave', 'FragmentDefinition', 3, null ],
|
|
|
|
[ 'enter', 'OperationDefinition', 4, null ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'enter', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
|
|
|
|
[ 'enter', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'enter', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'BooleanValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'BooleanValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 0, null ],
|
|
|
|
[ 'enter', 'Argument', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'BooleanValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'BooleanValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 1, null ],
|
2016-11-18 19:59:28 +03:00
|
|
|
[ 'enter', 'Argument', 2, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Argument' ],
|
|
|
|
[ 'enter', 'NullValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'NullValue', 'value', 'Argument' ],
|
|
|
|
[ 'leave', 'Argument', 2, null ],
|
2015-07-15 20:05:46 +03:00
|
|
|
[ 'leave', 'Field', 0, null ],
|
|
|
|
[ 'enter', 'Field', 1, null ],
|
|
|
|
[ 'enter', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Name', 'name', 'Field' ],
|
|
|
|
[ 'leave', 'Field', 1, null ],
|
|
|
|
[ 'leave', 'SelectionSet', 'selectionSet', 'OperationDefinition' ],
|
2016-04-24 14:35:14 +03:00
|
|
|
[ 'leave', 'OperationDefinition', 4, null ],
|
2018-08-13 11:36:03 +03:00
|
|
|
[ 'leave', 'Document', null, null ],
|
2015-07-15 20:05:46 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $visited);
|
|
|
|
}
|
2016-04-30 17:39:06 +03:00
|
|
|
|
|
|
|
/**
|
2018-08-13 11:36:03 +03:00
|
|
|
* Describe: visitInParallel
|
|
|
|
* Note: nearly identical to the above test of the same test but using visitInParallel.
|
2016-04-30 17:39:06 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSkippingSubTree() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
$ast = Parser::parse('{ a, b { x }, c }');
|
|
|
|
Visitor::visit($ast, Visitor::visitInParallel([
|
|
|
|
[
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = [ 'enter', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
|
|
|
|
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
|
|
|
|
return Visitor::skipNode();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $node->value ?? null];
|
|
|
|
},
|
|
|
|
],
|
2016-04-30 17:39:06 +03:00
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
[ 'enter', 'Document', null ],
|
|
|
|
[ 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Field', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'c' ],
|
|
|
|
[ 'leave', 'Name', 'c' ],
|
|
|
|
[ 'leave', 'Field', null ],
|
|
|
|
[ 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'leave', 'OperationDefinition', null ],
|
|
|
|
[ 'leave', 'Document', null ],
|
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsSkippingDifferentSubTrees() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
$ast = Parser::parse('{ a { x }, b { y} }');
|
|
|
|
Visitor::visit($ast, Visitor::visitInParallel([
|
|
|
|
[
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['no-a', 'enter', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'a') {
|
|
|
|
return Visitor::skipNode();
|
|
|
|
}
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = [ 'no-a', 'leave', $node->kind, $node->value ?? null ];
|
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
[
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['no-b', 'enter', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
|
|
|
|
return Visitor::skipNode();
|
|
|
|
}
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['no-b', 'leave', $node->kind, $node->value ?? null];
|
|
|
|
},
|
|
|
|
],
|
2016-04-30 17:39:06 +03:00
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
[ 'no-a', 'enter', 'Document', null ],
|
|
|
|
[ 'no-b', 'enter', 'Document', null ],
|
|
|
|
[ 'no-a', 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'no-b', 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'no-a', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'no-b', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'no-a', 'enter', 'Field', null ],
|
|
|
|
[ 'no-b', 'enter', 'Field', null ],
|
|
|
|
[ 'no-b', 'enter', 'Name', 'a' ],
|
|
|
|
[ 'no-b', 'leave', 'Name', 'a' ],
|
|
|
|
[ 'no-b', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'no-b', 'enter', 'Field', null ],
|
|
|
|
[ 'no-b', 'enter', 'Name', 'x' ],
|
|
|
|
[ 'no-b', 'leave', 'Name', 'x' ],
|
|
|
|
[ 'no-b', 'leave', 'Field', null ],
|
|
|
|
[ 'no-b', 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'no-b', 'leave', 'Field', null ],
|
|
|
|
[ 'no-a', 'enter', 'Field', null ],
|
|
|
|
[ 'no-b', 'enter', 'Field', null ],
|
|
|
|
[ 'no-a', 'enter', 'Name', 'b' ],
|
|
|
|
[ 'no-a', 'leave', 'Name', 'b' ],
|
|
|
|
[ 'no-a', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'no-a', 'enter', 'Field', null ],
|
|
|
|
[ 'no-a', 'enter', 'Name', 'y' ],
|
|
|
|
[ 'no-a', 'leave', 'Name', 'y' ],
|
|
|
|
[ 'no-a', 'leave', 'Field', null ],
|
|
|
|
[ 'no-a', 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'no-a', 'leave', 'Field', null ],
|
|
|
|
[ 'no-a', 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'no-b', 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'no-a', 'leave', 'OperationDefinition', null ],
|
|
|
|
[ 'no-b', 'leave', 'OperationDefinition', null ],
|
|
|
|
[ 'no-a', 'leave', 'Document', null ],
|
|
|
|
[ 'no-b', 'leave', 'Document', null ],
|
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsEarlyExitWhileVisiting2() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
$ast = Parser::parse('{ a, b { x }, c }');
|
|
|
|
Visitor::visit($ast, Visitor::visitInParallel([ [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$value = $node->value ?? null;
|
2016-04-30 17:39:06 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $value];
|
|
|
|
if ($node->kind === 'Name' && $value === 'x') {
|
|
|
|
return Visitor::stop();
|
|
|
|
}
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $node->value ?? null];
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]));
|
2016-04-30 17:39:06 +03:00
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
[ 'enter', 'Document', null ],
|
|
|
|
[ 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Field', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'b' ],
|
|
|
|
[ 'leave', 'Name', 'b' ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
2018-08-13 11:36:03 +03:00
|
|
|
[ 'enter', 'Name', 'x' ],
|
2016-04-30 17:39:06 +03:00
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsEarlyExitFromDifferentPoints() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
$ast = Parser::parse('{ a { y }, b { x } }');
|
|
|
|
Visitor::visit($ast, Visitor::visitInParallel([
|
|
|
|
[
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$value = $node->value ?? null;
|
2016-04-30 17:39:06 +03:00
|
|
|
$visited[] = ['break-a', 'enter', $node->kind, $value];
|
|
|
|
if ($node->kind === 'Name' && $value === 'a') {
|
|
|
|
return Visitor::stop();
|
|
|
|
}
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = [ 'break-a', 'leave', $node->kind, $node->value ?? null ];
|
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
[
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$value = $node->value ?? null;
|
2016-04-30 17:39:06 +03:00
|
|
|
$visited[] = ['break-b', 'enter', $node->kind, $value];
|
|
|
|
if ($node->kind === 'Name' && $value === 'b') {
|
|
|
|
return Visitor::stop();
|
|
|
|
}
|
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['break-b', 'leave', $node->kind, $node->value ?? null];
|
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
[ 'break-a', 'enter', 'Document', null ],
|
|
|
|
[ 'break-b', 'enter', 'Document', null ],
|
|
|
|
[ 'break-a', 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'break-b', 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'break-a', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'break-b', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'break-a', 'enter', 'Field', null ],
|
|
|
|
[ 'break-b', 'enter', 'Field', null ],
|
|
|
|
[ 'break-a', 'enter', 'Name', 'a' ],
|
|
|
|
[ 'break-b', 'enter', 'Name', 'a' ],
|
|
|
|
[ 'break-b', 'leave', 'Name', 'a' ],
|
|
|
|
[ 'break-b', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'break-b', 'enter', 'Field', null ],
|
|
|
|
[ 'break-b', 'enter', 'Name', 'y' ],
|
|
|
|
[ 'break-b', 'leave', 'Name', 'y' ],
|
|
|
|
[ 'break-b', 'leave', 'Field', null ],
|
|
|
|
[ 'break-b', 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'break-b', 'leave', 'Field', null ],
|
|
|
|
[ 'break-b', 'enter', 'Field', null ],
|
2018-08-13 11:36:03 +03:00
|
|
|
[ 'break-b', 'enter', 'Name', 'b' ],
|
2016-04-30 17:39:06 +03:00
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsEarlyExitWhileLeaving2() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
$ast = Parser::parse('{ a, b { x }, c }');
|
|
|
|
Visitor::visit($ast, Visitor::visitInParallel([ [
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$value = $node->value ?? null;
|
2016-04-30 17:39:06 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $value];
|
|
|
|
if ($node->kind === 'Name' && $value === 'x') {
|
|
|
|
return Visitor::stop();
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
]));
|
2016-04-30 17:39:06 +03:00
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
[ 'enter', 'Document', null ],
|
|
|
|
[ 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Name', 'a' ],
|
|
|
|
[ 'leave', 'Field', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'b' ],
|
|
|
|
[ 'leave', 'Name', 'b' ],
|
|
|
|
[ 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'enter', 'Field', null ],
|
|
|
|
[ 'enter', 'Name', 'x' ],
|
2018-08-13 11:36:03 +03:00
|
|
|
[ 'leave', 'Name', 'x' ],
|
2016-04-30 17:39:06 +03:00
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsEarlyExitFromLeavingDifferentPoints() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
|
|
|
$ast = Parser::parse('{ a { y }, b { x } }');
|
|
|
|
Visitor::visit($ast, Visitor::visitInParallel([
|
|
|
|
[
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['break-a', 'enter', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['break-a', 'leave', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'a') {
|
|
|
|
return Visitor::stop();
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
[
|
2018-08-13 11:36:03 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['break-b', 'enter', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
},
|
2018-08-13 11:36:03 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
2018-02-16 00:44:17 +03:00
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['break-b', 'leave', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
|
|
|
|
return Visitor::stop();
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
[ 'break-a', 'enter', 'Document', null ],
|
|
|
|
[ 'break-b', 'enter', 'Document', null ],
|
|
|
|
[ 'break-a', 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'break-b', 'enter', 'OperationDefinition', null ],
|
|
|
|
[ 'break-a', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'break-b', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'break-a', 'enter', 'Field', null ],
|
|
|
|
[ 'break-b', 'enter', 'Field', null ],
|
|
|
|
[ 'break-a', 'enter', 'Name', 'a' ],
|
|
|
|
[ 'break-b', 'enter', 'Name', 'a' ],
|
|
|
|
[ 'break-a', 'leave', 'Name', 'a' ],
|
|
|
|
[ 'break-b', 'leave', 'Name', 'a' ],
|
|
|
|
[ 'break-a', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'break-b', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'break-a', 'enter', 'Field', null ],
|
|
|
|
[ 'break-b', 'enter', 'Field', null ],
|
|
|
|
[ 'break-a', 'enter', 'Name', 'y' ],
|
|
|
|
[ 'break-b', 'enter', 'Name', 'y' ],
|
|
|
|
[ 'break-a', 'leave', 'Name', 'y' ],
|
|
|
|
[ 'break-b', 'leave', 'Name', 'y' ],
|
|
|
|
[ 'break-a', 'leave', 'Field', null ],
|
|
|
|
[ 'break-b', 'leave', 'Field', null ],
|
|
|
|
[ 'break-a', 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'break-b', 'leave', 'SelectionSet', null ],
|
|
|
|
[ 'break-a', 'leave', 'Field', null ],
|
|
|
|
[ 'break-b', 'leave', 'Field', null ],
|
|
|
|
[ 'break-b', 'enter', 'Field', null ],
|
|
|
|
[ 'break-b', 'enter', 'Name', 'b' ],
|
|
|
|
[ 'break-b', 'leave', 'Name', 'b' ],
|
|
|
|
[ 'break-b', 'enter', 'SelectionSet', null ],
|
|
|
|
[ 'break-b', 'enter', 'Field', null ],
|
|
|
|
[ 'break-b', 'enter', 'Name', 'x' ],
|
|
|
|
[ 'break-b', 'leave', 'Name', 'x' ],
|
|
|
|
[ 'break-b', 'leave', 'Field', null ],
|
|
|
|
[ 'break-b', 'leave', 'SelectionSet', null ],
|
2018-08-13 11:36:03 +03:00
|
|
|
[ 'break-b', 'leave', 'Field', null ],
|
2016-04-30 17:39:06 +03:00
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsForEditingOnEnter2() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]);
|
2016-04-30 17:39:06 +03:00
|
|
|
$editedAst = Visitor::visit($ast, Visitor::visitInParallel([
|
|
|
|
[
|
2018-02-16 00:44:17 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2016-04-30 17:39:06 +03:00
|
|
|
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
|
|
|
|
return Visitor::removeNode();
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
[
|
2018-02-16 00:44:17 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
},
|
2018-02-16 00:44:17 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $node->value ?? null];
|
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]),
|
|
|
|
$ast
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
Parser::parse('{ a, c { a, c } }', ['noLocation' => true]),
|
|
|
|
$editedAst
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
['enter', 'Document', null],
|
|
|
|
['enter', 'OperationDefinition', null],
|
|
|
|
['enter', 'SelectionSet', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'a'],
|
|
|
|
['leave', 'Name', 'a'],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'c'],
|
|
|
|
['leave', 'Name', 'c'],
|
|
|
|
['enter', 'SelectionSet', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'a'],
|
|
|
|
['leave', 'Name', 'a'],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'c'],
|
|
|
|
['leave', 'Name', 'c'],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['leave', 'SelectionSet', null],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['leave', 'SelectionSet', null],
|
|
|
|
['leave', 'OperationDefinition', null],
|
2018-08-13 11:36:03 +03:00
|
|
|
['leave', 'Document', null],
|
2016-04-30 17:39:06 +03:00
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsForEditingOnLeave2() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]);
|
2016-04-30 17:39:06 +03:00
|
|
|
$editedAst = Visitor::visit($ast, Visitor::visitInParallel([
|
|
|
|
[
|
2018-02-16 00:44:17 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2016-04-30 17:39:06 +03:00
|
|
|
if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') {
|
|
|
|
return Visitor::removeNode();
|
|
|
|
}
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
[
|
2018-02-16 00:44:17 +03:00
|
|
|
'enter' => function ($node) use (&$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['enter', $node->kind, $node->value ?? null];
|
2016-04-30 17:39:06 +03:00
|
|
|
},
|
2018-02-16 00:44:17 +03:00
|
|
|
'leave' => function ($node) use (&$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited[] = ['leave', $node->kind, $node->value ?? null];
|
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]),
|
|
|
|
$ast
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
Parser::parse('{ a, c { a, c } }', ['noLocation' => true]),
|
|
|
|
$editedAst
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
['enter', 'Document', null],
|
|
|
|
['enter', 'OperationDefinition', null],
|
|
|
|
['enter', 'SelectionSet', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'a'],
|
|
|
|
['leave', 'Name', 'a'],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'b'],
|
|
|
|
['leave', 'Name', 'b'],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'c'],
|
|
|
|
['leave', 'Name', 'c'],
|
|
|
|
['enter', 'SelectionSet', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'a'],
|
|
|
|
['leave', 'Name', 'a'],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'b'],
|
|
|
|
['leave', 'Name', 'b'],
|
|
|
|
['enter', 'Field', null],
|
|
|
|
['enter', 'Name', 'c'],
|
|
|
|
['leave', 'Name', 'c'],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['leave', 'SelectionSet', null],
|
|
|
|
['leave', 'Field', null],
|
|
|
|
['leave', 'SelectionSet', null],
|
|
|
|
['leave', 'OperationDefinition', null],
|
2018-08-13 11:36:03 +03:00
|
|
|
['leave', 'Document', null],
|
2016-04-30 17:39:06 +03:00
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-08-13 11:36:03 +03:00
|
|
|
* Describe: visitWithTypeInfo
|
2016-04-30 17:39:06 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMaintainsTypeInfoDuringVisit() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
|
|
|
$visited = [];
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
$typeInfo = new TypeInfo(ValidatorTestCase::getTestSchema());
|
2016-04-30 17:39:06 +03:00
|
|
|
|
2018-02-08 17:13:31 +03:00
|
|
|
$ast = Parser::parse('{ human(id: 4) { name, pets { ... { name } }, unknown } }');
|
2016-04-30 17:39:06 +03:00
|
|
|
Visitor::visit($ast, Visitor::visitWithTypeInfo($typeInfo, [
|
2018-02-16 00:44:17 +03:00
|
|
|
'enter' => function ($node) use ($typeInfo, &$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2016-04-30 17:39:06 +03:00
|
|
|
$parentType = $typeInfo->getParentType();
|
2018-08-13 11:36:03 +03:00
|
|
|
$type = $typeInfo->getType();
|
|
|
|
$inputType = $typeInfo->getInputType();
|
|
|
|
$visited[] = [
|
2016-04-30 17:39:06 +03:00
|
|
|
'enter',
|
|
|
|
$node->kind,
|
|
|
|
$node->kind === 'Name' ? $node->value : null,
|
2018-08-13 11:36:03 +03:00
|
|
|
$parentType ? (string) $parentType : null,
|
|
|
|
$type ? (string) $type : null,
|
|
|
|
$inputType ? (string) $inputType : null,
|
2016-04-30 17:39:06 +03:00
|
|
|
];
|
|
|
|
},
|
2018-02-16 00:44:17 +03:00
|
|
|
'leave' => function ($node) use ($typeInfo, &$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args());
|
2016-04-30 17:39:06 +03:00
|
|
|
$parentType = $typeInfo->getParentType();
|
2018-08-13 11:36:03 +03:00
|
|
|
$type = $typeInfo->getType();
|
|
|
|
$inputType = $typeInfo->getInputType();
|
|
|
|
$visited[] = [
|
2016-04-30 17:39:06 +03:00
|
|
|
'leave',
|
|
|
|
$node->kind,
|
|
|
|
$node->kind === 'Name' ? $node->value : null,
|
2018-08-13 11:36:03 +03:00
|
|
|
$parentType ? (string) $parentType : null,
|
|
|
|
$type ? (string) $type : null,
|
|
|
|
$inputType ? (string) $inputType : null,
|
2016-04-30 17:39:06 +03:00
|
|
|
];
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
['enter', 'Document', null, null, null, null],
|
|
|
|
['enter', 'OperationDefinition', null, null, 'QueryRoot', null],
|
|
|
|
['enter', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null],
|
|
|
|
['enter', 'Field', null, 'QueryRoot', 'Human', null],
|
|
|
|
['enter', 'Name', 'human', 'QueryRoot', 'Human', null],
|
|
|
|
['leave', 'Name', 'human', 'QueryRoot', 'Human', null],
|
|
|
|
['enter', 'Argument', null, 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['enter', 'Name', 'id', 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['leave', 'Name', 'id', 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['enter', 'IntValue', null, 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['leave', 'IntValue', null, 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['leave', 'Argument', null, 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['enter', 'SelectionSet', null, 'Human', 'Human', null],
|
|
|
|
['enter', 'Field', null, 'Human', 'String', null],
|
|
|
|
['enter', 'Name', 'name', 'Human', 'String', null],
|
|
|
|
['leave', 'Name', 'name', 'Human', 'String', null],
|
|
|
|
['leave', 'Field', null, 'Human', 'String', null],
|
|
|
|
['enter', 'Field', null, 'Human', '[Pet]', null],
|
|
|
|
['enter', 'Name', 'pets', 'Human', '[Pet]', null],
|
|
|
|
['leave', 'Name', 'pets', 'Human', '[Pet]', null],
|
|
|
|
['enter', 'SelectionSet', null, 'Pet', '[Pet]', null],
|
2018-02-08 17:13:31 +03:00
|
|
|
['enter', 'InlineFragment', null, 'Pet', 'Pet', null],
|
|
|
|
['enter', 'SelectionSet', null, 'Pet', 'Pet', null],
|
2016-04-30 17:39:06 +03:00
|
|
|
['enter', 'Field', null, 'Pet', 'String', null],
|
|
|
|
['enter', 'Name', 'name', 'Pet', 'String', null],
|
|
|
|
['leave', 'Name', 'name', 'Pet', 'String', null],
|
|
|
|
['leave', 'Field', null, 'Pet', 'String', null],
|
2018-02-08 17:13:31 +03:00
|
|
|
['leave', 'SelectionSet', null, 'Pet', 'Pet', null],
|
|
|
|
['leave', 'InlineFragment', null, 'Pet', 'Pet', null],
|
2016-04-30 17:39:06 +03:00
|
|
|
['leave', 'SelectionSet', null, 'Pet', '[Pet]', null],
|
|
|
|
['leave', 'Field', null, 'Human', '[Pet]', null],
|
|
|
|
['enter', 'Field', null, 'Human', null, null],
|
|
|
|
['enter', 'Name', 'unknown', 'Human', null, null],
|
|
|
|
['leave', 'Name', 'unknown', 'Human', null, null],
|
|
|
|
['leave', 'Field', null, 'Human', null, null],
|
|
|
|
['leave', 'SelectionSet', null, 'Human', 'Human', null],
|
|
|
|
['leave', 'Field', null, 'QueryRoot', 'Human', null],
|
|
|
|
['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null],
|
|
|
|
['leave', 'OperationDefinition', null, null, 'QueryRoot', null],
|
2018-08-13 11:36:03 +03:00
|
|
|
['leave', 'Document', null, null, null, null],
|
2016-04-30 17:39:06 +03:00
|
|
|
], $visited);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMaintainsTypeInfoDuringEdit() : void
|
2016-04-30 17:39:06 +03:00
|
|
|
{
|
2018-08-13 11:36:03 +03:00
|
|
|
$visited = [];
|
2018-07-29 18:43:10 +03:00
|
|
|
$typeInfo = new TypeInfo(ValidatorTestCase::getTestSchema());
|
2016-04-30 17:39:06 +03:00
|
|
|
|
2018-08-13 11:36:03 +03:00
|
|
|
$ast = Parser::parse(
|
2016-04-30 17:39:06 +03:00
|
|
|
'{ human(id: 4) { name, pets }, alien }'
|
|
|
|
);
|
|
|
|
$editedAst = Visitor::visit($ast, Visitor::visitWithTypeInfo($typeInfo, [
|
2018-02-16 00:44:17 +03:00
|
|
|
'enter' => function ($node) use ($typeInfo, &$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2016-04-30 17:39:06 +03:00
|
|
|
$parentType = $typeInfo->getParentType();
|
2018-08-13 11:36:03 +03:00
|
|
|
$type = $typeInfo->getType();
|
|
|
|
$inputType = $typeInfo->getInputType();
|
|
|
|
$visited[] = [
|
2016-04-30 17:39:06 +03:00
|
|
|
'enter',
|
|
|
|
$node->kind,
|
|
|
|
$node->kind === 'Name' ? $node->value : null,
|
2018-08-13 11:36:03 +03:00
|
|
|
$parentType ? (string) $parentType : null,
|
|
|
|
$type ? (string) $type : null,
|
|
|
|
$inputType ? (string) $inputType : null,
|
2016-04-30 17:39:06 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
// Make a query valid by adding missing selection sets.
|
2018-08-13 11:36:03 +03:00
|
|
|
if ($node->kind === 'Field' &&
|
|
|
|
! $node->selectionSet &&
|
2016-04-30 17:39:06 +03:00
|
|
|
Type::isCompositeType(Type::getNamedType($type))
|
|
|
|
) {
|
2016-11-19 02:12:18 +03:00
|
|
|
return new FieldNode([
|
2016-04-30 17:39:06 +03:00
|
|
|
'alias' => $node->alias,
|
|
|
|
'name' => $node->name,
|
|
|
|
'arguments' => $node->arguments,
|
|
|
|
'directives' => $node->directives,
|
2016-11-19 02:12:18 +03:00
|
|
|
'selectionSet' => new SelectionSetNode([
|
2016-04-30 17:39:06 +03:00
|
|
|
'kind' => 'SelectionSet',
|
2018-08-13 11:36:03 +03:00
|
|
|
'selections' => [new FieldNode([
|
|
|
|
'name' => new NameNode(['value' => '__typename']),
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
]),
|
2016-04-30 17:39:06 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
},
|
2018-02-16 00:44:17 +03:00
|
|
|
'leave' => function ($node) use ($typeInfo, &$visited, $ast) {
|
|
|
|
$this->checkVisitorFnArgs($ast, func_get_args(), true);
|
2016-04-30 17:39:06 +03:00
|
|
|
$parentType = $typeInfo->getParentType();
|
2018-08-13 11:36:03 +03:00
|
|
|
$type = $typeInfo->getType();
|
|
|
|
$inputType = $typeInfo->getInputType();
|
|
|
|
$visited[] = [
|
2016-04-30 17:39:06 +03:00
|
|
|
'leave',
|
|
|
|
$node->kind,
|
|
|
|
$node->kind === 'Name' ? $node->value : null,
|
2018-08-13 11:36:03 +03:00
|
|
|
$parentType ? (string) $parentType : null,
|
|
|
|
$type ? (string) $type : null,
|
|
|
|
$inputType ? (string) $inputType : null,
|
2016-04-30 17:39:06 +03:00
|
|
|
];
|
2018-08-13 11:36:03 +03:00
|
|
|
},
|
2016-04-30 17:39:06 +03:00
|
|
|
]));
|
|
|
|
|
|
|
|
$this->assertEquals(Printer::doPrint(Parser::parse(
|
|
|
|
'{ human(id: 4) { name, pets }, alien }'
|
|
|
|
)), Printer::doPrint($ast));
|
|
|
|
|
|
|
|
$this->assertEquals(Printer::doPrint(Parser::parse(
|
|
|
|
'{ human(id: 4) { name, pets { __typename } }, alien { __typename } }'
|
|
|
|
)), Printer::doPrint($editedAst));
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
['enter', 'Document', null, null, null, null],
|
|
|
|
['enter', 'OperationDefinition', null, null, 'QueryRoot', null],
|
|
|
|
['enter', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null],
|
|
|
|
['enter', 'Field', null, 'QueryRoot', 'Human', null],
|
|
|
|
['enter', 'Name', 'human', 'QueryRoot', 'Human', null],
|
|
|
|
['leave', 'Name', 'human', 'QueryRoot', 'Human', null],
|
|
|
|
['enter', 'Argument', null, 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['enter', 'Name', 'id', 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['leave', 'Name', 'id', 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['enter', 'IntValue', null, 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['leave', 'IntValue', null, 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['leave', 'Argument', null, 'QueryRoot', 'Human', 'ID'],
|
|
|
|
['enter', 'SelectionSet', null, 'Human', 'Human', null],
|
|
|
|
['enter', 'Field', null, 'Human', 'String', null],
|
|
|
|
['enter', 'Name', 'name', 'Human', 'String', null],
|
|
|
|
['leave', 'Name', 'name', 'Human', 'String', null],
|
|
|
|
['leave', 'Field', null, 'Human', 'String', null],
|
|
|
|
['enter', 'Field', null, 'Human', '[Pet]', null],
|
|
|
|
['enter', 'Name', 'pets', 'Human', '[Pet]', null],
|
|
|
|
['leave', 'Name', 'pets', 'Human', '[Pet]', null],
|
|
|
|
['enter', 'SelectionSet', null, 'Pet', '[Pet]', null],
|
|
|
|
['enter', 'Field', null, 'Pet', 'String!', null],
|
|
|
|
['enter', 'Name', '__typename', 'Pet', 'String!', null],
|
|
|
|
['leave', 'Name', '__typename', 'Pet', 'String!', null],
|
|
|
|
['leave', 'Field', null, 'Pet', 'String!', null],
|
|
|
|
['leave', 'SelectionSet', null, 'Pet', '[Pet]', null],
|
|
|
|
['leave', 'Field', null, 'Human', '[Pet]', null],
|
|
|
|
['leave', 'SelectionSet', null, 'Human', 'Human', null],
|
|
|
|
['leave', 'Field', null, 'QueryRoot', 'Human', null],
|
|
|
|
['enter', 'Field', null, 'QueryRoot', 'Alien', null],
|
|
|
|
['enter', 'Name', 'alien', 'QueryRoot', 'Alien', null],
|
|
|
|
['leave', 'Name', 'alien', 'QueryRoot', 'Alien', null],
|
|
|
|
['enter', 'SelectionSet', null, 'Alien', 'Alien', null],
|
|
|
|
['enter', 'Field', null, 'Alien', 'String!', null],
|
|
|
|
['enter', 'Name', '__typename', 'Alien', 'String!', null],
|
|
|
|
['leave', 'Name', '__typename', 'Alien', 'String!', null],
|
|
|
|
['leave', 'Field', null, 'Alien', 'String!', null],
|
|
|
|
['leave', 'SelectionSet', null, 'Alien', 'Alien', null],
|
|
|
|
['leave', 'Field', null, 'QueryRoot', 'Alien', null],
|
|
|
|
['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null],
|
|
|
|
['leave', 'OperationDefinition', null, null, 'QueryRoot', null],
|
2018-08-13 11:36:03 +03:00
|
|
|
['leave', 'Document', null, null, null, null],
|
2016-04-30 17:39:06 +03:00
|
|
|
], $visited);
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|