From fcb9c24bb51113dc37489c34e60a4ce18c0f0a34 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Wed, 8 Aug 2018 14:57:44 +0700 Subject: [PATCH] Fix astFromValue to correctly handle integers and strings --- src/Utils/AST.php | 4 +- tests/Utils/AstFromValueTest.php | 9 ++- tests/Utils/SchemaPrinterTest.php | 130 +++++++++++++++++++++++++----- 3 files changed, 119 insertions(+), 24 deletions(-) diff --git a/src/Utils/AST.php b/src/Utils/AST.php index fc46d12..ada3022 100644 --- a/src/Utils/AST.php +++ b/src/Utils/AST.php @@ -39,6 +39,8 @@ use GraphQL\Type\Schema; */ class AST { + public static $integerStringRegExp = '/^-?(0|[1-9][0-9]*)$/'; + /** * Convert representation of AST as an associative array to instance of GraphQL\Language\AST\Node. * @@ -250,7 +252,7 @@ class AST // Use json_encode, which uses the same string encoding as GraphQL, // then remove the quotes. return new StringValueNode([ - 'value' => substr(json_encode($serialized), 1, -1) + 'value' => $serialized ]); } diff --git a/tests/Utils/AstFromValueTest.php b/tests/Utils/AstFromValueTest.php index 952bba4..16df49e 100644 --- a/tests/Utils/AstFromValueTest.php +++ b/tests/Utils/AstFromValueTest.php @@ -40,6 +40,7 @@ class AstFromValueTest extends TestCase */ public function testConvertsIntValuesToASTs() { + $this->assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::int())); $this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::int())); $this->assertEquals(new IntValueNode(['value' => '10000']), AST::astFromValue(1e4, Type::int())); $this->assertEquals(new IntValueNode(['value' => '0']), AST::astFromValue(0e4, Type::int())); @@ -66,6 +67,7 @@ class AstFromValueTest extends TestCase */ public function testConvertsFloatValuesToIntOrFloatASTs() { + $this->assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::float())); $this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::float())); $this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::float())); $this->assertEquals(new FloatValueNode(['value' => '123.5']), AST::astFromValue(123.5, Type::float())); @@ -81,7 +83,7 @@ class AstFromValueTest extends TestCase { $this->assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::string())); $this->assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::string())); - $this->assertEquals(new StringValueNode(['value' => 'VA\\nLUE']), AST::astFromValue("VA\nLUE", Type::string())); + $this->assertEquals(new StringValueNode(['value' => "VA\nLUE"]), AST::astFromValue("VA\nLUE", Type::string())); $this->assertEquals(new StringValueNode(['value' => '123']), AST::astFromValue(123, Type::string())); $this->assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::string())); $this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::string())); @@ -95,8 +97,11 @@ class AstFromValueTest extends TestCase { $this->assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::id())); $this->assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::id())); - $this->assertEquals(new StringValueNode(['value' => 'VA\\nLUE']), AST::astFromValue("VA\nLUE", Type::id())); + $this->assertEquals(new StringValueNode(['value' => "VA\nLUE"]), AST::astFromValue("VA\nLUE", Type::id())); + $this->assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::id())); $this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::id())); + $this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue('123', Type::id())); + $this->assertEquals(new StringValueNode(['value' => '01']), AST::astFromValue('01', Type::id())); $this->assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::id())); $this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::id())); $this->assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::id()))); diff --git a/tests/Utils/SchemaPrinterTest.php b/tests/Utils/SchemaPrinterTest.php index 6c04696..db09452 100644 --- a/tests/Utils/SchemaPrinterTest.php +++ b/tests/Utils/SchemaPrinterTest.php @@ -27,7 +27,7 @@ class SchemaPrinterTest extends TestCase private function printSingleFieldSchema($fieldConfig) { $query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Root', 'fields' => [ 'singleField' => $fieldConfig ] @@ -44,7 +44,11 @@ class SchemaPrinterTest extends TestCase 'type' => Type::string() ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField: String } ', $output); @@ -59,7 +63,11 @@ type Query { 'type' => Type::listOf(Type::string()) ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField: [String] } ', $output); @@ -74,7 +82,11 @@ type Query { 'type' => Type::nonNull(Type::string()) ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField: String! } ', $output); @@ -89,7 +101,11 @@ type Query { 'type' => Type::nonNull(Type::listOf(Type::string())) ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField: [String]! } ', $output); @@ -104,7 +120,11 @@ type Query { 'type' => Type::listOf(Type::nonNull(Type::string())) ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField: [String!] } ', $output); @@ -119,7 +139,11 @@ type Query { 'type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string()))) ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField: [String!]! } ', $output); @@ -167,7 +191,11 @@ type Root { 'args' => ['argOne' => ['type' => Type::int()]] ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField(argOne: Int): String } ', $output); @@ -183,12 +211,36 @@ type Query { 'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => 2]] ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField(argOne: Int = 2): String } ', $output); } + /** + * @it Prints String Field With String Arg With Default + */ + public function testPrintsStringFieldWithStringArgWithDefault() + { + $output = $this->printSingleFieldSchema([ + 'type' => Type::string(), + 'args' => ['argOne' => ['type' => Type::string(), 'defaultValue' => "tes\t de\fault"]], + ]); + $this->assertEquals(' +schema { + query: Root +} + +type Root { + singleField(argOne: String = "tes\t de\fault"): String +} +', $output); + } + /** * @it Prints String Field With Int Arg With Default Null */ @@ -199,7 +251,11 @@ type Query { 'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => null]] ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField(argOne: Int = null): String } ', $output); @@ -215,7 +271,11 @@ type Query { 'args' => ['argOne' => ['type' => Type::nonNull(Type::int())]] ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField(argOne: Int!): String } ', $output); @@ -234,7 +294,11 @@ type Query { ] ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField(argOne: Int, argTwo: String): String } ', $output); @@ -254,7 +318,11 @@ type Query { ] ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String } ', $output); @@ -274,7 +342,11 @@ type Query { ] ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String } ', $output); @@ -294,7 +366,11 @@ type Query { ] ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String } ', $output); @@ -619,13 +695,17 @@ type Query { ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { """This field is awesome""" singleField: String } ', $output); - $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; + $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Root']; $recreatedField = $recreatedRoot->getFields()['singleField']; $this->assertEquals($description, $recreatedField->description); } @@ -642,7 +722,11 @@ type Query { ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { """ This field is "awesome" """ @@ -650,7 +734,7 @@ type Query { } ', $output); - $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; + $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Root']; $recreatedField = $recreatedRoot->getFields()['singleField']; $this->assertEquals($description, $recreatedField->description); } @@ -667,14 +751,18 @@ type Query { ]); $this->assertEquals(' -type Query { +schema { + query: Root +} + +type Root { """ This field is "awesome" """ singleField: String } ', $output); - $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; + $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Root']; $recreatedField = $recreatedRoot->getFields()['singleField']; $this->assertEquals($description, $recreatedField->description); }