diff --git a/tests/Utils/AssertValidNameTest.php b/tests/Utils/AssertValidNameTest.php index 87fbfea..0876acd 100644 --- a/tests/Utils/AssertValidNameTest.php +++ b/tests/Utils/AssertValidNameTest.php @@ -1,4 +1,7 @@ assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::boolean())); $this->assertEquals(new BooleanValueNode(['value' => false]), AST::astFromValue(0, Type::boolean())); $this->assertEquals(new BooleanValueNode(['value' => true]), AST::astFromValue(1, Type::boolean())); - $this->assertEquals(new BooleanValueNode(['value' => false]), AST::astFromValue(0, Type::nonNull(Type::boolean()))); - $this->assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::boolean()))); // Note: null means that AST cannot + $this->assertEquals( + new BooleanValueNode(['value' => false]), + AST::astFromValue(0, Type::nonNull(Type::boolean())) + ); + $this->assertEquals( + null, + AST::astFromValue(null, Type::nonNull(Type::boolean())) + ); // Note: null means that AST cannot } /** @@ -59,7 +70,10 @@ class AstFromValueTest extends TestCase { $this->expectException(\Throwable::class); $this->expectExceptionMessage('Int cannot represent non 32-bit signed integer value: 1.0E+40'); - AST::astFromValue(1e40, Type::int()); // Note: js version will produce 1e+40, both values are valid GraphQL floats + AST::astFromValue( + 1e40, + Type::int() + ); // Note: js version will produce 1e+40, both values are valid GraphQL floats } /** @@ -121,7 +135,10 @@ class AstFromValueTest extends TestCase public function testConvertsStringValuesToEnumASTsIfPossible() : void { $this->assertEquals(new EnumValueNode(['value' => 'HELLO']), AST::astFromValue('HELLO', $this->myEnum())); - $this->assertEquals(new EnumValueNode(['value' => 'COMPLEX']), AST::astFromValue($this->complexValue(), $this->myEnum())); + $this->assertEquals( + new EnumValueNode(['value' => 'COMPLEX']), + AST::astFromValue($this->complexValue(), $this->myEnum()) + ); // Note: case sensitive $this->assertEquals(null, AST::astFromValue('hello', $this->myEnum())); @@ -130,6 +147,31 @@ class AstFromValueTest extends TestCase $this->assertEquals(null, AST::astFromValue('VALUE', $this->myEnum())); } + /** + * @return EnumType + */ + private function myEnum() + { + return new EnumType([ + 'name' => 'MyEnum', + 'values' => [ + 'HELLO' => [], + 'GOODBYE' => [], + 'COMPLEX' => ['value' => $this->complexValue()], + ], + ]); + } + + private function complexValue() + { + if (! $this->complexValue) { + $this->complexValue = new \stdClass(); + $this->complexValue->someArbitrary = 'complexValue'; + } + + return $this->complexValue; + } + /** * @see it('converts array values to List ASTs') */ @@ -138,8 +180,8 @@ class AstFromValueTest extends TestCase $value1 = new ListValueNode([ 'values' => [ new StringValueNode(['value' => 'FOO']), - new StringValueNode(['value' => 'BAR']) - ] + new StringValueNode(['value' => 'BAR']), + ], ]); $this->assertEquals($value1, AST::astFromValue(['FOO', 'BAR'], Type::listOf(Type::string()))); @@ -147,7 +189,7 @@ class AstFromValueTest extends TestCase 'values' => [ new EnumValueNode(['value' => 'HELLO']), new EnumValueNode(['value' => 'GOODBYE']), - ] + ], ]); $this->assertEquals($value2, AST::astFromValue(['HELLO', 'GOODBYE'], Type::listOf($this->myEnum()))); } @@ -157,7 +199,10 @@ class AstFromValueTest extends TestCase */ public function testConvertsListSingletons() : void { - $this->assertEquals(new StringValueNode(['value' => 'FOO']), AST::astFromValue('FOO', Type::listOf(Type::string()))); + $this->assertEquals( + new StringValueNode(['value' => 'FOO']), + AST::astFromValue('FOO', Type::listOf(Type::string())) + ); } /** @@ -166,18 +211,18 @@ class AstFromValueTest extends TestCase public function testConvertsInputObjects() : void { $inputObj = new InputObjectType([ - 'name' => 'MyInputObj', + 'name' => 'MyInputObj', 'fields' => [ 'foo' => Type::float(), - 'bar' => $this->myEnum() - ] + 'bar' => $this->myEnum(), + ], ]); $expected = new ObjectValueNode([ 'fields' => [ $this->objectField('foo', new IntValueNode(['value' => '3'])), - $this->objectField('bar', new EnumValueNode(['value' => 'HELLO'])) - ] + $this->objectField('bar', new EnumValueNode(['value' => 'HELLO'])), + ], ]); $data = ['foo' => 3, 'bar' => 'HELLO']; @@ -185,62 +230,38 @@ class AstFromValueTest extends TestCase $this->assertEquals($expected, AST::astFromValue((object) $data, $inputObj)); } + /** + * @param mixed $value + * @return ObjectFieldNode + */ + private function objectField(string $name, $value) + { + return new ObjectFieldNode([ + 'name' => new NameNode(['value' => $name]), + 'value' => $value, + ]); + } + /** * @see it('converts input objects with explicit nulls') */ public function testConvertsInputObjectsWithExplicitNulls() : void { $inputObj = new InputObjectType([ - 'name' => 'MyInputObj', + 'name' => 'MyInputObj', 'fields' => [ 'foo' => Type::float(), - 'bar' => $this->myEnum() - ] + 'bar' => $this->myEnum(), + ], ]); - $this->assertEquals(new ObjectValueNode([ + $this->assertEquals( + new ObjectValueNode([ 'fields' => [ - $this->objectField('foo', new NullValueNode([])) - ] - ]), AST::astFromValue(['foo' => null], $inputObj)); - } - - private $complexValue; - - private function complexValue() - { - if (!$this->complexValue) { - $this->complexValue = new \stdClass(); - $this->complexValue->someArbitrary = 'complexValue'; - } - return $this->complexValue; - } - - /** - * @return EnumType - */ - private function myEnum() - { - return new EnumType([ - 'name' => 'MyEnum', - 'values' => [ - 'HELLO' => [], - 'GOODBYE' => [], - 'COMPLEX' => ['value' => $this->complexValue()] - ] - ]); - } - - /** - * @param $name - * @param $value - * @return ObjectFieldNode - */ - private function objectField($name, $value) - { - return new ObjectFieldNode([ - 'name' => new NameNode(['value' => $name]), - 'value' => $value - ]); + $this->objectField('foo', new NullValueNode([])), + ], + ]), + AST::astFromValue(['foo' => null], $inputObj) + ); } } diff --git a/tests/Utils/AstFromValueUntypedTest.php b/tests/Utils/AstFromValueUntypedTest.php index eb168cf..6c78fed 100644 --- a/tests/Utils/AstFromValueUntypedTest.php +++ b/tests/Utils/AstFromValueUntypedTest.php @@ -1,4 +1,7 @@ assertEquals( - $expected, - AST::valueFromASTUntyped(Parser::parseValue($valueText), $variables) - ); - } - /** * @see it('parses simple values') */ @@ -29,6 +24,17 @@ class AstFromValueUntypedTest extends TestCase $this->assertTestCase('abc123', 'abc123'); } + /** + * @param mixed[]|null $variables + */ + private function assertTestCase($valueText, $expected, ?array $variables = null) : void + { + $this->assertEquals( + $expected, + AST::valueFromASTUntyped(Parser::parseValue($valueText), $variables) + ); + } + /** * @see it('parses lists of values') */ diff --git a/tests/Utils/BreakingChangesFinderTest.php b/tests/Utils/BreakingChangesFinderTest.php index 37f575d..6af2723 100644 --- a/tests/Utils/BreakingChangesFinderTest.php +++ b/tests/Utils/BreakingChangesFinderTest.php @@ -1,5 +1,7 @@ queryType = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ 'field1' => [ - 'type' => Type::string() - ] - ] + 'type' => Type::string(), + ], + ], ]); } @@ -38,32 +42,32 @@ class BreakingChangesFinderTest extends TestCase */ public function testShouldDetectIfTypeWasRemovedOrNot() : void { - $type1 = new ObjectType([ - 'name' => 'Type1', + $type1 = new ObjectType([ + 'name' => 'Type1', 'fields' => [ 'field1' => ['type' => Type::string()], - ] + ], ]); - $type2 = new ObjectType([ - 'name' => 'Type2', + $type2 = new ObjectType([ + 'name' => 'Type2', 'fields' => [ 'field1' => ['type' => Type::string()], - ] + ], ]); $oldSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$type1, $type2] + 'types' => [$type1, $type2], ]); $newSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$type2] + 'types' => [$type2], ]); $expected = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED, - 'description' => 'Type1 was removed.' - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED, + 'description' => 'Type1 was removed.', + ], ]; $this->assertEquals( @@ -80,39 +84,39 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfATypeChangedItsType() : void { $objectType = new ObjectType([ - 'name' => 'ObjectType', + 'name' => 'ObjectType', 'fields' => [ 'field1' => ['type' => Type::string()], - ] + ], ]); $interfaceType = new InterfaceType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ - 'field1' => ['type' => Type::string()] - ] + 'field1' => ['type' => Type::string()], + ], ]); $unionType = new UnionType([ - 'name' => 'Type1', + 'name' => 'Type1', 'types' => [$objectType], ]); $oldSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$interfaceType] + 'types' => [$interfaceType], ]); $newSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$unionType] + 'types' => [$unionType], ]); $expected = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_CHANGED_KIND, - 'description' => 'Type1 changed from an Interface type to a Union type.' - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_CHANGED_KIND, + 'description' => 'Type1 changed from an Interface type to a Union type.', + ], ]; $this->assertEquals( @@ -127,36 +131,36 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfAFieldOnATypeWasDeletedOrChangedType() : void { $typeA = new ObjectType([ - 'name' => 'TypeA', + 'name' => 'TypeA', 'fields' => [ 'field1' => ['type' => Type::string()], - ] + ], ]); // logically equivalent to TypeA; findBreakingFieldChanges shouldn't // treat this as different than TypeA - $typeA2 = new ObjectType([ - 'name' => 'TypeA', + $typeA2 = new ObjectType([ + 'name' => 'TypeA', 'fields' => [ 'field1' => ['type' => Type::string()], - ] + ], ]); - $typeB = new ObjectType([ - 'name' => 'TypeB', + $typeB = new ObjectType([ + 'name' => 'TypeB', 'fields' => [ 'field1' => ['type' => Type::string()], - ] + ], ]); $oldType1 = new InterfaceType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ - 'field1' => ['type' => $typeA], - 'field2' => ['type' => Type::string()], - 'field3' => ['type' => Type::string()], - 'field4' => ['type' => $typeA], - 'field6' => ['type' => Type::string()], - 'field7' => ['type' => Type::listOf(Type::string())], - 'field8' => ['type' => Type::int()], - 'field9' => ['type' => Type::nonNull(Type::int())], + 'field1' => ['type' => $typeA], + 'field2' => ['type' => Type::string()], + 'field3' => ['type' => Type::string()], + 'field4' => ['type' => $typeA], + 'field6' => ['type' => Type::string()], + 'field7' => ['type' => Type::listOf(Type::string())], + 'field8' => ['type' => Type::int()], + 'field9' => ['type' => Type::nonNull(Type::int())], 'field10' => ['type' => Type::nonNull(Type::listOf(Type::int()))], 'field11' => ['type' => Type::int()], 'field12' => ['type' => Type::listOf(Type::int())], @@ -167,21 +171,22 @@ class BreakingChangesFinderTest extends TestCase 'field17' => ['type' => Type::listOf(Type::int())], 'field18' => [ 'type' => Type::listOf(Type::nonNull( - Type::listOf(Type::nonNull(Type::int())))), + Type::listOf(Type::nonNull(Type::int())) + )), ], - ] + ], ]); $newType1 = new InterfaceType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ - 'field1' => ['type' => $typeA2], - 'field3' => ['type' => Type::boolean()], - 'field4' => ['type' => $typeB], - 'field5' => ['type' => Type::string()], - 'field6' => ['type' => Type::listOf(Type::string())], - 'field7' => ['type' => Type::string()], - 'field8' => ['type' => Type::nonNull(Type::int())], - 'field9' => ['type' => Type::int()], + 'field1' => ['type' => $typeA2], + 'field3' => ['type' => Type::boolean()], + 'field4' => ['type' => $typeB], + 'field5' => ['type' => Type::string()], + 'field6' => ['type' => Type::listOf(Type::string())], + 'field7' => ['type' => Type::string()], + 'field8' => ['type' => Type::nonNull(Type::int())], + 'field9' => ['type' => Type::int()], 'field10' => ['type' => Type::listOf(Type::int())], 'field11' => ['type' => Type::nonNull(Type::listOf(Type::int()))], 'field12' => ['type' => Type::listOf(Type::nonNull(Type::int()))], @@ -192,9 +197,10 @@ class BreakingChangesFinderTest extends TestCase 'field17' => ['type' => Type::nonNull(Type::listOf(Type::int()))], 'field18' => [ 'type' => Type::listOf( - Type::listOf(Type::nonNull(Type::int()))), + Type::listOf(Type::nonNull(Type::int())) + ), ], - ] + ], ]); $oldSchema = new Schema([ @@ -209,60 +215,63 @@ class BreakingChangesFinderTest extends TestCase $expectedFieldChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED, 'description' => 'Type1.field2 was removed.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field3 changed type from String to Boolean.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field4 changed type from TypeA to TypeB.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field6 changed type from String to [String].', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field7 changed type from [String] to String.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field9 changed type from Int! to Int.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field10 changed type from [Int]! to [Int].', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field11 changed type from Int to [Int]!.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field13 changed type from [Int!] to [Int].', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field14 changed type from [Int] to [[Int]].', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field15 changed type from [[Int]] to [Int].', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field16 changed type from Int! to [Int]!.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'Type1.field18 changed type from [[Int!]!] to [[Int!]].', ], ]; - $this->assertEquals($expectedFieldChanges, BreakingChangesFinder::findFieldsThatChangedTypeOnObjectOrInterfaceTypes($oldSchema, $newSchema)); + $this->assertEquals( + $expectedFieldChanges, + BreakingChangesFinder::findFieldsThatChangedTypeOnObjectOrInterfaceTypes($oldSchema, $newSchema) + ); } /** @@ -271,162 +280,168 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfFieldsOnInputTypesChangedKindOrWereRemoved() : void { $oldInputType = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ - 'field1' => [ + 'field1' => [ 'type' => Type::string(), ], - 'field2' => [ + 'field2' => [ 'type' => Type::boolean(), ], - 'field3' => [ - 'type' => Type::listOf(Type::string()) + 'field3' => [ + 'type' => Type::listOf(Type::string()), ], - 'field4' => [ + 'field4' => [ 'type' => Type::nonNull(Type::string()), ], - 'field5' => [ + 'field5' => [ 'type' => Type::string(), ], - 'field6' => [ - 'type' => Type::listOf(Type::int()) + 'field6' => [ + 'type' => Type::listOf(Type::int()), ], - 'field7' => [ - 'type' => Type::nonNull(Type::listOf(Type::int())) + 'field7' => [ + 'type' => Type::nonNull(Type::listOf(Type::int())), ], - 'field8' => [ + 'field8' => [ 'type' => Type::int(), ], - 'field9' => [ - 'type' => Type::listOf(Type::int()) + 'field9' => [ + 'type' => Type::listOf(Type::int()), ], 'field10' => [ - 'type' => Type::listOf(Type::nonNull(Type::int())) + 'type' => Type::listOf(Type::nonNull(Type::int())), ], 'field11' => [ - 'type' => Type::listOf(Type::int()) + 'type' => Type::listOf(Type::int()), ], 'field12' => [ - 'type' => Type::listOf(Type::listOf(Type::int())) + 'type' => Type::listOf(Type::listOf(Type::int())), ], 'field13' => [ - 'type' => Type::nonNull(Type::int()) + 'type' => Type::nonNull(Type::int()), ], 'field14' => [ - 'type' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))) + 'type' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))), ], 'field15' => [ - 'type' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))) - ] - ] + 'type' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))), + ], + ], ]); $newInputType = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ - 'field1' => [ + 'field1' => [ 'type' => Type::int(), ], - 'field3' => [ - 'type' => Type::string() + 'field3' => [ + 'type' => Type::string(), ], - 'field4' => [ - 'type' => Type::string() + 'field4' => [ + 'type' => Type::string(), ], - 'field5' => [ - 'type' => Type::nonNull(Type::string()) + 'field5' => [ + 'type' => Type::nonNull(Type::string()), ], - 'field6' => [ - 'type' => Type::nonNull(Type::listOf(Type::int())) + 'field6' => [ + 'type' => Type::nonNull(Type::listOf(Type::int())), ], - 'field7' => [ - 'type' => Type::listOf(Type::int()) + 'field7' => [ + 'type' => Type::listOf(Type::int()), ], - 'field8' => [ - 'type' => Type::nonNull(Type::listOf(Type::int())) + 'field8' => [ + 'type' => Type::nonNull(Type::listOf(Type::int())), ], - 'field9' => [ - 'type' => Type::listOf(Type::nonNull(Type::int())) + 'field9' => [ + 'type' => Type::listOf(Type::nonNull(Type::int())), ], 'field10' => [ - 'type' => Type::listOf(Type::int()) + 'type' => Type::listOf(Type::int()), ], 'field11' => [ - 'type' => Type::listOf(Type::listOf(Type::int())) + 'type' => Type::listOf(Type::listOf(Type::int())), ], 'field12' => [ - 'type' => Type::listOf(Type::int()) + 'type' => Type::listOf(Type::int()), ], 'field13' => [ - 'type' => Type::nonNull(Type::listOf(Type::int())) + 'type' => Type::nonNull(Type::listOf(Type::int())), ], 'field14' => [ - 'type' => Type::listOf(Type::listOf(Type::int())) + 'type' => Type::listOf(Type::listOf(Type::int())), ], 'field15' => [ - 'type' => Type::listOf(Type::nonNull(Type::listOf(Type::nonNull(Type::int())))) - ] - ] + 'type' => Type::listOf(Type::nonNull(Type::listOf(Type::nonNull(Type::int())))), + ], + ], ]); $oldSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$oldInputType] + 'types' => [$oldInputType], ]); $newSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$newInputType] + 'types' => [$newInputType], ]); $expectedFieldChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field1 changed type from String to Int.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED, 'description' => 'InputType1.field2 was removed.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field3 changed type from [String] to String.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field5 changed type from String to String!.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field6 changed type from [Int] to [Int]!.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field8 changed type from Int to [Int]!.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field9 changed type from [Int] to [Int!].', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field11 changed type from [Int] to [[Int]].', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field12 changed type from [[Int]] to [Int].', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field13 changed type from Int! to [Int]!.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'InputType1.field15 changed type from [[Int]!] to [[Int!]!].', ], ]; - $this->assertEquals($expectedFieldChanges, BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes($oldSchema, $newSchema)['breakingChanges']); + $this->assertEquals( + $expectedFieldChanges, + BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes( + $oldSchema, + $newSchema + )['breakingChanges'] + ); } /** @@ -435,19 +450,19 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfANonNullFieldIsAddedToAnInputType() : void { $oldInputType = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $newInputType = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ - 'field1' => Type::string(), + 'field1' => Type::string(), 'requiredField' => Type::nonNull(Type::int()), - 'optionalField' => Type::boolean() - ] + 'optionalField' => Type::boolean(), + ], ]); $oldSchema = new Schema([ @@ -462,14 +477,17 @@ class BreakingChangesFinderTest extends TestCase $expected = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_INPUT_FIELD_ADDED, - 'description' => 'A non-null field requiredField on input type InputType1 was added.' + 'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_INPUT_FIELD_ADDED, + 'description' => 'A non-null field requiredField on input type InputType1 was added.', ], ]; $this->assertEquals( $expected, - BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes($oldSchema, $newSchema)['breakingChanges'] + BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes( + $oldSchema, + $newSchema + )['breakingChanges'] ); } @@ -479,38 +497,38 @@ class BreakingChangesFinderTest extends TestCase public function testShouldRetectIfATypeWasRemovedFromAUnionType() : void { $type1 = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); // logially equivalent to type1; findTypesRemovedFromUnions should not // treat this as different than type1 $type1a = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); - $type2 = new ObjectType([ - 'name' => 'Type2', + $type2 = new ObjectType([ + 'name' => 'Type2', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); - $type3 = new ObjectType([ - 'name' => 'Type3', + $type3 = new ObjectType([ + 'name' => 'Type3', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $oldUnionType = new UnionType([ - 'name' => 'UnionType1', + 'name' => 'UnionType1', 'types' => [$type1, $type2], ]); $newUnionType = new UnionType([ - 'name' => 'UnionType1', + 'name' => 'UnionType1', 'types' => [$type1a, $type3], ]); @@ -525,9 +543,9 @@ class BreakingChangesFinderTest extends TestCase $expected = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED_FROM_UNION, - 'description' => 'Type2 was removed from union type UnionType1.' - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED_FROM_UNION, + 'description' => 'Type2 was removed from union type UnionType1.', + ], ]; $this->assertEquals( @@ -542,37 +560,37 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfAValueWasRemovedFromAnEnumType() : void { $oldEnumType = new EnumType([ - 'name' => 'EnumType1', + 'name' => 'EnumType1', 'values' => [ 'VALUE0' => 0, 'VALUE1' => 1, - 'VALUE2' => 2 - ] + 'VALUE2' => 2, + ], ]); $newEnumType = new EnumType([ - 'name' => 'EnumType1', + 'name' => 'EnumType1', 'values' => [ 'VALUE0' => 0, 'VALUE2' => 1, - 'VALUE3' => 2 - ] + 'VALUE3' => 2, + ], ]); $oldSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$oldEnumType] + 'types' => [$oldEnumType], ]); $newSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$newEnumType] + 'types' => [$newEnumType], ]); $expected = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_VALUE_REMOVED_FROM_ENUM, - 'description' => 'VALUE1 was removed from enum type EnumType1.' - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_VALUE_REMOVED_FROM_ENUM, + 'description' => 'VALUE1 was removed from enum type EnumType1.', + ], ]; $this->assertEquals( @@ -587,52 +605,52 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfAFieldArgumentWasRemoved() : void { $oldType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ - 'name' => Type::string() - ] - ] - ] + 'name' => Type::string(), + ], + ], + ], ]); $inputType = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $oldInterfaceType = new InterfaceType([ - 'name' => 'Interface1', + 'name' => 'Interface1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ - 'arg1' => Type::boolean(), - 'objectArg' => $inputType - ] - ] - ] + 'arg1' => Type::boolean(), + 'objectArg' => $inputType, + ], + ], + ], ]); $newType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), - 'args' => [] - ] - ] + 'args' => [], + ], + ], ]); $newInterfaceType = new InterfaceType([ - 'name' => 'Interface1', + 'name' => 'Interface1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $oldSchema = new Schema([ @@ -647,20 +665,23 @@ class BreakingChangesFinderTest extends TestCase $expectedChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED, 'description' => 'Type1.field1 arg name was removed', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED, 'description' => 'Interface1.field1 arg arg1 was removed', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_REMOVED, 'description' => 'Interface1.field1 arg objectArg was removed', - ] + ], ]; - $this->assertEquals($expectedChanges, BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges']); + $this->assertEquals( + $expectedChanges, + BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges'] + ); } /** @@ -669,119 +690,122 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfAFieldArgumentHasChangedType() : void { $oldType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ - 'arg1' => Type::string(), - 'arg2' => Type::string(), - 'arg3' => Type::listOf(Type::string()), - 'arg4' => Type::string(), - 'arg5' => Type::nonNull(Type::string()), - 'arg6' => Type::nonNull(Type::string()), - 'arg7' => Type::nonNull(Type::listOf(Type::int())), - 'arg8' => Type::int(), - 'arg9' => Type::listOf(Type::int()), + 'arg1' => Type::string(), + 'arg2' => Type::string(), + 'arg3' => Type::listOf(Type::string()), + 'arg4' => Type::string(), + 'arg5' => Type::nonNull(Type::string()), + 'arg6' => Type::nonNull(Type::string()), + 'arg7' => Type::nonNull(Type::listOf(Type::int())), + 'arg8' => Type::int(), + 'arg9' => Type::listOf(Type::int()), 'arg10' => Type::listOf(Type::nonNull(Type::int())), 'arg11' => Type::listOf(Type::int()), 'arg12' => Type::listOf(Type::listOf(Type::int())), 'arg13' => Type::nonNull(Type::int()), 'arg14' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))), - 'arg15' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))) - ] - ] - ] + 'arg15' => Type::listOf(Type::nonNull(Type::listOf(Type::int()))), + ], + ], + ], ]); $newType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ - 'arg1' => Type::int(), - 'arg2' => Type::listOf(Type::string()), - 'arg3' => Type::string(), - 'arg4' => Type::nonNull(Type::string()), - 'arg5' => Type::int(), - 'arg6' => Type::nonNull(Type::int()), - 'arg7' => Type::listOf(Type::int()), - 'arg8' => Type::nonNull(Type::listOf(Type::int())), - 'arg9' => Type::listOf(Type::nonNull(Type::int())), + 'arg1' => Type::int(), + 'arg2' => Type::listOf(Type::string()), + 'arg3' => Type::string(), + 'arg4' => Type::nonNull(Type::string()), + 'arg5' => Type::int(), + 'arg6' => Type::nonNull(Type::int()), + 'arg7' => Type::listOf(Type::int()), + 'arg8' => Type::nonNull(Type::listOf(Type::int())), + 'arg9' => Type::listOf(Type::nonNull(Type::int())), 'arg10' => Type::listOf(Type::int()), 'arg11' => Type::listOf(Type::listOf(Type::int())), 'arg12' => Type::listOf(Type::int()), 'arg13' => Type::nonNull(Type::listOf(Type::int())), 'arg14' => Type::listOf(Type::listOf(Type::int())), - 'arg15' => Type::listOf(Type::nonNull(Type::listOf(Type::nonNull(Type::int())))) - ] - ] - ] + 'arg15' => Type::listOf(Type::nonNull(Type::listOf(Type::nonNull(Type::int())))), + ], + ], + ], ]); $oldSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$oldType] + 'types' => [$oldType], ]); $newSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$newType] + 'types' => [$newType], ]); $expectedChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg1 has changed type from String to Int', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, - 'description' => 'Type1.field1 arg arg2 has changed type from String to [String]' + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'description' => 'Type1.field1 arg arg2 has changed type from String to [String]', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg3 has changed type from [String] to String', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg4 has changed type from String to String!', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg5 has changed type from String! to Int', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg6 has changed type from String! to Int!', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg8 has changed type from Int to [Int]!', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg9 has changed type from [Int] to [Int!]', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg11 has changed type from [Int] to [[Int]]', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg12 has changed type from [[Int]] to [Int]', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg13 has changed type from Int! to [Int]!', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'Type1.field1 arg arg15 has changed type from [[Int]!] to [[Int!]!]', ], ]; - $this->assertEquals($expectedChanges, BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges']); + $this->assertEquals( + $expectedChanges, + BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['breakingChanges'] + ); } /** @@ -789,42 +813,44 @@ class BreakingChangesFinderTest extends TestCase */ public function testShouldDetectIfANonNullFieldArgumentWasAdded() : void { - $oldType = new ObjectType([ - 'name' => 'Type1', - 'fields' => [ - 'field1' => [ - 'type' => Type::string(), - 'args' => [ - 'arg1' => Type::string() - ]] - ] - ]); - $newType = new ObjectType([ - 'name' => 'Type1', + $oldType = new ObjectType([ + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ 'arg1' => Type::string(), + ], + ], + ], + ]); + $newType = new ObjectType([ + 'name' => 'Type1', + 'fields' => [ + 'field1' => [ + 'type' => Type::string(), + 'args' => [ + 'arg1' => Type::string(), 'newRequiredArg' => Type::nonNull(Type::string()), - 'newOptionalArg' => Type::int() - ]] - ] + 'newOptionalArg' => Type::int(), + ], + ], + ], ]); $oldSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$oldType] + 'types' => [$oldType], ]); $newSchema = new Schema([ 'query' => $this->queryType, - 'types' => [$newType] + 'types' => [$newType], ]); $expected = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_ARG_ADDED, - 'description' => 'A non-null arg newRequiredArg on Type1.field1 was added' - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_ARG_ADDED, + 'description' => 'A non-null arg newRequiredArg on Type1.field1 was added', + ], ]; $this->assertEquals( @@ -839,43 +865,43 @@ class BreakingChangesFinderTest extends TestCase public function testShouldNotFlagArgsWithTheSameTypeSignatureAsBreaking() : void { $inputType1a = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $inputType1b = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $oldType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::int(), 'args' => [ 'arg1' => Type::nonNull(Type::int()), - 'arg2' => $inputType1a - ] - ] - ] + 'arg2' => $inputType1a, + ], + ], + ], ]); $newType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::int(), 'args' => [ 'arg1' => Type::nonNull(Type::int()), - 'arg2' => $inputType1b - ] - ] - ] + 'arg2' => $inputType1b, + ], + ], + ], ]); $oldSchema = new Schema([ @@ -896,26 +922,26 @@ class BreakingChangesFinderTest extends TestCase public function testShouldConsiderArgsThatMoveAwayFromNonNullAsNonBreaking() : void { $oldType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ 'arg1' => Type::nonNull(Type::string()), - ] - ] - ] + ], + ], + ], ]); $newType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ - 'arg1' => Type::string() - ] - ] - ] + 'arg1' => Type::string(), + ], + ], + ], ]); $oldSchema = new Schema([ @@ -936,23 +962,23 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectInterfacesRemovedFromTypes() : void { $interface1 = new InterfaceType([ - 'name' => 'Interface1', + 'name' => 'Interface1', 'fields' => [ - 'field1' => Type::string() + 'field1' => Type::string(), ], ]); - $oldType = new ObjectType([ - 'name' => 'Type1', + $oldType = new ObjectType([ + 'name' => 'Type1', 'interfaces' => [$interface1], - 'fields' => [ - 'field1' => Type::string() - ] + 'fields' => [ + 'field1' => Type::string(), + ], ]); - $newType = new ObjectType([ - 'name' => 'Type1', + $newType = new ObjectType([ + 'name' => 'Type1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $oldSchema = new Schema([ @@ -966,8 +992,8 @@ class BreakingChangesFinderTest extends TestCase $expected = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_INTERFACE_REMOVED_FROM_OBJECT, - 'description' => 'Type1 no longer implements interface Interface1.' + 'type' => BreakingChangesFinder::BREAKING_CHANGE_INTERFACE_REMOVED_FROM_OBJECT, + 'description' => 'Type1 no longer implements interface Interface1.', ], ]; @@ -983,150 +1009,148 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectAllBreakingChanges() : void { $typeThatGetsRemoved = new ObjectType([ - 'name' => 'TypeThatGetsRemoved', + 'name' => 'TypeThatGetsRemoved', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $argThatChanges = new ObjectType([ - 'name' => 'ArgThatChanges', + 'name' => 'ArgThatChanges', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ - 'id' => Type::int() - ] - ] - ] + 'id' => Type::int(), + ], + ], + ], ]); $argChanged = new ObjectType([ - 'name' => 'ArgThatChanges', + 'name' => 'ArgThatChanges', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ - 'id' => Type::string() - ] - ] - ] + 'id' => Type::string(), + ], + ], + ], ]); $typeThatChangesTypeOld = new ObjectType([ - 'name' => 'TypeThatChangesType', + 'name' => 'TypeThatChangesType', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $typeThatChangesTypeNew = new InterfaceType([ - 'name' => 'TypeThatChangesType', + 'name' => 'TypeThatChangesType', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $typeThatHasBreakingFieldChangesOld = new InterfaceType([ - 'name' => 'TypeThatHasBreakingFieldChanges', + 'name' => 'TypeThatHasBreakingFieldChanges', 'fields' => [ 'field1' => Type::string(), - 'field2' => Type::string() - ] + 'field2' => Type::string(), + ], ]); $typeThatHasBreakingFieldChangesNew = new InterfaceType([ - 'name' => 'TypeThatHasBreakingFieldChanges', + 'name' => 'TypeThatHasBreakingFieldChanges', 'fields' => [ - 'field2' => Type::boolean() - ] + 'field2' => Type::boolean(), + ], ]); $typeInUnion1 = new ObjectType([ - 'name' => 'TypeInUnion1', + 'name' => 'TypeInUnion1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $typeInUnion2 = new ObjectType([ - 'name' => 'TypeInUnion2', + 'name' => 'TypeInUnion2', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $unionTypeThatLosesATypeOld = new UnionType([ - 'name' => 'UnionTypeThatLosesAType', + 'name' => 'UnionTypeThatLosesAType', 'types' => [$typeInUnion1, $typeInUnion2], ]); $unionTypeThatLosesATypeNew = new UnionType([ - 'name' => 'UnionTypeThatLosesAType', + 'name' => 'UnionTypeThatLosesAType', 'types' => [$typeInUnion1], ]); $enumTypeThatLosesAValueOld = new EnumType([ - 'name' => 'EnumTypeThatLosesAValue', + 'name' => 'EnumTypeThatLosesAValue', 'values' => [ 'VALUE0' => 0, 'VALUE1' => 1, - 'VALUE2' => 2 - ] + 'VALUE2' => 2, + ], ]); $enumTypeThatLosesAValueNew = new EnumType([ - 'name' => 'EnumTypeThatLosesAValue', + 'name' => 'EnumTypeThatLosesAValue', 'values' => [ 'VALUE1' => 1, - 'VALUE2' => 2 - ] + 'VALUE2' => 2, + ], ]); $interface1 = new InterfaceType([ - 'name' => 'Interface1', + 'name' => 'Interface1', 'fields' => [ - 'field1' => Type::string() + 'field1' => Type::string(), ], ]); $typeThatLosesInterfaceOld = new ObjectType([ - 'name' => 'TypeThatLosesInterface1', + 'name' => 'TypeThatLosesInterface1', 'interfaces' => [$interface1], - 'fields' => [ - 'field1' => Type::string() - ] + 'fields' => [ + 'field1' => Type::string(), + ], ]); $typeThatLosesInterfaceNew = new ObjectType([ - 'name' => 'TypeThatLosesInterface1', + 'name' => 'TypeThatLosesInterface1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); - $directiveThatIsRemoved = Directive::skipDirective(); - $directiveThatRemovesArgOld = new Directive([ - 'name' => 'DirectiveThatRemovesArg', + $directiveThatIsRemoved = Directive::skipDirective(); + $directiveThatRemovesArgOld = new Directive([ + 'name' => 'DirectiveThatRemovesArg', 'locations' => [DirectiveLocation::FIELD_DEFINITION], - 'args' => FieldArgument::createMap([ - 'arg1' => [ - 'name' => 'arg1', - ], + 'args' => FieldArgument::createMap([ + 'arg1' => ['name' => 'arg1'], ]), ]); - $directiveThatRemovesArgNew = new Directive([ - 'name' => 'DirectiveThatRemovesArg', + $directiveThatRemovesArgNew = new Directive([ + 'name' => 'DirectiveThatRemovesArg', 'locations' => [DirectiveLocation::FIELD_DEFINITION], ]); - $nonNullDirectiveAddedOld = new Directive([ - 'name' => 'NonNullDirectiveAdded', + $nonNullDirectiveAddedOld = new Directive([ + 'name' => 'NonNullDirectiveAdded', 'locations' => [DirectiveLocation::FIELD_DEFINITION], ]); - $nonNullDirectiveAddedNew = new Directive([ - 'name' => 'NonNullDirectiveAdded', + $nonNullDirectiveAddedNew = new Directive([ + 'name' => 'NonNullDirectiveAdded', 'locations' => [DirectiveLocation::FIELD_DEFINITION], - 'args' => FieldArgument::createMap([ + 'args' => FieldArgument::createMap([ 'arg1' => [ 'name' => 'arg1', 'type' => Type::nonNull(Type::boolean()), @@ -1134,58 +1158,58 @@ class BreakingChangesFinderTest extends TestCase ]), ]); $directiveRemovedLocationOld = new Directive([ - 'name' => 'Directive Name', + 'name' => 'Directive Name', 'locations' => [DirectiveLocation::FIELD_DEFINITION, DirectiveLocation::QUERY], ]); $directiveRemovedLocationNew = new Directive([ - 'name' => 'Directive Name', + 'name' => 'Directive Name', 'locations' => [DirectiveLocation::FIELD_DEFINITION], ]); $oldSchema = new Schema([ - 'query' => $this->queryType, - 'types' => [ + 'query' => $this->queryType, + 'types' => [ $typeThatGetsRemoved, $typeThatChangesTypeOld, $typeThatHasBreakingFieldChangesOld, $unionTypeThatLosesATypeOld, $enumTypeThatLosesAValueOld, $argThatChanges, - $typeThatLosesInterfaceOld + $typeThatLosesInterfaceOld, ], 'directives' => [ $directiveThatIsRemoved, $directiveThatRemovesArgOld, $nonNullDirectiveAddedOld, $directiveRemovedLocationOld, - ] + ], ]); $newSchema = new Schema([ - 'query' => $this->queryType, - 'types' => [ + 'query' => $this->queryType, + 'types' => [ $typeThatChangesTypeNew, $typeThatHasBreakingFieldChangesNew, $unionTypeThatLosesATypeNew, $enumTypeThatLosesAValueNew, $argChanged, $typeThatLosesInterfaceNew, - $interface1 + $interface1, ], 'directives' => [ $directiveThatRemovesArgNew, $nonNullDirectiveAddedNew, $directiveRemovedLocationNew, - ] + ], ]); $expectedBreakingChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED, 'description' => 'TypeThatGetsRemoved was removed.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED, 'description' => 'TypeInUnion2 was removed.', ], /* This is reported in the js version because builtin sclar types are added on demand @@ -1195,52 +1219,55 @@ class BreakingChangesFinderTest extends TestCase 'description' => 'Int was removed.' ],*/ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_CHANGED_KIND, 'description' => 'TypeThatChangesType changed from an Object type to an Interface type.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_REMOVED, 'description' => 'TypeThatHasBreakingFieldChanges.field1 was removed.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_FIELD_CHANGED_KIND, 'description' => 'TypeThatHasBreakingFieldChanges.field2 changed type from String to Boolean.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED_FROM_UNION, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_TYPE_REMOVED_FROM_UNION, 'description' => 'TypeInUnion2 was removed from union type UnionTypeThatLosesAType.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_VALUE_REMOVED_FROM_ENUM, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_VALUE_REMOVED_FROM_ENUM, 'description' => 'VALUE0 was removed from enum type EnumTypeThatLosesAValue.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_ARG_CHANGED_KIND, 'description' => 'ArgThatChanges.field1 arg id has changed type from Int to String', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_INTERFACE_REMOVED_FROM_OBJECT, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_INTERFACE_REMOVED_FROM_OBJECT, 'description' => 'TypeThatLosesInterface1 no longer implements interface Interface1.', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED, 'description' => 'skip was removed', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_ARG_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_ARG_REMOVED, 'description' => 'arg1 was removed from DirectiveThatRemovesArg', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_DIRECTIVE_ARG_ADDED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_DIRECTIVE_ARG_ADDED, 'description' => 'A non-null arg arg1 on directive NonNullDirectiveAdded was added', ], [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_LOCATION_REMOVED, + 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_LOCATION_REMOVED, 'description' => 'QUERY was removed from Directive Name', - ] + ], ]; - $this->assertEquals($expectedBreakingChanges, BreakingChangesFinder::findBreakingChanges($oldSchema, $newSchema)); + $this->assertEquals( + $expectedBreakingChanges, + BreakingChangesFinder::findBreakingChanges($oldSchema, $newSchema) + ); } /** @@ -1260,12 +1287,15 @@ class BreakingChangesFinderTest extends TestCase $expectedBreakingChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED, - 'description' => "{$includeDirective->name} was removed", - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED, + 'description' => sprintf('%s was removed', $includeDirective->name), + ], ]; - $this->assertEquals($expectedBreakingChanges, BreakingChangesFinder::findRemovedDirectives($oldSchema, $newSchema)); + $this->assertEquals( + $expectedBreakingChanges, + BreakingChangesFinder::findRemovedDirectives($oldSchema, $newSchema) + ); } /** @@ -1283,12 +1313,15 @@ class BreakingChangesFinderTest extends TestCase $expectedBreakingChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED, - 'description' => "{$deprecatedDirective->name} was removed", - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_REMOVED, + 'description' => sprintf('%s was removed', $deprecatedDirective->name), + ], ]; - $this->assertEquals($expectedBreakingChanges, BreakingChangesFinder::findRemovedDirectives($oldSchema, $newSchema)); + $this->assertEquals( + $expectedBreakingChanges, + BreakingChangesFinder::findRemovedDirectives($oldSchema, $newSchema) + ); } /** @@ -1299,34 +1332,35 @@ class BreakingChangesFinderTest extends TestCase $oldSchema = new Schema([ 'directives' => [ new Directive([ - 'name' => 'DirectiveWithArg', + 'name' => 'DirectiveWithArg', 'locations' => [DirectiveLocation::FIELD_DEFINITION], - 'args' => FieldArgument::createMap([ - 'arg1' => [ - 'name' => 'arg1', - ], + 'args' => FieldArgument::createMap([ + 'arg1' => ['name' => 'arg1'], ]), - ]) + ]), ], ]); $newSchema = new Schema([ 'directives' => [ new Directive([ - 'name' => 'DirectiveWithArg', + 'name' => 'DirectiveWithArg', 'locations' => [DirectiveLocation::FIELD_DEFINITION], - ]) + ]), ], ]); $expectedBreakingChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_ARG_REMOVED, - 'description' => "arg1 was removed from DirectiveWithArg", - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_ARG_REMOVED, + 'description' => 'arg1 was removed from DirectiveWithArg', + ], ]; - $this->assertEquals($expectedBreakingChanges, BreakingChangesFinder::findRemovedDirectiveArgs($oldSchema, $newSchema)); + $this->assertEquals( + $expectedBreakingChanges, + BreakingChangesFinder::findRemovedDirectiveArgs($oldSchema, $newSchema) + ); } /** @@ -1337,35 +1371,38 @@ class BreakingChangesFinderTest extends TestCase $oldSchema = new Schema([ 'directives' => [ new Directive([ - 'name' => 'DirectiveName', + 'name' => 'DirectiveName', 'locations' => [DirectiveLocation::FIELD_DEFINITION], - ]) + ]), ], ]); $newSchema = new Schema([ 'directives' => [ new Directive([ - 'name' => 'DirectiveName', + 'name' => 'DirectiveName', 'locations' => [DirectiveLocation::FIELD_DEFINITION], - 'args' => FieldArgument::createMap([ + 'args' => FieldArgument::createMap([ 'arg1' => [ 'name' => 'arg1', 'type' => Type::nonNull(Type::boolean()), ], ]), - ]) + ]), ], ]); $expectedBreakingChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_DIRECTIVE_ARG_ADDED, - 'description' => "A non-null arg arg1 on directive DirectiveName was added", - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_NON_NULL_DIRECTIVE_ARG_ADDED, + 'description' => 'A non-null arg arg1 on directive DirectiveName was added', + ], ]; - $this->assertEquals($expectedBreakingChanges, BreakingChangesFinder::findAddedNonNullDirectiveArgs($oldSchema, $newSchema)); + $this->assertEquals( + $expectedBreakingChanges, + BreakingChangesFinder::findAddedNonNullDirectiveArgs($oldSchema, $newSchema) + ); } /** @@ -1374,16 +1411,19 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectLocationsRemovedFromADirective() : void { $d1 = new Directive([ - 'name' => 'Directive Name', + 'name' => 'Directive Name', 'locations' => [DirectiveLocation::FIELD_DEFINITION, DirectiveLocation::QUERY], ]); $d2 = new Directive([ - 'name' => 'Directive Name', + 'name' => 'Directive Name', 'locations' => [DirectiveLocation::FIELD_DEFINITION], ]); - $this->assertEquals([DirectiveLocation::QUERY], BreakingChangesFinder::findRemovedLocationsForDirective($d1, $d2)); + $this->assertEquals( + [DirectiveLocation::QUERY], + BreakingChangesFinder::findRemovedLocationsForDirective($d1, $d2) + ); } /** @@ -1394,32 +1434,35 @@ class BreakingChangesFinderTest extends TestCase $oldSchema = new Schema([ 'directives' => [ new Directive([ - 'name' => 'Directive Name', + 'name' => 'Directive Name', 'locations' => [ DirectiveLocation::FIELD_DEFINITION, - DirectiveLocation::QUERY + DirectiveLocation::QUERY, ], - ]) + ]), ], ]); $newSchema = new Schema([ 'directives' => [ new Directive([ - 'name' => 'Directive Name', + 'name' => 'Directive Name', 'locations' => [DirectiveLocation::FIELD_DEFINITION], - ]) + ]), ], ]); $expectedBreakingChanges = [ [ - 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_LOCATION_REMOVED, - 'description' => "QUERY was removed from Directive Name", - ] + 'type' => BreakingChangesFinder::BREAKING_CHANGE_DIRECTIVE_LOCATION_REMOVED, + 'description' => 'QUERY was removed from Directive Name', + ], ]; - $this->assertEquals($expectedBreakingChanges, BreakingChangesFinder::findRemovedDirectiveLocations($oldSchema, $newSchema)); + $this->assertEquals( + $expectedBreakingChanges, + BreakingChangesFinder::findRemovedDirectiveLocations($oldSchema, $newSchema) + ); } // DESCRIBE: findDangerousChanges @@ -1431,33 +1474,33 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfAnArgumentsDefaultValueHasChanged() : void { $oldType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ 'name' => [ - 'type' => Type::string(), - 'defaultValue' => 'test' - ] - ] - ] - ] + 'type' => Type::string(), + 'defaultValue' => 'test', + ], + ], + ], + ], ]); $newType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ 'name' => [ - 'type' => Type::string(), - 'defaultValue' => 'Test' - ] - ] - ] - ] + 'type' => Type::string(), + 'defaultValue' => 'Test', + ], + ], + ], + ], ]); $oldSchema = new Schema([ @@ -1472,9 +1515,9 @@ class BreakingChangesFinderTest extends TestCase $expected = [ [ - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_ARG_DEFAULT_VALUE_CHANGED, - 'description' => 'Type1.field1 arg name has changed defaultValue' - ] + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_ARG_DEFAULT_VALUE_CHANGED, + 'description' => 'Type1.field1 arg name has changed defaultValue', + ], ]; $this->assertEquals( @@ -1489,19 +1532,19 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfAValueWasAddedToAnEnumType() : void { $oldEnumType = new EnumType([ - 'name' => 'EnumType1', + 'name' => 'EnumType1', 'values' => [ 'VALUE0' => 0, 'VALUE1' => 1, - ] + ], ]); $newEnumType = new EnumType([ - 'name' => 'EnumType1', + 'name' => 'EnumType1', 'values' => [ 'VALUE0' => 0, 'VALUE1' => 1, - 'VALUE2' => 2 - ] + 'VALUE2' => 2, + ], ]); $oldSchema = new Schema([ @@ -1516,9 +1559,9 @@ class BreakingChangesFinderTest extends TestCase $expected = [ [ - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_VALUE_ADDED_TO_ENUM, - 'description' => 'VALUE2 was added to enum type EnumType1.' - ] + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_VALUE_ADDED_TO_ENUM, + 'description' => 'VALUE2 was added to enum type EnumType1.', + ], ]; $this->assertEquals( @@ -1533,22 +1576,22 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectInterfacesAddedToTypes() : void { $interface1 = new InterfaceType([ - 'name' => 'Interface1', + 'name' => 'Interface1', 'fields' => [ 'field1' => Type::string(), ], ]); - $oldType = new ObjectType([ - 'name' => 'Type1', + $oldType = new ObjectType([ + 'name' => 'Type1', 'fields' => [ 'field1' => Type::string(), ], ]); $newType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'interfaces' => [$interface1], - 'fields' => [ + 'fields' => [ 'field1' => Type::string(), ], ]); @@ -1565,9 +1608,9 @@ class BreakingChangesFinderTest extends TestCase $expected = [ [ - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_INTERFACE_ADDED_TO_OBJECT, - 'description' => 'Interface1 added to interfaces implemented by Type1.' - ] + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_INTERFACE_ADDED_TO_OBJECT, + 'description' => 'Interface1 added to interfaces implemented by Type1.', + ], ]; $this->assertEquals( @@ -1582,32 +1625,32 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfATypeWasAddedToAUnionType() : void { $type1 = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); // logially equivalent to type1; findTypesRemovedFromUnions should not //treat this as different than type1 $type1a = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); - $type2 = new ObjectType([ - 'name' => 'Type2', + $type2 = new ObjectType([ + 'name' => 'Type2', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $oldUnionType = new UnionType([ - 'name' => 'UnionType1', + 'name' => 'UnionType1', 'types' => [$type1], ]); $newUnionType = new UnionType([ - 'name' => 'UnionType1', + 'name' => 'UnionType1', 'types' => [$type1a, $type2], ]); @@ -1623,9 +1666,9 @@ class BreakingChangesFinderTest extends TestCase $expected = [ [ - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION, - 'description' => 'Type2 was added to union type UnionType1.' - ] + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION, + 'description' => 'Type2 was added to union type UnionType1.', + ], ]; $this->assertEquals( @@ -1640,7 +1683,7 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfANullableFieldWasAddedToAnInput() : void { $oldInputType = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ 'field1' => [ 'type' => Type::string(), @@ -1648,7 +1691,7 @@ class BreakingChangesFinderTest extends TestCase ], ]); $newInputType = new InputObjectType([ - 'name' => 'InputType1', + 'name' => 'InputType1', 'fields' => [ 'field1' => [ 'type' => Type::string(), @@ -1661,26 +1704,28 @@ class BreakingChangesFinderTest extends TestCase $oldSchema = new Schema([ 'query' => $this->queryType, - 'types' => [ - $oldInputType, - ] + 'types' => [$oldInputType], ]); $newSchema = new Schema([ 'query' => $this->queryType, - 'types' => [ - $newInputType, - ] + 'types' => [$newInputType], ]); $expectedFieldChanges = [ [ 'description' => 'A nullable field field2 on input type InputType1 was added.', - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_NULLABLE_INPUT_FIELD_ADDED, + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_NULLABLE_INPUT_FIELD_ADDED, ], ]; - $this->assertEquals($expectedFieldChanges, BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes($oldSchema, $newSchema)['dangerousChanges']); + $this->assertEquals( + $expectedFieldChanges, + BreakingChangesFinder::findFieldsThatChangedTypeOnInputObjectTypes( + $oldSchema, + $newSchema + )['dangerousChanges'] + ); } /** @@ -1689,90 +1734,90 @@ class BreakingChangesFinderTest extends TestCase public function testShouldFindAllDangerousChanges() : void { $enumThatGainsAValueOld = new EnumType([ - 'name' => 'EnumType1', + 'name' => 'EnumType1', 'values' => [ 'VALUE0' => 0, 'VALUE1' => 1, - ] + ], ]); $enumThatGainsAValueNew = new EnumType([ - 'name' => 'EnumType1', + 'name' => 'EnumType1', 'values' => [ 'VALUE0' => 0, 'VALUE1' => 1, - 'VALUE2' => 2 - ] + 'VALUE2' => 2, + ], ]); $oldType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ 'name' => [ - 'type' => Type::string(), - 'defaultValue' => 'test' - ] - ] - ] - ] + 'type' => Type::string(), + 'defaultValue' => 'test', + ], + ], + ], + ], ]); - $typeInUnion1 = new ObjectType([ - 'name' => 'TypeInUnion1', + $typeInUnion1 = new ObjectType([ + 'name' => 'TypeInUnion1', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); - $typeInUnion2 = new ObjectType([ - 'name' => 'TypeInUnion2', + $typeInUnion2 = new ObjectType([ + 'name' => 'TypeInUnion2', 'fields' => [ - 'field1' => Type::string() - ] + 'field1' => Type::string(), + ], ]); $unionTypeThatGainsATypeOld = new UnionType([ - 'name' => 'UnionTypeThatGainsAType', + 'name' => 'UnionTypeThatGainsAType', 'types' => [$typeInUnion1], ]); $unionTypeThatGainsATypeNew = new UnionType([ - 'name' => 'UnionTypeThatGainsAType', + 'name' => 'UnionTypeThatGainsAType', 'types' => [$typeInUnion1, $typeInUnion2], ]); $newType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), 'args' => [ 'name' => [ - 'type' => Type::string(), - 'defaultValue' => 'Test' - ] - ] - ] - ] + 'type' => Type::string(), + 'defaultValue' => 'Test', + ], + ], + ], + ], ]); $interface1 = new InterfaceType([ - 'name' => 'Interface1', + 'name' => 'Interface1', 'fields' => [ 'field1' => Type::string(), ], ]); $typeThatGainsInterfaceOld = new ObjectType([ - 'name' => 'TypeThatGainsInterface1', + 'name' => 'TypeThatGainsInterface1', 'fields' => [ 'field1' => Type::string(), ], ]); $typeThatGainsInterfaceNew = new ObjectType([ - 'name' => 'TypeThatGainsInterface1', + 'name' => 'TypeThatGainsInterface1', 'interfaces' => [$interface1], - 'fields' => [ + 'fields' => [ 'field1' => Type::string(), ], ]); @@ -1783,8 +1828,8 @@ class BreakingChangesFinderTest extends TestCase $oldType, $enumThatGainsAValueOld, $typeThatGainsInterfaceOld, - $unionTypeThatGainsATypeOld - ] + $unionTypeThatGainsATypeOld, + ], ]); $newSchema = new Schema([ @@ -1793,30 +1838,33 @@ class BreakingChangesFinderTest extends TestCase $newType, $enumThatGainsAValueNew, $typeThatGainsInterfaceNew, - $unionTypeThatGainsATypeNew - ] + $unionTypeThatGainsATypeNew, + ], ]); $expectedDangerousChanges = [ [ 'description' => 'Type1.field1 arg name has changed defaultValue', - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_ARG_DEFAULT_VALUE_CHANGED, + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_ARG_DEFAULT_VALUE_CHANGED, ], [ 'description' => 'VALUE2 was added to enum type EnumType1.', - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_VALUE_ADDED_TO_ENUM, + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_VALUE_ADDED_TO_ENUM, ], [ - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_INTERFACE_ADDED_TO_OBJECT, + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_INTERFACE_ADDED_TO_OBJECT, 'description' => 'Interface1 added to interfaces implemented by TypeThatGainsInterface1.', ], [ - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION, + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_TYPE_ADDED_TO_UNION, 'description' => 'TypeInUnion2 was added to union type UnionTypeThatGainsAType.', - ] + ], ]; - $this->assertEquals($expectedDangerousChanges, BreakingChangesFinder::findDangerousChanges($oldSchema, $newSchema)); + $this->assertEquals( + $expectedDangerousChanges, + BreakingChangesFinder::findDangerousChanges($oldSchema, $newSchema) + ); } /** @@ -1825,7 +1873,7 @@ class BreakingChangesFinderTest extends TestCase public function testShouldDetectIfANullableFieldArgumentWasAdded() : void { $oldType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), @@ -1839,7 +1887,7 @@ class BreakingChangesFinderTest extends TestCase ]); $newType = new ObjectType([ - 'name' => 'Type1', + 'name' => 'Type1', 'fields' => [ 'field1' => [ 'type' => Type::string(), @@ -1868,10 +1916,13 @@ class BreakingChangesFinderTest extends TestCase $expectedFieldChanges = [ [ 'description' => 'A nullable arg arg2 on Type1.field1 was added', - 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_NULLABLE_ARG_ADDED, + 'type' => BreakingChangesFinder::DANGEROUS_CHANGE_NULLABLE_ARG_ADDED, ], ]; - $this->assertEquals($expectedFieldChanges, BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['dangerousChanges']); + $this->assertEquals( + $expectedFieldChanges, + BreakingChangesFinder::findArgChanges($oldSchema, $newSchema)['dangerousChanges'] + ); } } diff --git a/tests/Utils/BuildSchemaTest.php b/tests/Utils/BuildSchemaTest.php index eb0e619..45dc5dd 100644 --- a/tests/Utils/BuildSchemaTest.php +++ b/tests/Utils/BuildSchemaTest.php @@ -1,4 +1,7 @@ function ($root, $args) { return $args['x'] + $args['y']; - } + }, ]; $result = GraphQL::executeQuery( @@ -71,7 +69,7 @@ class BuildSchemaTest extends TestCase */ public function testSimpleType() : void { - $body = ' + $body = ' type HelloScalars { str: String int: Int @@ -84,12 +82,20 @@ type HelloScalars { $this->assertEquals($output, $body); } + private function cycleOutput($body, $options = []) + { + $ast = Parser::parse($body); + $schema = BuildSchema::buildAST($ast, null, $options); + + return "\n" . SchemaPrinter::doPrint($schema, $options); + } + /** * @see it('With directives') */ public function testWithDirectives() : void { - $body = ' + $body = ' directive @foo(arg: Int) on FIELD type Query { @@ -105,7 +111,7 @@ type Query { */ public function testSupportsDescriptions() : void { - $body = ' + $body = ' """This is a directive""" directive @foo( """It has an argument""" @@ -137,7 +143,7 @@ type Query { */ public function testSupportsOptionForCommentDescriptions() : void { - $body = ' + $body = ' # This is a directive directive @foo( # It has an argument @@ -159,7 +165,7 @@ type Query { str: String } '; - $output = $this->cycleOutput($body, [ 'commentDescriptions' => true ]); + $output = $this->cycleOutput($body, ['commentDescriptions' => true]); $this->assertEquals($body, $output); } @@ -168,7 +174,7 @@ type Query { */ public function testMaintainsSkipAndInclude() : void { - $body = ' + $body = ' type Query { str: String } @@ -185,7 +191,7 @@ type Query { */ public function testOverridingDirectivesExcludesSpecified() : void { - $body = ' + $body = ' directive @skip on FIELD directive @include on FIELD directive @deprecated on FIELD_DEFINITION @@ -206,7 +212,7 @@ type Query { */ public function testAddingDirectivesMaintainsSkipAndInclude() : void { - $body = ' + $body = ' directive @foo(arg: Int) on FIELD type Query { @@ -225,7 +231,7 @@ type Query { */ public function testTypeModifiers() : void { - $body = ' + $body = ' type HelloScalars { nonNullStr: String! listOfStrs: [String] @@ -243,7 +249,7 @@ type HelloScalars { */ public function testRecursiveType() : void { - $body = ' + $body = ' type Query { str: String recurse: Query @@ -258,7 +264,7 @@ type Query { */ public function testTwoTypesCircular() : void { - $body = ' + $body = ' schema { query: TypeOne } @@ -282,7 +288,7 @@ type TypeTwo { */ public function testSingleArgumentField() : void { - $body = ' + $body = ' type Query { str(int: Int): String floatToStr(float: Float): String @@ -300,7 +306,7 @@ type Query { */ public function testSimpleTypeWithMultipleArguments() : void { - $body = ' + $body = ' type Query { str(int: Int, bool: Boolean): String } @@ -314,7 +320,7 @@ type Query { */ public function testSimpleTypeWithInterface() : void { - $body = ' + $body = ' type Query implements WorldInterface { str: String } @@ -332,7 +338,7 @@ interface WorldInterface { */ public function testSimpleOutputEnum() : void { - $body = ' + $body = ' enum Hello { WORLD } @@ -350,7 +356,7 @@ type Query { */ public function testSimpleInputEnum() : void { - $body = ' + $body = ' enum Hello { WORLD } @@ -368,7 +374,7 @@ type Query { */ public function testMultipleValueEnum() : void { - $body = ' + $body = ' enum Hello { WO RLD @@ -387,7 +393,7 @@ type Query { */ public function testSimpleUnion() : void { - $body = ' + $body = ' union Hello = World type Query { @@ -407,7 +413,7 @@ type World { */ public function testMultipleUnion() : void { - $body = ' + $body = ' union Hello = WorldOne | WorldTwo type Query { @@ -431,7 +437,7 @@ type WorldTwo { */ public function testSpecifyingUnionTypeUsingTypename() : void { - $schema = BuildSchema::buildAST(Parser::parse(' + $schema = BuildSchema::buildAST(Parser::parse(' type Query { fruits: [Fruit] } @@ -446,7 +452,7 @@ type WorldTwo { length: Int } ')); - $query = ' + $query = ' { fruits { ... on Apple { @@ -458,25 +464,25 @@ type WorldTwo { } } '; - $root = [ + $root = [ 'fruits' => [ [ - 'color' => 'green', + 'color' => 'green', '__typename' => 'Apple', ], [ - 'length' => 5, + 'length' => 5, '__typename' => 'Banana', - ] - ] + ], + ], ]; $expected = [ 'data' => [ 'fruits' => [ ['color' => 'green'], ['length' => 5], - ] - ] + ], + ], ]; $result = GraphQL::executeQuery($schema, $query, $root); @@ -488,7 +494,7 @@ type WorldTwo { */ public function testSpecifyingInterfaceUsingTypename() : void { - $schema = BuildSchema::buildAST(Parser::parse(' + $schema = BuildSchema::buildAST(Parser::parse(' type Query { characters: [Character] } @@ -507,7 +513,7 @@ type WorldTwo { primaryFunction: String } ')); - $query = ' + $query = ' { characters { name @@ -520,27 +526,27 @@ type WorldTwo { } } '; - $root = [ + $root = [ 'characters' => [ [ - 'name' => 'Han Solo', + 'name' => 'Han Solo', 'totalCredits' => 10, - '__typename' => 'Human', + '__typename' => 'Human', ], [ - 'name' => 'R2-D2', + 'name' => 'R2-D2', 'primaryFunction' => 'Astromech', - '__typename' => 'Droid', - ] - ] + '__typename' => 'Droid', + ], + ], ]; $expected = [ 'data' => [ 'characters' => [ ['name' => 'Han Solo', 'totalCredits' => 10], ['name' => 'R2-D2', 'primaryFunction' => 'Astromech'], - ] - ] + ], + ], ]; $result = GraphQL::executeQuery($schema, $query, $root); @@ -552,7 +558,7 @@ type WorldTwo { */ public function testCustomScalar() : void { - $body = ' + $body = ' scalar CustomScalar type Query { @@ -568,7 +574,7 @@ type Query { */ public function testInputObject() : void { - $body = ' + $body = ' input Input { int: Int } @@ -586,7 +592,7 @@ type Query { */ public function testSimpleArgumentFieldWithDefault() : void { - $body = ' + $body = ' type Query { str(int: Int = 2): String } @@ -600,7 +606,7 @@ type Query { */ public function testCustomScalarArgumentFieldWithDefault() : void { - $body = ' + $body = ' scalar CustomScalar type Query { @@ -616,7 +622,7 @@ type Query { */ public function testSimpleTypeWithMutation() : void { - $body = ' + $body = ' schema { query: HelloScalars mutation: Mutation @@ -641,7 +647,7 @@ type Mutation { */ public function testSimpleTypeWithSubscription() : void { - $body = ' + $body = ' schema { query: HelloScalars subscription: Subscription @@ -666,7 +672,7 @@ type Subscription { */ public function testUnreferencedTypeImplementingReferencedInterface() : void { - $body = ' + $body = ' type Concrete implements Iface { key: String } @@ -688,7 +694,7 @@ type Query { */ public function testUnreferencedTypeImplementingReferencedUnion() : void { - $body = ' + $body = ' type Concrete { key: String } @@ -708,7 +714,7 @@ union Union = Concrete */ public function testSupportsDeprecated() : void { - $body = ' + $body = ' enum MyEnum { VALUE OLD_VALUE @deprecated @@ -724,7 +730,7 @@ type Query { $output = $this->cycleOutput($body); $this->assertEquals($output, $body); - $ast = Parser::parse($body); + $ast = Parser::parse($body); $schema = BuildSchema::buildAST($ast); /** @var EnumType $myEnum */ @@ -785,15 +791,15 @@ type Query { directive @test(arg: TestScalar) on FIELD '); - $schema = BuildSchema::buildAST($schemaAST); + $schema = BuildSchema::buildAST($schemaAST); /** @var ObjectType $query */ - $query = $schema->getType('Query'); - $testInput = $schema->getType('TestInput'); - $testEnum = $schema->getType('TestEnum'); - $testUnion = $schema->getType('TestUnion'); + $query = $schema->getType('Query'); + $testInput = $schema->getType('TestInput'); + $testEnum = $schema->getType('TestEnum'); + $testUnion = $schema->getType('TestUnion'); $testInterface = $schema->getType('TestInterface'); - $testType = $schema->getType('TestType'); - $testScalar = $schema->getType('TestScalar'); + $testType = $schema->getType('TestType'); + $testScalar = $schema->getType('TestScalar'); $testDirective = $schema->getDirective('test'); $restoredIDL = SchemaPrinter::doPrint(BuildSchema::build( @@ -813,9 +819,15 @@ type Query { $testField = $query->getField('testField'); $this->assertEquals('testField(testArg: TestInput): TestUnion', Printer::doPrint($testField->astNode)); $this->assertEquals('testArg: TestInput', Printer::doPrint($testField->args[0]->astNode)); - $this->assertEquals('testInputField: TestEnum', Printer::doPrint($testInput->getField('testInputField')->astNode)); + $this->assertEquals( + 'testInputField: TestEnum', + Printer::doPrint($testInput->getField('testInputField')->astNode) + ); $this->assertEquals('TEST_VALUE', Printer::doPrint($testEnum->getValue('TEST_VALUE')->astNode)); - $this->assertEquals('interfaceField: String', Printer::doPrint($testInterface->getField('interfaceField')->astNode)); + $this->assertEquals( + 'interfaceField: String', + Printer::doPrint($testInterface->getField('interfaceField')->astNode) + ); $this->assertEquals('interfaceField: String', Printer::doPrint($testType->getField('interfaceField')->astNode)); $this->assertEquals('arg: TestScalar', Printer::doPrint($testDirective->args[0]->astNode)); } @@ -893,7 +905,7 @@ type Hello { bar: Bar } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -918,7 +930,7 @@ type Yellow { isColor: Boolean } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -944,7 +956,7 @@ type Yellow { isColor: Boolean } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -970,7 +982,7 @@ type Yellow { isColor: Boolean } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -981,7 +993,7 @@ type Yellow { { $this->expectException(Error::class); $this->expectExceptionMessage('Type "Bar" not found in document.'); - $body = ' + $body = ' schema { query: Hello } @@ -990,7 +1002,7 @@ type Hello { bar: Bar } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); $schema = BuildSchema::buildAST($doc); $schema->getTypeMap(); } @@ -1002,12 +1014,12 @@ type Hello { { $this->expectException(Error::class); $this->expectExceptionMessage('Type "Bar" not found in document.'); - $body = ' + $body = ' type Query implements Bar { field: String } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); $schema = BuildSchema::buildAST($doc); $schema->getTypeMap(); } @@ -1019,11 +1031,11 @@ type Query implements Bar { { $this->expectException(Error::class); $this->expectExceptionMessage('Type "Bar" not found in document.'); - $body = ' + $body = ' union TestUnion = Bar type Query { testUnion: TestUnion } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); $schema = BuildSchema::buildAST($doc); $schema->getTypeMap(); } @@ -1044,7 +1056,7 @@ type Hello { str: String } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -1065,7 +1077,7 @@ type Hello { str: String } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -1091,7 +1103,7 @@ type Wat { str: String } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -1109,7 +1121,7 @@ schema { query Foo { field } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -1127,7 +1139,7 @@ schema { fragment Foo on Type { field } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); BuildSchema::buildAST($doc); } @@ -1149,7 +1161,7 @@ type Repeated { id: String } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); $this->expectException(Error::class); $this->expectExceptionMessage('Type "Repeated" was defined more than once.'); @@ -1179,14 +1191,15 @@ interface Hello { world: String } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); $decorated = []; - $calls = []; + $calls = []; - $typeConfigDecorator = function($defaultConfig, $node, $allNodesMap) use (&$decorated, &$calls) { + $typeConfigDecorator = function ($defaultConfig, $node, $allNodesMap) use (&$decorated, &$calls) { $decorated[] = $defaultConfig['name']; - $calls[] = [$defaultConfig, $node, $allNodesMap]; + $calls[] = [$defaultConfig, $node, $allNodesMap]; + return ['description' => 'My description of ' . $node->name->value] + $defaultConfig; }; @@ -1204,19 +1217,21 @@ interface Hello { $this->assertEquals(array_keys($allNodesMap), ['Query', 'Color', 'Hello']); $this->assertEquals('My description of Query', $schema->getType('Query')->description); - list($defaultConfig, $node, $allNodesMap) = $calls[1]; $this->assertInstanceOf(EnumTypeDefinitionNode::class, $node); $this->assertEquals('Color', $defaultConfig['name']); $enumValue = [ - 'description' => '', - 'deprecationReason' => '' + 'description' => '', + 'deprecationReason' => '', ]; - $this->assertArraySubset([ - 'RED' => $enumValue, - 'GREEN' => $enumValue, - 'BLUE' => $enumValue, - ], $defaultConfig['values']); + $this->assertArraySubset( + [ + 'RED' => $enumValue, + 'GREEN' => $enumValue, + 'BLUE' => $enumValue, + ], + $defaultConfig['values'] + ); $this->assertCount(4, $defaultConfig); // 3 + astNode $this->assertEquals(array_keys($allNodesMap), ['Query', 'Color', 'Hello']); $this->assertEquals('My description of Color', $schema->getType('Color')->description); @@ -1233,7 +1248,7 @@ interface Hello { public function testCreatesTypesLazily() : void { - $body = ' + $body = ' schema { query: Query } @@ -1258,11 +1273,12 @@ type World implements Hello { world: String } '; - $doc = Parser::parse($body); + $doc = Parser::parse($body); $created = []; - $typeConfigDecorator = function($config, $node) use (&$created) { + $typeConfigDecorator = function ($config, $node) use (&$created) { $created[] = $node->name->value; + return $config; }; @@ -1282,5 +1298,4 @@ type World implements Hello { $this->assertArrayHasKey('Hello', $types); $this->assertArrayHasKey('World', $types); } - } diff --git a/tests/Utils/CoerceValueTest.php b/tests/Utils/CoerceValueTest.php index 0585214..803e272 100644 --- a/tests/Utils/CoerceValueTest.php +++ b/tests/Utils/CoerceValueTest.php @@ -1,8 +1,9 @@ testEnum = new EnumType([ - 'name' => 'TestEnum', + 'name' => 'TestEnum', 'values' => [ 'FOO' => 'InternalFoo', 'BAR' => 123456789, @@ -26,7 +30,7 @@ class CoerceValueTest extends TestCase ]); $this->testInputObject = new InputObjectType([ - 'name' => 'TestInputObject', + 'name' => 'TestInputObject', 'fields' => [ 'foo' => Type::nonNull(Type::int()), 'bar' => Type::int(), @@ -34,9 +38,9 @@ class CoerceValueTest extends TestCase ]); } - // Describe: coerceValue - /** + * Describe: coerceValue + * * @see it('coercing an array to GraphQLString produces an error') */ public function testCoercingAnArrayToGraphQLStringProducesAnError() : void @@ -53,7 +57,17 @@ class CoerceValueTest extends TestCase ); } - // Describe: for GraphQLInt + /** + * Describe: for GraphQLInt + */ + private function expectError($result, $expected) + { + $this->assertInternalType('array', $result); + $this->assertInternalType('array', $result['errors']); + $this->assertCount(1, $result['errors']); + $this->assertEquals($expected, $result['errors'][0]->getMessage()); + $this->assertEquals(Utils::undefined(), $result['value']); + } /** * @see it('returns no error for int input') @@ -64,6 +78,13 @@ class CoerceValueTest extends TestCase $this->expectNoErrors($result); } + private function expectNoErrors($result) + { + $this->assertInternalType('array', $result); + $this->assertNull($result['errors']); + $this->assertNotEquals(Utils::undefined(), $result['value']); + } + /** * @see it('returns no error for negative int input') */ @@ -115,6 +136,8 @@ class CoerceValueTest extends TestCase ); } + // Describe: for GraphQLFloat + /** * @see it('returns a single error for char input') */ @@ -139,8 +162,6 @@ class CoerceValueTest extends TestCase ); } - // Describe: for GraphQLFloat - /** * @see it('returns no error for int input') */ @@ -189,6 +210,8 @@ class CoerceValueTest extends TestCase ); } + // DESCRIBE: for GraphQLEnum + /** * @see it('returns a single error for char input') */ @@ -213,8 +236,6 @@ class CoerceValueTest extends TestCase ); } - // DESCRIBE: for GraphQLEnum - /** * @see it('returns no error for a known enum name') */ @@ -229,6 +250,8 @@ class CoerceValueTest extends TestCase $this->assertEquals(123456789, $barResult['value']); } + // DESCRIBE: for GraphQLInputObject + /** * @see it('results error for misspelled enum value') */ @@ -250,8 +273,6 @@ class CoerceValueTest extends TestCase $this->expectError($result2, 'Expected type TestEnum.'); } - // DESCRIBE: for GraphQLInputObject - /** * @see it('returns no error for a valid input') */ @@ -277,7 +298,10 @@ class CoerceValueTest extends TestCase public function testReturnErrorForAnInvalidField() : void { $result = Value::coerceValue(['foo' => 'abc'], $this->testInputObject); - $this->expectError($result, 'Expected type Int at value.foo; Int cannot represent non 32-bit signed integer value: abc'); + $this->expectError( + $result, + 'Expected type Int at value.foo; Int cannot represent non 32-bit signed integer value: abc' + ); } /** @@ -286,10 +310,13 @@ class CoerceValueTest extends TestCase public function testReturnsMultipleErrorsForMultipleInvalidFields() : void { $result = Value::coerceValue(['foo' => 'abc', 'bar' => 'def'], $this->testInputObject); - $this->assertEquals([ - 'Expected type Int at value.foo; Int cannot represent non 32-bit signed integer value: abc', - 'Expected type Int at value.bar; Int cannot represent non 32-bit signed integer value: def', - ], $result['errors']); + $this->assertEquals( + [ + 'Expected type Int at value.foo; Int cannot represent non 32-bit signed integer value: abc', + 'Expected type Int at value.bar; Int cannot represent non 32-bit signed integer value: def', + ], + $result['errors'] + ); } /** @@ -318,20 +345,4 @@ class CoerceValueTest extends TestCase $result = Value::coerceValue(['foo' => 123, 'bart' => 123], $this->testInputObject); $this->expectError($result, 'Field "bart" is not defined by type TestInputObject; did you mean bar?'); } - - private function expectNoErrors($result) - { - $this->assertInternalType('array', $result); - $this->assertNull($result['errors']); - $this->assertNotEquals(Utils::undefined(), $result['value']); - } - - - private function expectError($result, $expected) { - $this->assertInternalType('array', $result); - $this->assertInternalType('array', $result['errors']); - $this->assertCount(1, $result['errors']); - $this->assertEquals($expected, $result['errors'][0]->getMessage()); - $this->assertEquals(Utils::undefined(), $result['value']); - } } diff --git a/tests/Utils/ExtractTypesTest.php b/tests/Utils/ExtractTypesTest.php index 8818899..33f60cf 100644 --- a/tests/Utils/ExtractTypesTest.php +++ b/tests/Utils/ExtractTypesTest.php @@ -1,5 +1,8 @@ node = new InterfaceType([ - 'name' => 'Node', + 'name' => 'Node', 'fields' => [ - 'id' => Type::string() - ] + 'id' => Type::string(), + ], ]); $this->content = new InterfaceType([ - 'name' => 'Content', - 'fields' => function() { + 'name' => 'Content', + 'fields' => function () { return [ - 'title' => Type::string(), - 'body' => Type::string(), - 'author' => $this->user, - 'comments' => Type::listOf($this->comment), - 'categories' => Type::listOf($this->category) + 'title' => Type::string(), + 'body' => Type::string(), + 'author' => $this->user, + 'comments' => Type::listOf($this->comment), + 'categories' => Type::listOf($this->category), ]; - } + }, ]); $this->blogStory = new ObjectType([ - 'name' => 'BlogStory', + 'name' => 'BlogStory', 'interfaces' => [ $this->node, - $this->content + $this->content, ], - 'fields' => function() { - return [ - $this->node->getField('id'), - $this->content->getField('title'), - $this->content->getField('body'), - $this->content->getField('author'), - $this->content->getField('comments'), - $this->content->getField('categories') - ]; - }, - ]); - - $this->link = new ObjectType([ - 'name' => 'Link', - 'interfaces' => [ - $this->node, - $this->content - ], - 'fields' => function() { + 'fields' => function () { return [ $this->node->getField('id'), $this->content->getField('title'), @@ -134,156 +90,176 @@ class ExtractTypesTest extends TestCase $this->content->getField('author'), $this->content->getField('comments'), $this->content->getField('categories'), - 'url' => Type::string() ]; }, ]); - $this->video = new ObjectType([ - 'name' => 'Video', + new ObjectType([ + 'name' => 'Link', 'interfaces' => [ $this->node, - $this->content + $this->content, ], - 'fields' => function() { + 'fields' => function () { return [ - $this->node->getField('id'), - $this->content->getField('title'), - $this->content->getField('body'), - $this->content->getField('author'), - $this->content->getField('comments'), - $this->content->getField('categories'), - 'streamUrl' => Type::string(), + 'id' => $this->node->getField('id'), + 'title' => $this->content->getField('title'), + 'body' => $this->content->getField('body'), + 'author' => $this->content->getField('author'), + 'comments' => $this->content->getField('comments'), + 'categories' => $this->content->getField('categories'), + 'url' => Type::string(), + ]; + }, + ]); + + new ObjectType([ + 'name' => 'Video', + 'interfaces' => [ + $this->node, + $this->content, + ], + 'fields' => function () { + return [ + 'id' => $this->node->getField('id'), + 'title' => $this->content->getField('title'), + 'body' => $this->content->getField('body'), + 'author' => $this->content->getField('author'), + 'comments' => $this->content->getField('comments'), + 'categories' => $this->content->getField('categories'), + 'streamUrl' => Type::string(), 'downloadUrl' => Type::string(), - 'metadata' => $this->videoMetadata = new ObjectType([ - 'name' => 'VideoMetadata', + 'metadata' => new ObjectType([ + 'name' => 'VideoMetadata', 'fields' => [ 'lat' => Type::float(), - 'lng' => Type::float() - ] - ]) + 'lng' => Type::float(), + ], + ]), ]; - } + }, ]); $this->comment = new ObjectType([ - 'name' => 'Comment', + 'name' => 'Comment', 'interfaces' => [ - $this->node + $this->node, ], - 'fields' => function() { + 'fields' => function () { return [ - $this->node->getField('id'), - 'author' => $this->user, - 'text' => Type::string(), + 'id' => $this->node->getField('id'), + 'author' => $this->user, + 'text' => Type::string(), 'replies' => Type::listOf($this->comment), - 'parent' => $this->comment, - 'content' => $this->content + 'parent' => $this->comment, + 'content' => $this->content, ]; - } + }, ]); $this->user = new ObjectType([ - 'name' => 'User', + 'name' => 'User', 'interfaces' => [ - $this->node + $this->node, ], - 'fields' => function() { + 'fields' => function () { return [ - $this->node->getField('id'), + 'id' => $this->node->getField('id'), 'name' => Type::string(), ]; - } + }, ]); $this->category = new ObjectType([ - 'name' => 'Category', + 'name' => 'Category', 'interfaces' => [ - $this->node + $this->node, ], - 'fields' => function() { + 'fields' => function () { return [ - $this->node->getField('id'), - 'name' => Type::string() + 'id' => $this->node->getField('id'), + 'name' => Type::string(), ]; - } + }, ]); $this->mention = new UnionType([ - 'name' => 'Mention', + 'name' => 'Mention', 'types' => [ $this->user, - $this->category - ] + $this->category, + ], ]); $this->query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ - 'viewer' => $this->user, + 'viewer' => $this->user, 'latestContent' => $this->content, - 'node' => $this->node, - 'mentions' => Type::listOf($this->mention) - ] + 'node' => $this->node, + 'mentions' => Type::listOf($this->mention), + ], + ]); + + $this->postStoryMutationInput = new InputObjectType([ + 'name' => 'PostStoryMutationInput', + 'fields' => [ + 'title' => Type::string(), + 'body' => Type::string(), + 'author' => Type::id(), + 'category' => Type::id(), + ], ]); $this->mutation = new ObjectType([ - 'name' => 'Mutation', + 'name' => 'Mutation', 'fields' => [ - 'postStory' => [ + 'postStory' => [ 'type' => $this->postStoryMutation = new ObjectType([ - 'name' => 'PostStoryMutation', + 'name' => 'PostStoryMutation', 'fields' => [ - 'story' => $this->blogStory - ] + 'story' => $this->blogStory, + ], ]), 'args' => [ - 'input' => Type::nonNull($this->postStoryMutationInput = new InputObjectType([ - 'name' => 'PostStoryMutationInput', - 'fields' => [ - 'title' => Type::string(), - 'body' => Type::string(), - 'author' => Type::id(), - 'category' => Type::id() - ] - ])), - 'clientRequestId' => Type::string() - ] + 'input' => Type::nonNull($this->postStoryMutationInput), + 'clientRequestId' => Type::string(), + ], ], 'postComment' => [ 'type' => $this->postCommentMutation = new ObjectType([ - 'name' => 'PostCommentMutation', + 'name' => 'PostCommentMutation', 'fields' => [ - 'comment' => $this->comment - ] + 'comment' => $this->comment, + ], ]), 'args' => [ - 'input' => Type::nonNull($this->postCommentMutationInput = new InputObjectType([ - 'name' => 'PostCommentMutationInput', + 'input' => Type::nonNull($this->postCommentMutationInput = new InputObjectType([ + 'name' => 'PostCommentMutationInput', 'fields' => [ - 'text' => Type::nonNull(Type::string()), - 'author' => Type::nonNull(Type::id()), + 'text' => Type::nonNull(Type::string()), + 'author' => Type::nonNull(Type::id()), 'content' => Type::id(), - 'parent' => Type::id() - ] + 'parent' => Type::id(), + ], ])), - 'clientRequestId' => Type::string() - ] - ] - ] + 'clientRequestId' => Type::string(), + ], + ], + ], ]); } public function testExtractTypesFromQuery() : void { $expectedTypeMap = [ - 'Query' => $this->query, - 'User' => $this->user, - 'Node' => $this->node, - 'String' => Type::string(), - 'Content' => $this->content, - 'Comment' => $this->comment, - 'Mention' => $this->mention, + 'Query' => $this->query, + 'User' => $this->user, + 'Node' => $this->node, + 'String' => Type::string(), + 'Content' => $this->content, + 'Comment' => $this->comment, + 'Mention' => $this->mention, 'Category' => $this->category, ]; @@ -294,19 +270,19 @@ class ExtractTypesTest extends TestCase public function testExtractTypesFromMutation() : void { $expectedTypeMap = [ - 'Mutation' => $this->mutation, - 'User' => $this->user, - 'Node' => $this->node, - 'String' => Type::string(), - 'Content' => $this->content, - 'Comment' => $this->comment, - 'BlogStory' => $this->blogStory, - 'Category' => $this->category, - 'PostStoryMutationInput' => $this->postStoryMutationInput, - 'ID' => Type::id(), - 'PostStoryMutation' => $this->postStoryMutation, + 'Mutation' => $this->mutation, + 'User' => $this->user, + 'Node' => $this->node, + 'String' => Type::string(), + 'Content' => $this->content, + 'Comment' => $this->comment, + 'BlogStory' => $this->blogStory, + 'Category' => $this->category, + 'PostStoryMutationInput' => $this->postStoryMutationInput, + 'ID' => Type::id(), + 'PostStoryMutation' => $this->postStoryMutation, 'PostCommentMutationInput' => $this->postCommentMutationInput, - 'PostCommentMutation' => $this->postCommentMutation, + 'PostCommentMutation' => $this->postCommentMutation, ]; $actualTypeMap = TypeInfo::extractTypes($this->mutation); @@ -316,16 +292,16 @@ class ExtractTypesTest extends TestCase public function testThrowsOnMultipleTypesWithSameName() : void { $otherUserType = new ObjectType([ - 'name' => 'User', - 'fields' => ['a' => Type::string()] + 'name' => 'User', + 'fields' => ['a' => Type::string()], ]); $queryType = new ObjectType([ - 'name' => 'Test', + 'name' => 'Test', 'fields' => [ 'otherUser' => $otherUserType, - 'user' => $this->user - ] + 'user' => $this->user, + ], ]); $this->expectException(InvariantViolation::class); diff --git a/tests/Utils/IsValidLiteralValueTest.php b/tests/Utils/IsValidLiteralValueTest.php index 9f322e4..0e4bc04 100644 --- a/tests/Utils/IsValidLiteralValueTest.php +++ b/tests/Utils/IsValidLiteralValueTest.php @@ -1,17 +1,18 @@ mixedStore = new MixedStore(); } + public function testAcceptsNullKeys() : void + { + foreach ($this->getPossibleValues() as $value) { + $this->assertAcceptsKeyValue(null, $value); + } + } + public function getPossibleValues() { return [ @@ -30,16 +37,38 @@ class MixedStoreTest extends TestCase 'a', [], new \stdClass(), - function() {}, - new MixedStore() + function () { + }, + new MixedStore(), ]; } - public function testAcceptsNullKeys() : void + private function assertAcceptsKeyValue($key, $value) { - foreach ($this->getPossibleValues() as $value) { - $this->assertAcceptsKeyValue(null, $value); - } + $err = 'Failed assertion that MixedStore accepts key ' . + Utils::printSafe($key) . ' with value ' . Utils::printSafe($value); + + $this->assertFalse($this->mixedStore->offsetExists($key), $err); + $this->mixedStore->offsetSet($key, $value); + $this->assertTrue($this->mixedStore->offsetExists($key), $err); + $this->assertSame($value, $this->mixedStore->offsetGet($key), $err); + $this->mixedStore->offsetUnset($key); + $this->assertFalse($this->mixedStore->offsetExists($key), $err); + $this->assertProvidesArrayAccess($key, $value); + } + + private function assertProvidesArrayAccess($key, $value) + { + $err = 'Failed assertion that MixedStore provides array access for key ' . + Utils::printSafe($key) . ' with value ' . Utils::printSafe($value); + + $this->assertFalse(isset($this->mixedStore[$key]), $err); + $this->mixedStore[$key] = $value; + $this->assertTrue(isset($this->mixedStore[$key]), $err); + $this->assertEquals(! empty($value), ! empty($this->mixedStore[$key]), $err); + $this->assertSame($value, $this->mixedStore[$key], $err); + unset($this->mixedStore[$key]); + $this->assertFalse(isset($this->mixedStore[$key]), $err); } public function testAcceptsBoolKeys() : void @@ -93,35 +122,11 @@ class MixedStoreTest extends TestCase foreach ($this->getPossibleValues() as $value) { $this->assertAcceptsKeyValue(new \stdClass(), $value); $this->assertAcceptsKeyValue(new MixedStore(), $value); - $this->assertAcceptsKeyValue(function() {}, $value); + $this->assertAcceptsKeyValue( + function () { + }, + $value + ); } } - - private function assertAcceptsKeyValue($key, $value) - { - $err = 'Failed assertion that MixedStore accepts key ' . - Utils::printSafe($key) . ' with value ' . Utils::printSafe($value); - - $this->assertFalse($this->mixedStore->offsetExists($key), $err); - $this->mixedStore->offsetSet($key, $value); - $this->assertTrue($this->mixedStore->offsetExists($key), $err); - $this->assertSame($value, $this->mixedStore->offsetGet($key), $err); - $this->mixedStore->offsetUnset($key); - $this->assertFalse($this->mixedStore->offsetExists($key), $err); - $this->assertProvidesArrayAccess($key, $value); - } - - private function assertProvidesArrayAccess($key, $value) - { - $err = 'Failed assertion that MixedStore provides array access for key ' . - Utils::printSafe($key) . ' with value ' . Utils::printSafe($value); - - $this->assertFalse(isset($this->mixedStore[$key]), $err); - $this->mixedStore[$key] = $value; - $this->assertTrue(isset($this->mixedStore[$key]), $err); - $this->assertEquals(!empty($value), !empty($this->mixedStore[$key]), $err); - $this->assertSame($value, $this->mixedStore[$key], $err); - unset($this->mixedStore[$key]); - $this->assertFalse(isset($this->mixedStore[$key]), $err); - } } diff --git a/tests/Utils/QuotedOrListTest.php b/tests/Utils/QuotedOrListTest.php index 4b0c7b1..65d393b 100644 --- a/tests/Utils/QuotedOrListTest.php +++ b/tests/Utils/QuotedOrListTest.php @@ -1,16 +1,15 @@ assertEquals($schemaText, SchemaPrinter::doPrint(BuildSchema::build($schemaText))); - return "\n" . $schemaText; - } - - private function printSingleFieldSchema($fieldConfig) - { - $query = new ObjectType([ - 'name' => 'Query', - 'fields' => [ - 'singleField' => $fieldConfig - ] - ]); - return $this->printForTest(new Schema(['query' => $query])); - } - /** * @see it('Prints String Field') */ public function testPrintsStringField() : void { $output = $this->printSingleFieldSchema([ - 'type' => Type::string() + 'type' => Type::string(), ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField: String } -', $output); +', + $output + ); + } + + private function printSingleFieldSchema($fieldConfig) + { + $query = new ObjectType([ + 'name' => 'Query', + 'fields' => ['singleField' => $fieldConfig], + ]); + + return $this->printForTest(new Schema(['query' => $query])); + } + + private function printForTest($schema) + { + $schemaText = SchemaPrinter::doPrint($schema); + $this->assertEquals($schemaText, SchemaPrinter::doPrint(BuildSchema::build($schemaText))); + + return "\n" . $schemaText; } /** @@ -58,13 +64,16 @@ type Query { public function testPrintArrayStringField() : void { $output = $this->printSingleFieldSchema([ - 'type' => Type::listOf(Type::string()) + 'type' => Type::listOf(Type::string()), ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField: [String] } -', $output); +', + $output + ); } /** @@ -73,13 +82,16 @@ type Query { public function testPrintNonNullStringField() : void { $output = $this->printSingleFieldSchema([ - 'type' => Type::nonNull(Type::string()) + 'type' => Type::nonNull(Type::string()), ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField: String! } -', $output); +', + $output + ); } /** @@ -88,13 +100,16 @@ type Query { public function testPrintNonNullArrayStringField() : void { $output = $this->printSingleFieldSchema([ - 'type' => Type::nonNull(Type::listOf(Type::string())) + 'type' => Type::nonNull(Type::listOf(Type::string())), ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField: [String]! } -', $output); +', + $output + ); } /** @@ -103,13 +118,16 @@ type Query { public function testPrintArrayNonNullStringField() : void { $output = $this->printSingleFieldSchema([ - 'type' => Type::listOf(Type::nonNull(Type::string())) + 'type' => Type::listOf(Type::nonNull(Type::string())), ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField: [String!] } -', $output); +', + $output + ); } /** @@ -118,13 +136,16 @@ type Query { public function testPrintNonNullArrayNonNullStringField() : void { $output = $this->printSingleFieldSchema([ - 'type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string()))) + 'type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string()))), ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField: [String!]! } -', $output); +', + $output + ); } /** @@ -133,18 +154,19 @@ type Query { public function testPrintObjectField() : void { $fooType = new ObjectType([ - 'name' => 'Foo', - 'fields' => ['str' => ['type' => Type::string()]] + 'name' => 'Foo', + 'fields' => ['str' => ['type' => Type::string()]], ]); $root = new ObjectType([ - 'name' => 'Query', - 'fields' => ['foo' => ['type' => $fooType]] + 'name' => 'Query', + 'fields' => ['foo' => ['type' => $fooType]], ]); $schema = new Schema(['query' => $root]); $output = $this->printForTest($schema); - $this->assertEquals(' + $this->assertEquals( + ' type Foo { str: String } @@ -152,7 +174,9 @@ type Foo { type Query { foo: Foo } -', $output); +', + $output + ); } /** @@ -162,13 +186,16 @@ type Query { { $output = $this->printSingleFieldSchema([ 'type' => Type::string(), - 'args' => ['argOne' => ['type' => Type::int()]] + 'args' => ['argOne' => ['type' => Type::int()]], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: Int): String } -', $output); +', + $output + ); } /** @@ -178,13 +205,16 @@ type Query { { $output = $this->printSingleFieldSchema([ 'type' => Type::string(), - 'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => 2]] + 'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => 2]], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: Int = 2): String } -', $output); +', + $output + ); } /** @@ -196,11 +226,14 @@ type Query { 'type' => Type::string(), 'args' => ['argOne' => ['type' => Type::string(), 'defaultValue' => "tes\t de\fault"]], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: String = "tes\t de\fault"): String } -', $output); +', + $output + ); } /** @@ -210,13 +243,16 @@ type Query { { $output = $this->printSingleFieldSchema([ 'type' => Type::string(), - 'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => null]] + 'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => null]], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: Int = null): String } -', $output); +', + $output + ); } /** @@ -226,13 +262,16 @@ type Query { { $output = $this->printSingleFieldSchema([ 'type' => Type::string(), - 'args' => ['argOne' => ['type' => Type::nonNull(Type::int())]] + 'args' => ['argOne' => ['type' => Type::nonNull(Type::int())]], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: Int!): String } -', $output); +', + $output + ); } /** @@ -244,14 +283,17 @@ type Query { 'type' => Type::string(), 'args' => [ 'argOne' => ['type' => Type::int()], - 'argTwo' => ['type' => Type::string()] - ] + 'argTwo' => ['type' => Type::string()], + ], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: Int, argTwo: String): String } -', $output); +', + $output + ); } /** @@ -262,16 +304,19 @@ type Query { $output = $this->printSingleFieldSchema([ 'type' => Type::string(), 'args' => [ - 'argOne' => ['type' => Type::int(), 'defaultValue' => 1], - 'argTwo' => ['type' => Type::string()], - 'argThree' => ['type' => Type::boolean()] - ] + 'argOne' => ['type' => Type::int(), 'defaultValue' => 1], + 'argTwo' => ['type' => Type::string()], + 'argThree' => ['type' => Type::boolean()], + ], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String } -', $output); +', + $output + ); } /** @@ -282,16 +327,19 @@ type Query { $output = $this->printSingleFieldSchema([ 'type' => Type::string(), 'args' => [ - 'argOne' => ['type' => Type::int()], - 'argTwo' => ['type' => Type::string(), 'defaultValue' => 'foo'], - 'argThree' => ['type' => Type::boolean()] - ] + 'argOne' => ['type' => Type::int()], + 'argTwo' => ['type' => Type::string(), 'defaultValue' => 'foo'], + 'argThree' => ['type' => Type::boolean()], + ], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String } -', $output); +', + $output + ); } /** @@ -302,16 +350,19 @@ type Query { $output = $this->printSingleFieldSchema([ 'type' => Type::string(), 'args' => [ - 'argOne' => ['type' => Type::int()], - 'argTwo' => ['type' => Type::string()], - 'argThree' => ['type' => Type::boolean(), 'defaultValue' => false] - ] + 'argOne' => ['type' => Type::int()], + 'argTwo' => ['type' => Type::string()], + 'argThree' => ['type' => Type::boolean(), 'defaultValue' => false], + ], ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String } -', $output); +', + $output + ); } /** @@ -320,14 +371,12 @@ type Query { public function testPrintsCustomQueryRootType() : void { $customQueryType = new ObjectType([ - 'name' => 'CustomQueryType', + 'name' => 'CustomQueryType', 'fields' => ['bar' => ['type' => Type::string()]], ]); - $schema = new Schema([ - 'query' => $customQueryType, - ]); - $output = $this->printForTest($schema); + $schema = new Schema(['query' => $customQueryType]); + $output = $this->printForTest($schema); $expected = ' schema { query: CustomQueryType @@ -346,27 +395,28 @@ type CustomQueryType { public function testPrintInterface() : void { $fooType = new InterfaceType([ - 'name' => 'Foo', - 'fields' => ['str' => ['type' => Type::string()]] + 'name' => 'Foo', + 'fields' => ['str' => ['type' => Type::string()]], ]); $barType = new ObjectType([ - 'name' => 'Bar', - 'fields' => ['str' => ['type' => Type::string()]], - 'interfaces' => [$fooType] + 'name' => 'Bar', + 'fields' => ['str' => ['type' => Type::string()]], + 'interfaces' => [$fooType], ]); $query = new ObjectType([ - 'name' => 'Query', - 'fields' => ['bar' => ['type' => $barType]] + 'name' => 'Query', + 'fields' => ['bar' => ['type' => $barType]], ]); $schema = new Schema([ 'query' => $query, - 'types' => [$barType] + 'types' => [$barType], ]); $output = $this->printForTest($schema); - $this->assertEquals(' + $this->assertEquals( + ' type Bar implements Foo { str: String } @@ -378,7 +428,9 @@ interface Foo { type Query { bar: Bar } -', $output); +', + $output + ); } /** @@ -387,35 +439,36 @@ type Query { public function testPrintMultipleInterface() : void { $fooType = new InterfaceType([ - 'name' => 'Foo', - 'fields' => ['str' => ['type' => Type::string()]] + 'name' => 'Foo', + 'fields' => ['str' => ['type' => Type::string()]], ]); $baazType = new InterfaceType([ - 'name' => 'Baaz', - 'fields' => ['int' => ['type' => Type::int()]] + 'name' => 'Baaz', + 'fields' => ['int' => ['type' => Type::int()]], ]); $barType = new ObjectType([ - 'name' => 'Bar', - 'fields' => [ + 'name' => 'Bar', + 'fields' => [ 'str' => ['type' => Type::string()], - 'int' => ['type' => Type::int()] + 'int' => ['type' => Type::int()], ], - 'interfaces' => [$fooType, $baazType] + 'interfaces' => [$fooType, $baazType], ]); $query = new ObjectType([ - 'name' => 'Query', - 'fields' => ['bar' => ['type' => $barType]] + 'name' => 'Query', + 'fields' => ['bar' => ['type' => $barType]], ]); $schema = new Schema([ 'query' => $query, - 'types' => [$barType] + 'types' => [$barType], ]); $output = $this->printForTest($schema); - $this->assertEquals(' + $this->assertEquals( + ' interface Baaz { int: Int } @@ -432,7 +485,9 @@ interface Foo { type Query { bar: Bar } -', $output); +', + $output + ); } /** @@ -441,36 +496,37 @@ type Query { public function testPrintUnions() : void { $fooType = new ObjectType([ - 'name' => 'Foo', - 'fields' => ['bool' => ['type' => Type::boolean()]] + 'name' => 'Foo', + 'fields' => ['bool' => ['type' => Type::boolean()]], ]); $barType = new ObjectType([ - 'name' => 'Bar', - 'fields' => ['str' => ['type' => Type::string()]] + 'name' => 'Bar', + 'fields' => ['str' => ['type' => Type::string()]], ]); $singleUnion = new UnionType([ - 'name' => 'SingleUnion', - 'types' => [$fooType] + 'name' => 'SingleUnion', + 'types' => [$fooType], ]); $multipleUnion = new UnionType([ - 'name' => 'MultipleUnion', - 'types' => [$fooType, $barType] + 'name' => 'MultipleUnion', + 'types' => [$fooType, $barType], ]); $query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ - 'single' => ['type' => $singleUnion], - 'multiple' => ['type' => $multipleUnion] - ] + 'single' => ['type' => $singleUnion], + 'multiple' => ['type' => $multipleUnion], + ], ]); $schema = new Schema(['query' => $query]); $output = $this->printForTest($schema); - $this->assertEquals(' + $this->assertEquals( + ' type Bar { str: String } @@ -487,7 +543,9 @@ type Query { } union SingleUnion = Foo -', $output); +', + $output + ); } /** @@ -496,23 +554,24 @@ union SingleUnion = Foo public function testInputType() : void { $inputType = new InputObjectType([ - 'name' => 'InputType', - 'fields' => ['int' => ['type' => Type::int()]] + 'name' => 'InputType', + 'fields' => ['int' => ['type' => Type::int()]], ]); $query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ 'str' => [ 'type' => Type::string(), - 'args' => ['argOne' => ['type' => $inputType]] - ] - ] + 'args' => ['argOne' => ['type' => $inputType]], + ], + ], ]); $schema = new Schema(['query' => $query]); $output = $this->printForTest($schema); - $this->assertEquals(' + $this->assertEquals( + ' input InputType { int: Int } @@ -520,7 +579,9 @@ input InputType { type Query { str(argOne: InputType): String } -', $output); +', + $output + ); } /** @@ -529,28 +590,31 @@ type Query { public function testCustomScalar() : void { $oddType = new CustomScalarType([ - 'name' => 'Odd', - 'serialize' => function($value) { + 'name' => 'Odd', + 'serialize' => function ($value) { return $value % 2 === 1 ? $value : null; - } + }, ]); $query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ - 'odd' => ['type' => $oddType] - ] + 'odd' => ['type' => $oddType], + ], ]); $schema = new Schema(['query' => $query]); $output = $this->printForTest($schema); - $this->assertEquals(' + $this->assertEquals( + ' scalar Odd type Query { odd: Odd } -', $output); +', + $output + ); } /** @@ -559,24 +623,25 @@ type Query { public function testEnum() : void { $RGBType = new EnumType([ - 'name' => 'RGB', + 'name' => 'RGB', 'values' => [ - 'RED' => ['value' => 0], + 'RED' => ['value' => 0], 'GREEN' => ['value' => 1], - 'BLUE' => ['value' => 2] - ] + 'BLUE' => ['value' => 2], + ], ]); $query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ - 'rgb' => ['type' => $RGBType] - ] + 'rgb' => ['type' => $RGBType], + ], ]); $schema = new Schema(['query' => $query]); $output = $this->printForTest($schema); - $this->assertEquals(' + $this->assertEquals( + ' type Query { rgb: RGB } @@ -586,7 +651,9 @@ enum RGB { GREEN BLUE } -', $output); +', + $output + ); } /** @@ -595,32 +662,35 @@ enum RGB { public function testPrintsCustomDirectives() : void { $query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ 'field' => ['type' => Type::string()], - ] + ], ]); $customDirectives = new Directive([ - 'name' => 'customDirective', + 'name' => 'customDirective', 'locations' => [ - DirectiveLocation::FIELD - ] + DirectiveLocation::FIELD, + ], ]); $schema = new Schema([ - 'query' => $query, + 'query' => $query, 'directives' => [$customDirectives], ]); $output = $this->printForTest($schema); - $this->assertEquals(' + $this->assertEquals( + ' directive @customDirective on FIELD type Query { field: String } -', $output); +', + $output + ); } /** @@ -629,19 +699,22 @@ type Query { public function testOneLinePrintsAShortDescription() : void { $description = 'This field is awesome'; - $output = $this->printSingleFieldSchema([ - "type" => Type::string(), - "description" => $description + $output = $this->printSingleFieldSchema([ + 'type' => Type::string(), + 'description' => $description, ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { """This field is awesome""" singleField: String } -', $output); +', + $output + ); - $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; + $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; $recreatedField = $recreatedRoot->getFields()['singleField']; $this->assertEquals($description, $recreatedField->description); } @@ -652,21 +725,24 @@ type Query { public function testDoesNotOneLinePrintADescriptionThatEndsWithAQuote() : void { $description = 'This field is "awesome"'; - $output = $this->printSingleFieldSchema([ - "type" => Type::string(), - "description" => $description + $output = $this->printSingleFieldSchema([ + 'type' => Type::string(), + 'description' => $description, ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { """ This field is "awesome" """ singleField: String } -', $output); +', + $output + ); - $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; + $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; $recreatedField = $recreatedRoot->getFields()['singleField']; $this->assertEquals($description, $recreatedField->description); } @@ -677,20 +753,23 @@ type Query { public function testPReservesLeadingSpacesWhenPrintingADescription() : void { $description = ' This field is "awesome"'; - $output = $this->printSingleFieldSchema([ - "type" => Type::string(), - "description" => $description + $output = $this->printSingleFieldSchema([ + 'type' => Type::string(), + 'description' => $description, ]); - $this->assertEquals(' + $this->assertEquals( + ' type Query { """ This field is "awesome" """ singleField: String } -', $output); +', + $output + ); - $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; + $recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query']; $recreatedField = $recreatedRoot->getFields()['singleField']; $this->assertEquals($description, $recreatedField->description); } @@ -701,14 +780,14 @@ type Query { public function testPrintIntrospectionSchema() : void { $query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ - 'onlyField' => ['type' => Type::string()] - ] + 'onlyField' => ['type' => Type::string()], + ], ]); - $schema = new Schema(['query' => $query]); - $output = SchemaPrinter::printIntrosepctionSchema($schema); + $schema = new Schema(['query' => $query]); + $output = SchemaPrinter::printIntrosepctionSchema($schema); $introspectionSchema = <<<'EOT' """ Directs the executor to include this field or fragment only when the `if` argument is true. @@ -946,16 +1025,17 @@ EOT; public function testPrintIntrospectionSchemaWithCommentDescriptions() : void { $query = new ObjectType([ - 'name' => 'Query', + 'name' => 'Query', 'fields' => [ - 'onlyField' => ['type' => Type::string()] - ] + 'onlyField' => ['type' => Type::string()], + ], ]); - $schema = new Schema(['query' => $query]); - $output = SchemaPrinter::printIntrosepctionSchema($schema, [ - 'commentDescriptions' => true - ]); + $schema = new Schema(['query' => $query]); + $output = SchemaPrinter::printIntrosepctionSchema( + $schema, + ['commentDescriptions' => true] + ); $introspectionSchema = <<<'EOT' # Directs the executor to include this field or fragment only when the `if` argument is true. directive @include( diff --git a/tests/Utils/SuggestionListTest.php b/tests/Utils/SuggestionListTest.php index dcb152f..465f3a2 100644 --- a/tests/Utils/SuggestionListTest.php +++ b/tests/Utils/SuggestionListTest.php @@ -1,16 +1,15 @@ assertEquals($expected, AST::valueFromAST(Parser::parseValue($valueText), $type)); - } - - private function runTestCaseWithVars($variables, $type, $valueText, $expected) - { - $this->assertEquals($expected, AST::valueFromAST(Parser::parseValue($valueText), $type, $variables)); - } + /** @var InputObjectType */ + private $inputObj; /** * @see it('rejects empty input') @@ -45,6 +40,11 @@ class ValueFromAstTest extends TestCase $this->runTestCase(Type::id(), '"123456"', '123456'); } + private function runTestCase($type, $valueText, $expected) + { + $this->assertEquals($expected, AST::valueFromAST(Parser::parseValue($valueText), $type)); + } + /** * @see it('does not convert when input coercion rules reject a value') */ @@ -68,13 +68,13 @@ class ValueFromAstTest extends TestCase public function testConvertsEnumValuesAccordingToInputCoercionRules() : void { $testEnum = new EnumType([ - 'name' => 'TestColor', + 'name' => 'TestColor', 'values' => [ - 'RED' => ['value' => 1], + 'RED' => ['value' => 1], 'GREEN' => ['value' => 2], - 'BLUE' => ['value' => 3], - 'NULL' => ['value' => null], - ] + 'BLUE' => ['value' => 3], + 'NULL' => ['value' => null], + ], ]); $this->runTestCase($testEnum, 'RED', 1); @@ -100,14 +100,14 @@ class ValueFromAstTest extends TestCase public function testCoercesListsOfValues() : void { $listOfBool = Type::listOf(Type::boolean()); - $undefined = Utils::undefined(); + $undefined = Utils::undefined(); - $this->runTestCase($listOfBool, 'true', [ true ]); + $this->runTestCase($listOfBool, 'true', [true]); $this->runTestCase($listOfBool, '123', $undefined); $this->runTestCase($listOfBool, 'null', null); - $this->runTestCase($listOfBool, '[true, false]', [ true, false ]); + $this->runTestCase($listOfBool, '[true, false]', [true, false]); $this->runTestCase($listOfBool, '[true, 123]', $undefined); - $this->runTestCase($listOfBool, '[true, null]', [ true, null ]); + $this->runTestCase($listOfBool, '[true, null]', [true, null]); $this->runTestCase($listOfBool, '{ true: true }', $undefined); } @@ -117,14 +117,14 @@ class ValueFromAstTest extends TestCase public function testCoercesNonNullListsOfValues() : void { $nonNullListOfBool = Type::nonNull(Type::listOf(Type::boolean())); - $undefined = Utils::undefined(); + $undefined = Utils::undefined(); - $this->runTestCase($nonNullListOfBool, 'true', [ true ]); + $this->runTestCase($nonNullListOfBool, 'true', [true]); $this->runTestCase($nonNullListOfBool, '123', $undefined); $this->runTestCase($nonNullListOfBool, 'null', $undefined); - $this->runTestCase($nonNullListOfBool, '[true, false]', [ true, false ]); + $this->runTestCase($nonNullListOfBool, '[true, false]', [true, false]); $this->runTestCase($nonNullListOfBool, '[true, 123]', $undefined); - $this->runTestCase($nonNullListOfBool, '[true, null]', [ true, null ]); + $this->runTestCase($nonNullListOfBool, '[true, null]', [true, null]); } /** @@ -133,12 +133,12 @@ class ValueFromAstTest extends TestCase public function testCoercesListsOfNonNullValues() : void { $listOfNonNullBool = Type::listOf(Type::nonNull(Type::boolean())); - $undefined = Utils::undefined(); + $undefined = Utils::undefined(); - $this->runTestCase($listOfNonNullBool, 'true', [ true ]); + $this->runTestCase($listOfNonNullBool, 'true', [true]); $this->runTestCase($listOfNonNullBool, '123', $undefined); $this->runTestCase($listOfNonNullBool, 'null', null); - $this->runTestCase($listOfNonNullBool, '[true, false]', [ true, false ]); + $this->runTestCase($listOfNonNullBool, '[true, false]', [true, false]); $this->runTestCase($listOfNonNullBool, '[true, 123]', $undefined); $this->runTestCase($listOfNonNullBool, '[true, null]', $undefined); } @@ -149,56 +149,63 @@ class ValueFromAstTest extends TestCase public function testCoercesNonNullListsOfNonNullValues() : void { $nonNullListOfNonNullBool = Type::nonNull(Type::listOf(Type::nonNull(Type::boolean()))); - $undefined = Utils::undefined(); + $undefined = Utils::undefined(); - $this->runTestCase($nonNullListOfNonNullBool, 'true', [ true ]); + $this->runTestCase($nonNullListOfNonNullBool, 'true', [true]); $this->runTestCase($nonNullListOfNonNullBool, '123', $undefined); $this->runTestCase($nonNullListOfNonNullBool, 'null', $undefined); - $this->runTestCase($nonNullListOfNonNullBool, '[true, false]', [ true, false ]); + $this->runTestCase($nonNullListOfNonNullBool, '[true, false]', [true, false]); $this->runTestCase($nonNullListOfNonNullBool, '[true, 123]', $undefined); $this->runTestCase($nonNullListOfNonNullBool, '[true, null]', $undefined); } - private $inputObj; - - private function inputObj() - { - return $this->inputObj ?: $this->inputObj = new InputObjectType([ - 'name' => 'TestInput', - 'fields' => [ - 'int' => [ 'type' => Type::int(), 'defaultValue' => 42 ], - 'bool' => [ 'type' => Type::boolean() ], - 'requiredBool' => [ 'type' => Type::nonNull(Type::boolean()) ], - ] - ]); - } - /** * @see it('coerces input objects according to input coercion rules') */ public function testCoercesInputObjectsAccordingToInputCoercionRules() : void { $testInputObj = $this->inputObj(); - $undefined = Utils::undefined(); + $undefined = Utils::undefined(); $this->runTestCase($testInputObj, 'null', null); $this->runTestCase($testInputObj, '123', $undefined); $this->runTestCase($testInputObj, '[]', $undefined); $this->runTestCase($testInputObj, '{ int: 123, requiredBool: false }', ['int' => 123, 'requiredBool' => false]); - $this->runTestCase($testInputObj, '{ bool: true, requiredBool: false }', [ 'int' => 42, 'bool' => true, 'requiredBool' => false ]); + $this->runTestCase( + $testInputObj, + '{ bool: true, requiredBool: false }', + ['int' => 42, 'bool' => true, 'requiredBool' => false] + ); $this->runTestCase($testInputObj, '{ int: true, requiredBool: true }', $undefined); $this->runTestCase($testInputObj, '{ requiredBool: null }', $undefined); $this->runTestCase($testInputObj, '{ bool: true }', $undefined); } + private function inputObj() + { + return $this->inputObj ?: $this->inputObj = new InputObjectType([ + 'name' => 'TestInput', + 'fields' => [ + 'int' => ['type' => Type::int(), 'defaultValue' => 42], + 'bool' => ['type' => Type::boolean()], + 'requiredBool' => ['type' => Type::nonNull(Type::boolean())], + ], + ]); + } + /** * @see it('accepts variable values assuming already coerced') */ public function testAcceptsVariableValuesAssumingAlreadyCoerced() : void { $this->runTestCaseWithVars([], Type::boolean(), '$var', Utils::undefined()); - $this->runTestCaseWithVars([ 'var' => true ], Type::boolean(), '$var', true); - $this->runTestCaseWithVars([ 'var' => null ], Type::boolean(), '$var', null); + $this->runTestCaseWithVars(['var' => true], Type::boolean(), '$var', true); + $this->runTestCaseWithVars(['var' => null], Type::boolean(), '$var', null); + } + + private function runTestCaseWithVars($variables, $type, $valueText, $expected) + { + $this->assertEquals($expected, AST::valueFromAST(Parser::parseValue($valueText), $type, $variables)); } /** @@ -206,16 +213,16 @@ class ValueFromAstTest extends TestCase */ public function testAssertsVariablesAreProvidedAsItemsInLists() : void { - $listOfBool = Type::listOf(Type::boolean()); + $listOfBool = Type::listOf(Type::boolean()); $listOfNonNullBool = Type::listOf(Type::nonNull(Type::boolean())); - $this->runTestCaseWithVars([], $listOfBool, '[ $foo ]', [ null ]); + $this->runTestCaseWithVars([], $listOfBool, '[ $foo ]', [null]); $this->runTestCaseWithVars([], $listOfNonNullBool, '[ $foo ]', Utils::undefined()); - $this->runTestCaseWithVars([ 'foo' => true ], $listOfNonNullBool, '[ $foo ]', [ true ]); + $this->runTestCaseWithVars(['foo' => true], $listOfNonNullBool, '[ $foo ]', [true]); // Note: variables are expected to have already been coerced, so we // do not expect the singleton wrapping behavior for variables. - $this->runTestCaseWithVars([ 'foo' => true ], $listOfNonNullBool, '$foo', true); - $this->runTestCaseWithVars([ 'foo' => [ true ] ], $listOfNonNullBool, '$foo', [ true ]); + $this->runTestCaseWithVars(['foo' => true], $listOfNonNullBool, '$foo', true); + $this->runTestCaseWithVars(['foo' => [true]], $listOfNonNullBool, '$foo', [true]); } /** @@ -229,7 +236,7 @@ class ValueFromAstTest extends TestCase [], $testInputObj, '{ int: $foo, bool: $foo, requiredBool: true }', - [ 'int' => 42, 'requiredBool' => true ] + ['int' => 42, 'requiredBool' => true] ); $this->runTestCaseWithVars( [], @@ -238,10 +245,10 @@ class ValueFromAstTest extends TestCase Utils::undefined() ); $this->runTestCaseWithVars( - [ 'foo' => true ], + ['foo' => true], $testInputObj, '{ requiredBool: $foo }', - [ 'int' => 42, 'requiredBool' => true ] + ['int' => 42, 'requiredBool' => true] ); } }