From 5d889ccacdb7fbe45d61a0d0f5413049526416f9 Mon Sep 17 00:00:00 2001 From: vladar Date: Wed, 16 Nov 2016 18:37:35 +0700 Subject: [PATCH] Reverted several breaking changes from #75; tests are green --- src/Language/Printer.php | 12 +++++++- src/Utils/TypeInfo.php | 41 +++++++++++++------------- src/Validator/Rules/KnownTypeNames.php | 4 +-- src/Validator/Rules/QueryDepth.php | 1 + tests/Language/PrinterTest.php | 23 +++++---------- tests/Language/VisitorTest.php | 11 +++---- tests/Validator/TestCase.php | 6 ++-- 7 files changed, 50 insertions(+), 48 deletions(-) diff --git a/src/Language/Printer.php b/src/Language/Printer.php index a3ace52..b42d88b 100644 --- a/src/Language/Printer.php +++ b/src/Language/Printer.php @@ -41,7 +41,17 @@ use GraphQL\Language\AST\VariableDefinition; class Printer { - public function doPrint($ast) + public static function doPrint($ast) + { + static $instance; + $instance = $instance ?: new static(); + return $instance->printAST($ast); + } + + protected function __construct() + {} + + public function printAST($ast) { return Visitor::visit($ast, [ 'leave' => [ diff --git a/src/Utils/TypeInfo.php b/src/Utils/TypeInfo.php index 94f4c2c..1eb4d29 100644 --- a/src/Utils/TypeInfo.php +++ b/src/Utils/TypeInfo.php @@ -5,6 +5,7 @@ use GraphQL\Language\AST\Field; use GraphQL\Language\AST\ListType; use GraphQL\Language\AST\NamedType; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\NonNullType; use GraphQL\Schema; use GraphQL\Type\Definition\AbstractType; @@ -309,7 +310,7 @@ class TypeInfo $schema = $this->schema; switch ($node->kind) { - case Node::SELECTION_SET: + case NodeType::SELECTION_SET: $namedType = Type::getNamedType($this->getType()); $compositeType = null; if (Type::isCompositeType($namedType)) { @@ -319,7 +320,7 @@ class TypeInfo $this->parentTypeStack[] = $compositeType; // push break; - case Node::FIELD: + case NodeType::FIELD: $parentType = $this->getParentType(); $fieldDef = null; if ($parentType) { @@ -329,11 +330,11 @@ class TypeInfo $this->typeStack[] = $fieldDef ? $fieldDef->getType() : null; // push break; - case Node::DIRECTIVE: + case NodeType::DIRECTIVE: $this->directive = $schema->getDirective($node->name->value); break; - case Node::OPERATION_DEFINITION: + case NodeType::OPERATION_DEFINITION: $type = null; if ($node->operation === 'query') { $type = $schema->getQueryType(); @@ -345,19 +346,19 @@ class TypeInfo $this->typeStack[] = $type; // push break; - case Node::INLINE_FRAGMENT: - case Node::FRAGMENT_DEFINITION: + case NodeType::INLINE_FRAGMENT: + case NodeType::FRAGMENT_DEFINITION: $typeConditionAST = $node->typeCondition; $outputType = $typeConditionAST ? self::typeFromAST($schema, $typeConditionAST) : $this->getType(); $this->typeStack[] = $outputType; // push break; - case Node::VARIABLE_DEFINITION: + case NodeType::VARIABLE_DEFINITION: $inputType = self::typeFromAST($schema, $node->type); $this->inputTypeStack[] = $inputType; // push break; - case Node::ARGUMENT: + case NodeType::ARGUMENT: $fieldOrDirective = $this->getDirective() ?: $this->getFieldDef(); $argDef = $argType = null; if ($fieldOrDirective) { @@ -370,12 +371,12 @@ class TypeInfo $this->inputTypeStack[] = $argType; // push break; - case Node::LST: + case NodeType::LST: $listType = Type::getNullableType($this->getInputType()); $this->inputTypeStack[] = ($listType instanceof ListOfType ? $listType->getWrappedType() : null); // push break; - case Node::OBJECT_FIELD: + case NodeType::OBJECT_FIELD: $objectType = Type::getNamedType($this->getInputType()); $fieldType = null; if ($objectType instanceof InputObjectType) { @@ -394,33 +395,33 @@ class TypeInfo function leave(Node $node) { switch ($node->kind) { - case Node::SELECTION_SET: + case NodeType::SELECTION_SET: array_pop($this->parentTypeStack); break; - case Node::FIELD: + case NodeType::FIELD: array_pop($this->fieldDefStack); array_pop($this->typeStack); break; - case Node::DIRECTIVE: + case NodeType::DIRECTIVE: $this->directive = null; break; - case Node::OPERATION_DEFINITION: - case Node::INLINE_FRAGMENT: - case Node::FRAGMENT_DEFINITION: + case NodeType::OPERATION_DEFINITION: + case NodeType::INLINE_FRAGMENT: + case NodeType::FRAGMENT_DEFINITION: array_pop($this->typeStack); break; - case Node::VARIABLE_DEFINITION: + case NodeType::VARIABLE_DEFINITION: array_pop($this->inputTypeStack); break; - case Node::ARGUMENT: + case NodeType::ARGUMENT: $this->argument = null; array_pop($this->inputTypeStack); break; - case Node::LST: - case Node::OBJECT_FIELD: + case NodeType::LST: + case NodeType::OBJECT_FIELD: array_pop($this->inputTypeStack); break; } diff --git a/src/Validator/Rules/KnownTypeNames.php b/src/Validator/Rules/KnownTypeNames.php index 7683ae5..db70c50 100644 --- a/src/Validator/Rules/KnownTypeNames.php +++ b/src/Validator/Rules/KnownTypeNames.php @@ -3,11 +3,9 @@ namespace GraphQL\Validator\Rules; use GraphQL\Error\Error; -use GraphQL\Language\AST\Name; use GraphQL\Language\AST\NamedType; -use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\Visitor; -use GraphQL\Validator\Messages; use GraphQL\Validator\ValidationContext; class KnownTypeNames diff --git a/src/Validator/Rules/QueryDepth.php b/src/Validator/Rules/QueryDepth.php index d3f4ec3..859ec7b 100644 --- a/src/Validator/Rules/QueryDepth.php +++ b/src/Validator/Rules/QueryDepth.php @@ -6,6 +6,7 @@ use GraphQL\Language\AST\Field; use GraphQL\Language\AST\FragmentSpread; use GraphQL\Language\AST\InlineFragment; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\SelectionSet; use GraphQL\Validator\ValidationContext; diff --git a/tests/Language/PrinterTest.php b/tests/Language/PrinterTest.php index ef6d628..a73d55e 100644 --- a/tests/Language/PrinterTest.php +++ b/tests/Language/PrinterTest.php @@ -26,8 +26,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase $astCopy = $ast->cloneDeep(); $this->assertEquals($astCopy, $ast); - $printer = new Printer(); - $printer->doPrint($ast); + Printer::doPrint($ast); $this->assertEquals($astCopy, $ast); } @@ -36,10 +35,8 @@ class PrinterTest extends \PHPUnit_Framework_TestCase */ public function testPrintsMinimalAst() { - $printer = new Printer(); - $ast = new Field(['name' => new Name(['value' => 'foo'])]); - $this->assertEquals('foo', $printer->doPrint($ast)); + $this->assertEquals('foo', Printer::doPrint($ast)); } /** @@ -49,8 +46,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase { $badAst1 = new \ArrayObject(array('random' => 'Data')); try { - $printer = new Printer(); - $printer->doPrint($badAst1); + Printer::doPrint($badAst1); $this->fail('Expected exception not thrown'); } catch (\Exception $e) { $this->assertEquals('Invalid AST Node: {"random":"Data"}', $e->getMessage()); @@ -64,14 +60,12 @@ class PrinterTest extends \PHPUnit_Framework_TestCase { $queryAstShorthanded = Parser::parse('query { id, name }'); - $printer = new Printer(); - $expected = '{ id name } '; - $this->assertEquals($expected, $printer->doPrint($queryAstShorthanded)); + $this->assertEquals($expected, Printer::doPrint($queryAstShorthanded)); $mutationAst = Parser::parse('mutation { id, name }'); $expected = 'mutation { @@ -79,7 +73,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase name } '; - $this->assertEquals($expected, $printer->doPrint($mutationAst)); + $this->assertEquals($expected, Printer::doPrint($mutationAst)); $queryAstWithArtifacts = Parser::parse( 'query ($foo: TestType) @testDirective { id, name }' @@ -89,7 +83,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase name } '; - $this->assertEquals($expected, $printer->doPrint($queryAstWithArtifacts)); + $this->assertEquals($expected, Printer::doPrint($queryAstWithArtifacts)); $mutationAstWithArtifacts = Parser::parse( 'mutation ($foo: TestType) @testDirective { id, name }' @@ -99,7 +93,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase name } '; - $this->assertEquals($expected, $printer->doPrint($mutationAstWithArtifacts)); + $this->assertEquals($expected, Printer::doPrint($mutationAstWithArtifacts)); } /** @@ -110,8 +104,7 @@ class PrinterTest extends \PHPUnit_Framework_TestCase $kitchenSink = file_get_contents(__DIR__ . '/kitchen-sink.graphql'); $ast = Parser::parse($kitchenSink); - $printer = new Printer(); - $printed = $printer->doPrint($ast); + $printed = Printer::doPrint($ast); $expected = <<<'EOT' query queryName($foo: ComplexType, $site: Site = MOBILE) { diff --git a/tests/Language/VisitorTest.php b/tests/Language/VisitorTest.php index 1ad9d59..2710702 100644 --- a/tests/Language/VisitorTest.php +++ b/tests/Language/VisitorTest.php @@ -5,6 +5,7 @@ use GraphQL\Language\AST\Document; use GraphQL\Language\AST\Field; use GraphQL\Language\AST\Name; use GraphQL\Language\AST\Node; +use GraphQL\Language\AST\NodeType; use GraphQL\Language\AST\OperationDefinition; use GraphQL\Language\AST\SelectionSet; use GraphQL\Language\Parser; @@ -25,7 +26,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase $selectionSet = null; $editedAst = Visitor::visit($ast, [ - Node::OPERATION_DEFINITION => [ + NodeType::OPERATION_DEFINITION => [ 'enter' => function(OperationDefinition $node) use (&$selectionSet) { $selectionSet = $node->selectionSet; @@ -63,7 +64,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase $definitions = $ast->definitions; $editedAst = Visitor::visit($ast, [ - Node::DOCUMENT => [ + NodeType::DOCUMENT => [ 'enter' => function (Document $node) { $tmp = clone $node; $tmp->definitions = []; @@ -264,7 +265,7 @@ class VisitorTest extends \PHPUnit_Framework_TestCase 'leave' => function($node) use (&$visited) { $visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; - if ($node->kind === Node::NAME && $node->value === 'x') { + if ($node->kind === NodeType::NAME && $node->value === 'x') { return Visitor::stop(); } } @@ -297,10 +298,10 @@ class VisitorTest extends \PHPUnit_Framework_TestCase $ast = Parser::parse('{ a, b { x }, c }'); Visitor::visit($ast, [ - Node::NAME => function(Name $node) use (&$visited) { + NodeType::NAME => function(Name $node) use (&$visited) { $visited[] = ['enter', $node->kind, $node->value]; }, - Node::SELECTION_SET => [ + NodeType::SELECTION_SET => [ 'enter' => function(SelectionSet $node) use (&$visited) { $visited[] = ['enter', $node->kind, null]; }, diff --git a/tests/Validator/TestCase.php b/tests/Validator/TestCase.php index 994c131..d2af941 100644 --- a/tests/Validator/TestCase.php +++ b/tests/Validator/TestCase.php @@ -309,18 +309,16 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase function expectValid($schema, $rules, $queryString) { - $parser = new Parser(new Lexer()); $this->assertEquals( [], - DocumentValidator::validate($schema, $parser->parse($queryString), $rules), + DocumentValidator::validate($schema, Parser::parse($queryString), $rules), 'Should validate' ); } function expectInvalid($schema, $rules, $queryString, $expectedErrors) { - $parser = new Parser(new Lexer()); - $errors = DocumentValidator::validate($schema, $parser->parse($queryString), $rules); + $errors = DocumentValidator::validate($schema, Parser::parse($queryString), $rules); $this->assertNotEmpty($errors, 'GraphQL should not validate'); $this->assertEquals($expectedErrors, array_map(['GraphQL\Error\Error', 'formatError'], $errors));