From caa50d6db9aaa67c8c7ca3e241ad4fa5264dc239 Mon Sep 17 00:00:00 2001 From: Simon Podlipsky Date: Sun, 2 Sep 2018 13:08:49 +0200 Subject: [PATCH] Fix CS in tests/Validator --- tests/Validator/DisableIntrospectionTest.php | 56 +- tests/Validator/ExecutableDefinitionsTest.php | 28 +- tests/Validator/FieldsOnCorrectTypeTest.php | 159 ++- .../FragmentsOnCompositeTypesTest.php | 145 +- tests/Validator/KnownArgumentNamesTest.php | 159 ++- tests/Validator/KnownDirectivesTest.php | 114 +- tests/Validator/KnownFragmentNamesTest.php | 29 +- tests/Validator/KnownTypeNamesTest.php | 53 +- .../Validator/LoneAnonymousOperationTest.php | 121 +- tests/Validator/NoFragmentCyclesTest.php | 287 ++-- tests/Validator/NoUndefinedVariablesTest.php | 220 +-- tests/Validator/NoUnusedFragmentsTest.php | 73 +- tests/Validator/NoUnusedVariablesTest.php | 138 +- .../OverlappingFieldsCanBeMergedTest.php | 1001 ++++++++------ .../Validator/PossibleFragmentSpreadsTest.php | 170 ++- .../ProvidedNonNullArgumentsTest.php | 163 ++- tests/Validator/QueryComplexityTest.php | 107 +- tests/Validator/QueryDepthTest.php | 159 ++- tests/Validator/QuerySecuritySchema.php | 35 +- tests/Validator/QuerySecurityTestCase.php | 95 +- tests/Validator/ScalarLeafsTest.php | 167 ++- tests/Validator/UniqueArgumentNamesTest.php | 175 ++- .../UniqueDirectivesPerLocationTest.php | 110 +- tests/Validator/UniqueFragmentNamesTest.php | 85 +- tests/Validator/UniqueInputFieldNamesTest.php | 76 +- tests/Validator/UniqueOperationNamesTest.php | 128 +- tests/Validator/UniqueVariableNamesTest.php | 29 +- tests/Validator/ValidationTest.php | 12 +- tests/Validator/ValidatorTestCase.php | 303 ++-- tests/Validator/ValuesOfCorrectTypeTest.php | 1222 ++++++++++------- .../Validator/VariablesAreInputTypesTest.php | 20 +- .../VariablesDefaultValueAllowedTest.php | 179 +-- .../VariablesInAllowedPositionTest.php | 235 ++-- 33 files changed, 3694 insertions(+), 2359 deletions(-) diff --git a/tests/Validator/DisableIntrospectionTest.php b/tests/Validator/DisableIntrospectionTest.php index 461daae..f5d61b9 100644 --- a/tests/Validator/DisableIntrospectionTest.php +++ b/tests/Validator/DisableIntrospectionTest.php @@ -1,4 +1,7 @@ expectFailsRule(new DisableIntrospection(DisableIntrospection::ENABLED), ' + $this->expectFailsRule( + new DisableIntrospection(DisableIntrospection::ENABLED), + ' query { __schema { queryType { @@ -23,16 +27,26 @@ class DisableIntrospectionTest extends ValidatorTestCase } } ', - [$this->error(3, 9)] + [$this->error(3, 9)] ); } - + + private function error($line, $column) + { + return FormattedError::create( + DisableIntrospection::introspectionDisabledMessage(), + [new SourceLocation($line, $column)] + ); + } + /** * @see it('fails if the query contains __type') */ public function testQueryContainsType() : void { - $this->expectFailsRule(new DisableIntrospection(DisableIntrospection::ENABLED), ' + $this->expectFailsRule( + new DisableIntrospection(DisableIntrospection::ENABLED), + ' query { __type( name: "Query" @@ -50,7 +64,9 @@ class DisableIntrospectionTest extends ValidatorTestCase */ public function testValidQuery() : void { - $this->expectPassesRule(new DisableIntrospection(DisableIntrospection::ENABLED), ' + $this->expectPassesRule( + new DisableIntrospection(DisableIntrospection::ENABLED), + ' query { user { name @@ -60,7 +76,8 @@ class DisableIntrospectionTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -68,7 +85,9 @@ class DisableIntrospectionTest extends ValidatorTestCase */ public function testQueryWhenDisabled() : void { - $this->expectPassesRule(new DisableIntrospection(DisableIntrospection::DISABLED), ' + $this->expectPassesRule( + new DisableIntrospection(DisableIntrospection::DISABLED), + ' query { __type( name: "Query" @@ -76,7 +95,8 @@ class DisableIntrospectionTest extends ValidatorTestCase name } } - '); + ' + ); } /** @@ -86,7 +106,9 @@ class DisableIntrospectionTest extends ValidatorTestCase { $disableIntrospection = new DisableIntrospection(DisableIntrospection::DISABLED); $disableIntrospection->setEnabled(DisableIntrospection::ENABLED); - $this->expectFailsRule($disableIntrospection, ' + $this->expectFailsRule( + $disableIntrospection, + ' query { __type( name: "Query" @@ -106,7 +128,9 @@ class DisableIntrospectionTest extends ValidatorTestCase { $disableIntrospection = new DisableIntrospection(DisableIntrospection::ENABLED); $disableIntrospection->setEnabled(DisableIntrospection::DISABLED); - $this->expectPassesRule($disableIntrospection, ' + $this->expectPassesRule( + $disableIntrospection, + ' query { __type( name: "Query" @@ -114,15 +138,7 @@ class DisableIntrospectionTest extends ValidatorTestCase name } } - '); - } - - - private function error($line, $column) - { - return FormattedError::create( - DisableIntrospection::introspectionDisabledMessage(), - [ new SourceLocation($line, $column) ] + ' ); } } diff --git a/tests/Validator/ExecutableDefinitionsTest.php b/tests/Validator/ExecutableDefinitionsTest.php index 28ab13a..0751755 100644 --- a/tests/Validator/ExecutableDefinitionsTest.php +++ b/tests/Validator/ExecutableDefinitionsTest.php @@ -1,27 +1,31 @@ expectPassesRule(new ExecutableDefinitions, ' + $this->expectPassesRule( + new ExecutableDefinitions(), + ' query Foo { dog { name } } - '); + ' + ); } /** @@ -29,7 +33,9 @@ class ExecutableDefinitionsTest extends ValidatorTestCase */ public function testWithOperationAndFragment() : void { - $this->expectPassesRule(new ExecutableDefinitions, ' + $this->expectPassesRule( + new ExecutableDefinitions(), + ' query Foo { dog { name @@ -40,7 +46,8 @@ class ExecutableDefinitionsTest extends ValidatorTestCase fragment Frag on Dog { name } - '); + ' + ); } /** @@ -48,7 +55,9 @@ class ExecutableDefinitionsTest extends ValidatorTestCase */ public function testWithTypeDefinition() : void { - $this->expectFailsRule(new ExecutableDefinitions, ' + $this->expectFailsRule( + new ExecutableDefinitions(), + ' query Foo { dog { name @@ -66,14 +75,15 @@ class ExecutableDefinitionsTest extends ValidatorTestCase [ $this->nonExecutableDefinition('Cow', 8, 12), $this->nonExecutableDefinition('Dog', 12, 19), - ]); + ] + ); } private function nonExecutableDefinition($defName, $line, $column) { return FormattedError::create( ExecutableDefinitions::nonExecutableDefinitionMessage($defName), - [ new SourceLocation($line, $column) ] + [new SourceLocation($line, $column)] ); } } diff --git a/tests/Validator/FieldsOnCorrectTypeTest.php b/tests/Validator/FieldsOnCorrectTypeTest.php index a838f3c..5897b5a 100644 --- a/tests/Validator/FieldsOnCorrectTypeTest.php +++ b/tests/Validator/FieldsOnCorrectTypeTest.php @@ -1,4 +1,7 @@ expectPassesRule(new FieldsOnCorrectType(), ' + $this->expectPassesRule( + new FieldsOnCorrectType(), + ' fragment objectFieldSelection on Dog { __typename name } - '); + ' + ); } /** @@ -27,12 +32,15 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testAliasedObjectFieldSelection() : void { - $this->expectPassesRule(new FieldsOnCorrectType, ' + $this->expectPassesRule( + new FieldsOnCorrectType(), + ' fragment aliasedObjectFieldSelection on Dog { tn : __typename otherName : name } - '); + ' + ); } /** @@ -40,12 +48,15 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testInterfaceFieldSelection() : void { - $this->expectPassesRule(new FieldsOnCorrectType, ' + $this->expectPassesRule( + new FieldsOnCorrectType(), + ' fragment interfaceFieldSelection on Pet { __typename name } - '); + ' + ); } /** @@ -53,11 +64,14 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testAliasedInterfaceFieldSelection() : void { - $this->expectPassesRule(new FieldsOnCorrectType, ' + $this->expectPassesRule( + new FieldsOnCorrectType(), + ' fragment interfaceFieldSelection on Pet { otherName : name } - '); + ' + ); } /** @@ -65,11 +79,14 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testLyingAliasSelection() : void { - $this->expectPassesRule(new FieldsOnCorrectType, ' + $this->expectPassesRule( + new FieldsOnCorrectType(), + ' fragment lyingAliasSelection on Dog { name : nickname } - '); + ' + ); } /** @@ -77,11 +94,14 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testIgnoresFieldsOnUnknownType() : void { - $this->expectPassesRule(new FieldsOnCorrectType, ' + $this->expectPassesRule( + new FieldsOnCorrectType(), + ' fragment unknownSelection on UnknownType { unknownField } - '); + ' + ); } /** @@ -89,7 +109,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testReportsErrorsWhenTypeIsKnownAgain() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment typeKnownAgain on Pet { unknown_pet_field { ... on Cat { @@ -99,17 +121,27 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase }', [ $this->undefinedField('unknown_pet_field', 'Pet', [], [], 3, 9), - $this->undefinedField('unknown_cat_field', 'Cat', [], [], 5, 13) + $this->undefinedField('unknown_cat_field', 'Cat', [], [], 5, 13), ] ); } + private function undefinedField($field, $type, $suggestedTypes, $suggestedFields, $line, $column) + { + return FormattedError::create( + FieldsOnCorrectType::undefinedFieldMessage($field, $type, $suggestedTypes, $suggestedFields), + [new SourceLocation($line, $column)] + ); + } + /** * @see it('Field not defined on fragment') */ public function testFieldNotDefinedOnFragment() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment fieldNotDefined on Dog { meowVolume }', @@ -122,7 +154,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testIgnoresDeeplyUnknownField() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment deepFieldNotDefined on Dog { unknown_field { deeper_unknown_field @@ -137,7 +171,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testSubFieldNotDefined() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment subFieldNotDefined on Human { pets { unknown_field @@ -152,7 +188,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testFieldNotDefinedOnInlineFragment() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment fieldNotDefined on Pet { ... on Dog { meowVolume @@ -167,7 +205,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testAliasedFieldTargetNotDefined() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment aliasedFieldTargetNotDefined on Dog { volume : mooVolume }', @@ -180,7 +220,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testAliasedLyingFieldTargetNotDefined() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment aliasedLyingFieldTargetNotDefined on Dog { barkVolume : kawVolume }', @@ -193,7 +235,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testNotDefinedOnInterface() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment notDefinedOnInterface on Pet { tailLength }', @@ -206,7 +250,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testDefinedOnImplmentorsButNotOnInterface() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment definedOnImplementorsButNotInterface on Pet { nickname }', @@ -219,7 +265,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testMetaFieldSelectionOnUnion() : void { - $this->expectPassesRule(new FieldsOnCorrectType, ' + $this->expectPassesRule( + new FieldsOnCorrectType(), + ' fragment directFieldSelectionOnUnion on CatOrDog { __typename }' @@ -231,7 +279,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testDirectFieldSelectionOnUnion() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment directFieldSelectionOnUnion on CatOrDog { directField }', @@ -244,7 +294,9 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase */ public function testDefinedOnImplementorsQueriedOnUnion() : void { - $this->expectFailsRule(new FieldsOnCorrectType, ' + $this->expectFailsRule( + new FieldsOnCorrectType(), + ' fragment definedOnImplementorsQueriedOnUnion on CatOrDog { name }', @@ -255,32 +307,39 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase [], 3, 9 - )] + ), + ] ); } + // Describe: Fields on correct type error message + /** * @see it('valid field in inline fragment') */ public function testValidFieldInInlineFragment() : void { - $this->expectPassesRule(new FieldsOnCorrectType, ' + $this->expectPassesRule( + new FieldsOnCorrectType(), + ' fragment objectFieldSelection on Pet { ... on Dog { name } } - '); + ' + ); } - // Describe: Fields on correct type error message - /** * @see it('Works with no suggestions') */ public function testWorksWithNoSuggestions() : void { - $this->assertEquals('Cannot query field "f" on type "T".', FieldsOnCorrectType::undefinedFieldMessage('f', 'T', [], [])); + $this->assertEquals( + 'Cannot query field "f" on type "T".', + FieldsOnCorrectType::undefinedFieldMessage('f', 'T', [], []) + ); } /** @@ -324,12 +383,15 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase $expected = 'Cannot query field "f" on type "T". ' . 'Did you mean to use an inline fragment on "A", "B", "C", "D", or "E"?'; - $this->assertEquals($expected, FieldsOnCorrectType::undefinedFieldMessage( - 'f', - 'T', - ['A', 'B', 'C', 'D', 'E', 'F'], - [] - )); + $this->assertEquals( + $expected, + FieldsOnCorrectType::undefinedFieldMessage( + 'f', + 'T', + ['A', 'B', 'C', 'D', 'E', 'F'], + [] + ) + ); } /** @@ -340,19 +402,14 @@ class FieldsOnCorrectTypeTest extends ValidatorTestCase $expected = 'Cannot query field "f" on type "T". ' . 'Did you mean "z", "y", "x", "w", or "v"?'; - $this->assertEquals($expected, FieldsOnCorrectType::undefinedFieldMessage( - 'f', - 'T', - [], - ['z', 'y', 'x', 'w', 'v', 'u'] - )); - } - - private function undefinedField($field, $type, $suggestedTypes, $suggestedFields, $line, $column) - { - return FormattedError::create( - FieldsOnCorrectType::undefinedFieldMessage($field, $type, $suggestedTypes, $suggestedFields), - [new SourceLocation($line, $column)] + $this->assertEquals( + $expected, + FieldsOnCorrectType::undefinedFieldMessage( + 'f', + 'T', + [], + ['z', 'y', 'x', 'w', 'v', 'u'] + ) ); } } diff --git a/tests/Validator/FragmentsOnCompositeTypesTest.php b/tests/Validator/FragmentsOnCompositeTypesTest.php index b1a116a..deec5c4 100644 --- a/tests/Validator/FragmentsOnCompositeTypesTest.php +++ b/tests/Validator/FragmentsOnCompositeTypesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new FragmentsOnCompositeTypes, ' + $this->expectPassesRule( + new FragmentsOnCompositeTypes(), + ' fragment validFragment on Dog { barks } - '); + ' + ); } /** @@ -26,11 +31,14 @@ class FragmentsOnCompositeTypesTest extends ValidatorTestCase */ public function testInterfaceIsValidFragmentType() : void { - $this->expectPassesRule(new FragmentsOnCompositeTypes, ' + $this->expectPassesRule( + new FragmentsOnCompositeTypes(), + ' fragment validFragment on Pet { name } - '); + ' + ); } /** @@ -38,13 +46,16 @@ class FragmentsOnCompositeTypesTest extends ValidatorTestCase */ public function testObjectIsValidInlineFragmentType() : void { - $this->expectPassesRule(new FragmentsOnCompositeTypes, ' + $this->expectPassesRule( + new FragmentsOnCompositeTypes(), + ' fragment validFragment on Pet { ... on Dog { barks } } - '); + ' + ); } /** @@ -52,13 +63,16 @@ class FragmentsOnCompositeTypesTest extends ValidatorTestCase */ public function testInlineFragmentWithoutTypeIsValid() : void { - $this->expectPassesRule(new FragmentsOnCompositeTypes, ' + $this->expectPassesRule( + new FragmentsOnCompositeTypes(), + ' fragment validFragment on Pet { ... { name } } - '); + ' + ); } /** @@ -66,11 +80,14 @@ class FragmentsOnCompositeTypesTest extends ValidatorTestCase */ public function testUnionIsValidFragmentType() : void { - $this->expectPassesRule(new FragmentsOnCompositeTypes, ' + $this->expectPassesRule( + new FragmentsOnCompositeTypes(), + ' fragment validFragment on CatOrDog { __typename } - '); + ' + ); } /** @@ -78,56 +95,14 @@ class FragmentsOnCompositeTypesTest extends ValidatorTestCase */ public function testScalarIsInvalidFragmentType() : void { - $this->expectFailsRule(new FragmentsOnCompositeTypes, ' + $this->expectFailsRule( + new FragmentsOnCompositeTypes(), + ' fragment scalarFragment on Boolean { bad } ', - [$this->error('scalarFragment', 'Boolean', 2, 34)]); - } - - /** - * @see it('enum is invalid fragment type') - */ - public function testEnumIsInvalidFragmentType() : void - { - $this->expectFailsRule(new FragmentsOnCompositeTypes, ' - fragment scalarFragment on FurColor { - bad - } - ', - [$this->error('scalarFragment', 'FurColor', 2, 34)]); - } - - /** - * @see it('input object is invalid fragment type') - */ - public function testInputObjectIsInvalidFragmentType() : void - { - $this->expectFailsRule(new FragmentsOnCompositeTypes, ' - fragment inputFragment on ComplexInput { - stringField - } - ', - [$this->error('inputFragment', 'ComplexInput', 2, 33)]); - } - - /** - * @see it('scalar is invalid inline fragment type') - */ - public function testScalarIsInvalidInlineFragmentType() : void - { - $this->expectFailsRule(new FragmentsOnCompositeTypes, ' - fragment invalidFragment on Pet { - ... on String { - barks - } - } - ', - [FormattedError::create( - FragmentsOnCompositeTypes::inlineFragmentOnNonCompositeErrorMessage('String'), - [new SourceLocation(3, 16)] - )] + [$this->error('scalarFragment', 'Boolean', 2, 34)] ); } @@ -135,7 +110,61 @@ class FragmentsOnCompositeTypesTest extends ValidatorTestCase { return FormattedError::create( FragmentsOnCompositeTypes::fragmentOnNonCompositeErrorMessage($fragName, $typeName), - [ new SourceLocation($line, $column) ] + [new SourceLocation($line, $column)] + ); + } + + /** + * @see it('enum is invalid fragment type') + */ + public function testEnumIsInvalidFragmentType() : void + { + $this->expectFailsRule( + new FragmentsOnCompositeTypes(), + ' + fragment scalarFragment on FurColor { + bad + } + ', + [$this->error('scalarFragment', 'FurColor', 2, 34)] + ); + } + + /** + * @see it('input object is invalid fragment type') + */ + public function testInputObjectIsInvalidFragmentType() : void + { + $this->expectFailsRule( + new FragmentsOnCompositeTypes(), + ' + fragment inputFragment on ComplexInput { + stringField + } + ', + [$this->error('inputFragment', 'ComplexInput', 2, 33)] + ); + } + + /** + * @see it('scalar is invalid inline fragment type') + */ + public function testScalarIsInvalidInlineFragmentType() : void + { + $this->expectFailsRule( + new FragmentsOnCompositeTypes(), + ' + fragment invalidFragment on Pet { + ... on String { + barks + } + } + ', + [FormattedError::create( + FragmentsOnCompositeTypes::inlineFragmentOnNonCompositeErrorMessage('String'), + [new SourceLocation(3, 16)] + ), + ] ); } } diff --git a/tests/Validator/KnownArgumentNamesTest.php b/tests/Validator/KnownArgumentNamesTest.php index 8c3fc24..4c3c163 100644 --- a/tests/Validator/KnownArgumentNamesTest.php +++ b/tests/Validator/KnownArgumentNamesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new KnownArgumentNames, ' + $this->expectPassesRule( + new KnownArgumentNames(), + ' fragment argOnRequiredArg on Dog { doesKnowCommand(dogCommand: SIT) } - '); + ' + ); } /** @@ -26,11 +31,14 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testMultipleArgsAreKnown() : void { - $this->expectPassesRule(new KnownArgumentNames, ' + $this->expectPassesRule( + new KnownArgumentNames(), + ' fragment multipleArgs on ComplicatedArgs { multipleReqs(req1: 1, req2: 2) } - '); + ' + ); } /** @@ -38,11 +46,14 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testIgnoresArgsOfUnknownFields() : void { - $this->expectPassesRule(new KnownArgumentNames, ' + $this->expectPassesRule( + new KnownArgumentNames(), + ' fragment argOnUnknownField on Dog { unknownField(unknownArg: SIT) } - '); + ' + ); } /** @@ -50,11 +61,14 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testMultipleArgsInReverseOrderAreKnown() : void { - $this->expectPassesRule(new KnownArgumentNames, ' + $this->expectPassesRule( + new KnownArgumentNames(), + ' fragment multipleArgsReverseOrder on ComplicatedArgs { multipleReqs(req2: 2, req1: 1) } - '); + ' + ); } /** @@ -62,11 +76,14 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testNoArgsOnOptionalArg() : void { - $this->expectPassesRule(new KnownArgumentNames, ' + $this->expectPassesRule( + new KnownArgumentNames(), + ' fragment noArgOnOptionalArg on Dog { isHousetrained } - '); + ' + ); } /** @@ -74,7 +91,9 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testArgsAreKnownDeeply() : void { - $this->expectPassesRule(new KnownArgumentNames, ' + $this->expectPassesRule( + new KnownArgumentNames(), + ' { dog { doesKnowCommand(dogCommand: SIT) @@ -87,7 +106,8 @@ class KnownArgumentNamesTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -95,11 +115,14 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testDirectiveArgsAreKnown() : void { - $this->expectPassesRule(new KnownArgumentNames, ' + $this->expectPassesRule( + new KnownArgumentNames(), + ' { dog @skip(if: true) } - '); + ' + ); } /** @@ -107,13 +130,25 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testUndirectiveArgsAreInvalid() : void { - $this->expectFailsRule(new KnownArgumentNames, ' + $this->expectFailsRule( + new KnownArgumentNames(), + ' { dog @skip(unless: true) } - ', [ - $this->unknownDirectiveArg('unless', 'skip', [], 3, 19), - ]); + ', + [ + $this->unknownDirectiveArg('unless', 'skip', [], 3, 19), + ] + ); + } + + private function unknownDirectiveArg($argName, $directiveName, $suggestedArgs, $line, $column) + { + return FormattedError::create( + KnownArgumentNames::unknownDirectiveArgMessage($argName, $directiveName, $suggestedArgs), + [new SourceLocation($line, $column)] + ); } /** @@ -121,13 +156,17 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testMisspelledDirectiveArgsAreReported() : void { - $this->expectFailsRule(new KnownArgumentNames, ' + $this->expectFailsRule( + new KnownArgumentNames(), + ' { dog @skip(iff: true) } - ', [ - $this->unknownDirectiveArg('iff', 'skip', ['if'], 3, 19), - ]); + ', + [ + $this->unknownDirectiveArg('iff', 'skip', ['if'], 3, 19), + ] + ); } /** @@ -135,13 +174,25 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testInvalidArgName() : void { - $this->expectFailsRule(new KnownArgumentNames, ' + $this->expectFailsRule( + new KnownArgumentNames(), + ' fragment invalidArgName on Dog { doesKnowCommand(unknown: true) } - ', [ - $this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [],3, 25), - ]); + ', + [ + $this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 3, 25), + ] + ); + } + + private function unknownArg($argName, $fieldName, $typeName, $suggestedArgs, $line, $column) + { + return FormattedError::create( + KnownArgumentNames::unknownArgMessage($argName, $fieldName, $typeName, $suggestedArgs), + [new SourceLocation($line, $column)] + ); } /** @@ -149,13 +200,17 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testMisspelledArgNameIsReported() : void { - $this->expectFailsRule(new KnownArgumentNames, ' + $this->expectFailsRule( + new KnownArgumentNames(), + ' fragment invalidArgName on Dog { doesKnowCommand(dogcommand: true) } - ', [ - $this->unknownArg('dogcommand', 'doesKnowCommand', 'Dog', ['dogCommand'],3, 25), - ]); + ', + [ + $this->unknownArg('dogcommand', 'doesKnowCommand', 'Dog', ['dogCommand'], 3, 25), + ] + ); } /** @@ -163,14 +218,18 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testUnknownArgsAmongstKnownArgs() : void { - $this->expectFailsRule(new KnownArgumentNames, ' + $this->expectFailsRule( + new KnownArgumentNames(), + ' fragment oneGoodArgOneInvalidArg on Dog { doesKnowCommand(whoknows: 1, dogCommand: SIT, unknown: true) } - ', [ - $this->unknownArg('whoknows', 'doesKnowCommand', 'Dog', [], 3, 25), - $this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 3, 55), - ]); + ', + [ + $this->unknownArg('whoknows', 'doesKnowCommand', 'Dog', [], 3, 25), + $this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 3, 55), + ] + ); } /** @@ -178,7 +237,9 @@ class KnownArgumentNamesTest extends ValidatorTestCase */ public function testUnknownArgsDeeply() : void { - $this->expectFailsRule(new KnownArgumentNames, ' + $this->expectFailsRule( + new KnownArgumentNames(), + ' { dog { doesKnowCommand(unknown: true) @@ -191,25 +252,11 @@ class KnownArgumentNamesTest extends ValidatorTestCase } } } - ', [ - $this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 4, 27), - $this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 9, 31), - ]); - } - - private function unknownArg($argName, $fieldName, $typeName, $suggestedArgs, $line, $column) - { - return FormattedError::create( - KnownArgumentNames::unknownArgMessage($argName, $fieldName, $typeName, $suggestedArgs), - [new SourceLocation($line, $column)] - ); - } - - private function unknownDirectiveArg($argName, $directiveName, $suggestedArgs, $line, $column) - { - return FormattedError::create( - KnownArgumentNames::unknownDirectiveArgMessage($argName, $directiveName, $suggestedArgs), - [new SourceLocation($line, $column)] + ', + [ + $this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 4, 27), + $this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 9, 31), + ] ); } } diff --git a/tests/Validator/KnownDirectivesTest.php b/tests/Validator/KnownDirectivesTest.php index 7f9fd39..014e892 100644 --- a/tests/Validator/KnownDirectivesTest.php +++ b/tests/Validator/KnownDirectivesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new KnownDirectives, ' + $this->expectPassesRule( + new KnownDirectives(), + ' query Foo { name ...Frag @@ -23,7 +27,8 @@ class KnownDirectivesTest extends ValidatorTestCase fragment Frag on Dog { name } - '); + ' + ); } /** @@ -31,7 +36,9 @@ class KnownDirectivesTest extends ValidatorTestCase */ public function testWithKnownDirectives() : void { - $this->expectPassesRule(new KnownDirectives, ' + $this->expectPassesRule( + new KnownDirectives(), + ' { dog @include(if: true) { name @@ -40,7 +47,8 @@ class KnownDirectivesTest extends ValidatorTestCase name } } - '); + ' + ); } /** @@ -48,15 +56,25 @@ class KnownDirectivesTest extends ValidatorTestCase */ public function testWithUnknownDirective() : void { - $this->expectFailsRule(new KnownDirectives, ' + $this->expectFailsRule( + new KnownDirectives(), + ' { dog @unknown(directive: "value") { name } } - ', [ - $this->unknownDirective('unknown', 3, 13) - ]); + ', + [$this->unknownDirective('unknown', 3, 13)] + ); + } + + private function unknownDirective($directiveName, $line, $column) + { + return FormattedError::create( + KnownDirectives::unknownDirectiveMessage($directiveName), + [new SourceLocation($line, $column)] + ); } /** @@ -64,7 +82,9 @@ class KnownDirectivesTest extends ValidatorTestCase */ public function testWithManyUnknownDirectives() : void { - $this->expectFailsRule(new KnownDirectives, ' + $this->expectFailsRule( + new KnownDirectives(), + ' { dog @unknown(directive: "value") { name @@ -76,11 +96,13 @@ class KnownDirectivesTest extends ValidatorTestCase } } } - ', [ - $this->unknownDirective('unknown', 3, 13), - $this->unknownDirective('unknown', 6, 15), - $this->unknownDirective('unknown', 8, 16) - ]); + ', + [ + $this->unknownDirective('unknown', 3, 13), + $this->unknownDirective('unknown', 6, 15), + $this->unknownDirective('unknown', 8, 16), + ] + ); } /** @@ -88,7 +110,9 @@ class KnownDirectivesTest extends ValidatorTestCase */ public function testWithWellPlacedDirectives() : void { - $this->expectPassesRule(new KnownDirectives, ' + $this->expectPassesRule( + new KnownDirectives(), + ' query Foo @onQuery { name @include(if: true) ...Frag @include(if: true) @@ -99,15 +123,20 @@ class KnownDirectivesTest extends ValidatorTestCase mutation Bar @onMutation { someField } - '); + ' + ); } + // within schema language + /** * @see it('with misplaced directives') */ public function testWithMisplacedDirectives() : void { - $this->expectFailsRule(new KnownDirectives, ' + $this->expectFailsRule( + new KnownDirectives(), + ' query Foo @include(if: true) { name @onQuery ...Frag @onQuery @@ -116,22 +145,32 @@ class KnownDirectivesTest extends ValidatorTestCase mutation Bar @onQuery { someField } - ', [ - $this->misplacedDirective('include', 'QUERY', 2, 17), - $this->misplacedDirective('onQuery', 'FIELD', 3, 14), - $this->misplacedDirective('onQuery', 'FRAGMENT_SPREAD', 4, 17), - $this->misplacedDirective('onQuery', 'MUTATION', 7, 20), - ]); + ', + [ + $this->misplacedDirective('include', 'QUERY', 2, 17), + $this->misplacedDirective('onQuery', 'FIELD', 3, 14), + $this->misplacedDirective('onQuery', 'FRAGMENT_SPREAD', 4, 17), + $this->misplacedDirective('onQuery', 'MUTATION', 7, 20), + ] + ); } - // within schema language + private function misplacedDirective($directiveName, $placement, $line, $column) + { + return FormattedError::create( + KnownDirectives::misplacedDirectiveMessage($directiveName, $placement), + [new SourceLocation($line, $column)] + ); + } /** * @see it('with well placed directives') */ public function testWSLWithWellPlacedDirectives() : void { - $this->expectPassesRule(new KnownDirectives, ' + $this->expectPassesRule( + new KnownDirectives(), + ' type MyObj implements MyInterface @onObject { myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition } @@ -167,7 +206,8 @@ class KnownDirectivesTest extends ValidatorTestCase schema @onSchema { query: MyQuery } - '); + ' + ); } /** @@ -175,7 +215,9 @@ class KnownDirectivesTest extends ValidatorTestCase */ public function testWSLWithMisplacedDirectives() : void { - $this->expectFailsRule(new KnownDirectives, ' + $this->expectFailsRule( + new KnownDirectives(), + ' type MyObj implements MyInterface @onInterface { myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition } @@ -217,20 +259,4 @@ class KnownDirectivesTest extends ValidatorTestCase ] ); } - - private function unknownDirective($directiveName, $line, $column) - { - return FormattedError::create( - KnownDirectives::unknownDirectiveMessage($directiveName), - [ new SourceLocation($line, $column) ] - ); - } - - function misplacedDirective($directiveName, $placement, $line, $column) - { - return FormattedError::create( - KnownDirectives::misplacedDirectiveMessage($directiveName, $placement), - [new SourceLocation($line, $column)] - ); - } } diff --git a/tests/Validator/KnownFragmentNamesTest.php b/tests/Validator/KnownFragmentNamesTest.php index 5764150..cc9ce86 100644 --- a/tests/Validator/KnownFragmentNamesTest.php +++ b/tests/Validator/KnownFragmentNamesTest.php @@ -1,20 +1,24 @@ expectPassesRule(new KnownFragmentNames, ' + $this->expectPassesRule( + new KnownFragmentNames(), + ' { human(id: 4) { ...HumanFields1 @@ -33,7 +37,8 @@ class KnownFragmentNamesTest extends ValidatorTestCase fragment HumanFields3 on Human { name } - '); + ' + ); } /** @@ -41,7 +46,9 @@ class KnownFragmentNamesTest extends ValidatorTestCase */ public function testUnknownFragmentNamesAreInvalid() : void { - $this->expectFailsRule(new KnownFragmentNames, ' + $this->expectFailsRule( + new KnownFragmentNames(), + ' { human(id: 4) { ...UnknownFragment1 @@ -54,11 +61,13 @@ class KnownFragmentNamesTest extends ValidatorTestCase name ...UnknownFragment3 } - ', [ - $this->undefFrag('UnknownFragment1', 4, 14), - $this->undefFrag('UnknownFragment2', 6, 16), - $this->undefFrag('UnknownFragment3', 12, 12) - ]); + ', + [ + $this->undefFrag('UnknownFragment1', 4, 14), + $this->undefFrag('UnknownFragment2', 6, 16), + $this->undefFrag('UnknownFragment3', 12, 12), + ] + ); } private function undefFrag($fragName, $line, $column) diff --git a/tests/Validator/KnownTypeNamesTest.php b/tests/Validator/KnownTypeNamesTest.php index 9604cb8..12834aa 100644 --- a/tests/Validator/KnownTypeNamesTest.php +++ b/tests/Validator/KnownTypeNamesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new KnownTypeNames, ' + $this->expectPassesRule( + new KnownTypeNames(), + ' query Foo($var: String, $required: [String!]!) { user(id: 4) { pets { ... on Pet { name }, ...PetFields } @@ -23,7 +27,8 @@ class KnownTypeNamesTest extends ValidatorTestCase fragment PetFields on Pet { name } - '); + ' + ); } /** @@ -31,7 +36,9 @@ class KnownTypeNamesTest extends ValidatorTestCase */ public function testUnknownTypeNamesAreInvalid() : void { - $this->expectFailsRule(new KnownTypeNames, ' + $this->expectFailsRule( + new KnownTypeNames(), + ' query Foo($var: JumbledUpLetters) { user(id: 4) { name @@ -41,11 +48,21 @@ class KnownTypeNamesTest extends ValidatorTestCase fragment PetFields on Peettt { name } - ', [ - $this->unknownType('JumbledUpLetters', [], 2, 23), - $this->unknownType('Badger', [], 5, 25), - $this->unknownType('Peettt', ['Pet'], 8, 29) - ]); + ', + [ + $this->unknownType('JumbledUpLetters', [], 2, 23), + $this->unknownType('Badger', [], 5, 25), + $this->unknownType('Peettt', ['Pet'], 8, 29), + ] + ); + } + + private function unknownType($typeName, $suggestedTypes, $line, $column) + { + return FormattedError::create( + KnownTypeNames::unknownTypeMessage($typeName, $suggestedTypes), + [new SourceLocation($line, $column)] + ); } /** @@ -53,7 +70,9 @@ class KnownTypeNamesTest extends ValidatorTestCase */ public function testIgnoresTypeDefinitions() : void { - $this->expectFailsRule(new KnownTypeNames, ' + $this->expectFailsRule( + new KnownTypeNames(), + ' type NotInTheSchema { field: FooBar } @@ -69,16 +88,10 @@ class KnownTypeNamesTest extends ValidatorTestCase id } } - ', [ - $this->unknownType('NotInTheSchema', [], 12, 23), - ]); - } - - private function unknownType($typeName, $suggestedTypes, $line, $column) - { - return FormattedError::create( - KnownTypeNames::unknownTypeMessage($typeName, $suggestedTypes), - [new SourceLocation($line, $column)] + ', + [ + $this->unknownType('NotInTheSchema', [], 12, 23), + ] ); } } diff --git a/tests/Validator/LoneAnonymousOperationTest.php b/tests/Validator/LoneAnonymousOperationTest.php index e8540c8..cdd7983 100644 --- a/tests/Validator/LoneAnonymousOperationTest.php +++ b/tests/Validator/LoneAnonymousOperationTest.php @@ -1,4 +1,7 @@ expectPassesRule(new LoneAnonymousOperation, ' + $this->expectPassesRule( + new LoneAnonymousOperation(), + ' fragment fragA on Type { field } - '); + ' + ); } /** @@ -26,11 +31,14 @@ class LoneAnonymousOperationTest extends ValidatorTestCase */ public function testOneAnonOperation() : void { - $this->expectPassesRule(new LoneAnonymousOperation, ' + $this->expectPassesRule( + new LoneAnonymousOperation(), + ' { field } - '); + ' + ); } /** @@ -38,7 +46,9 @@ class LoneAnonymousOperationTest extends ValidatorTestCase */ public function testMultipleNamedOperations() : void { - $this->expectPassesRule(new LoneAnonymousOperation, ' + $this->expectPassesRule( + new LoneAnonymousOperation(), + ' query Foo { field } @@ -46,7 +56,8 @@ class LoneAnonymousOperationTest extends ValidatorTestCase query Bar { field } - '); + ' + ); } /** @@ -54,14 +65,17 @@ class LoneAnonymousOperationTest extends ValidatorTestCase */ public function testAnonOperationWithFragment() : void { - $this->expectPassesRule(new LoneAnonymousOperation, ' + $this->expectPassesRule( + new LoneAnonymousOperation(), + ' { ...Foo } fragment Foo on Type { field } - '); + ' + ); } /** @@ -69,51 +83,21 @@ class LoneAnonymousOperationTest extends ValidatorTestCase */ public function testMultipleAnonOperations() : void { - $this->expectFailsRule(new LoneAnonymousOperation, ' + $this->expectFailsRule( + new LoneAnonymousOperation(), + ' { fieldA } { fieldB } - ', [ - $this->anonNotAlone(2, 7), - $this->anonNotAlone(5, 7) - ]); - } - - /** - * @see it('anon operation with a mutation') - */ - public function testAnonOperationWithMutation() : void - { - $this->expectFailsRule(new LoneAnonymousOperation, ' - { - fieldA - } - mutation Foo { - fieldB - } - ', [ - $this->anonNotAlone(2, 7) - ]); - } - - /** - * @see it('anon operation with a subscription') - */ - public function testAnonOperationWithSubscription() : void - { - $this->expectFailsRule(new LoneAnonymousOperation, ' - { - fieldA - } - subscription Foo { - fieldB - } - ', [ - $this->anonNotAlone(2, 7) - ]); + ', + [ + $this->anonNotAlone(2, 7), + $this->anonNotAlone(5, 7), + ] + ); } private function anonNotAlone($line, $column) @@ -122,6 +106,47 @@ class LoneAnonymousOperationTest extends ValidatorTestCase LoneAnonymousOperation::anonOperationNotAloneMessage(), [new SourceLocation($line, $column)] ); + } + /** + * @see it('anon operation with a mutation') + */ + public function testAnonOperationWithMutation() : void + { + $this->expectFailsRule( + new LoneAnonymousOperation(), + ' + { + fieldA + } + mutation Foo { + fieldB + } + ', + [ + $this->anonNotAlone(2, 7), + ] + ); + } + + /** + * @see it('anon operation with a subscription') + */ + public function testAnonOperationWithSubscription() : void + { + $this->expectFailsRule( + new LoneAnonymousOperation(), + ' + { + fieldA + } + subscription Foo { + fieldB + } + ', + [ + $this->anonNotAlone(2, 7), + ] + ); } } diff --git a/tests/Validator/NoFragmentCyclesTest.php b/tests/Validator/NoFragmentCyclesTest.php index a962dc3..f0a2a63 100644 --- a/tests/Validator/NoFragmentCyclesTest.php +++ b/tests/Validator/NoFragmentCyclesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new NoFragmentCycles(), ' + $this->expectPassesRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragB } fragment fragB on Dog { name } - '); + ' + ); } /** @@ -25,10 +30,13 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testSpreadingTwiceIsNotCircular() : void { - $this->expectPassesRule(new NoFragmentCycles, ' + $this->expectPassesRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragB, ...fragB } fragment fragB on Dog { name } - '); + ' + ); } /** @@ -36,11 +44,14 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testSpreadingTwiceIndirectlyIsNotCircular() : void { - $this->expectPassesRule(new NoFragmentCycles, ' + $this->expectPassesRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragB, ...fragC } fragment fragB on Dog { ...fragC } fragment fragC on Dog { name } - '); + ' + ); } /** @@ -48,7 +59,9 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testDoubleSpreadWithinAbstractTypes() : void { - $this->expectPassesRule(new NoFragmentCycles, ' + $this->expectPassesRule( + new NoFragmentCycles(), + ' fragment nameFragment on Pet { ... on Dog { name } ... on Cat { name } @@ -58,7 +71,8 @@ class NoFragmentCyclesTest extends ValidatorTestCase ... on Dog { ...nameFragment } ... on Cat { ...nameFragment } } - '); + ' + ); } /** @@ -66,11 +80,14 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testDoesNotFalsePositiveOnUnknownFragment() : void { - $this->expectPassesRule(new NoFragmentCycles, ' + $this->expectPassesRule( + new NoFragmentCycles(), + ' fragment nameFragment on Pet { ...UnknownFragment } - '); + ' + ); } /** @@ -78,11 +95,23 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testSpreadingRecursivelyWithinFieldFails() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Human { relatives { ...fragA } }, - ', [ - $this->cycleError('fragA', [], 2, 45) - ]); + ', + [ + $this->cycleError('fragA', [], 2, 45), + ] + ); + } + + private function cycleError($fargment, $spreadNames, $line, $column) + { + return FormattedError::create( + NoFragmentCycles::cycleErrorMessage($fargment, $spreadNames), + [new SourceLocation($line, $column)] + ); } /** @@ -90,11 +119,15 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfDirectly() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragA } - ', [ - $this->cycleError('fragA', [], 2, 31) - ]); + ', + [ + $this->cycleError('fragA', [], 2, 31), + ] + ); } /** @@ -102,15 +135,19 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfDirectlyWithinInlineFragment() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Pet { ... on Dog { ...fragA } } - ', [ - $this->cycleError('fragA', [], 4, 11) - ]); + ', + [ + $this->cycleError('fragA', [], 4, 11), + ] + ); } /** @@ -118,15 +155,19 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfIndirectly() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragB } fragment fragB on Dog { ...fragA } - ', [ - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragA', ['fragB']), - [ new SourceLocation(2, 31), new SourceLocation(3, 31) ] - ) - ]); + ', + [ + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragA', ['fragB']), + [new SourceLocation(2, 31), new SourceLocation(3, 31)] + ), + ] + ); } /** @@ -134,15 +175,19 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfIndirectlyReportsOppositeOrder() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragB on Dog { ...fragA } fragment fragA on Dog { ...fragB } - ', [ - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragB', ['fragA']), - [new SourceLocation(2, 31), new SourceLocation(3, 31)] - ) - ]); + ', + [ + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragB', ['fragA']), + [new SourceLocation(2, 31), new SourceLocation(3, 31)] + ), + ] + ); } /** @@ -150,7 +195,9 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfIndirectlyWithinInlineFragment() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Pet { ... on Dog { ...fragB @@ -161,12 +208,14 @@ class NoFragmentCyclesTest extends ValidatorTestCase ...fragA } } - ', [ - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragA', ['fragB']), - [new SourceLocation(4, 11), new SourceLocation(9, 11)] - ) - ]); + ', + [ + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragA', ['fragB']), + [new SourceLocation(4, 11), new SourceLocation(9, 11)] + ), + ] + ); } /** @@ -174,7 +223,9 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfDeeply() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragB } fragment fragB on Dog { ...fragC } fragment fragC on Dog { ...fragO } @@ -183,28 +234,30 @@ class NoFragmentCyclesTest extends ValidatorTestCase fragment fragZ on Dog { ...fragO } fragment fragO on Dog { ...fragP } fragment fragP on Dog { ...fragA, ...fragX } - ', [ - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragA', [ 'fragB', 'fragC', 'fragO', 'fragP' ]), - [ - new SourceLocation(2, 31), - new SourceLocation(3, 31), - new SourceLocation(4, 31), - new SourceLocation(8, 31), - new SourceLocation(9, 31), - ] - ), - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragO', [ 'fragP', 'fragX', 'fragY', 'fragZ' ]), - [ - new SourceLocation(8, 31), - new SourceLocation(9, 41), - new SourceLocation(5, 31), - new SourceLocation(6, 31), - new SourceLocation(7, 31), - ] - ) - ]); + ', + [ + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragA', ['fragB', 'fragC', 'fragO', 'fragP']), + [ + new SourceLocation(2, 31), + new SourceLocation(3, 31), + new SourceLocation(4, 31), + new SourceLocation(8, 31), + new SourceLocation(9, 31), + ] + ), + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragO', ['fragP', 'fragX', 'fragY', 'fragZ']), + [ + new SourceLocation(8, 31), + new SourceLocation(9, 41), + new SourceLocation(5, 31), + new SourceLocation(6, 31), + new SourceLocation(7, 31), + ] + ), + ] + ); } /** @@ -212,20 +265,24 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfDeeplyTwoPaths() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragB, ...fragC } fragment fragB on Dog { ...fragA } fragment fragC on Dog { ...fragA } - ', [ - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragA', ['fragB']), - [new SourceLocation(2, 31), new SourceLocation(3, 31)] - ), - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragA', ['fragC']), - [new SourceLocation(2, 41), new SourceLocation(4, 31)] - ) - ]); + ', + [ + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragA', ['fragB']), + [new SourceLocation(2, 31), new SourceLocation(3, 31)] + ), + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragA', ['fragC']), + [new SourceLocation(2, 41), new SourceLocation(4, 31)] + ), + ] + ); } /** @@ -233,20 +290,24 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfDeeplyTwoPathsTraverseOrder() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragC } fragment fragB on Dog { ...fragC } fragment fragC on Dog { ...fragA, ...fragB } - ', [ - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragA', [ 'fragC' ]), - [new SourceLocation(2,31), new SourceLocation(4,31)] - ), - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragC', [ 'fragB' ]), - [new SourceLocation(4, 41), new SourceLocation(3, 31)] - ) - ]); + ', + [ + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragA', ['fragC']), + [new SourceLocation(2, 31), new SourceLocation(4, 31)] + ), + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragC', ['fragB']), + [new SourceLocation(4, 41), new SourceLocation(3, 31)] + ), + ] + ); } /** @@ -254,35 +315,31 @@ class NoFragmentCyclesTest extends ValidatorTestCase */ public function testNoSpreadingItselfDeeplyAndImmediately() : void { - $this->expectFailsRule(new NoFragmentCycles, ' + $this->expectFailsRule( + new NoFragmentCycles(), + ' fragment fragA on Dog { ...fragB } fragment fragB on Dog { ...fragB, ...fragC } fragment fragC on Dog { ...fragA, ...fragB } - ', [ - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragB', []), - [new SourceLocation(3, 31)] - ), - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragA', [ 'fragB', 'fragC' ]), - [ - new SourceLocation(2, 31), - new SourceLocation(3, 41), - new SourceLocation(4, 31) - ] - ), - FormattedError::create( - NoFragmentCycles::cycleErrorMessage('fragB', [ 'fragC' ]), - [new SourceLocation(3, 41), new SourceLocation(4, 41)] - ) - ]); - } - - private function cycleError($fargment, $spreadNames, $line, $column) - { - return FormattedError::create( - NoFragmentCycles::cycleErrorMessage($fargment, $spreadNames), - [new SourceLocation($line, $column)] + ', + [ + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragB', []), + [new SourceLocation(3, 31)] + ), + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragA', ['fragB', 'fragC']), + [ + new SourceLocation(2, 31), + new SourceLocation(3, 41), + new SourceLocation(4, 31), + ] + ), + FormattedError::create( + NoFragmentCycles::cycleErrorMessage('fragB', ['fragC']), + [new SourceLocation(3, 41), new SourceLocation(4, 41)] + ), + ] ); } } diff --git a/tests/Validator/NoUndefinedVariablesTest.php b/tests/Validator/NoUndefinedVariablesTest.php index b4b56b3..ea2e25e 100644 --- a/tests/Validator/NoUndefinedVariablesTest.php +++ b/tests/Validator/NoUndefinedVariablesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new NoUndefinedVariables(), ' + $this->expectPassesRule( + new NoUndefinedVariables(), + ' query Foo($a: String, $b: String, $c: String) { field(a: $a, b: $b, c: $c) } - '); + ' + ); } /** @@ -26,7 +31,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testAllVariablesDeeplyDefined() : void { - $this->expectPassesRule(new NoUndefinedVariables, ' + $this->expectPassesRule( + new NoUndefinedVariables(), + ' query Foo($a: String, $b: String, $c: String) { field(a: $a) { field(b: $b) { @@ -34,7 +41,8 @@ class NoUndefinedVariablesTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -42,7 +50,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testAllVariablesDeeplyInInlineFragmentsDefined() : void { - $this->expectPassesRule(new NoUndefinedVariables, ' + $this->expectPassesRule( + new NoUndefinedVariables(), + ' query Foo($a: String, $b: String, $c: String) { ... on Type { field(a: $a) { @@ -54,7 +64,8 @@ class NoUndefinedVariablesTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -62,7 +73,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testAllVariablesInFragmentsDeeplyDefined() : void { - $this->expectPassesRule(new NoUndefinedVariables, ' + $this->expectPassesRule( + new NoUndefinedVariables(), + ' query Foo($a: String, $b: String, $c: String) { ...FragA } @@ -79,7 +92,8 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragC on Type { field(c: $c) } - '); + ' + ); } /** @@ -88,7 +102,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase public function testVariableWithinSingleFragmentDefinedInMultipleOperations() : void { // variable within single fragment defined in multiple operations - $this->expectPassesRule(new NoUndefinedVariables, ' + $this->expectPassesRule( + new NoUndefinedVariables(), + ' query Foo($a: String) { ...FragA } @@ -98,7 +114,8 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragA on Type { field(a: $a) } - '); + ' + ); } /** @@ -106,7 +123,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testVariableWithinFragmentsDefinedInOperations() : void { - $this->expectPassesRule(new NoUndefinedVariables, ' + $this->expectPassesRule( + new NoUndefinedVariables(), + ' query Foo($a: String) { ...FragA } @@ -119,7 +138,8 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragB on Type { field(b: $b) } - '); + ' + ); } /** @@ -127,7 +147,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testVariableWithinRecursiveFragmentDefined() : void { - $this->expectPassesRule(new NoUndefinedVariables, ' + $this->expectPassesRule( + new NoUndefinedVariables(), + ' query Foo($a: String) { ...FragA } @@ -136,7 +158,8 @@ class NoUndefinedVariablesTest extends ValidatorTestCase ...FragA } } - '); + ' + ); } /** @@ -144,13 +167,31 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testVariableNotDefined() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' query Foo($a: String, $b: String, $c: String) { field(a: $a, b: $b, c: $c, d: $d) } - ', [ - $this->undefVar('d', 3, 39, 'Foo', 2, 7) - ]); + ', + [ + $this->undefVar('d', 3, 39, 'Foo', 2, 7), + ] + ); + } + + private function undefVar($varName, $line, $column, $opName = null, $l2 = null, $c2 = null) + { + $locs = [new SourceLocation($line, $column)]; + + if ($l2 && $c2) { + $locs[] = new SourceLocation($l2, $c2); + } + + return FormattedError::create( + NoUndefinedVariables::undefinedVarMessage($varName, $opName), + $locs + ); } /** @@ -158,13 +199,17 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testVariableNotDefinedByUnNamedQuery() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' { field(a: $a) } - ', [ - $this->undefVar('a', 3, 18, '', 2, 7) - ]); + ', + [ + $this->undefVar('a', 3, 18, '', 2, 7), + ] + ); } /** @@ -172,14 +217,18 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testMultipleVariablesNotDefined() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' query Foo($b: String) { field(a: $a, b: $b, c: $c) } - ', [ - $this->undefVar('a', 3, 18, 'Foo', 2, 7), - $this->undefVar('c', 3, 32, 'Foo', 2, 7) - ]); + ', + [ + $this->undefVar('a', 3, 18, 'Foo', 2, 7), + $this->undefVar('c', 3, 32, 'Foo', 2, 7), + ] + ); } /** @@ -187,16 +236,20 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testVariableInFragmentNotDefinedByUnNamedQuery() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' { ...FragA } fragment FragA on Type { field(a: $a) } - ', [ - $this->undefVar('a', 6, 18, '', 2, 7) - ]); + ', + [ + $this->undefVar('a', 6, 18, '', 2, 7), + ] + ); } /** @@ -204,7 +257,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testVariableInFragmentNotDefinedByOperation() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' query Foo($a: String, $b: String) { ...FragA } @@ -221,9 +276,11 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragC on Type { field(c: $c) } - ', [ - $this->undefVar('c', 16, 18, 'Foo', 2, 7) - ]); + ', + [ + $this->undefVar('c', 16, 18, 'Foo', 2, 7), + ] + ); } /** @@ -231,7 +288,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testMultipleVariablesInFragmentsNotDefined() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' query Foo($b: String) { ...FragA } @@ -248,10 +307,12 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragC on Type { field(c: $c) } - ', [ - $this->undefVar('a', 6, 18, 'Foo', 2, 7), - $this->undefVar('c', 16, 18, 'Foo', 2, 7) - ]); + ', + [ + $this->undefVar('a', 6, 18, 'Foo', 2, 7), + $this->undefVar('c', 16, 18, 'Foo', 2, 7), + ] + ); } /** @@ -259,7 +320,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testSingleVariableInFragmentNotDefinedByMultipleOperations() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' query Foo($a: String) { ...FragAB } @@ -269,10 +332,12 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragAB on Type { field(a: $a, b: $b) } - ', [ - $this->undefVar('b', 9, 25, 'Foo', 2, 7), - $this->undefVar('b', 9, 25, 'Bar', 5, 7) - ]); + ', + [ + $this->undefVar('b', 9, 25, 'Foo', 2, 7), + $this->undefVar('b', 9, 25, 'Bar', 5, 7), + ] + ); } /** @@ -280,7 +345,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testVariablesInFragmentNotDefinedByMultipleOperations() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' query Foo($b: String) { ...FragAB } @@ -290,10 +357,12 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragAB on Type { field(a: $a, b: $b) } - ', [ - $this->undefVar('a', 9, 18, 'Foo', 2, 7), - $this->undefVar('b', 9, 25, 'Bar', 5, 7) - ]); + ', + [ + $this->undefVar('a', 9, 18, 'Foo', 2, 7), + $this->undefVar('b', 9, 25, 'Bar', 5, 7), + ] + ); } /** @@ -301,7 +370,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testVariableInFragmentUsedByOtherOperation() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' query Foo($b: String) { ...FragA } @@ -314,10 +385,12 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragB on Type { field(b: $b) } - ', [ - $this->undefVar('a', 9, 18, 'Foo', 2, 7), - $this->undefVar('b', 12, 18, 'Bar', 5, 7) - ]); + ', + [ + $this->undefVar('a', 9, 18, 'Foo', 2, 7), + $this->undefVar('b', 12, 18, 'Bar', 5, 7), + ] + ); } /** @@ -325,7 +398,9 @@ class NoUndefinedVariablesTest extends ValidatorTestCase */ public function testMultipleUndefinedVariablesProduceMultipleErrors() : void { - $this->expectFailsRule(new NoUndefinedVariables, ' + $this->expectFailsRule( + new NoUndefinedVariables(), + ' query Foo($b: String) { ...FragAB } @@ -340,28 +415,15 @@ class NoUndefinedVariablesTest extends ValidatorTestCase fragment FragC on Type { field2(c: $c) } - ', [ - $this->undefVar('a', 9, 19, 'Foo', 2, 7), - $this->undefVar('a', 11, 19, 'Foo', 2, 7), - $this->undefVar('c', 14, 19, 'Foo', 2, 7), - $this->undefVar('b', 9, 26, 'Bar', 5, 7), - $this->undefVar('b', 11, 26, 'Bar', 5, 7), - $this->undefVar('c', 14, 19, 'Bar', 5, 7), - ]); - } - - - private function undefVar($varName, $line, $column, $opName = null, $l2 = null, $c2 = null) - { - $locs = [new SourceLocation($line, $column)]; - - if ($l2 && $c2) { - $locs[] = new SourceLocation($l2, $c2); - } - - return FormattedError::create( - NoUndefinedVariables::undefinedVarMessage($varName, $opName), - $locs + ', + [ + $this->undefVar('a', 9, 19, 'Foo', 2, 7), + $this->undefVar('a', 11, 19, 'Foo', 2, 7), + $this->undefVar('c', 14, 19, 'Foo', 2, 7), + $this->undefVar('b', 9, 26, 'Bar', 5, 7), + $this->undefVar('b', 11, 26, 'Bar', 5, 7), + $this->undefVar('c', 14, 19, 'Bar', 5, 7), + ] ); } } diff --git a/tests/Validator/NoUnusedFragmentsTest.php b/tests/Validator/NoUnusedFragmentsTest.php index 323f915..9d794a8 100644 --- a/tests/Validator/NoUnusedFragmentsTest.php +++ b/tests/Validator/NoUnusedFragmentsTest.php @@ -1,4 +1,7 @@ expectPassesRule(new NoUnusedFragments(), ' + $this->expectPassesRule( + new NoUnusedFragments(), + ' { human(id: 4) { ...HumanFields1 @@ -33,7 +37,8 @@ class NoUnusedFragmentsTest extends ValidatorTestCase fragment HumanFields3 on Human { name } - '); + ' + ); } /** @@ -41,7 +46,9 @@ class NoUnusedFragmentsTest extends ValidatorTestCase */ public function testAllFragmentNamesAreUsedByMultipleOperations() : void { - $this->expectPassesRule(new NoUnusedFragments, ' + $this->expectPassesRule( + new NoUnusedFragments(), + ' query Foo { human(id: 4) { ...HumanFields1 @@ -62,7 +69,8 @@ class NoUnusedFragmentsTest extends ValidatorTestCase fragment HumanFields3 on Human { name } - '); + ' + ); } /** @@ -70,7 +78,9 @@ class NoUnusedFragmentsTest extends ValidatorTestCase */ public function testContainsUnknownFragments() : void { - $this->expectFailsRule(new NoUnusedFragments, ' + $this->expectFailsRule( + new NoUnusedFragments(), + ' query Foo { human(id: 4) { ...HumanFields1 @@ -97,10 +107,20 @@ class NoUnusedFragmentsTest extends ValidatorTestCase fragment Unused2 on Human { name } - ', [ - $this->unusedFrag('Unused1', 22, 7), - $this->unusedFrag('Unused2', 25, 7), - ]); + ', + [ + $this->unusedFrag('Unused1', 22, 7), + $this->unusedFrag('Unused2', 25, 7), + ] + ); + } + + private function unusedFrag($fragName, $line, $column) + { + return FormattedError::create( + NoUnusedFragments::unusedFragMessage($fragName), + [new SourceLocation($line, $column)] + ); } /** @@ -108,7 +128,9 @@ class NoUnusedFragmentsTest extends ValidatorTestCase */ public function testContainsUnknownFragmentsWithRefCycle() : void { - $this->expectFailsRule(new NoUnusedFragments, ' + $this->expectFailsRule( + new NoUnusedFragments(), + ' query Foo { human(id: 4) { ...HumanFields1 @@ -137,10 +159,12 @@ class NoUnusedFragmentsTest extends ValidatorTestCase name ...Unused1 } - ', [ - $this->unusedFrag('Unused1', 22, 7), - $this->unusedFrag('Unused2', 26, 7), - ]); + ', + [ + $this->unusedFrag('Unused1', 22, 7), + $this->unusedFrag('Unused2', 26, 7), + ] + ); } /** @@ -148,8 +172,9 @@ class NoUnusedFragmentsTest extends ValidatorTestCase */ public function testContainsUnknownAndUndefFragments() : void { - - $this->expectFailsRule(new NoUnusedFragments, ' + $this->expectFailsRule( + new NoUnusedFragments(), + ' query Foo { human(id: 4) { ...bar @@ -158,16 +183,10 @@ class NoUnusedFragmentsTest extends ValidatorTestCase fragment foo on Human { name } - ', [ - $this->unusedFrag('foo', 7, 7), - ]); - } - - private function unusedFrag($fragName, $line, $column) - { - return FormattedError::create( - NoUnusedFragments::unusedFragMessage($fragName), - [new SourceLocation($line, $column)] + ', + [ + $this->unusedFrag('foo', 7, 7), + ] ); } } diff --git a/tests/Validator/NoUnusedVariablesTest.php b/tests/Validator/NoUnusedVariablesTest.php index c261c15..7dc7d40 100644 --- a/tests/Validator/NoUnusedVariablesTest.php +++ b/tests/Validator/NoUnusedVariablesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new NoUnusedVariables(), ' + $this->expectPassesRule( + new NoUnusedVariables(), + ' query Foo($a: String, $b: String, $c: String) { field(a: $a, b: $b, c: $c) } - '); + ' + ); } /** @@ -26,7 +31,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testUsesAllVariablesDeeply() : void { - $this->expectPassesRule(new NoUnusedVariables, ' + $this->expectPassesRule( + new NoUnusedVariables(), + ' query Foo($a: String, $b: String, $c: String) { field(a: $a) { field(b: $b) { @@ -34,7 +41,8 @@ class NoUnusedVariablesTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -42,7 +50,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testUsesAllVariablesDeeplyInInlineFragments() : void { - $this->expectPassesRule(new NoUnusedVariables, ' + $this->expectPassesRule( + new NoUnusedVariables(), + ' query Foo($a: String, $b: String, $c: String) { ... on Type { field(a: $a) { @@ -54,7 +64,8 @@ class NoUnusedVariablesTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -62,7 +73,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testUsesAllVariablesInFragments() : void { - $this->expectPassesRule(new NoUnusedVariables, ' + $this->expectPassesRule( + new NoUnusedVariables(), + ' query Foo($a: String, $b: String, $c: String) { ...FragA } @@ -79,7 +92,8 @@ class NoUnusedVariablesTest extends ValidatorTestCase fragment FragC on Type { field(c: $c) } - '); + ' + ); } /** @@ -87,7 +101,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testVariableUsedByFragmentInMultipleOperations() : void { - $this->expectPassesRule(new NoUnusedVariables, ' + $this->expectPassesRule( + new NoUnusedVariables(), + ' query Foo($a: String) { ...FragA } @@ -100,7 +116,8 @@ class NoUnusedVariablesTest extends ValidatorTestCase fragment FragB on Type { field(b: $b) } - '); + ' + ); } /** @@ -108,7 +125,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testVariableUsedByRecursiveFragment() : void { - $this->expectPassesRule(new NoUnusedVariables, ' + $this->expectPassesRule( + new NoUnusedVariables(), + ' query Foo($a: String) { ...FragA } @@ -117,7 +136,8 @@ class NoUnusedVariablesTest extends ValidatorTestCase ...FragA } } - '); + ' + ); } /** @@ -125,13 +145,25 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testVariableNotUsed() : void { - $this->expectFailsRule(new NoUnusedVariables, ' + $this->expectFailsRule( + new NoUnusedVariables(), + ' query ($a: String, $b: String, $c: String) { field(a: $a, b: $b) } - ', [ - $this->unusedVar('c', null, 2, 38) - ]); + ', + [ + $this->unusedVar('c', null, 2, 38), + ] + ); + } + + private function unusedVar($varName, $opName, $line, $column) + { + return FormattedError::create( + NoUnusedVariables::unusedVariableMessage($varName, $opName), + [new SourceLocation($line, $column)] + ); } /** @@ -139,14 +171,18 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testMultipleVariablesNotUsed() : void { - $this->expectFailsRule(new NoUnusedVariables, ' + $this->expectFailsRule( + new NoUnusedVariables(), + ' query Foo($a: String, $b: String, $c: String) { field(b: $b) } - ', [ - $this->unusedVar('a', 'Foo', 2, 17), - $this->unusedVar('c', 'Foo', 2, 41) - ]); + ', + [ + $this->unusedVar('a', 'Foo', 2, 17), + $this->unusedVar('c', 'Foo', 2, 41), + ] + ); } /** @@ -154,7 +190,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testVariableNotUsedInFragments() : void { - $this->expectFailsRule(new NoUnusedVariables, ' + $this->expectFailsRule( + new NoUnusedVariables(), + ' query Foo($a: String, $b: String, $c: String) { ...FragA } @@ -171,9 +209,11 @@ class NoUnusedVariablesTest extends ValidatorTestCase fragment FragC on Type { field } - ', [ - $this->unusedVar('c', 'Foo', 2, 41) - ]); + ', + [ + $this->unusedVar('c', 'Foo', 2, 41), + ] + ); } /** @@ -181,7 +221,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testMultipleVariablesNotUsed2() : void { - $this->expectFailsRule(new NoUnusedVariables, ' + $this->expectFailsRule( + new NoUnusedVariables(), + ' query Foo($a: String, $b: String, $c: String) { ...FragA } @@ -198,10 +240,12 @@ class NoUnusedVariablesTest extends ValidatorTestCase fragment FragC on Type { field } - ', [ - $this->unusedVar('a', 'Foo', 2, 17), - $this->unusedVar('c', 'Foo', 2, 41) - ]); + ', + [ + $this->unusedVar('a', 'Foo', 2, 17), + $this->unusedVar('c', 'Foo', 2, 41), + ] + ); } /** @@ -209,7 +253,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testVariableNotUsedByUnreferencedFragment() : void { - $this->expectFailsRule(new NoUnusedVariables, ' + $this->expectFailsRule( + new NoUnusedVariables(), + ' query Foo($b: String) { ...FragA } @@ -219,9 +265,11 @@ class NoUnusedVariablesTest extends ValidatorTestCase fragment FragB on Type { field(b: $b) } - ', [ - $this->unusedVar('b', 'Foo', 2, 17) - ]); + ', + [ + $this->unusedVar('b', 'Foo', 2, 17), + ] + ); } /** @@ -229,7 +277,9 @@ class NoUnusedVariablesTest extends ValidatorTestCase */ public function testVariableNotUsedByFragmentUsedByOtherOperation() : void { - $this->expectFailsRule(new NoUnusedVariables, ' + $this->expectFailsRule( + new NoUnusedVariables(), + ' query Foo($b: String) { ...FragA } @@ -242,17 +292,11 @@ class NoUnusedVariablesTest extends ValidatorTestCase fragment FragB on Type { field(b: $b) } - ', [ - $this->unusedVar('b', 'Foo', 2, 17), - $this->unusedVar('a', 'Bar', 5, 17) - ]); - } - - private function unusedVar($varName, $opName, $line, $column) - { - return FormattedError::create( - NoUnusedVariables::unusedVariableMessage($varName, $opName), - [new SourceLocation($line, $column)] + ', + [ + $this->unusedVar('b', 'Foo', 2, 17), + $this->unusedVar('a', 'Bar', 5, 17), + ] ); } } diff --git a/tests/Validator/OverlappingFieldsCanBeMergedTest.php b/tests/Validator/OverlappingFieldsCanBeMergedTest.php index 2aaed7e..e10e020 100644 --- a/tests/Validator/OverlappingFieldsCanBeMergedTest.php +++ b/tests/Validator/OverlappingFieldsCanBeMergedTest.php @@ -1,29 +1,34 @@ expectPassesRule(new OverlappingFieldsCanBeMerged(), ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment uniqueFields on Dog { name nickname } - '); + ' + ); } /** @@ -31,12 +36,15 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testIdenticalFields() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment mergeIdenticalFields on Dog { name name } - '); + ' + ); } /** @@ -44,12 +52,15 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testIdenticalFieldsWithIdenticalArgs() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment mergeIdenticalFieldsWithIdenticalArgs on Dog { doesKnowCommand(dogCommand: SIT) doesKnowCommand(dogCommand: SIT) } - '); + ' + ); } /** @@ -57,12 +68,15 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testIdenticalFieldsWithIdenticalDirectives() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment mergeSameFieldsWithSameDirectives on Dog { name @include(if: true) name @include(if: true) } - '); + ' + ); } /** @@ -70,12 +84,15 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDifferentArgsWithDifferentAliases() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment differentArgsWithDifferentAliases on Dog { knowsSit : doesKnowCommand(dogCommand: SIT) knowsDown : doesKnowCommand(dogCommand: DOWN) } - '); + ' + ); } /** @@ -83,12 +100,15 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDifferentDirectivesWithDifferentAliases() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment differentDirectivesWithDifferentAliases on Dog { nameIfTrue : name @include(if: true) nameIfFalse : name @include(if: false) } - '); + ' + ); } /** @@ -99,12 +119,15 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase // Note: Differing skip/include directives don't create an ambiguous return // value and are acceptable in conditions where differing runtime values // may have the same desired effect of including or skipping a field. - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment differentDirectivesWithDifferentAliases on Dog { name @include(if: true) name @include(if: false) } - '); + ' + ); } /** @@ -112,17 +135,24 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testSameAliasesWithDifferentFieldTargets() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' fragment sameAliasesWithDifferentFieldTargets on Dog { fido : name fido : nickname } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('fido', 'name and nickname are different fields'), - [new SourceLocation(3, 9), new SourceLocation(4, 9)] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'fido', + 'name and nickname are different fields' + ), + [new SourceLocation(3, 9), new SourceLocation(4, 9)] + ), + ] + ); } /** @@ -132,7 +162,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase { // This is valid since no object can be both a "Dog" and a "Cat", thus // these fields can never overlap. - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment sameAliasesWithDifferentFieldTargets on Pet { ... on Dog { name @@ -141,7 +173,8 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase name: nickname } } - '); + ' + ); } /** @@ -149,17 +182,24 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testAliasMaskingDirectFieldAccess() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' fragment aliasMaskingDirectFieldAccess on Dog { name : nickname name } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('name', 'nickname and name are different fields'), - [new SourceLocation(3, 9), new SourceLocation(4, 9)] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'name', + 'nickname and name are different fields' + ), + [new SourceLocation(3, 9), new SourceLocation(4, 9)] + ), + ] + ); } /** @@ -167,17 +207,24 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDifferentArgsSecondAddsAnArgument() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' fragment conflictingArgs on Dog { doesKnowCommand doesKnowCommand(dogCommand: HEEL) } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('doesKnowCommand', 'they have differing arguments'), - [new SourceLocation(3, 9), new SourceLocation(4, 9)] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'doesKnowCommand', + 'they have differing arguments' + ), + [new SourceLocation(3, 9), new SourceLocation(4, 9)] + ), + ] + ); } /** @@ -185,7 +232,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDifferentArgsSecondMissingAnArgument() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' fragment conflictingArgs on Dog { doesKnowCommand(dogCommand: SIT) doesKnowCommand @@ -193,9 +242,12 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase ', [ FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('doesKnowCommand', 'they have differing arguments'), + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'doesKnowCommand', + 'they have differing arguments' + ), [new SourceLocation(3, 9), new SourceLocation(4, 9)] - ) + ), ] ); } @@ -205,17 +257,24 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testConflictingArgs() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' fragment conflictingArgs on Dog { doesKnowCommand(dogCommand: SIT) doesKnowCommand(dogCommand: HEEL) } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('doesKnowCommand', 'they have differing arguments'), - [new SourceLocation(3,9), new SourceLocation(4,9)] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'doesKnowCommand', + 'they have differing arguments' + ), + [new SourceLocation(3, 9), new SourceLocation(4, 9)] + ), + ] + ); } /** @@ -225,7 +284,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase { // This is valid since no object can be both a "Dog" and a "Cat", thus // these fields can never overlap. - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment conflictingArgs on Pet { ... on Dog { name(surname: true) @@ -234,7 +295,8 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase name } } - '); + ' + ); } /** @@ -242,7 +304,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testEncountersConflictInFragments() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' { ...A ...B @@ -253,12 +317,14 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase fragment B on Type { x: b } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'a and b are different fields'), - [new SourceLocation(7, 9), new SourceLocation(10, 9)] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'a and b are different fields'), + [new SourceLocation(7, 9), new SourceLocation(10, 9)] + ), + ] + ); } /** @@ -266,7 +332,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testReportsEachConflictOnce() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' { f1 { ...A @@ -288,20 +356,22 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase fragment B on Type { x: b } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'a and b are different fields'), - [new SourceLocation(18, 9), new SourceLocation(21, 9)] - ), - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'c and a are different fields'), - [new SourceLocation(14, 11), new SourceLocation(18, 9)] - ), - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'c and b are different fields'), - [new SourceLocation(14, 11), new SourceLocation(21, 9)] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'a and b are different fields'), + [new SourceLocation(18, 9), new SourceLocation(21, 9)] + ), + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'c and a are different fields'), + [new SourceLocation(14, 11), new SourceLocation(18, 9)] + ), + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'c and b are different fields'), + [new SourceLocation(14, 11), new SourceLocation(21, 9)] + ), + ] + ); } /** @@ -309,7 +379,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDeepConflict() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' { field { x: a @@ -318,17 +390,22 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase x: b } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('field', [['x', 'a and b are different fields']]), - [ - new SourceLocation(3, 9), - new SourceLocation(4, 11), - new SourceLocation(6,9), - new SourceLocation(7, 11) - ] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'field', + [['x', 'a and b are different fields']] + ), + [ + new SourceLocation(3, 9), + new SourceLocation(4, 11), + new SourceLocation(6, 9), + new SourceLocation(7, 11), + ] + ), + ] + ); } /** @@ -336,7 +413,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDeepConflictWithMultipleIssues() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' { field { x: a @@ -347,22 +426,27 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase y: d } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('field', [ - ['x', 'a and b are different fields'], - ['y', 'c and d are different fields'] - ]), - [ - new SourceLocation(3,9), - new SourceLocation(4,11), - new SourceLocation(5,11), - new SourceLocation(7,9), - new SourceLocation(8,11), - new SourceLocation(9,11) - ] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'field', + [ + ['x', 'a and b are different fields'], + ['y', 'c and d are different fields'], + ] + ), + [ + new SourceLocation(3, 9), + new SourceLocation(4, 11), + new SourceLocation(5, 11), + new SourceLocation(7, 9), + new SourceLocation(8, 11), + new SourceLocation(9, 11), + ] + ), + ] + ); } /** @@ -370,7 +454,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testVeryDeepConflict() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' { field { deepField { @@ -383,19 +469,24 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('field', [['deepField', [['x', 'a and b are different fields']]]]), - [ - new SourceLocation(3,9), - new SourceLocation(4,11), - new SourceLocation(5,13), - new SourceLocation(8,9), - new SourceLocation(9,11), - new SourceLocation(10,13) - ] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'field', + [['deepField', [['x', 'a and b are different fields']]]] + ), + [ + new SourceLocation(3, 9), + new SourceLocation(4, 11), + new SourceLocation(5, 13), + new SourceLocation(8, 9), + new SourceLocation(9, 11), + new SourceLocation(10, 13), + ] + ), + ] + ); } /** @@ -403,7 +494,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testReportsDeepConflictToNearestCommonAncestor() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' { field { deepField { @@ -419,17 +512,22 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('deepField', [['x', 'a and b are different fields']]), - [ - new SourceLocation(4,11), - new SourceLocation(5,13), - new SourceLocation(7,11), - new SourceLocation(8,13) - ] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'deepField', + [['x', 'a and b are different fields']] + ), + [ + new SourceLocation(4, 11), + new SourceLocation(5, 13), + new SourceLocation(7, 11), + new SourceLocation(8, 13), + ] + ), + ] + ); } /** @@ -437,7 +535,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testReportsDeepConflictToNearestCommonAncestorInFragments() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' { field { ...F @@ -461,17 +561,22 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('deeperField', [['x', 'a and b are different fields']]), - [ - new SourceLocation(12,11), - new SourceLocation(13,13), - new SourceLocation(15,11), - new SourceLocation(16,13), - ] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'deeperField', + [['x', 'a and b are different fields']] + ), + [ + new SourceLocation(12, 11), + new SourceLocation(13, 13), + new SourceLocation(15, 11), + new SourceLocation(16, 13), + ] + ), + ] + ); } /** @@ -479,7 +584,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testReportsDeepConflictInNestedFragments() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' { field { ...F @@ -502,22 +609,27 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase fragment J on T { x: b } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('field', [ - ['x', 'a and b are different fields'], - ['y', 'c and d are different fields'], - ]), - [ - new SourceLocation(3,9), - new SourceLocation(11,9), - new SourceLocation(15,9), - new SourceLocation(6,9), - new SourceLocation(22,9), - new SourceLocation(18,9), - ] - ) - ]); + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'field', + [ + ['x', 'a and b are different fields'], + ['y', 'c and d are different fields'], + ] + ), + [ + new SourceLocation(3, 9), + new SourceLocation(11, 9), + new SourceLocation(15, 9), + new SourceLocation(6, 9), + new SourceLocation(22, 9), + new SourceLocation(18, 9), + ] + ), + ] + ); } /** @@ -525,7 +637,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testIgnoresUnknownFragments() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' { field { ...Unknown @@ -536,7 +650,8 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase field ...OtherUnknown } - '); + ' + ); } // Describe: return types must be unambiguous @@ -550,7 +665,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase // type IntBox and the interface type NonNullStringBox1. While that // condition does not exist in the current schema, the schema could // expand in the future to allow this. Thus it is invalid. - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ...on IntBox { @@ -561,16 +679,136 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage( - 'scalar', - 'they return conflicting types Int and String!' + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'scalar', + 'they return conflicting types Int and String!' + ), + [ + new SourceLocation(5, 15), + new SourceLocation(8, 15), + ] ), - [new SourceLocation(5, 15), - new SourceLocation(8, 15)] - ) + ] + ); + } + + private function getSchema() + { + $StringBox = null; + $IntBox = null; + $SomeBox = null; + + $SomeBox = new InterfaceType([ + 'name' => 'SomeBox', + 'fields' => function () use (&$SomeBox) { + return [ + 'deepBox' => ['type' => $SomeBox], + 'unrelatedField' => ['type' => Type::string()], + ]; + }, ]); + + $StringBox = new ObjectType([ + 'name' => 'StringBox', + 'interfaces' => [$SomeBox], + 'fields' => function () use (&$StringBox, &$IntBox) { + return [ + 'scalar' => ['type' => Type::string()], + 'deepBox' => ['type' => $StringBox], + 'unrelatedField' => ['type' => Type::string()], + 'listStringBox' => ['type' => Type::listOf($StringBox)], + 'stringBox' => ['type' => $StringBox], + 'intBox' => ['type' => $IntBox], + ]; + }, + ]); + + $IntBox = new ObjectType([ + 'name' => 'IntBox', + 'interfaces' => [$SomeBox], + 'fields' => function () use (&$StringBox, &$IntBox) { + return [ + 'scalar' => ['type' => Type::int()], + 'deepBox' => ['type' => $IntBox], + 'unrelatedField' => ['type' => Type::string()], + 'listStringBox' => ['type' => Type::listOf($StringBox)], + 'stringBox' => ['type' => $StringBox], + 'intBox' => ['type' => $IntBox], + ]; + }, + ]); + + $NonNullStringBox1 = new InterfaceType([ + 'name' => 'NonNullStringBox1', + 'fields' => [ + 'scalar' => ['type' => Type::nonNull(Type::string())], + ], + ]); + + $NonNullStringBox1Impl = new ObjectType([ + 'name' => 'NonNullStringBox1Impl', + 'interfaces' => [$SomeBox, $NonNullStringBox1], + 'fields' => [ + 'scalar' => ['type' => Type::nonNull(Type::string())], + 'unrelatedField' => ['type' => Type::string()], + 'deepBox' => ['type' => $SomeBox], + ], + ]); + + $NonNullStringBox2 = new InterfaceType([ + 'name' => 'NonNullStringBox2', + 'fields' => [ + 'scalar' => ['type' => Type::nonNull(Type::string())], + ], + ]); + + $NonNullStringBox2Impl = new ObjectType([ + 'name' => 'NonNullStringBox2Impl', + 'interfaces' => [$SomeBox, $NonNullStringBox2], + 'fields' => [ + 'scalar' => ['type' => Type::nonNull(Type::string())], + 'unrelatedField' => ['type' => Type::string()], + 'deepBox' => ['type' => $SomeBox], + ], + ]); + + $Connection = new ObjectType([ + 'name' => 'Connection', + 'fields' => [ + 'edges' => [ + 'type' => Type::listOf(new ObjectType([ + 'name' => 'Edge', + 'fields' => [ + 'node' => [ + 'type' => new ObjectType([ + 'name' => 'Node', + 'fields' => [ + 'id' => ['type' => Type::id()], + 'name' => ['type' => Type::string()], + ], + ]), + ], + ], + ])), + ], + ], + ]); + + $schema = new Schema([ + 'query' => new ObjectType([ + 'name' => 'QueryRoot', + 'fields' => [ + 'someBox' => ['type' => $SomeBox], + 'connection' => ['type' => $Connection], + ], + ]), + 'types' => [$IntBox, $StringBox, $NonNullStringBox1Impl, $NonNullStringBox2Impl], + ]); + + return $schema; } /** @@ -581,7 +819,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase // In this case `deepBox` returns `SomeBox` in the first usage, and // `StringBox` in the second usage. These return types are not the same! // however this is valid because the return *shapes* are compatible. - $this->expectPassesRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on SomeBox { @@ -596,7 +837,8 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -604,7 +846,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDisallowsDifferingReturnTypesDespiteNoOverlap() : void { - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on IntBox { @@ -615,16 +860,20 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage( - 'scalar', - 'they return conflicting types Int and String' + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'scalar', + 'they return conflicting types Int and String' + ), + [ + new SourceLocation(5, 15), + new SourceLocation(8, 15), + ] ), - [ new SourceLocation(5, 15), - new SourceLocation(8, 15)] - ) - ]); + ] + ); } /** @@ -632,7 +881,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testReportsCorrectlyWhenANonExclusiveFollowsAnExclusive() : void { - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on IntBox { @@ -675,20 +927,22 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase fragment Y on SomeBox { scalar: unrelatedField } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage( - 'other', - [['scalar', 'scalar and unrelatedField are different fields']] + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'other', + [['scalar', 'scalar and unrelatedField are different fields']] + ), + [ + new SourceLocation(31, 11), + new SourceLocation(39, 11), + new SourceLocation(34, 11), + new SourceLocation(42, 11), + ] ), - [ - new SourceLocation(31, 11), - new SourceLocation(39, 11), - new SourceLocation(34, 11), - new SourceLocation(42, 11), - ] - ) - ]); + ] + ); } /** @@ -696,7 +950,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDisallowsDifferingReturnTypeNullabilityDespiteNoOverlap() : void { - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on NonNullStringBox1 { @@ -707,16 +964,20 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage( - 'scalar', - 'they return conflicting types String! and String' + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'scalar', + 'they return conflicting types String! and String' + ), + [ + new SourceLocation(5, 15), + new SourceLocation(8, 15), + ] ), - [new SourceLocation(5, 15), - new SourceLocation(8, 15)] - ) - ]); + ] + ); } /** @@ -724,7 +985,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDisallowsDifferingReturnTypeListDespiteNoOverlap() : void { - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on IntBox { @@ -739,19 +1003,25 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage( - 'box', - 'they return conflicting types [StringBox] and StringBox' + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'box', + 'they return conflicting types [StringBox] and StringBox' + ), + [ + new SourceLocation(5, 15), + new SourceLocation(10, 15), + ] ), - [new SourceLocation(5, 15), - new SourceLocation(10, 15)] - ) - ]); + ] + ); - - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on IntBox { @@ -766,21 +1036,28 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage( - 'box', - 'they return conflicting types StringBox and [StringBox]' + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'box', + 'they return conflicting types StringBox and [StringBox]' + ), + [ + new SourceLocation(5, 15), + new SourceLocation(10, 15), + ] ), - [new SourceLocation(5, 15), - new SourceLocation(10, 15)] - ) - ]); + ] + ); } public function testDisallowsDifferingSubfields() : void { - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on IntBox { @@ -796,17 +1073,21 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ + ', + [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage( - 'val', - 'scalar and unrelatedField are different fields' + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'val', + 'scalar and unrelatedField are different fields' + ), + [ + new SourceLocation(6, 17), + new SourceLocation(7, 17), + ] ), - [new SourceLocation(6, 17), - new SourceLocation(7, 17)] - ) - ]); + ] + ); } /** @@ -814,7 +1095,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDisallowsDifferingDeepReturnTypesDespiteNoOverlap() : void { - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on IntBox { @@ -829,20 +1113,22 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage( - 'box', - [ [ 'scalar', 'they return conflicting types String and Int' ] ] + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'box', + [['scalar', 'they return conflicting types String and Int']] + ), + [ + new SourceLocation(5, 15), + new SourceLocation(6, 17), + new SourceLocation(10, 15), + new SourceLocation(11, 17), + ] ), - [ - new SourceLocation(5, 15), - new SourceLocation(6, 17), - new SourceLocation(10, 15), - new SourceLocation(11, 17) - ] - ) - ]); + ] + ); } /** @@ -850,7 +1136,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testAllowsNonConflictingOverlapingTypes() : void { - $this->expectPassesRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ... on IntBox { @@ -861,7 +1150,8 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -869,7 +1159,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testSameWrappedScalarReturnTypes() : void { - $this->expectPassesRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ...on NonNullStringBox1 { @@ -880,7 +1173,8 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -888,14 +1182,18 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testAllowsInlineTypelessFragments() : void { - $this->expectPassesRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { a ... { a } } - '); + ' + ); } /** @@ -903,7 +1201,10 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testComparesDeepTypesIncludingList() : void { - $this->expectFailsRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { connection { ...edgeID @@ -922,27 +1223,35 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - ', [ - FormattedError::create( - OverlappingFieldsCanBeMerged::fieldsConflictMessage('edges', [['node', [['id', 'name and id are different fields']]]]), - [ - new SourceLocation(5, 13), - new SourceLocation(6, 15), - new SourceLocation(7, 17), - new SourceLocation(14, 11), - new SourceLocation(15, 13), - new SourceLocation(16, 15), - ] - ) - ]); - } + ', + [ + FormattedError::create( + OverlappingFieldsCanBeMerged::fieldsConflictMessage( + 'edges', + [['node', [['id', 'name and id are different fields']]]] + ), + [ + new SourceLocation(5, 13), + new SourceLocation(6, 15), + new SourceLocation(7, 17), + new SourceLocation(14, 11), + new SourceLocation(15, 13), + new SourceLocation(16, 15), + ] + ), + ] + ); + } /** * @see it('ignores unknown types') */ public function testIgnoresUnknownTypes() : void { - $this->expectPassesRuleWithSchema($this->getSchema(), new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRuleWithSchema( + $this->getSchema(), + new OverlappingFieldsCanBeMerged(), + ' { someBox { ...on UnknownType { @@ -953,7 +1262,8 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase } } } - '); + ' + ); } /** @@ -964,7 +1274,7 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase // The error template should end with a hint for the user to try using // different aliases. $error = OverlappingFieldsCanBeMerged::fieldsConflictMessage('x', 'a and b are different fields'); - $hint = 'Use different aliases on the fields to fetch both if this was intentional.'; + $hint = 'Use different aliases on the fields to fetch both if this was intentional.'; $this->assertStringEndsWith($hint, $error); } @@ -974,9 +1284,12 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDoesNotInfiniteLoopOnRecursiveFragment() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment fragA on Human { name, relatives { name, ...fragA } } - '); + ' + ); } /** @@ -984,9 +1297,12 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDoesNotInfiniteLoopOnImmeditelyRecursiveFragment() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment fragA on Human { name, ...fragA } - '); + ' + ); } /** @@ -994,11 +1310,14 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testDoesNotInfiniteLoopOnTransitivelyRecursiveFragment() : void { - $this->expectPassesRule(new OverlappingFieldsCanBeMerged, ' + $this->expectPassesRule( + new OverlappingFieldsCanBeMerged(), + ' fragment fragA on Human { name, ...fragB } fragment fragB on Human { name, ...fragC } fragment fragC on Human { name, ...fragA } - '); + ' + ); } /** @@ -1006,7 +1325,9 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase */ public function testFindInvalidCaseEvenWithImmediatelyRecursiveFragment() : void { - $this->expectFailsRule(new OverlappingFieldsCanBeMerged, ' + $this->expectFailsRule( + new OverlappingFieldsCanBeMerged(), + ' fragment sameAliasesWithDifferentFieldTargets on Dob { ...sameAliasesWithDifferentFieldTargets fido: name @@ -1023,124 +1344,8 @@ class OverlappingFieldsCanBeMergedTest extends ValidatorTestCase new SourceLocation(4, 9), new SourceLocation(5, 9), ] - ) - ]); - } - - private function getSchema() - { - $StringBox = null; - $IntBox = null; - $SomeBox = null; - - $SomeBox = new InterfaceType([ - 'name' => 'SomeBox', - 'fields' => function() use (&$SomeBox) { - return [ - 'deepBox' => ['type' => $SomeBox], - 'unrelatedField' => ['type' => Type::string()] - ]; - - } - ]); - - $StringBox = new ObjectType([ - 'name' => 'StringBox', - 'interfaces' => [$SomeBox], - 'fields' => function() use (&$StringBox, &$IntBox) { - return [ - 'scalar' => ['type' => Type::string()], - 'deepBox' => ['type' => $StringBox], - 'unrelatedField' => ['type' => Type::string()], - 'listStringBox' => ['type' => Type::listOf($StringBox)], - 'stringBox' => ['type' => $StringBox], - 'intBox' => ['type' => $IntBox], - ]; - } - ]); - - $IntBox = new ObjectType([ - 'name' => 'IntBox', - 'interfaces' => [$SomeBox], - 'fields' => function() use (&$StringBox, &$IntBox) { - return [ - 'scalar' => ['type' => Type::int()], - 'deepBox' => ['type' => $IntBox], - 'unrelatedField' => ['type' => Type::string()], - 'listStringBox' => ['type' => Type::listOf($StringBox)], - 'stringBox' => ['type' => $StringBox], - 'intBox' => ['type' => $IntBox], - ]; - } - ]); - - $NonNullStringBox1 = new InterfaceType([ - 'name' => 'NonNullStringBox1', - 'fields' => [ - 'scalar' => [ 'type' => Type::nonNull(Type::string()) ] + ), ] - ]); - - $NonNullStringBox1Impl = new ObjectType([ - 'name' => 'NonNullStringBox1Impl', - 'interfaces' => [ $SomeBox, $NonNullStringBox1 ], - 'fields' => [ - 'scalar' => [ 'type' => Type::nonNull(Type::string()) ], - 'unrelatedField' => ['type' => Type::string() ], - 'deepBox' => [ 'type' => $SomeBox ], - ] - ]); - - $NonNullStringBox2 = new InterfaceType([ - 'name' => 'NonNullStringBox2', - 'fields' => [ - 'scalar' => ['type' => Type::nonNull(Type::string())] - ] - ]); - - $NonNullStringBox2Impl = new ObjectType([ - 'name' => 'NonNullStringBox2Impl', - 'interfaces' => [ $SomeBox, $NonNullStringBox2 ], - 'fields' => [ - 'scalar' => [ 'type' => Type::nonNull(Type::string()) ], - 'unrelatedField' => [ 'type' => Type::string() ], - 'deepBox' => [ 'type' => $SomeBox ], - ] - ]); - - $Connection = new ObjectType([ - 'name' => 'Connection', - 'fields' => [ - 'edges' => [ - 'type' => Type::listOf(new ObjectType([ - 'name' => 'Edge', - 'fields' => [ - 'node' => [ - 'type' => new ObjectType([ - 'name' => 'Node', - 'fields' => [ - 'id' => ['type' => Type::id()], - 'name' => ['type' => Type::string()] - ] - ]) - ] - ] - ])) - ] - ] - ]); - - $schema = new Schema([ - 'query' => new ObjectType([ - 'name' => 'QueryRoot', - 'fields' => [ - 'someBox' => ['type' => $SomeBox], - 'connection' => ['type' => $Connection] - ] - ]), - 'types' => [$IntBox, $StringBox, $NonNullStringBox1Impl, $NonNullStringBox2Impl] - ]); - - return $schema; + ); } } diff --git a/tests/Validator/PossibleFragmentSpreadsTest.php b/tests/Validator/PossibleFragmentSpreadsTest.php index f5f074b..c4a8b29 100644 --- a/tests/Validator/PossibleFragmentSpreadsTest.php +++ b/tests/Validator/PossibleFragmentSpreadsTest.php @@ -1,4 +1,7 @@ expectPassesRule(new PossibleFragmentSpreads(), ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment objectWithinObject on Dog { ...dogFragment } fragment dogFragment on Dog { barkVolume } - '); + ' + ); } /** @@ -25,9 +30,12 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testOfTheSameObjectWithInlineFragment() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment objectWithinObjectAnon on Dog { ... on Dog { barkVolume } } - '); + ' + ); } /** @@ -35,10 +43,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testObjectIntoAnImplementedInterface() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment objectWithinInterface on Pet { ...dogFragment } fragment dogFragment on Dog { barkVolume } - '); + ' + ); } /** @@ -46,10 +57,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testObjectIntoContainingUnion() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment objectWithinUnion on CatOrDog { ...dogFragment } fragment dogFragment on Dog { barkVolume } - '); + ' + ); } /** @@ -57,10 +71,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testUnionIntoContainedObject() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment unionWithinObject on Dog { ...catOrDogFragment } fragment catOrDogFragment on CatOrDog { __typename } - '); + ' + ); } /** @@ -68,10 +85,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testUnionIntoOverlappingInterface() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment unionWithinInterface on Pet { ...catOrDogFragment } fragment catOrDogFragment on CatOrDog { __typename } - '); + ' + ); } /** @@ -79,10 +99,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testUnionIntoOverlappingUnion() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment unionWithinUnion on DogOrHuman { ...catOrDogFragment } fragment catOrDogFragment on CatOrDog { __typename } - '); + ' + ); } /** @@ -90,10 +113,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testInterfaceIntoImplementedObject() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment interfaceWithinObject on Dog { ...petFragment } fragment petFragment on Pet { name } - '); + ' + ); } /** @@ -101,10 +127,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testInterfaceIntoOverlappingInterface() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment interfaceWithinInterface on Pet { ...beingFragment } fragment beingFragment on Being { name } - '); + ' + ); } /** @@ -112,9 +141,12 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testInterfaceIntoOverlappingInterfaceInInlineFragment() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment interfaceWithinInterface on Pet { ... on Being { name } } - '); + ' + ); } /** @@ -122,10 +154,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testInterfaceIntoOverlappingUnion() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment interfaceWithinUnion on CatOrDog { ...petFragment } fragment petFragment on Pet { name } - '); + ' + ); } /** @@ -133,10 +168,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testIgnoresIncorrectTypeCaughtByFragmentsOnCompositeTypes() : void { - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment petFragment on Pet { ...badInADifferentWay } fragment badInADifferentWay on String { name } - '); + ' + ); } /** @@ -144,7 +182,9 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testDifferentObjectIntoObject() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidObjectWithinObject on Cat { ...dogFragment } fragment dogFragment on Dog { barkVolume } ', @@ -152,12 +192,22 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase ); } + private function error($fragName, $parentType, $fragType, $line, $column) + { + return FormattedError::create( + PossibleFragmentSpreads::typeIncompatibleSpreadMessage($fragName, $parentType, $fragType), + [new SourceLocation($line, $column)] + ); + } + /** * @see it('different object into object in inline fragment') */ public function testDifferentObjectIntoObjectInInlineFragment() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidObjectWithinObjectAnon on Cat { ... on Dog { barkVolume } } @@ -166,12 +216,22 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase ); } + private function errorAnon($parentType, $fragType, $line, $column) + { + return FormattedError::create( + PossibleFragmentSpreads::typeIncompatibleAnonSpreadMessage($parentType, $fragType), + [new SourceLocation($line, $column)] + ); + } + /** * @see it('object into not implementing interface') */ public function testObjectIntoNotImplementingInterface() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidObjectWithinInterface on Pet { ...humanFragment } fragment humanFragment on Human { pets { name } } ', @@ -184,7 +244,9 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testObjectIntoNotContainingUnion() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidObjectWithinUnion on CatOrDog { ...humanFragment } fragment humanFragment on Human { pets { name } } ', @@ -197,7 +259,9 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testUnionIntoNotContainedObject() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidUnionWithinObject on Human { ...catOrDogFragment } fragment catOrDogFragment on CatOrDog { __typename } ', @@ -210,7 +274,9 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testUnionIntoNonOverlappingInterface() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidUnionWithinInterface on Pet { ...humanOrAlienFragment } fragment humanOrAlienFragment on HumanOrAlien { __typename } ', @@ -223,7 +289,9 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testUnionIntoNonOverlappingUnion() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidUnionWithinUnion on CatOrDog { ...humanOrAlienFragment } fragment humanOrAlienFragment on HumanOrAlien { __typename } ', @@ -236,7 +304,9 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testInterfaceIntoNonImplementingObject() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidInterfaceWithinObject on Cat { ...intelligentFragment } fragment intelligentFragment on Intelligent { iq } ', @@ -252,12 +322,15 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase // Ideally this should fail, but our new lazy schema doesn't scan through all types and fields // So we don't have enough knowledge to check interface intersection and always allow this to pass: - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment invalidInterfaceWithinInterface on Pet { ...intelligentFragment } fragment intelligentFragment on Intelligent { iq } - '); + ' + ); } /** @@ -268,11 +341,14 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase // Ideally this should fail, but our new lazy schema doesn't scan through all types and fields // So we don't have enough knowledge to check interface intersection and always allow this to pass: - $this->expectPassesRule(new PossibleFragmentSpreads, ' + $this->expectPassesRule( + new PossibleFragmentSpreads(), + ' fragment invalidInterfaceWithinInterfaceAnon on Pet { ...on Intelligent { iq } } - '); + ' + ); } /** @@ -280,27 +356,13 @@ class PossibleFragmentSpreadsTest extends ValidatorTestCase */ public function testInterfaceIntoNonOverlappingUnion() : void { - $this->expectFailsRule(new PossibleFragmentSpreads, ' + $this->expectFailsRule( + new PossibleFragmentSpreads(), + ' fragment invalidInterfaceWithinUnion on HumanOrAlien { ...petFragment } fragment petFragment on Pet { name } ', [$this->error('petFragment', 'HumanOrAlien', 'Pet', 2, 62)] ); } - - private function error($fragName, $parentType, $fragType, $line, $column) - { - return FormattedError::create( - PossibleFragmentSpreads::typeIncompatibleSpreadMessage($fragName, $parentType, $fragType), - [new SourceLocation($line, $column)] - ); - } - - private function errorAnon($parentType, $fragType, $line, $column) - { - return FormattedError::create( - PossibleFragmentSpreads::typeIncompatibleAnonSpreadMessage($parentType, $fragType), - [new SourceLocation($line, $column)] - ); - } } diff --git a/tests/Validator/ProvidedNonNullArgumentsTest.php b/tests/Validator/ProvidedNonNullArgumentsTest.php index 25291e7..803fcda 100644 --- a/tests/Validator/ProvidedNonNullArgumentsTest.php +++ b/tests/Validator/ProvidedNonNullArgumentsTest.php @@ -1,4 +1,7 @@ expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { dog { isHousetrained(unknownArgument: true) } } - '); + ' + ); } // Valid non-nullable value: @@ -31,13 +36,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testArgOnOptionalArg() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { dog { isHousetrained(atOtherHomes: true) } } - '); + ' + ); } /** @@ -45,13 +53,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testNoArgOnOptionalArg() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { dog { isHousetrained } } - '); + ' + ); } /** @@ -59,13 +70,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testMultipleArgs() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleReqs(req1: 1, req2: 2) } } - '); + ' + ); } /** @@ -73,13 +87,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testMultipleArgsReverseOrder() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleReqs(req2: 2, req1: 1) } } - '); + ' + ); } /** @@ -87,13 +104,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testNoArgsOnMultipleOptional() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleOpts } } - '); + ' + ); } /** @@ -101,13 +121,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testOneArgOnMultipleOptional() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleOpts(opt1: 1) } } - '); + ' + ); } /** @@ -115,13 +138,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testSecondArgOnMultipleOptional() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleOpts(opt2: 1) } } - '); + ' + ); } /** @@ -129,13 +155,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testMultipleReqsOnMixedList() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleOptAndReq(req1: 3, req2: 4) } } - '); + ' + ); } /** @@ -143,13 +172,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testMultipleReqsAndOneOptOnMixedList() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleOptAndReq(req1: 3, req2: 4, opt1: 5) } } - '); + ' + ); } /** @@ -157,13 +189,16 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testAllReqsAndOptsOnMixedList() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleOptAndReq(req1: 3, req2: 4, opt1: 5, opt2: 6) } } - '); + ' + ); } // Invalid non-nullable value @@ -173,15 +208,25 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testMissingOneNonNullableArgument() : void { - $this->expectFailsRule(new ProvidedNonNullArguments, ' + $this->expectFailsRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleReqs(req2: 2) } } - ', [ - $this->missingFieldArg('multipleReqs', 'req1', 'Int!', 4, 13) - ]); + ', + [$this->missingFieldArg('multipleReqs', 'req1', 'Int!', 4, 13)] + ); + } + + private function missingFieldArg($fieldName, $argName, $typeName, $line, $column) + { + return FormattedError::create( + ProvidedNonNullArguments::missingFieldArgMessage($fieldName, $argName, $typeName), + [new SourceLocation($line, $column)] + ); } /** @@ -189,46 +234,57 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testMissingMultipleNonNullableArguments() : void { - $this->expectFailsRule(new ProvidedNonNullArguments, ' + $this->expectFailsRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleReqs } } - ', [ - $this->missingFieldArg('multipleReqs', 'req1', 'Int!', 4, 13), - $this->missingFieldArg('multipleReqs', 'req2', 'Int!', 4, 13), - ]); + ', + [ + $this->missingFieldArg('multipleReqs', 'req1', 'Int!', 4, 13), + $this->missingFieldArg('multipleReqs', 'req2', 'Int!', 4, 13), + ] + ); } + // Describe: Directive arguments + /** * @see it('Incorrect value and missing argument') */ public function testIncorrectValueAndMissingArgument() : void { - $this->expectFailsRule(new ProvidedNonNullArguments, ' + $this->expectFailsRule( + new ProvidedNonNullArguments(), + ' { complicatedArgs { multipleReqs(req1: "one") } } - ', [ - $this->missingFieldArg('multipleReqs', 'req2', 'Int!', 4, 13), - ]); + ', + [ + $this->missingFieldArg('multipleReqs', 'req2', 'Int!', 4, 13), + ] + ); } - // Describe: Directive arguments - /** * @see it('ignores unknown directives') */ public function testIgnoresUnknownDirectives() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { dog @unknown } - '); + ' + ); } /** @@ -236,7 +292,9 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testWithDirectivesOfValidTypes() : void { - $this->expectPassesRule(new ProvidedNonNullArguments, ' + $this->expectPassesRule( + new ProvidedNonNullArguments(), + ' { dog @include(if: true) { name @@ -245,7 +303,8 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase name } } - '); + ' + ); } /** @@ -253,23 +312,19 @@ class ProvidedNonNullArgumentsTest extends ValidatorTestCase */ public function testWithDirectiveWithMissingTypes() : void { - $this->expectFailsRule(new ProvidedNonNullArguments, ' + $this->expectFailsRule( + new ProvidedNonNullArguments(), + ' { dog @include { name @skip } } - ', [ - $this->missingDirectiveArg('include', 'if', 'Boolean!', 3, 15), - $this->missingDirectiveArg('skip', 'if', 'Boolean!', 4, 18) - ]); - } - - private function missingFieldArg($fieldName, $argName, $typeName, $line, $column) - { - return FormattedError::create( - ProvidedNonNullArguments::missingFieldArgMessage($fieldName, $argName, $typeName), - [new SourceLocation($line, $column)] + ', + [ + $this->missingDirectiveArg('include', 'if', 'Boolean!', 3, 15), + $this->missingDirectiveArg('skip', 'if', 'Boolean!', 4, 18), + ] ); } diff --git a/tests/Validator/QueryComplexityTest.php b/tests/Validator/QueryComplexityTest.php index dcc1852..8847f76 100644 --- a/tests/Validator/QueryComplexityTest.php +++ b/tests/Validator/QueryComplexityTest.php @@ -1,4 +1,7 @@ setMaxQueryComplexity($maxDepth); - } - - return self::$rule; - } - public function testSimpleQueries() : void { $query = 'query MyQuery { human { firstName } }'; @@ -48,6 +25,19 @@ class QueryComplexityTest extends QuerySecurityTestCase $this->assertDocumentValidators($query, 2, 3); } + private function assertDocumentValidators($query, $queryComplexity, $startComplexity) + { + for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) { + $positions = []; + + if ($maxComplexity < $queryComplexity && $maxComplexity !== QueryComplexity::DISABLED) { + $positions = [$this->createFormattedError($maxComplexity, $queryComplexity)]; + } + + $this->assertDocumentValidator($query, $maxComplexity, $positions); + } + } + public function testInlineFragmentQueries() : void { $query = 'query MyQuery { human { ... on Human { firstName } } }'; @@ -92,6 +82,22 @@ class QueryComplexityTest extends QuerySecurityTestCase $this->assertDocumentValidators($query, 3, 4); } + /** + * @param int $maxDepth + * + * @return QueryComplexity + */ + protected function getRule($maxDepth = null) + { + if (self::$rule === null) { + self::$rule = new QueryComplexity($maxDepth); + } elseif ($maxDepth !== null) { + self::$rule->setMaxQueryComplexity($maxDepth); + } + + return self::$rule; + } + public function testQueryWithEnabledIncludeDirectives() : void { $query = 'query MyQuery($withDogs: Boolean!) { human { dogs(name: "Root") @include(if:$withDogs) { name } } }'; @@ -133,8 +139,8 @@ class QueryComplexityTest extends QuerySecurityTestCase $query = 'query MyQuery($withDogs: Boolean!, $withoutDogName: Boolean!) { human { dogs(name: "Root") @include(if:$withDogs) { name @skip(if:$withoutDogName) } } }'; $this->getRule()->setRawVariableValues([ - 'withDogs' => true, - 'withoutDogName' => true + 'withDogs' => true, + 'withoutDogName' => true, ]); $this->assertDocumentValidators($query, 2, 3); @@ -159,16 +165,19 @@ class QueryComplexityTest extends QuerySecurityTestCase { $query = 'query MyQuery { human(name: INVALID_VALUE) { dogs {name} } }'; - $reportedError = new Error("OtherValidatorError"); - $otherRule = new CustomValidationRule('otherRule', function(ValidationContext $context) use ($reportedError) { - return [ - NodeKind::OPERATION_DEFINITION => [ - 'leave' => function() use ($context, $reportedError) { - $context->reportError($reportedError); - } - ] - ]; - }); + $reportedError = new Error('OtherValidatorError'); + $otherRule = new CustomValidationRule( + 'otherRule', + function (ValidationContext $context) use ($reportedError) { + return [ + NodeKind::OPERATION_DEFINITION => [ + 'leave' => function () use ($context, $reportedError) { + $context->reportError($reportedError); + }, + ], + ]; + } + ); $errors = DocumentValidator::validate( QuerySecuritySchema::buildSchema(), @@ -187,16 +196,14 @@ class QueryComplexityTest extends QuerySecurityTestCase ); } - private function assertDocumentValidators($query, $queryComplexity, $startComplexity) + /** + * @param int $max + * @param int $count + * + * @return string + */ + protected function getErrorMessage($max, $count) { - for ($maxComplexity = $startComplexity; $maxComplexity >= 0; --$maxComplexity) { - $positions = []; - - if ($maxComplexity < $queryComplexity && $maxComplexity !== QueryComplexity::DISABLED) { - $positions = [$this->createFormattedError($maxComplexity, $queryComplexity)]; - } - - $this->assertDocumentValidator($query, $maxComplexity, $positions); - } + return QueryComplexity::maxQueryComplexityErrorMessage($max, $count); } } diff --git a/tests/Validator/QueryDepthTest.php b/tests/Validator/QueryDepthTest.php index 1063156..bccb26c 100644 --- a/tests/Validator/QueryDepthTest.php +++ b/tests/Validator/QueryDepthTest.php @@ -1,35 +1,19 @@ assertDocumentValidator($this->buildRecursiveQuery($queryDepth), $maxQueryDepth, $expectedErrors); } + private function buildRecursiveQuery($depth) + { + $query = sprintf('query MyQuery { human%s }', $this->buildRecursiveQueryPart($depth)); + + return $query; + } + + private function buildRecursiveQueryPart($depth) + { + $templates = [ + 'human' => ' { firstName%s } ', + 'dog' => ' dogs { name%s } ', + ]; + + $part = $templates['human']; + + for ($i = 1; $i <= $depth; ++$i) { + $key = ($i % 2 === 1) ? 'human' : 'dog'; + $template = $templates[$key]; + + $part = sprintf($part, ($key === 'human' ? ' owner ' : '') . $template); + } + $part = str_replace('%s', '', $part); + + return $part; + } + /** - * @param $queryDepth - * @param int $maxQueryDepth - * @param array $expectedErrors + * @param int $queryDepth + * @param int $maxQueryDepth + * @param string[][] $expectedErrors * @dataProvider queryDataProvider */ public function testFragmentQueries($queryDepth, $maxQueryDepth = 7, $expectedErrors = []) : void { - $this->assertDocumentValidator($this->buildRecursiveUsingFragmentQuery($queryDepth), $maxQueryDepth, $expectedErrors); + $this->assertDocumentValidator( + $this->buildRecursiveUsingFragmentQuery($queryDepth), + $maxQueryDepth, + $expectedErrors + ); + } + + private function buildRecursiveUsingFragmentQuery($depth) + { + $query = sprintf( + 'query MyQuery { human { ...F1 } } fragment F1 on Human %s', + $this->buildRecursiveQueryPart($depth) + ); + + return $query; } /** - * @param $queryDepth - * @param int $maxQueryDepth - * @param array $expectedErrors + * @param int $queryDepth + * @param int $maxQueryDepth + * @param string[][] $expectedErrors * @dataProvider queryDataProvider */ public function testInlineFragmentQueries($queryDepth, $maxQueryDepth = 7, $expectedErrors = []) : void { - $this->assertDocumentValidator($this->buildRecursiveUsingInlineFragmentQuery($queryDepth), $maxQueryDepth, $expectedErrors); + $this->assertDocumentValidator( + $this->buildRecursiveUsingInlineFragmentQuery($queryDepth), + $maxQueryDepth, + $expectedErrors + ); + } + + private function buildRecursiveUsingInlineFragmentQuery($depth) + { + $query = sprintf( + 'query MyQuery { human { ...on Human %s } }', + $this->buildRecursiveQueryPart($depth) + ); + + return $query; } public function testComplexityIntrospectionQuery() : void @@ -99,50 +138,24 @@ class QueryDepthTest extends QuerySecurityTestCase ]; } - private function buildRecursiveQuery($depth) + /** + * @param int $max + * @param int $count + * + * @return string + */ + protected function getErrorMessage($max, $count) { - $query = sprintf('query MyQuery { human%s }', $this->buildRecursiveQueryPart($depth)); - - return $query; + return QueryDepth::maxQueryDepthErrorMessage($max, $count); } - private function buildRecursiveUsingFragmentQuery($depth) + /** + * @param int $maxDepth + * + * @return QueryDepth + */ + protected function getRule($maxDepth) { - $query = sprintf( - 'query MyQuery { human { ...F1 } } fragment F1 on Human %s', - $this->buildRecursiveQueryPart($depth) - ); - - return $query; - } - - private function buildRecursiveUsingInlineFragmentQuery($depth) - { - $query = sprintf( - 'query MyQuery { human { ...on Human %s } }', - $this->buildRecursiveQueryPart($depth) - ); - - return $query; - } - - private function buildRecursiveQueryPart($depth) - { - $templates = [ - 'human' => ' { firstName%s } ', - 'dog' => ' dogs { name%s } ', - ]; - - $part = $templates['human']; - - for ($i = 1; $i <= $depth; ++$i) { - $key = ($i % 2 == 1) ? 'human' : 'dog'; - $template = $templates[$key]; - - $part = sprintf($part, ('human' == $key ? ' owner ' : '').$template); - } - $part = str_replace('%s', '', $part); - - return $part; + return new QueryDepth($maxDepth); } } diff --git a/tests/Validator/QuerySecuritySchema.php b/tests/Validator/QuerySecuritySchema.php index 024990a..534a340 100644 --- a/tests/Validator/QuerySecuritySchema.php +++ b/tests/Validator/QuerySecuritySchema.php @@ -1,18 +1,25 @@ static::buildQueryRootType() + 'query' => static::buildQueryRootType(), ]); return self::$schema; @@ -33,12 +40,12 @@ class QuerySecuritySchema public static function buildQueryRootType() { - if (null !== self::$queryRootType) { + if (self::$queryRootType !== null) { return self::$queryRootType; } self::$queryRootType = new ObjectType([ - 'name' => 'QueryRoot', + 'name' => 'QueryRoot', 'fields' => [ 'human' => [ 'type' => self::buildHumanType(), @@ -52,18 +59,18 @@ class QuerySecuritySchema public static function buildHumanType() { - if (null !== self::$humanType) { + if (self::$humanType !== null) { return self::$humanType; } self::$humanType = new ObjectType( [ - 'name' => 'Human', - 'fields' => function() { + 'name' => 'Human', + 'fields' => function () { return [ 'firstName' => ['type' => Type::nonNull(Type::string())], - 'dogs' => [ - 'type' => Type::nonNull( + 'dogs' => [ + 'type' => Type::nonNull( Type::listOf( Type::nonNull(self::buildDogType()) ) @@ -73,7 +80,7 @@ class QuerySecuritySchema return $childrenComplexity + $complexity; }, - 'args' => ['name' => ['type' => Type::string()]], + 'args' => ['name' => ['type' => Type::string()]], ], ]; }, @@ -85,15 +92,15 @@ class QuerySecuritySchema public static function buildDogType() { - if (null !== self::$dogType) { + if (self::$dogType !== null) { return self::$dogType; } self::$dogType = new ObjectType( [ - 'name' => 'Dog', + 'name' => 'Dog', 'fields' => [ - 'name' => ['type' => Type::nonNull(Type::string())], + 'name' => ['type' => Type::nonNull(Type::string())], 'master' => [ 'type' => self::buildHumanType(), ], diff --git a/tests/Validator/QuerySecurityTestCase.php b/tests/Validator/QuerySecurityTestCase.php index 5188e3e..a4e5ba5 100644 --- a/tests/Validator/QuerySecurityTestCase.php +++ b/tests/Validator/QuerySecurityTestCase.php @@ -1,30 +1,20 @@ getRule(-1); } - protected function createFormattedError($max, $count, $locations = []) - { - return FormattedError::create($this->getErrorMessage($max, $count), $locations); - } - - protected function assertDocumentValidator($queryString, $max, array $expectedErrors = []) - { - $errors = DocumentValidator::validate( - QuerySecuritySchema::buildSchema(), - Parser::parse($queryString), - [$this->getRule($max)] - ); - - $this->assertEquals($expectedErrors, array_map(['GraphQL\Error\Error', 'formatError'], $errors), $queryString); - - return $errors; - } + /** + * @param int $max + * + * @return QuerySecurityRule + */ + abstract protected function getRule($max); protected function assertIntrospectionQuery($maxExpected) { @@ -59,6 +38,48 @@ abstract class QuerySecurityTestCase extends TestCase $this->assertMaxValue($query, $maxExpected); } + protected function assertMaxValue($query, $maxExpected) + { + $this->assertDocumentValidator($query, $maxExpected); + $newMax = $maxExpected - 1; + if ($newMax === QuerySecurityRule::DISABLED) { + return; + } + $this->assertDocumentValidator($query, $newMax, [$this->createFormattedError($newMax, $maxExpected)]); + } + + /** + * @param string $queryString + * @param int $max + * @param string[][] $expectedErrors + * @return Error[] + */ + protected function assertDocumentValidator($queryString, $max, array $expectedErrors = []) : array + { + $errors = DocumentValidator::validate( + QuerySecuritySchema::buildSchema(), + Parser::parse($queryString), + [$this->getRule($max)] + ); + + $this->assertEquals($expectedErrors, array_map([Error::class, 'formatError'], $errors), $queryString); + + return $errors; + } + + protected function createFormattedError($max, $count, $locations = []) + { + return FormattedError::create($this->getErrorMessage($max, $count), $locations); + } + + /** + * @param int $max + * @param int $count + * + * @return string + */ + abstract protected function getErrorMessage($max, $count); + protected function assertIntrospectionTypeMetaFieldQuery($maxExpected) { $query = ' @@ -84,14 +105,4 @@ abstract class QuerySecurityTestCase extends TestCase '; $this->assertMaxValue($query, $maxExpected); } - - protected function assertMaxValue($query, $maxExpected) - { - $this->assertDocumentValidator($query, $maxExpected); - $newMax = $maxExpected - 1; - if ($newMax === QuerySecurityRule::DISABLED) { - return; - } - $this->assertDocumentValidator($query, $newMax, [$this->createFormattedError($newMax, $maxExpected)]); - } } diff --git a/tests/Validator/ScalarLeafsTest.php b/tests/Validator/ScalarLeafsTest.php index 1d9db96..c787be3 100644 --- a/tests/Validator/ScalarLeafsTest.php +++ b/tests/Validator/ScalarLeafsTest.php @@ -1,4 +1,7 @@ expectPassesRule(new ScalarLeafs, ' + $this->expectPassesRule( + new ScalarLeafs(), + ' fragment scalarSelection on Dog { barks } - '); + ' + ); } /** @@ -26,11 +31,23 @@ class ScalarLeafsTest extends ValidatorTestCase */ public function testObjectTypeMissingSelection() : void { - $this->expectFailsRule(new ScalarLeafs, ' + $this->expectFailsRule( + new ScalarLeafs(), + ' query directQueryOnObjectWithoutSubFields { human } - ', [$this->missingObjSubselection('human', 'Human', 3, 9)]); + ', + [$this->missingObjSubselection('human', 'Human', 3, 9)] + ); + } + + private function missingObjSubselection($field, $type, $line, $column) + { + return FormattedError::create( + ScalarLeafs::requiredSubselectionMessage($field, $type), + [new SourceLocation($line, $column)] + ); } /** @@ -38,11 +55,15 @@ class ScalarLeafsTest extends ValidatorTestCase */ public function testInterfaceTypeMissingSelection() : void { - $this->expectFailsRule(new ScalarLeafs, ' + $this->expectFailsRule( + new ScalarLeafs(), + ' { human { pets } } - ', [$this->missingObjSubselection('pets', '[Pet]', 3, 17)]); + ', + [$this->missingObjSubselection('pets', '[Pet]', 3, 17)] + ); } /** @@ -50,11 +71,14 @@ class ScalarLeafsTest extends ValidatorTestCase */ public function testValidScalarSelectionWithArgs() : void { - $this->expectPassesRule(new ScalarLeafs, ' + $this->expectPassesRule( + new ScalarLeafs(), + ' fragment scalarSelectionWithArgs on Dog { doesKnowCommand(dogCommand: SIT) } - '); + ' + ); } /** @@ -62,67 +86,14 @@ class ScalarLeafsTest extends ValidatorTestCase */ public function testScalarSelectionNotAllowedOnBoolean() : void { - $this->expectFailsRule(new ScalarLeafs, ' + $this->expectFailsRule( + new ScalarLeafs(), + ' fragment scalarSelectionsNotAllowedOnBoolean on Dog { barks { sinceWhen } } ', - [$this->noScalarSubselection('barks', 'Boolean', 3, 15)]); - } - - /** - * @see it('scalar selection not allowed on Enum') - */ - public function testScalarSelectionNotAllowedOnEnum() : void - { - $this->expectFailsRule(new ScalarLeafs, ' - fragment scalarSelectionsNotAllowedOnEnum on Cat { - furColor { inHexdec } - } - ', - [$this->noScalarSubselection('furColor', 'FurColor', 3, 18)] - ); - } - - /** - * @see it('scalar selection not allowed with args') - */ - public function testScalarSelectionNotAllowedWithArgs() : void - { - $this->expectFailsRule(new ScalarLeafs, ' - fragment scalarSelectionsNotAllowedWithArgs on Dog { - doesKnowCommand(dogCommand: SIT) { sinceWhen } - } - ', - [$this->noScalarSubselection('doesKnowCommand', 'Boolean', 3, 42)] - ); - } - - /** - * @see it('Scalar selection not allowed with directives') - */ - public function testScalarSelectionNotAllowedWithDirectives() : void - { - $this->expectFailsRule(new ScalarLeafs, ' - fragment scalarSelectionsNotAllowedWithDirectives on Dog { - name @include(if: true) { isAlsoHumanName } - } - ', - [$this->noScalarSubselection('name', 'String', 3, 33)] - ); - } - - /** - * @see it('Scalar selection not allowed with directives and args') - */ - public function testScalarSelectionNotAllowedWithDirectivesAndArgs() : void - { - $this->expectFailsRule(new ScalarLeafs, ' - fragment scalarSelectionsNotAllowedWithDirectivesAndArgs on Dog { - doesKnowCommand(dogCommand: SIT) @include(if: true) { sinceWhen } - } - ', - [$this->noScalarSubselection('doesKnowCommand', 'Boolean', 3, 61)] + [$this->noScalarSubselection('barks', 'Boolean', 3, 15)] ); } @@ -134,11 +105,67 @@ class ScalarLeafsTest extends ValidatorTestCase ); } - private function missingObjSubselection($field, $type, $line, $column) + /** + * @see it('scalar selection not allowed on Enum') + */ + public function testScalarSelectionNotAllowedOnEnum() : void { - return FormattedError::create( - ScalarLeafs::requiredSubselectionMessage($field, $type), - [new SourceLocation($line, $column)] + $this->expectFailsRule( + new ScalarLeafs(), + ' + fragment scalarSelectionsNotAllowedOnEnum on Cat { + furColor { inHexdec } + } + ', + [$this->noScalarSubselection('furColor', 'FurColor', 3, 18)] + ); + } + + /** + * @see it('scalar selection not allowed with args') + */ + public function testScalarSelectionNotAllowedWithArgs() : void + { + $this->expectFailsRule( + new ScalarLeafs(), + ' + fragment scalarSelectionsNotAllowedWithArgs on Dog { + doesKnowCommand(dogCommand: SIT) { sinceWhen } + } + ', + [$this->noScalarSubselection('doesKnowCommand', 'Boolean', 3, 42)] + ); + } + + /** + * @see it('Scalar selection not allowed with directives') + */ + public function testScalarSelectionNotAllowedWithDirectives() : void + { + $this->expectFailsRule( + new ScalarLeafs(), + ' + fragment scalarSelectionsNotAllowedWithDirectives on Dog { + name @include(if: true) { isAlsoHumanName } + } + ', + [$this->noScalarSubselection('name', 'String', 3, 33)] + ); + } + + /** + * @see it('Scalar selection not allowed with directives and args') + */ + public function testScalarSelectionNotAllowedWithDirectivesAndArgs() : void + { + $this->expectFailsRule( + new ScalarLeafs(), + ' + fragment scalarSelectionsNotAllowedWithDirectivesAndArgs on Dog { + doesKnowCommand(dogCommand: SIT) @include(if: true) { sinceWhen } + } + ', + [$this->noScalarSubselection('doesKnowCommand', 'Boolean', 3, 61)] ); } } diff --git a/tests/Validator/UniqueArgumentNamesTest.php b/tests/Validator/UniqueArgumentNamesTest.php index 4652aa1..44b0ac9 100644 --- a/tests/Validator/UniqueArgumentNamesTest.php +++ b/tests/Validator/UniqueArgumentNamesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new UniqueArgumentNames(), ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { field } - '); + ' + ); } /** @@ -26,11 +31,14 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testNoArgumentsOnDirective() : void { - $this->expectPassesRule(new UniqueArgumentNames, ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { field @directive } - '); + ' + ); } /** @@ -38,11 +46,14 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testArgumentOnField() : void { - $this->expectPassesRule(new UniqueArgumentNames, ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { field(arg: "value") } - '); + ' + ); } /** @@ -50,11 +61,14 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testArgumentOnDirective() : void { - $this->expectPassesRule(new UniqueArgumentNames, ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { field @directive(arg: "value") } - '); + ' + ); } /** @@ -62,12 +76,15 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testSameArgumentOnTwoFields() : void { - $this->expectPassesRule(new UniqueArgumentNames, ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { one: field(arg: "value") two: field(arg: "value") } - '); + ' + ); } /** @@ -75,11 +92,14 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testSameArgumentOnFieldAndDirective() : void { - $this->expectPassesRule(new UniqueArgumentNames, ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { field(arg: "value") @directive(arg: "value") } - '); + ' + ); } /** @@ -87,11 +107,14 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testSameArgumentOnTwoDirectives() : void { - $this->expectPassesRule(new UniqueArgumentNames, ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { field @directive1(arg: "value") @directive2(arg: "value") } - '); + ' + ); } /** @@ -99,11 +122,14 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testMultipleFieldArguments() : void { - $this->expectPassesRule(new UniqueArgumentNames, ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { field(arg1: "value", arg2: "value", arg3: "value") } - '); + ' + ); } /** @@ -111,11 +137,14 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testMultipleDirectiveArguments() : void { - $this->expectPassesRule(new UniqueArgumentNames, ' + $this->expectPassesRule( + new UniqueArgumentNames(), + ' { field @directive(arg1: "value", arg2: "value", arg3: "value") } - '); + ' + ); } /** @@ -123,57 +152,15 @@ class UniqueArgumentNamesTest extends ValidatorTestCase */ public function testDuplicateFieldArguments() : void { - $this->expectFailsRule(new UniqueArgumentNames, ' + $this->expectFailsRule( + new UniqueArgumentNames(), + ' { field(arg1: "value", arg1: "value") } - ', [ - $this->duplicateArg('arg1', 3, 15, 3, 30) - ]); - } - - /** - * @see it('many duplicate field arguments') - */ - public function testManyDuplicateFieldArguments() : void - { - $this->expectFailsRule(new UniqueArgumentNames, ' - { - field(arg1: "value", arg1: "value", arg1: "value") - } - ', [ - $this->duplicateArg('arg1', 3, 15, 3, 30), - $this->duplicateArg('arg1', 3, 15, 3, 45) - ]); - } - - /** - * @see it('duplicate directive arguments') - */ - public function testDuplicateDirectiveArguments() : void - { - $this->expectFailsRule(new UniqueArgumentNames, ' - { - field @directive(arg1: "value", arg1: "value") - } - ', [ - $this->duplicateArg('arg1', 3, 26, 3, 41) - ]); - } - - /** - * @see it('many duplicate directive arguments') - */ - public function testManyDuplicateDirectiveArguments() : void - { - $this->expectFailsRule(new UniqueArgumentNames, ' - { - field @directive(arg1: "value", arg1: "value", arg1: "value") - } - ', [ - $this->duplicateArg('arg1', 3, 26, 3, 41), - $this->duplicateArg('arg1', 3, 26, 3, 56) - ]); + ', + [$this->duplicateArg('arg1', 3, 15, 3, 30)] + ); } private function duplicateArg($argName, $l1, $c1, $l2, $c2) @@ -183,4 +170,58 @@ class UniqueArgumentNamesTest extends ValidatorTestCase [new SourceLocation($l1, $c1), new SourceLocation($l2, $c2)] ); } + + /** + * @see it('many duplicate field arguments') + */ + public function testManyDuplicateFieldArguments() : void + { + $this->expectFailsRule( + new UniqueArgumentNames(), + ' + { + field(arg1: "value", arg1: "value", arg1: "value") + } + ', + [ + $this->duplicateArg('arg1', 3, 15, 3, 30), + $this->duplicateArg('arg1', 3, 15, 3, 45), + ] + ); + } + + /** + * @see it('duplicate directive arguments') + */ + public function testDuplicateDirectiveArguments() : void + { + $this->expectFailsRule( + new UniqueArgumentNames(), + ' + { + field @directive(arg1: "value", arg1: "value") + } + ', + [$this->duplicateArg('arg1', 3, 26, 3, 41)] + ); + } + + /** + * @see it('many duplicate directive arguments') + */ + public function testManyDuplicateDirectiveArguments() : void + { + $this->expectFailsRule( + new UniqueArgumentNames(), + ' + { + field @directive(arg1: "value", arg1: "value", arg1: "value") + } + ', + [ + $this->duplicateArg('arg1', 3, 26, 3, 41), + $this->duplicateArg('arg1', 3, 26, 3, 56), + ] + ); + } } diff --git a/tests/Validator/UniqueDirectivesPerLocationTest.php b/tests/Validator/UniqueDirectivesPerLocationTest.php index 199f106..e185220 100644 --- a/tests/Validator/UniqueDirectivesPerLocationTest.php +++ b/tests/Validator/UniqueDirectivesPerLocationTest.php @@ -1,4 +1,7 @@ expectPassesRule(new UniqueDirectivesPerLocation(), ' + $this->expectPassesRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type { field } - '); + ' + ); } /** @@ -22,11 +28,14 @@ class UniqueDirectivesPerLocationTest extends ValidatorTestCase */ public function testUniqueDirectivesInDifferentLocations() : void { - $this->expectPassesRule(new UniqueDirectivesPerLocation(), ' + $this->expectPassesRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type @directiveA { field @directiveB } - '); + ' + ); } /** @@ -34,11 +43,14 @@ class UniqueDirectivesPerLocationTest extends ValidatorTestCase */ public function testUniqueDirectivesInSameLocations() : void { - $this->expectPassesRule(new UniqueDirectivesPerLocation(), ' + $this->expectPassesRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type @directiveA @directiveB { field @directiveA @directiveB } - '); + ' + ); } /** @@ -46,11 +58,14 @@ class UniqueDirectivesPerLocationTest extends ValidatorTestCase */ public function testSameDirectivesInDifferentLocations() : void { - $this->expectPassesRule(new UniqueDirectivesPerLocation(), ' + $this->expectPassesRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type @directiveA { field @directiveA } - '); + ' + ); } /** @@ -58,12 +73,15 @@ class UniqueDirectivesPerLocationTest extends ValidatorTestCase */ public function testSameDirectivesInSimilarLocations() : void { - $this->expectPassesRule(new UniqueDirectivesPerLocation(), ' + $this->expectPassesRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type { field @directive field @directive } - '); + ' + ); } /** @@ -71,13 +89,26 @@ class UniqueDirectivesPerLocationTest extends ValidatorTestCase */ public function testDuplicateDirectivesInOneLocation() : void { - $this->expectFailsRule(new UniqueDirectivesPerLocation(), ' + $this->expectFailsRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type { field @directive @directive } - ', [ - $this->duplicateDirective('directive', 3, 15, 3, 26) - ]); + ', + [$this->duplicateDirective('directive', 3, 15, 3, 26)] + ); + } + + private function duplicateDirective($directiveName, $l1, $c1, $l2, $c2) + { + return [ + 'message' => UniqueDirectivesPerLocation::duplicateDirectiveMessage($directiveName), + 'locations' => [ + ['line' => $l1, 'column' => $c1], + ['line' => $l2, 'column' => $c2], + ], + ]; } /** @@ -85,14 +116,18 @@ class UniqueDirectivesPerLocationTest extends ValidatorTestCase */ public function testManyDuplicateDirectivesInOneLocation() : void { - $this->expectFailsRule(new UniqueDirectivesPerLocation(), ' + $this->expectFailsRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type { field @directive @directive @directive } - ', [ - $this->duplicateDirective('directive', 3, 15, 3, 26), - $this->duplicateDirective('directive', 3, 15, 3, 37) - ]); + ', + [ + $this->duplicateDirective('directive', 3, 15, 3, 26), + $this->duplicateDirective('directive', 3, 15, 3, 37), + ] + ); } /** @@ -100,14 +135,18 @@ class UniqueDirectivesPerLocationTest extends ValidatorTestCase */ public function testDifferentDuplicateDirectivesInOneLocation() : void { - $this->expectFailsRule(new UniqueDirectivesPerLocation, ' + $this->expectFailsRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type { field @directiveA @directiveB @directiveA @directiveB } - ', [ - $this->duplicateDirective('directiveA', 3, 15, 3, 39), - $this->duplicateDirective('directiveB', 3, 27, 3, 51) - ]); + ', + [ + $this->duplicateDirective('directiveA', 3, 15, 3, 39), + $this->duplicateDirective('directiveB', 3, 27, 3, 51), + ] + ); } /** @@ -115,24 +154,17 @@ class UniqueDirectivesPerLocationTest extends ValidatorTestCase */ public function testDuplicateDirectivesInManyLocations() : void { - $this->expectFailsRule(new UniqueDirectivesPerLocation(), ' + $this->expectFailsRule( + new UniqueDirectivesPerLocation(), + ' fragment Test on Type @directive @directive { field @directive @directive } - ', [ - $this->duplicateDirective('directive', 2, 29, 2, 40), - $this->duplicateDirective('directive', 3, 15, 3, 26) - ]); - } - - private function duplicateDirective($directiveName, $l1, $c1, $l2, $c2) - { - return [ - 'message' =>UniqueDirectivesPerLocation::duplicateDirectiveMessage($directiveName), - 'locations' => [ - ['line' => $l1, 'column' => $c1], - ['line' => $l2, 'column' => $c2] + ', + [ + $this->duplicateDirective('directive', 2, 29, 2, 40), + $this->duplicateDirective('directive', 3, 15, 3, 26), ] - ]; + ); } } diff --git a/tests/Validator/UniqueFragmentNamesTest.php b/tests/Validator/UniqueFragmentNamesTest.php index 426e803..b34b76f 100644 --- a/tests/Validator/UniqueFragmentNamesTest.php +++ b/tests/Validator/UniqueFragmentNamesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new UniqueFragmentNames(), ' + $this->expectPassesRule( + new UniqueFragmentNames(), + ' { field } - '); + ' + ); } /** @@ -26,7 +31,9 @@ class UniqueFragmentNamesTest extends ValidatorTestCase */ public function testOneFragment() : void { - $this->expectPassesRule(new UniqueFragmentNames, ' + $this->expectPassesRule( + new UniqueFragmentNames(), + ' { ...fragA } @@ -34,7 +41,8 @@ class UniqueFragmentNamesTest extends ValidatorTestCase fragment fragA on Type { field } - '); + ' + ); } /** @@ -42,7 +50,9 @@ class UniqueFragmentNamesTest extends ValidatorTestCase */ public function testManyFragments() : void { - $this->expectPassesRule(new UniqueFragmentNames, ' + $this->expectPassesRule( + new UniqueFragmentNames(), + ' { ...fragA ...fragB @@ -57,7 +67,8 @@ class UniqueFragmentNamesTest extends ValidatorTestCase fragment fragC on Type { fieldC } - '); + ' + ); } /** @@ -65,7 +76,9 @@ class UniqueFragmentNamesTest extends ValidatorTestCase */ public function testInlineFragmentsAreAlwaysUnique() : void { - $this->expectPassesRule(new UniqueFragmentNames, ' + $this->expectPassesRule( + new UniqueFragmentNames(), + ' { ...on Type { fieldA @@ -74,7 +87,8 @@ class UniqueFragmentNamesTest extends ValidatorTestCase fieldB } } - '); + ' + ); } /** @@ -82,14 +96,17 @@ class UniqueFragmentNamesTest extends ValidatorTestCase */ public function testFragmentAndOperationNamedTheSame() : void { - $this->expectPassesRule(new UniqueFragmentNames, ' + $this->expectPassesRule( + new UniqueFragmentNames(), + ' query Foo { ...Foo } fragment Foo on Type { field } - '); + ' + ); } /** @@ -97,7 +114,9 @@ class UniqueFragmentNamesTest extends ValidatorTestCase */ public function testFragmentsNamedTheSame() : void { - $this->expectFailsRule(new UniqueFragmentNames, ' + $this->expectFailsRule( + new UniqueFragmentNames(), + ' { ...fragA } @@ -107,26 +126,9 @@ class UniqueFragmentNamesTest extends ValidatorTestCase fragment fragA on Type { fieldB } - ', [ - $this->duplicateFrag('fragA', 5, 16, 8, 16) - ]); - } - - /** - * @see it('fragments named the same without being referenced') - */ - public function testFragmentsNamedTheSameWithoutBeingReferenced() : void - { - $this->expectFailsRule(new UniqueFragmentNames, ' - fragment fragA on Type { - fieldA - } - fragment fragA on Type { - fieldB - } - ', [ - $this->duplicateFrag('fragA', 2, 16, 5, 16) - ]); + ', + [$this->duplicateFrag('fragA', 5, 16, 8, 16)] + ); } private function duplicateFrag($fragName, $l1, $c1, $l2, $c2) @@ -136,4 +138,23 @@ class UniqueFragmentNamesTest extends ValidatorTestCase [new SourceLocation($l1, $c1), new SourceLocation($l2, $c2)] ); } + + /** + * @see it('fragments named the same without being referenced') + */ + public function testFragmentsNamedTheSameWithoutBeingReferenced() : void + { + $this->expectFailsRule( + new UniqueFragmentNames(), + ' + fragment fragA on Type { + fieldA + } + fragment fragA on Type { + fieldB + } + ', + [$this->duplicateFrag('fragA', 2, 16, 5, 16)] + ); + } } diff --git a/tests/Validator/UniqueInputFieldNamesTest.php b/tests/Validator/UniqueInputFieldNamesTest.php index 676dea3..81c35a0 100644 --- a/tests/Validator/UniqueInputFieldNamesTest.php +++ b/tests/Validator/UniqueInputFieldNamesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new UniqueInputFieldNames(), ' + $this->expectPassesRule( + new UniqueInputFieldNames(), + ' { field(arg: { f: true }) } - '); + ' + ); } /** @@ -26,11 +31,14 @@ class UniqueInputFieldNamesTest extends ValidatorTestCase */ public function testSameInputObjectWithinTwoArgs() : void { - $this->expectPassesRule(new UniqueInputFieldNames, ' + $this->expectPassesRule( + new UniqueInputFieldNames(), + ' { field(arg1: { f: true }, arg2: { f: true }) } - '); + ' + ); } /** @@ -38,11 +46,14 @@ class UniqueInputFieldNamesTest extends ValidatorTestCase */ public function testMultipleInputObjectFields() : void { - $this->expectPassesRule(new UniqueInputFieldNames, ' + $this->expectPassesRule( + new UniqueInputFieldNames(), + ' { field(arg: { f1: "value", f2: "value", f3: "value" }) } - '); + ' + ); } /** @@ -50,7 +61,9 @@ class UniqueInputFieldNamesTest extends ValidatorTestCase */ public function testAllowsForNestedInputObjectsWithSimilarFields() : void { - $this->expectPassesRule(new UniqueInputFieldNames, ' + $this->expectPassesRule( + new UniqueInputFieldNames(), + ' { field(arg: { deep: { @@ -62,7 +75,8 @@ class UniqueInputFieldNamesTest extends ValidatorTestCase id: 1 }) } - '); + ' + ); } /** @@ -70,28 +84,15 @@ class UniqueInputFieldNamesTest extends ValidatorTestCase */ public function testDuplicateInputObjectFields() : void { - $this->expectFailsRule(new UniqueInputFieldNames, ' + $this->expectFailsRule( + new UniqueInputFieldNames(), + ' { field(arg: { f1: "value", f1: "value" }) } - ', [ - $this->duplicateField('f1', 3, 22, 3, 35) - ]); - } - - /** - * @see it('many duplicate input object fields') - */ - public function testManyDuplicateInputObjectFields() : void - { - $this->expectFailsRule(new UniqueInputFieldNames, ' - { - field(arg: { f1: "value", f1: "value", f1: "value" }) - } - ', [ - $this->duplicateField('f1', 3, 22, 3, 35), - $this->duplicateField('f1', 3, 22, 3, 48) - ]); + ', + [$this->duplicateField('f1', 3, 22, 3, 35)] + ); } private function duplicateField($name, $l1, $c1, $l2, $c2) @@ -101,4 +102,23 @@ class UniqueInputFieldNamesTest extends ValidatorTestCase [new SourceLocation($l1, $c1), new SourceLocation($l2, $c2)] ); } + + /** + * @see it('many duplicate input object fields') + */ + public function testManyDuplicateInputObjectFields() : void + { + $this->expectFailsRule( + new UniqueInputFieldNames(), + ' + { + field(arg: { f1: "value", f1: "value", f1: "value" }) + } + ', + [ + $this->duplicateField('f1', 3, 22, 3, 35), + $this->duplicateField('f1', 3, 22, 3, 48), + ] + ); + } } diff --git a/tests/Validator/UniqueOperationNamesTest.php b/tests/Validator/UniqueOperationNamesTest.php index 11cecdc..3bc90b5 100644 --- a/tests/Validator/UniqueOperationNamesTest.php +++ b/tests/Validator/UniqueOperationNamesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new UniqueOperationNames(), ' + $this->expectPassesRule( + new UniqueOperationNames(), + ' fragment fragA on Type { field } - '); + ' + ); } /** @@ -26,11 +31,14 @@ class UniqueOperationNamesTest extends ValidatorTestCase */ public function testOneAnonOperation() : void { - $this->expectPassesRule(new UniqueOperationNames, ' + $this->expectPassesRule( + new UniqueOperationNames(), + ' { field } - '); + ' + ); } /** @@ -38,11 +46,14 @@ class UniqueOperationNamesTest extends ValidatorTestCase */ public function testOneNamedOperation() : void { - $this->expectPassesRule(new UniqueOperationNames, ' + $this->expectPassesRule( + new UniqueOperationNames(), + ' query Foo { field } - '); + ' + ); } /** @@ -50,7 +61,9 @@ class UniqueOperationNamesTest extends ValidatorTestCase */ public function testMultipleOperations() : void { - $this->expectPassesRule(new UniqueOperationNames, ' + $this->expectPassesRule( + new UniqueOperationNames(), + ' query Foo { field } @@ -58,7 +71,8 @@ class UniqueOperationNamesTest extends ValidatorTestCase query Bar { field } - '); + ' + ); } /** @@ -66,7 +80,9 @@ class UniqueOperationNamesTest extends ValidatorTestCase */ public function testMultipleOperationsOfDifferentTypes() : void { - $this->expectPassesRule(new UniqueOperationNames, ' + $this->expectPassesRule( + new UniqueOperationNames(), + ' query Foo { field } @@ -78,7 +94,8 @@ class UniqueOperationNamesTest extends ValidatorTestCase subscription Baz { field } - '); + ' + ); } /** @@ -86,14 +103,17 @@ class UniqueOperationNamesTest extends ValidatorTestCase */ public function testFragmentAndOperationNamedTheSame() : void { - $this->expectPassesRule(new UniqueOperationNames, ' + $this->expectPassesRule( + new UniqueOperationNames(), + ' query Foo { ...Foo } fragment Foo on Type { field } - '); + ' + ); } /** @@ -101,50 +121,18 @@ class UniqueOperationNamesTest extends ValidatorTestCase */ public function testMultipleOperationsOfSameName() : void { - $this->expectFailsRule(new UniqueOperationNames, ' + $this->expectFailsRule( + new UniqueOperationNames(), + ' query Foo { fieldA } query Foo { fieldB } - ', [ - $this->duplicateOp('Foo', 2, 13, 5, 13) - ]); - } - - /** - * @see it('multiple ops of same name of different types (mutation)') - */ - public function testMultipleOpsOfSameNameOfDifferentTypesMutation() : void - { - $this->expectFailsRule(new UniqueOperationNames, ' - query Foo { - fieldA - } - mutation Foo { - fieldB - } - ', [ - $this->duplicateOp('Foo', 2, 13, 5, 16) - ]); - } - - /** - * @see it('multiple ops of same name of different types (subscription)') - */ - public function testMultipleOpsOfSameNameOfDifferentTypesSubscription() : void - { - $this->expectFailsRule(new UniqueOperationNames, ' - query Foo { - fieldA - } - subscription Foo { - fieldB - } - ', [ - $this->duplicateOp('Foo', 2, 13, 5, 20) - ]); + ', + [$this->duplicateOp('Foo', 2, 13, 5, 13)] + ); } private function duplicateOp($opName, $l1, $c1, $l2, $c2) @@ -154,4 +142,42 @@ class UniqueOperationNamesTest extends ValidatorTestCase [new SourceLocation($l1, $c1), new SourceLocation($l2, $c2)] ); } + + /** + * @see it('multiple ops of same name of different types (mutation)') + */ + public function testMultipleOpsOfSameNameOfDifferentTypesMutation() : void + { + $this->expectFailsRule( + new UniqueOperationNames(), + ' + query Foo { + fieldA + } + mutation Foo { + fieldB + } + ', + [$this->duplicateOp('Foo', 2, 13, 5, 16)] + ); + } + + /** + * @see it('multiple ops of same name of different types (subscription)') + */ + public function testMultipleOpsOfSameNameOfDifferentTypesSubscription() : void + { + $this->expectFailsRule( + new UniqueOperationNames(), + ' + query Foo { + fieldA + } + subscription Foo { + fieldB + } + ', + [$this->duplicateOp('Foo', 2, 13, 5, 20)] + ); + } } diff --git a/tests/Validator/UniqueVariableNamesTest.php b/tests/Validator/UniqueVariableNamesTest.php index a5776a4..f587efb 100644 --- a/tests/Validator/UniqueVariableNamesTest.php +++ b/tests/Validator/UniqueVariableNamesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new UniqueVariableNames(), ' + $this->expectPassesRule( + new UniqueVariableNames(), + ' query A($x: Int, $y: String) { __typename } query B($x: String, $y: Int) { __typename } - '); + ' + ); } /** @@ -25,16 +30,20 @@ class UniqueVariableNamesTest extends ValidatorTestCase */ public function testDuplicateVariableNames() : void { - $this->expectFailsRule(new UniqueVariableNames, ' + $this->expectFailsRule( + new UniqueVariableNames(), + ' query A($x: Int, $x: Int, $x: String) { __typename } query B($x: String, $x: Int) { __typename } query C($x: Int, $x: Int) { __typename } - ', [ - $this->duplicateVariable('x', 2, 16, 2, 25), - $this->duplicateVariable('x', 2, 16, 2, 34), - $this->duplicateVariable('x', 3, 16, 3, 28), - $this->duplicateVariable('x', 4, 16, 4, 25) - ]); + ', + [ + $this->duplicateVariable('x', 2, 16, 2, 25), + $this->duplicateVariable('x', 2, 16, 2, 34), + $this->duplicateVariable('x', 3, 16, 3, 28), + $this->duplicateVariable('x', 4, 16, 4, 25), + ] + ); } private function duplicateVariable($name, $l1, $c1, $l2, $c2) diff --git a/tests/Validator/ValidationTest.php b/tests/Validator/ValidationTest.php index 7bfb4b2..4b5534f 100644 --- a/tests/Validator/ValidationTest.php +++ b/tests/Validator/ValidationTest.php @@ -1,10 +1,12 @@ "Expected type Invalid, found \"bad value\"; Invalid scalar is always invalid: bad value", - 'locations' => [ ['line' => 3, 'column' => 25] ] + 'message' => 'Expected type Invalid, found "bad value"; Invalid scalar is always invalid: bad value', + 'locations' => [['line' => 3, 'column' => 25]], ]; $this->expectInvalid( @@ -53,8 +55,8 @@ class ValidationTest extends ValidatorTestCase $query = '{invalid}'; $expectedError = [ - 'message' => 'Cannot query field "invalid" on type "QueryRoot". Did you mean "invalidArg"?', - 'locations' => [ ['line' => 1, 'column' => 2] ] + 'message' => 'Cannot query field "invalid" on type "QueryRoot". Did you mean "invalidArg"?', + 'locations' => [['line' => 1, 'column' => 2]], ]; $this->expectFailsCompleteValidation($query, [$expectedError]); $this->expectValid($this->getTestSchema(), [], $query); diff --git a/tests/Validator/ValidatorTestCase.php b/tests/Validator/ValidatorTestCase.php index e0b9d20..22aa1e1 100644 --- a/tests/Validator/ValidatorTestCase.php +++ b/tests/Validator/ValidatorTestCase.php @@ -1,8 +1,10 @@ expectValid(self::getTestSchema(), [$rule], $queryString); + } + + protected function expectValid($schema, $rules, $queryString) : void + { + $this->assertEquals( + [], + DocumentValidator::validate($schema, Parser::parse($queryString), $rules), + 'Should validate' + ); + } + /** * @return Schema */ @@ -24,198 +42,198 @@ abstract class ValidatorTestCase extends TestCase $FurColor = null; $Being = new InterfaceType([ - 'name' => 'Being', + 'name' => 'Being', 'fields' => [ 'name' => [ 'type' => Type::string(), - 'args' => ['surname' => ['type' => Type::boolean()]] - ] + 'args' => ['surname' => ['type' => Type::boolean()]], + ], ], ]); $Pet = new InterfaceType([ - 'name' => 'Pet', + 'name' => 'Pet', 'fields' => [ 'name' => [ 'type' => Type::string(), - 'args' => ['surname' => ['type' => Type::boolean()]] - ] + 'args' => ['surname' => ['type' => Type::boolean()]], + ], ], ]); $Canine = new InterfaceType([ - 'name' => 'Canine', + 'name' => 'Canine', 'fields' => function () { return [ 'name' => [ 'type' => Type::string(), - 'args' => ['surname' => ['type' => Type::boolean()]] - ] + 'args' => ['surname' => ['type' => Type::boolean()]], + ], ]; - } + }, ]); $DogCommand = new EnumType([ - 'name' => 'DogCommand', + 'name' => 'DogCommand', 'values' => [ - 'SIT' => ['value' => 0], + 'SIT' => ['value' => 0], 'HEEL' => ['value' => 1], - 'DOWN' => ['value' => 2] - ] + 'DOWN' => ['value' => 2], + ], ]); $Dog = new ObjectType([ - 'name' => 'Dog', - 'fields' => [ - 'name' => [ + 'name' => 'Dog', + 'fields' => [ + 'name' => [ 'type' => Type::string(), - 'args' => ['surname' => ['type' => Type::boolean()]] + 'args' => ['surname' => ['type' => Type::boolean()]], ], - 'nickname' => ['type' => Type::string()], - 'barkVolume' => ['type' => Type::int()], - 'barks' => ['type' => Type::boolean()], + 'nickname' => ['type' => Type::string()], + 'barkVolume' => ['type' => Type::int()], + 'barks' => ['type' => Type::boolean()], 'doesKnowCommand' => [ 'type' => Type::boolean(), - 'args' => ['dogCommand' => ['type' => $DogCommand]] + 'args' => ['dogCommand' => ['type' => $DogCommand]], ], - 'isHousetrained' => [ + 'isHousetrained' => [ 'type' => Type::boolean(), - 'args' => ['atOtherHomes' => ['type' => Type::boolean(), 'defaultValue' => true]] + 'args' => ['atOtherHomes' => ['type' => Type::boolean(), 'defaultValue' => true]], ], - 'isAtLocation' => [ + 'isAtLocation' => [ 'type' => Type::boolean(), - 'args' => ['x' => ['type' => Type::int()], 'y' => ['type' => Type::int()]] - ] + 'args' => ['x' => ['type' => Type::int()], 'y' => ['type' => Type::int()]], + ], ], - 'interfaces' => [$Being, $Pet, $Canine] + 'interfaces' => [$Being, $Pet, $Canine], ]); $Cat = new ObjectType([ - 'name' => 'Cat', - 'fields' => function () use (&$FurColor) { + 'name' => 'Cat', + 'fields' => function () use (&$FurColor) { return [ - 'name' => [ + 'name' => [ 'type' => Type::string(), - 'args' => ['surname' => ['type' => Type::boolean()]] + 'args' => ['surname' => ['type' => Type::boolean()]], ], - 'nickname' => ['type' => Type::string()], - 'meows' => ['type' => Type::boolean()], + 'nickname' => ['type' => Type::string()], + 'meows' => ['type' => Type::boolean()], 'meowVolume' => ['type' => Type::int()], - 'furColor' => $FurColor + 'furColor' => $FurColor, ]; }, - 'interfaces' => [$Being, $Pet] + 'interfaces' => [$Being, $Pet], ]); $CatOrDog = new UnionType([ - 'name' => 'CatOrDog', + 'name' => 'CatOrDog', 'types' => [$Dog, $Cat], ]); $Intelligent = new InterfaceType([ - 'name' => 'Intelligent', + 'name' => 'Intelligent', 'fields' => [ - 'iq' => ['type' => Type::int()] - ] + 'iq' => ['type' => Type::int()], + ], ]); $Human = null; $Human = new ObjectType([ - 'name' => 'Human', + 'name' => 'Human', 'interfaces' => [$Being, $Intelligent], - 'fields' => function () use (&$Human, $Pet) { + 'fields' => function () use (&$Human, $Pet) { return [ - 'name' => [ + 'name' => [ 'type' => Type::string(), - 'args' => ['surname' => ['type' => Type::boolean()]] + 'args' => ['surname' => ['type' => Type::boolean()]], ], - 'pets' => ['type' => Type::listOf($Pet)], + 'pets' => ['type' => Type::listOf($Pet)], 'relatives' => ['type' => Type::listOf($Human)], - 'iq' => ['type' => Type::int()] + 'iq' => ['type' => Type::int()], ]; - } + }, ]); $Alien = new ObjectType([ - 'name' => 'Alien', + 'name' => 'Alien', 'interfaces' => [$Being, $Intelligent], - 'fields' => [ - 'iq' => ['type' => Type::int()], - 'name' => [ + 'fields' => [ + 'iq' => ['type' => Type::int()], + 'name' => [ 'type' => Type::string(), - 'args' => ['surname' => ['type' => Type::boolean()]] + 'args' => ['surname' => ['type' => Type::boolean()]], ], - 'numEyes' => ['type' => Type::int()] - ] + 'numEyes' => ['type' => Type::int()], + ], ]); $DogOrHuman = new UnionType([ - 'name' => 'DogOrHuman', + 'name' => 'DogOrHuman', 'types' => [$Dog, $Human], ]); $HumanOrAlien = new UnionType([ - 'name' => 'HumanOrAlien', + 'name' => 'HumanOrAlien', 'types' => [$Human, $Alien], ]); $FurColor = new EnumType([ - 'name' => 'FurColor', + 'name' => 'FurColor', 'values' => [ - 'BROWN' => ['value' => 0], - 'BLACK' => ['value' => 1], - 'TAN' => ['value' => 2], + 'BROWN' => ['value' => 0], + 'BLACK' => ['value' => 1], + 'TAN' => ['value' => 2], 'SPOTTED' => ['value' => 3], - 'NO_FUR' => ['value' => null], + 'NO_FUR' => ['value' => null], ], ]); $ComplexInput = new InputObjectType([ - 'name' => 'ComplexInput', + 'name' => 'ComplexInput', 'fields' => [ - 'requiredField' => ['type' => Type::nonNull(Type::boolean())], - 'intField' => ['type' => Type::int()], - 'stringField' => ['type' => Type::string()], - 'booleanField' => ['type' => Type::boolean()], - 'stringListField' => ['type' => Type::listOf(Type::string())] - ] + 'requiredField' => ['type' => Type::nonNull(Type::boolean())], + 'intField' => ['type' => Type::int()], + 'stringField' => ['type' => Type::string()], + 'booleanField' => ['type' => Type::boolean()], + 'stringListField' => ['type' => Type::listOf(Type::string())], + ], ]); $ComplicatedArgs = new ObjectType([ - 'name' => 'ComplicatedArgs', + 'name' => 'ComplicatedArgs', // TODO List // TODO Coercion // TODO NotNulls 'fields' => [ - 'intArgField' => [ + 'intArgField' => [ 'type' => Type::string(), 'args' => ['intArg' => ['type' => Type::int()]], ], - 'nonNullIntArgField' => [ + 'nonNullIntArgField' => [ 'type' => Type::string(), 'args' => ['nonNullIntArg' => ['type' => Type::nonNull(Type::int())]], ], - 'stringArgField' => [ + 'stringArgField' => [ 'type' => Type::string(), 'args' => ['stringArg' => ['type' => Type::string()]], ], - 'booleanArgField' => [ + 'booleanArgField' => [ 'type' => Type::string(), 'args' => ['booleanArg' => ['type' => Type::boolean()]], ], - 'enumArgField' => [ + 'enumArgField' => [ 'type' => Type::string(), 'args' => ['enumArg' => ['type' => $FurColor]], ], - 'floatArgField' => [ + 'floatArgField' => [ 'type' => Type::string(), 'args' => ['floatArg' => ['type' => Type::float()]], ], - 'idArgField' => [ + 'idArgField' => [ 'type' => Type::string(), 'args' => ['idArg' => ['type' => Type::id()]], ], - 'stringListArgField' => [ + 'stringListArgField' => [ 'type' => Type::string(), 'args' => ['stringListArg' => ['type' => Type::listOf(Type::string())]], ], @@ -227,194 +245,191 @@ abstract class ValidatorTestCase extends TestCase ], ], ], - 'complexArgField' => [ + 'complexArgField' => [ 'type' => Type::string(), 'args' => ['complexArg' => ['type' => $ComplexInput]], ], - 'multipleReqs' => [ + 'multipleReqs' => [ 'type' => Type::string(), 'args' => [ 'req1' => ['type' => Type::nonNull(Type::int())], 'req2' => ['type' => Type::nonNull(Type::int())], ], ], - 'multipleOpts' => [ + 'multipleOpts' => [ 'type' => Type::string(), 'args' => [ 'opt1' => [ - 'type' => Type::int(), + 'type' => Type::int(), 'defaultValue' => 0, ], 'opt2' => [ - 'type' => Type::int(), + 'type' => Type::int(), 'defaultValue' => 0, ], ], ], - 'multipleOptAndReq' => [ + 'multipleOptAndReq' => [ 'type' => Type::string(), 'args' => [ 'req1' => ['type' => Type::nonNull(Type::int())], 'req2' => ['type' => Type::nonNull(Type::int())], 'opt1' => [ - 'type' => Type::int(), + 'type' => Type::int(), 'defaultValue' => 0, ], 'opt2' => [ - 'type' => Type::int(), + 'type' => Type::int(), 'defaultValue' => 0, ], ], ], - ] + ], ]); $invalidScalar = new CustomScalarType([ - 'name' => 'Invalid', - 'serialize' => function ($value) { + 'name' => 'Invalid', + 'serialize' => function ($value) { return $value; }, 'parseLiteral' => function ($node) { throw new \Exception('Invalid scalar is always invalid: ' . $node->value); }, - 'parseValue' => function ($node) { + 'parseValue' => function ($node) { throw new \Exception('Invalid scalar is always invalid: ' . $node); }, ]); $anyScalar = new CustomScalarType([ - 'name' => 'Any', - 'serialize' => function ($value) { + 'name' => 'Any', + 'serialize' => function ($value) { return $value; }, 'parseLiteral' => function ($node) { return $node; }, // Allows any value - 'parseValue' => function ($value) { + 'parseValue' => function ($value) { return $value; }, // Allows any value ]); $queryRoot = new ObjectType([ - 'name' => 'QueryRoot', + 'name' => 'QueryRoot', 'fields' => [ - 'human' => [ + 'human' => [ 'args' => ['id' => ['type' => Type::id()]], - 'type' => $Human + 'type' => $Human, ], - 'alien' => ['type' => $Alien], - 'dog' => ['type' => $Dog], - 'cat' => ['type' => $Cat], - 'pet' => ['type' => $Pet], - 'catOrDog' => ['type' => $CatOrDog], - 'dogOrHuman' => ['type' => $DogOrHuman], - 'humanOrAlien' => ['type' => $HumanOrAlien], + 'alien' => ['type' => $Alien], + 'dog' => ['type' => $Dog], + 'cat' => ['type' => $Cat], + 'pet' => ['type' => $Pet], + 'catOrDog' => ['type' => $CatOrDog], + 'dogOrHuman' => ['type' => $DogOrHuman], + 'humanOrAlien' => ['type' => $HumanOrAlien], 'complicatedArgs' => ['type' => $ComplicatedArgs], - 'invalidArg' => [ + 'invalidArg' => [ 'args' => [ - 'arg' => ['type' => $invalidScalar] + 'arg' => ['type' => $invalidScalar], ], 'type' => Type::string(), ], - 'anyArg' => [ + 'anyArg' => [ 'args' => ['arg' => ['type' => $anyScalar]], 'type' => Type::string(), ], - ] + ], ]); $testSchema = new Schema([ - 'query' => $queryRoot, + 'query' => $queryRoot, 'directives' => [ Directive::includeDirective(), Directive::skipDirective(), new Directive([ - 'name' => 'onQuery', + 'name' => 'onQuery', 'locations' => ['QUERY'], ]), new Directive([ - 'name' => 'onMutation', + 'name' => 'onMutation', 'locations' => ['MUTATION'], ]), new Directive([ - 'name' => 'onSubscription', + 'name' => 'onSubscription', 'locations' => ['SUBSCRIPTION'], ]), new Directive([ - 'name' => 'onField', + 'name' => 'onField', 'locations' => ['FIELD'], ]), new Directive([ - 'name' => 'onFragmentDefinition', + 'name' => 'onFragmentDefinition', 'locations' => ['FRAGMENT_DEFINITION'], ]), new Directive([ - 'name' => 'onFragmentSpread', + 'name' => 'onFragmentSpread', 'locations' => ['FRAGMENT_SPREAD'], ]), new Directive([ - 'name' => 'onInlineFragment', + 'name' => 'onInlineFragment', 'locations' => ['INLINE_FRAGMENT'], ]), new Directive([ - 'name' => 'onSchema', + 'name' => 'onSchema', 'locations' => ['SCHEMA'], ]), new Directive([ - 'name' => 'onScalar', + 'name' => 'onScalar', 'locations' => ['SCALAR'], ]), new Directive([ - 'name' => 'onObject', + 'name' => 'onObject', 'locations' => ['OBJECT'], ]), new Directive([ - 'name' => 'onFieldDefinition', + 'name' => 'onFieldDefinition', 'locations' => ['FIELD_DEFINITION'], ]), new Directive([ - 'name' => 'onArgumentDefinition', + 'name' => 'onArgumentDefinition', 'locations' => ['ARGUMENT_DEFINITION'], ]), new Directive([ - 'name' => 'onInterface', + 'name' => 'onInterface', 'locations' => ['INTERFACE'], ]), new Directive([ - 'name' => 'onUnion', + 'name' => 'onUnion', 'locations' => ['UNION'], ]), new Directive([ - 'name' => 'onEnum', + 'name' => 'onEnum', 'locations' => ['ENUM'], ]), new Directive([ - 'name' => 'onEnumValue', + 'name' => 'onEnumValue', 'locations' => ['ENUM_VALUE'], ]), new Directive([ - 'name' => 'onInputObject', + 'name' => 'onInputObject', 'locations' => ['INPUT_OBJECT'], ]), new Directive([ - 'name' => 'onInputFieldDefinition', + 'name' => 'onInputFieldDefinition', 'locations' => ['INPUT_FIELD_DEFINITION'], ]), ], ]); + return $testSchema; } - function expectValid($schema, $rules, $queryString) + protected function expectFailsRule($rule, $queryString, $errors) { - $this->assertEquals( - [], - DocumentValidator::validate($schema, Parser::parse($queryString), $rules), - 'Should validate' - ); + return $this->expectInvalid(self::getTestSchema(), [$rule], $queryString, $errors); } - function expectInvalid($schema, $rules, $queryString, $expectedErrors) + protected function expectInvalid($schema, $rules, $queryString, $expectedErrors) { $errors = DocumentValidator::validate($schema, Parser::parse($queryString), $rules); @@ -424,33 +439,23 @@ abstract class ValidatorTestCase extends TestCase return $errors; } - function expectPassesRule($rule, $queryString) - { - $this->expectValid($this->getTestSchema(), [$rule], $queryString); - } - - function expectFailsRule($rule, $queryString, $errors) - { - return $this->expectInvalid($this->getTestSchema(), [$rule], $queryString, $errors); - } - - function expectPassesRuleWithSchema($schema, $rule, $queryString) + protected function expectPassesRuleWithSchema($schema, $rule, $queryString) : void { $this->expectValid($schema, [$rule], $queryString); } - function expectFailsRuleWithSchema($schema, $rule, $queryString, $errors) + protected function expectFailsRuleWithSchema($schema, $rule, $queryString, $errors) : void { $this->expectInvalid($schema, [$rule], $queryString, $errors); } - function expectPassesCompleteValidation($queryString) + protected function expectPassesCompleteValidation($queryString) : void { - $this->expectValid($this->getTestSchema(), DocumentValidator::allRules(), $queryString); + $this->expectValid(self::getTestSchema(), DocumentValidator::allRules(), $queryString); } - function expectFailsCompleteValidation($queryString, $errors) + protected function expectFailsCompleteValidation($queryString, $errors) : void { - $this->expectInvalid($this->getTestSchema(), DocumentValidator::allRules(), $queryString, $errors); + $this->expectInvalid(self::getTestSchema(), DocumentValidator::allRules(), $queryString, $errors); } } diff --git a/tests/Validator/ValuesOfCorrectTypeTest.php b/tests/Validator/ValuesOfCorrectTypeTest.php index 2d9f79b..f0e035e 100644 --- a/tests/Validator/ValuesOfCorrectTypeTest.php +++ b/tests/Validator/ValuesOfCorrectTypeTest.php @@ -1,4 +1,7 @@ expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + intArgField(intArg: 2) + } + } + ' + ); + } + + /** + * @see it('Good negative int value') + */ + public function testGoodNegativeIntValue() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + intArgField(intArg: -2) + } + } + ' + ); + } + + /** + * @see it('Good boolean value') + */ + public function testGoodBooleanValue() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + booleanArgField(booleanArg: true) + } + } + ' + ); + } + + // Validate: Values of correct type + // Valid values + + /** + * @see it('Good string value') + */ + public function testGoodStringValue() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + stringArgField(stringArg: "foo") + } + } + ' + ); + } + + /** + * @see it('Good float value') + */ + public function testGoodFloatValue() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + floatArgField(floatArg: 1.1) + } + } + ' + ); + } + + public function testGoodNegativeFloatValue() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + floatArgField(floatArg: -1.1) + } + } + ' + ); + } + + /** + * @see it('Int into Float') + */ + public function testIntIntoFloat() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + floatArgField(floatArg: 1) + } + } + ' + ); + } + + /** + * @see it('Int into ID') + */ + public function testIntIntoID() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + idArgField(idArg: 1) + } + } + ' + ); + } + + /** + * @see it('String into ID') + */ + public function testStringIntoID() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + idArgField(idArg: "someIdString") + } + } + ' + ); + } + + /** + * @see it('Good enum value') + */ + public function testGoodEnumValue() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + dog { + doesKnowCommand(dogCommand: SIT) + } + } + ' + ); + } + + /** + * @see it('Enum with null value') + */ + public function testEnumWithNullValue() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + enumArgField(enumArg: NO_FUR) + } + } + ' + ); + } + + /** + * @see it('null into nullable type') + */ + public function testNullIntoNullableType() : void + { + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + intArgField(intArg: null) + } + } + ' + ); + + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' + { + dog(a: null, b: null, c:{ requiredField: true, intField: null }) { + name + } + } + ' + ); + } + + /** + * @see it('Int into String') + */ + public function testIntIntoString() : void + { + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' + { + complicatedArgs { + stringArgField(stringArg: 1) + } + } + ', + [ + $this->badValue('String', '1', 4, 39), + ] + ); + } + private function badValue($typeName, $value, $line, $column, $message = null) { return FormattedError::create( @@ -19,252 +257,46 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase ); } - private function requiredField($typeName, $fieldName, $fieldTypeName, $line, $column) { - return FormattedError::create( - ValuesOfCorrectType::requiredFieldMessage( - $typeName, - $fieldName, - $fieldTypeName - ), - [new SourceLocation($line, $column)] - ); - } - - private function unknownField($typeName, $fieldName, $line, $column, $message = null) { - return FormattedError::create( - ValuesOfCorrectType::unknownFieldMessage( - $typeName, - $fieldName, - $message - ), - [new SourceLocation($line, $column)] - ); - } - - // Validate: Values of correct type - // Valid values - - /** - * @see it('Good int value') - */ - public function testGoodIntValue() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - intArgField(intArg: 2) - } - } - '); - } - - /** - * @see it('Good negative int value') - */ - public function testGoodNegativeIntValue() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - intArgField(intArg: -2) - } - } - '); - } - - /** - * @see it('Good boolean value') - */ - public function testGoodBooleanValue() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - booleanArgField(booleanArg: true) - } - } - '); - } - - /** - * @see it('Good string value') - */ - public function testGoodStringValue() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - stringArgField(stringArg: "foo") - } - } - '); - } - - /** - * @see it('Good float value') - */ - public function testGoodFloatValue() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - floatArgField(floatArg: 1.1) - } - } - '); - } - - public function testGoodNegativeFloatValue() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - floatArgField(floatArg: -1.1) - } - } - '); - } - - /** - * @see it('Int into Float') - */ - public function testIntIntoFloat() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - floatArgField(floatArg: 1) - } - } - '); - } - - /** - * @see it('Int into ID') - */ - public function testIntIntoID() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - idArgField(idArg: 1) - } - } - '); - } - - /** - * @see it('String into ID') - */ - public function testStringIntoID() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - idArgField(idArg: "someIdString") - } - } - '); - } - - /** - * @see it('Good enum value') - */ - public function testGoodEnumValue() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - dog { - doesKnowCommand(dogCommand: SIT) - } - } - '); - } - - /** - * @see it('Enum with null value') - */ - public function testEnumWithNullValue() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - enumArgField(enumArg: NO_FUR) - } - } - '); - } - - /** - * @see it('null into nullable type') - */ - public function testNullIntoNullableType() : void - { - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - intArgField(intArg: null) - } - } - '); - - $this->expectPassesRule(new ValuesOfCorrectType, ' - { - dog(a: null, b: null, c:{ requiredField: true, intField: null }) { - name - } - } - '); - } - - // Invalid String values - - /** - * @see it('Int into String') - */ - public function testIntIntoString() : void - { - $this->expectFailsRule(new ValuesOfCorrectType, ' - { - complicatedArgs { - stringArgField(stringArg: 1) - } - } - ', [ - $this->badValue('String', '1', 4, 39) - ]); - } - /** * @see it('Float into String') */ public function testFloatIntoString() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringArgField(stringArg: 1.0) } } - ', [ - $this->badValue('String', '1.0', 4, 39) - ]); + ', + [ + $this->badValue('String', '1.0', 4, 39), + ] + ); } + // Invalid String values + /** * @see it('Boolean into String') */ public function testBooleanIntoString() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringArgField(stringArg: true) } } - ', [ - $this->badValue('String', 'true', 4, 39) - ]); + ', + [ + $this->badValue('String', 'true', 4, 39), + ] + ); } /** @@ -272,33 +304,39 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testUnquotedStringIntoString() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringArgField(stringArg: BAR) } } - ', [ - $this->badValue('String', 'BAR', 4, 39) - ]); + ', + [ + $this->badValue('String', 'BAR', 4, 39), + ] + ); } - // Invalid Int values - /** * @see it('String into Int') */ public function testStringIntoInt() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { intArgField(intArg: "3") } } - ', [ - $this->badValue('Int', '"3"', 4, 33) - ]); + ', + [ + $this->badValue('Int', '"3"', 4, 33), + ] + ); } /** @@ -306,31 +344,41 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testBigIntIntoInt() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { intArgField(intArg: 829384293849283498239482938) } } - ', [ - $this->badValue('Int', '829384293849283498239482938', 4, 33) - ]); + ', + [ + $this->badValue('Int', '829384293849283498239482938', 4, 33), + ] + ); } + // Invalid Int values + /** * @see it('Unquoted String into Int') */ public function testUnquotedStringIntoInt() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { intArgField(intArg: FOO) } } - ', [ - $this->badValue('Int', 'FOO', 4, 33) - ]); + ', + [ + $this->badValue('Int', 'FOO', 4, 33), + ] + ); } /** @@ -338,15 +386,19 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testSimpleFloatIntoInt() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { intArgField(intArg: 3.0) } } - ', [ - $this->badValue('Int', '3.0', 4, 33) - ]); + ', + [ + $this->badValue('Int', '3.0', 4, 33), + ] + ); } /** @@ -354,33 +406,39 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testFloatIntoInt() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { intArgField(intArg: 3.333) } } - ', [ - $this->badValue('Int', '3.333', 4, 33) - ]); + ', + [ + $this->badValue('Int', '3.333', 4, 33), + ] + ); } - // Invalid Float values - /** * @see it('String into Float') */ public function testStringIntoFloat() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { floatArgField(floatArg: "3.333") } } - ', [ - $this->badValue('Float', '"3.333"', 4, 37) - ]); + ', + [ + $this->badValue('Float', '"3.333"', 4, 37), + ] + ); } /** @@ -388,49 +446,61 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testBooleanIntoFloat() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { floatArgField(floatArg: true) } } - ', [ - $this->badValue('Float', 'true', 4, 37) - ]); + ', + [ + $this->badValue('Float', 'true', 4, 37), + ] + ); } + // Invalid Float values + /** * @see it('Unquoted into Float') */ public function testUnquotedIntoFloat() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { floatArgField(floatArg: FOO) } } - ', [ - $this->badValue('Float', 'FOO', 4, 37) - ]); + ', + [ + $this->badValue('Float', 'FOO', 4, 37), + ] + ); } - // Invalid Boolean value - /** * @see it('Int into Boolean') */ public function testIntIntoBoolean() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { booleanArgField(booleanArg: 2) } } - ', [ - $this->badValue('Boolean', '2', 4, 41) - ]); + ', + [ + $this->badValue('Boolean', '2', 4, 41), + ] + ); } /** @@ -438,31 +508,41 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testFloatIntoBoolean() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { booleanArgField(booleanArg: 1.0) } } - ', [ - $this->badValue('Boolean', '1.0', 4, 41) - ]); + ', + [ + $this->badValue('Boolean', '1.0', 4, 41), + ] + ); } + // Invalid Boolean value + /** * @see it('String into Boolean') */ public function testStringIntoBoolean() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { booleanArgField(booleanArg: "true") } } - ', [ - $this->badValue('Boolean', '"true"', 4, 41) - ]); + ', + [ + $this->badValue('Boolean', '"true"', 4, 41), + ] + ); } /** @@ -470,33 +550,39 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testUnquotedIntoBoolean() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { booleanArgField(booleanArg: TRUE) } } - ', [ - $this->badValue('Boolean', 'TRUE', 4, 41) - ]); + ', + [ + $this->badValue('Boolean', 'TRUE', 4, 41), + ] + ); } - // Invalid ID value - /** * @see it('Float into ID') */ public function testFloatIntoID() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { idArgField(idArg: 1.0) } } - ', [ - $this->badValue('ID', '1.0', 4, 31) - ]); + ', + [ + $this->badValue('ID', '1.0', 4, 31), + ] + ); } /** @@ -504,49 +590,61 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testBooleanIntoID() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { idArgField(idArg: true) } } - ', [ - $this->badValue('ID', 'true', 4, 31) - ]); + ', + [ + $this->badValue('ID', 'true', 4, 31), + ] + ); } + // Invalid ID value + /** * @see it('Unquoted into ID') */ public function testUnquotedIntoID() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { idArgField(idArg: SOMETHING) } } - ', [ - $this->badValue('ID', 'SOMETHING', 4, 31) - ]); + ', + [ + $this->badValue('ID', 'SOMETHING', 4, 31), + ] + ); } - // Invalid Enum value - /** * @see it('Int into Enum') */ public function testIntIntoEnum() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { dog { doesKnowCommand(dogCommand: 2) } } - ', [ - $this->badValue('DogCommand', '2', 4, 41) - ]); + ', + [ + $this->badValue('DogCommand', '2', 4, 41), + ] + ); } /** @@ -554,37 +652,47 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testFloatIntoEnum() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { dog { doesKnowCommand(dogCommand: 1.0) } } - ', [ - $this->badValue('DogCommand', '1.0', 4, 41) - ]); + ', + [ + $this->badValue('DogCommand', '1.0', 4, 41), + ] + ); } + // Invalid Enum value + /** * @see it('String into Enum') */ public function testStringIntoEnum() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { dog { doesKnowCommand(dogCommand: "SIT") } } - ', [ - $this->badValue( - 'DogCommand', - '"SIT"', - 4, - 41, - 'Did you mean the enum value SIT?' - ) - ]); + ', + [ + $this->badValue( + 'DogCommand', + '"SIT"', + 4, + 41, + 'Did you mean the enum value SIT?' + ), + ] + ); } /** @@ -592,15 +700,19 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testBooleanIntoEnum() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { dog { doesKnowCommand(dogCommand: true) } } - ', [ - $this->badValue('DogCommand', 'true', 4, 41) - ]); + ', + [ + $this->badValue('DogCommand', 'true', 4, 41), + ] + ); } /** @@ -608,15 +720,19 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testUnknownEnumValueIntoEnum() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { dog { doesKnowCommand(dogCommand: JUGGLE) } } - ', [ - $this->badValue('DogCommand', 'JUGGLE', 4, 41) - ]); + ', + [ + $this->badValue('DogCommand', 'JUGGLE', 4, 41), + ] + ); } /** @@ -624,37 +740,42 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testDifferentCaseEnumValueIntoEnum() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { dog { doesKnowCommand(dogCommand: sit) } } - ', [ - $this->badValue( - 'DogCommand', - 'sit', - 4, - 41, - 'Did you mean the enum value SIT?' - ) - ]); + ', + [ + $this->badValue( + 'DogCommand', + 'sit', + 4, + 41, + 'Did you mean the enum value SIT?' + ), + ] + ); } - // Valid List value - /** * @see it('Good list value') */ public function testGoodListValue() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringListArgField(stringListArg: ["one", null, "two"]) } } - '); + ' + ); } /** @@ -662,27 +783,35 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testEmptyListValue() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringListArgField(stringListArg: []) } } - '); + ' + ); } + // Valid List value + /** * @see it('Null value') */ public function testNullValue() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringListArgField(stringListArg: null) } } - '); + ' + ); } /** @@ -690,31 +819,36 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testSingleValueIntoList() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringListArgField(stringListArg: "one") } } - '); + ' + ); } - // Invalid List value - /** * @see it('Incorrect item type') */ public function testIncorrectItemtype() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringListArgField(stringListArg: ["one", 2]) } } - ', [ - $this->badValue('String', '2', 4, 55), - ]); + ', + [ + $this->badValue('String', '2', 4, 55), + ] + ); } /** @@ -722,31 +856,38 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testSingleValueOfIncorrectType() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { stringListArgField(stringListArg: 1) } } - ', [ - $this->badValue('[String]', '1', 4, 47), - ]); + ', + [ + $this->badValue('[String]', '1', 4, 47), + ] + ); } - // Valid non-nullable value + // Invalid List value /** * @see it('Arg on optional arg') */ public function testArgOnOptionalArg() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { dog { isHousetrained(atOtherHomes: true) } } - '); + ' + ); } /** @@ -754,27 +895,35 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testNoArgOnOptionalArg() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { dog { isHousetrained } } - '); + ' + ); } + // Valid non-nullable value + /** * @see it('Multiple args') */ public function testMultipleArgs() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleReqs(req1: 1, req2: 2) } } - '); + ' + ); } /** @@ -782,13 +931,16 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testMultipleArgsReverseOrder() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleReqs(req2: 2, req1: 1) } } - '); + ' + ); } /** @@ -796,13 +948,16 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testNoArgsOnMultipleOptional() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleOpts } } - '); + ' + ); } /** @@ -810,13 +965,16 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testOneArgOnMultipleOptional() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleOpts(opt1: 1) } } - '); + ' + ); } /** @@ -824,13 +982,16 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testSecondArgOnMultipleOptional() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleOpts(opt2: 1) } } - '); + ' + ); } /** @@ -838,13 +999,16 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testMultipleReqsOnMixedList() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleOptAndReq(req1: 3, req2: 4) } } - '); + ' + ); } /** @@ -852,13 +1016,16 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testMultipleReqsAndOneOptOnMixedList() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleOptAndReq(req1: 3, req2: 4, opt1: 5) } } - '); + ' + ); } /** @@ -866,32 +1033,37 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testAllReqsAndOptsOnMixedList() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleOptAndReq(req1: 3, req2: 4, opt1: 5, opt2: 6) } } - '); + ' + ); } - // Invalid non-nullable value - /** * @see it('Incorrect value type') */ public function testIncorrectValueType() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleReqs(req2: "two", req1: "one") } } - ', [ - $this->badValue('Int!', '"two"', 4, 32), - $this->badValue('Int!', '"one"', 4, 45), - ]); + ', + [ + $this->badValue('Int!', '"two"', 4, 32), + $this->badValue('Int!', '"one"', 4, 45), + ] + ); } /** @@ -899,48 +1071,58 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testIncorrectValueAndMissingArgumentProvidedNonNullArguments() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleReqs(req1: "one") } } - ', [ - $this->badValue('Int!', '"one"', 4, 32), - ]); + ', + [ + $this->badValue('Int!', '"one"', 4, 32), + ] + ); } + // Invalid non-nullable value + /** * @see it('Null value') */ public function testNullValue2() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { multipleReqs(req1: null) } } - ', [ - $this->badValue('Int!', 'null', 4, 32), - ]); + ', + [ + $this->badValue('Int!', 'null', 4, 32), + ] + ); } - - // DESCRIBE: Valid input object value - /** * @see it('Optional arg, despite required field in type') */ public function testOptionalArgDespiteRequiredFieldInType() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField } } - '); + ' + ); } /** @@ -948,27 +1130,36 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testPartialObjectOnlyRequired() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField(complexArg: { requiredField: true }) } } - '); + ' + ); } + + // DESCRIBE: Valid input object value + /** * @see it('Partial object, required field can be falsey') */ public function testPartialObjectRequiredFieldCanBeFalsey() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField(complexArg: { requiredField: false }) } } - '); + ' + ); } /** @@ -976,13 +1167,16 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testPartialObjectIncludingRequired() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField(complexArg: { requiredField: true, intField: 4 }) } } - '); + ' + ); } /** @@ -990,7 +1184,9 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testFullObject() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField(complexArg: { @@ -1002,7 +1198,8 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase }) } } - '); + ' + ); } /** @@ -1010,7 +1207,9 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testFullObjectWithFieldsInDifferentOrder() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField(complexArg: { @@ -1022,33 +1221,52 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase }) } } - '); + ' + ); } - // DESCRIBE: Invalid input object value - /** * @see it('Partial object, missing required') */ public function testPartialObjectMissingRequired() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField(complexArg: { intField: 4 }) } } - ', [ - $this->requiredField('ComplexInput', 'requiredField', 'Boolean!', 4, 41), - ]); + ', + [ + $this->requiredField('ComplexInput', 'requiredField', 'Boolean!', 4, 41), + ] + ); } + private function requiredField($typeName, $fieldName, $fieldTypeName, $line, $column) + { + return FormattedError::create( + ValuesOfCorrectType::requiredFieldMessage( + $typeName, + $fieldName, + $fieldTypeName + ), + [new SourceLocation($line, $column)] + ); + } + + // DESCRIBE: Invalid input object value + /** * @see it('Partial object, invalid field type') */ public function testPartialObjectInvalidFieldType() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField(complexArg: { @@ -1057,9 +1275,11 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase }) } } - ', [ - $this->badValue('String', '2', 5, 40), - ]); + ', + [ + $this->badValue('String', '2', 5, 40), + ] + ); } /** @@ -1070,7 +1290,9 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testPartialObjectUnknownFieldArg() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { complicatedArgs { complexArgField(complexArg: { @@ -1079,37 +1301,53 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase }) } } - ', [ - $this->unknownField( - 'ComplexInput', - 'unknownField', - 6, - 15, - 'Did you mean intField or booleanField?' - ), - ]); + ', + [ + $this->unknownField( + 'ComplexInput', + 'unknownField', + 6, + 15, + 'Did you mean intField or booleanField?' + ), + ] + ); } - + private function unknownField($typeName, $fieldName, $line, $column, $message = null) + { + return FormattedError::create( + ValuesOfCorrectType::unknownFieldMessage( + $typeName, + $fieldName, + $message + ), + [new SourceLocation($line, $column)] + ); + } /** * @see it('reports original error for custom scalar which throws') */ public function testReportsOriginalErrorForCustomScalarWhichThrows() : void { - $errors = $this->expectFailsRule(new ValuesOfCorrectType, ' + $errors = $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { invalidArg(arg: 123) } - ', [ - $this->badValue( - 'Invalid', - '123', - 3, - 27, - 'Invalid scalar is always invalid: 123' - ), - ]); + ', + [ + $this->badValue( + 'Invalid', + '123', + 3, + 27, + 'Invalid scalar is always invalid: 123' + ), + ] + ); $this->assertEquals( 'Invalid scalar is always invalid: 123', @@ -1122,14 +1360,17 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testAllowsCustomScalarToAcceptComplexLiterals() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { test1: anyArg(arg: 123) test2: anyArg(arg: "abc") test3: anyArg(arg: [123, "abc"]) test4: anyArg(arg: {deep: [123, "abc"]}) } - '); + ' + ); } // DESCRIBE: Directive arguments @@ -1139,7 +1380,9 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testWithDirectivesOfValidTypes() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' { dog @include(if: true) { name @@ -1148,7 +1391,8 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase name } } - '); + ' + ); } /** @@ -1156,16 +1400,20 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testWithDirectiveWithIncorrectTypes() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' { dog @include(if: "yes") { name @skip(if: ENUM) } } - ', [ - $this->badValue('Boolean!', '"yes"', 3, 28), - $this->badValue('Boolean!', 'ENUM', 4, 28), - ]); + ', + [ + $this->badValue('Boolean!', '"yes"', 3, 28), + $this->badValue('Boolean!', 'ENUM', 4, 28), + ] + ); } // DESCRIBE: Variable default values @@ -1175,7 +1423,9 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testVariablesWithValidDefaultValues() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' query WithDefaultValues( $a: Int = 1, $b: String = "ok", @@ -1183,7 +1433,8 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase ) { dog { name } } - '); + ' + ); } /** @@ -1191,7 +1442,9 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testVariablesWithValidDefaultNullValues() : void { - $this->expectPassesRule(new ValuesOfCorrectType, ' + $this->expectPassesRule( + new ValuesOfCorrectType(), + ' query WithDefaultValues( $a: Int = null, $b: String = null, @@ -1199,7 +1452,8 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase ) { dog { name } } - '); + ' + ); } /** @@ -1207,7 +1461,9 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testVariablesWithInvalidDefaultNullValues() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' query WithDefaultValues( $a: Int! = null, $b: String! = null, @@ -1215,11 +1471,13 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase ) { dog { name } } - ', [ - $this->badValue('Int!', 'null', 3, 22), - $this->badValue('String!', 'null', 4, 25), - $this->badValue('Boolean!', 'null', 5, 47), - ]); + ', + [ + $this->badValue('Int!', 'null', 3, 22), + $this->badValue('String!', 'null', 4, 25), + $this->badValue('Boolean!', 'null', 5, 47), + ] + ); } /** @@ -1227,7 +1485,9 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testVariablesWithInvalidDefaultValues() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' query InvalidDefaultValues( $a: Int = "one", $b: String = 4, @@ -1235,11 +1495,13 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase ) { dog { name } } - ', [ - $this->badValue('Int', '"one"', 3, 21), - $this->badValue('String', '4', 4, 24), - $this->badValue('ComplexInput', '"notverycomplex"', 5, 30), - ]); + ', + [ + $this->badValue('Int', '"one"', 3, 21), + $this->badValue('String', '4', 4, 24), + $this->badValue('ComplexInput', '"notverycomplex"', 5, 30), + ] + ); } /** @@ -1247,16 +1509,20 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testVariablesWithComplexInvalidDefaultValues() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' query WithDefaultValues( $a: ComplexInput = { requiredField: 123, intField: "abc" } ) { dog { name } } - ', [ - $this->badValue('Boolean!', '123', 3, 47), - $this->badValue('Int', '"abc"', 3, 62), - ]); + ', + [ + $this->badValue('Boolean!', '123', 3, 47), + $this->badValue('Int', '"abc"', 3, 62), + ] + ); } /** @@ -1264,13 +1530,17 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testComplexVariablesMissingRequiredField() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' query MissingRequiredField($a: ComplexInput = {intField: 3}) { dog { name } } - ', [ - $this->requiredField('ComplexInput', 'requiredField', 'Boolean!', 2, 55), - ]); + ', + [ + $this->requiredField('ComplexInput', 'requiredField', 'Boolean!', 2, 55), + ] + ); } /** @@ -1278,12 +1548,16 @@ class ValuesOfCorrectTypeTest extends ValidatorTestCase */ public function testListVariablesWithInvalidItem() : void { - $this->expectFailsRule(new ValuesOfCorrectType, ' + $this->expectFailsRule( + new ValuesOfCorrectType(), + ' query InvalidItem($a: [String] = ["one", 2]) { dog { name } } - ', [ - $this->badValue('String', '2', 2, 50), - ]); + ', + [ + $this->badValue('String', '2', 2, 50), + ] + ); } } diff --git a/tests/Validator/VariablesAreInputTypesTest.php b/tests/Validator/VariablesAreInputTypesTest.php index 5031434..5264314 100644 --- a/tests/Validator/VariablesAreInputTypesTest.php +++ b/tests/Validator/VariablesAreInputTypesTest.php @@ -1,4 +1,7 @@ expectPassesRule(new VariablesAreInputTypes(), ' + $this->expectPassesRule( + new VariablesAreInputTypes(), + ' query Foo($a: String, $b: [Boolean!]!, $c: ComplexInput) { field(a: $a, b: $b, c: $c) } - '); + ' + ); } /** @@ -26,11 +31,14 @@ class VariablesAreInputTypesTest extends ValidatorTestCase */ public function testOutputTypesAreInvalid() : void { - $this->expectFailsRule(new VariablesAreInputTypes, ' + $this->expectFailsRule( + new VariablesAreInputTypes(), + ' query Foo($a: Dog, $b: [[CatOrDog!]]!, $c: Pet) { field(a: $a, b: $b, c: $c) } - ', [ + ', + [ FormattedError::create( VariablesAreInputTypes::nonInputTypeOnVarMessage('a', 'Dog'), [new SourceLocation(2, 21)] @@ -42,7 +50,7 @@ class VariablesAreInputTypesTest extends ValidatorTestCase FormattedError::create( VariablesAreInputTypes::nonInputTypeOnVarMessage('c', 'Pet'), [new SourceLocation(2, 50)] - ) + ), ] ); } diff --git a/tests/Validator/VariablesDefaultValueAllowedTest.php b/tests/Validator/VariablesDefaultValueAllowedTest.php index 468b6a4..ddebae2 100644 --- a/tests/Validator/VariablesDefaultValueAllowedTest.php +++ b/tests/Validator/VariablesDefaultValueAllowedTest.php @@ -1,4 +1,7 @@ expectPassesRule( + new VariablesDefaultValueAllowed(), + ' + query NullableValues($a: Int, $b: String, $c: ComplexInput) { + dog { name } + } + ' + ); + } + + // DESCRIBE: Validate: Variable default value is allowed + + /** + * @see it('required variables without default values') + */ + public function testRequiredVariablesWithoutDefaultValues() : void + { + $this->expectPassesRule( + new VariablesDefaultValueAllowed(), + ' + query RequiredValues($a: Int!, $b: String!) { + dog { name } + } + ' + ); + } + + /** + * @see it('variables with valid default values') + */ + public function testVariablesWithValidDefaultValues() : void + { + $this->expectPassesRule( + new VariablesDefaultValueAllowed(), + ' + query WithDefaultValues( + $a: Int = 1, + $b: String = "ok", + $c: ComplexInput = { requiredField: true, intField: 3 } + ) { + dog { name } + } + ' + ); + } + + /** + * @see it('variables with valid default null values') + */ + public function testVariablesWithValidDefaultNullValues() : void + { + $this->expectPassesRule( + new VariablesDefaultValueAllowed(), + ' + query WithDefaultValues( + $a: Int = null, + $b: String = null, + $c: ComplexInput = { requiredField: true, intField: null } + ) { + dog { name } + } + ' + ); + } + + /** + * @see it('no required variables with default values') + */ + public function testNoRequiredVariablesWithDefaultValues() : void + { + $this->expectFailsRule( + new VariablesDefaultValueAllowed(), + ' + query UnreachableDefaultValues($a: Int! = 3, $b: String! = "default") { + dog { name } + } + ', + [ + $this->defaultForRequiredVar('a', 'Int!', 'Int', 2, 49), + $this->defaultForRequiredVar('b', 'String!', 'String', 2, 66), + ] + ); + } + private function defaultForRequiredVar($varName, $typeName, $guessTypeName, $line, $column) { return FormattedError::create( @@ -19,91 +111,22 @@ class VariablesDefaultValueAllowedTest extends ValidatorTestCase ); } - // DESCRIBE: Validate: Variable default value is allowed - - /** - * @see it('variables with no default values') - */ - public function testVariablesWithNoDefaultValues() : void - { - $this->expectPassesRule(new VariablesDefaultValueAllowed(), ' - query NullableValues($a: Int, $b: String, $c: ComplexInput) { - dog { name } - } - '); - } - - /** - * @see it('required variables without default values') - */ - public function testRequiredVariablesWithoutDefaultValues() : void - { - $this->expectPassesRule(new VariablesDefaultValueAllowed(), ' - query RequiredValues($a: Int!, $b: String!) { - dog { name } - } - '); - } - - /** - * @see it('variables with valid default values') - */ - public function testVariablesWithValidDefaultValues() : void - { - $this->expectPassesRule(new VariablesDefaultValueAllowed(), ' - query WithDefaultValues( - $a: Int = 1, - $b: String = "ok", - $c: ComplexInput = { requiredField: true, intField: 3 } - ) { - dog { name } - } - '); - } - - /** - * @see it('variables with valid default null values') - */ - public function testVariablesWithValidDefaultNullValues() : void - { - $this->expectPassesRule(new VariablesDefaultValueAllowed(), ' - query WithDefaultValues( - $a: Int = null, - $b: String = null, - $c: ComplexInput = { requiredField: true, intField: null } - ) { - dog { name } - } - '); - } - - /** - * @see it('no required variables with default values') - */ - public function testNoRequiredVariablesWithDefaultValues() : void - { - $this->expectFailsRule(new VariablesDefaultValueAllowed(), ' - query UnreachableDefaultValues($a: Int! = 3, $b: String! = "default") { - dog { name } - } - ', [ - $this->defaultForRequiredVar('a', 'Int!', 'Int', 2, 49), - $this->defaultForRequiredVar('b', 'String!', 'String', 2, 66), - ]); - } - /** * @see it('variables with invalid default null values') */ public function testNullIntoNullableType() : void { - $this->expectFailsRule(new VariablesDefaultValueAllowed(), ' + $this->expectFailsRule( + new VariablesDefaultValueAllowed(), + ' query WithDefaultValues($a: Int! = null, $b: String! = null) { dog { name } } - ', [ - $this->defaultForRequiredVar('a', 'Int!', 'Int', 2, 42), - $this->defaultForRequiredVar('b', 'String!', 'String', 2, 62), - ]); + ', + [ + $this->defaultForRequiredVar('a', 'Int!', 'Int', 2, 42), + $this->defaultForRequiredVar('b', 'String!', 'String', 2, 62), + ] + ); } } diff --git a/tests/Validator/VariablesInAllowedPositionTest.php b/tests/Validator/VariablesInAllowedPositionTest.php index fa8a69a..b1a1edd 100644 --- a/tests/Validator/VariablesInAllowedPositionTest.php +++ b/tests/Validator/VariablesInAllowedPositionTest.php @@ -1,4 +1,7 @@ Boolean') */ public function testBooleanXBoolean() : void { // Boolean => Boolean - $this->expectPassesRule(new VariablesInAllowedPosition(), ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($booleanArg: Boolean) { complicatedArgs { booleanArgField(booleanArg: $booleanArg) } } - '); + ' + ); } /** @@ -31,7 +36,9 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase public function testBooleanXBooleanWithinFragment() : void { // Boolean => Boolean within fragment - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' fragment booleanArgFrag on ComplicatedArgs { booleanArgField(booleanArg: $booleanArg) } @@ -41,9 +48,12 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase ...booleanArgFrag } } - '); + ' + ); - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($booleanArg: Boolean) { complicatedArgs { @@ -53,7 +63,8 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase fragment booleanArgFrag on ComplicatedArgs { booleanArgField(booleanArg: $booleanArg) } - '); + ' + ); } /** @@ -62,14 +73,17 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase public function testBooleanNonNullXBoolean() : void { // Boolean! => Boolean - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($nonNullBooleanArg: Boolean!) { complicatedArgs { booleanArgField(booleanArg: $nonNullBooleanArg) } } - '); + ' + ); } /** @@ -78,7 +92,9 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase public function testBooleanNonNullXBooleanWithinFragment() : void { // Boolean! => Boolean within fragment - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' fragment booleanArgFrag on ComplicatedArgs { booleanArgField(booleanArg: $nonNullBooleanArg) } @@ -89,7 +105,8 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase ...booleanArgFrag } } - '); + ' + ); } /** @@ -98,14 +115,17 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase public function testIntXIntNonNullWithDefault() : void { // Int => Int! with default - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($intArg: Int = 1) { complicatedArgs { nonNullIntArgField(nonNullIntArg: $intArg) } } - '); + ' + ); } /** @@ -113,14 +133,17 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testListOfStringXListOfString() : void { - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($stringListVar: [String]) { complicatedArgs { stringListArgField(stringListArg: $stringListVar) } } - '); + ' + ); } /** @@ -128,14 +151,17 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testListOfStringNonNullXListOfString() : void { - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($stringListVar: [String!]) { complicatedArgs { stringListArgField(stringListArg: $stringListVar) } } - '); + ' + ); } /** @@ -143,14 +169,17 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testStringXListOfStringInItemPosition() : void { - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($stringVar: String) { complicatedArgs { stringListArgField(stringListArg: [$stringVar]) } } - '); + ' + ); } /** @@ -158,14 +187,17 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testStringNonNullXListOfStringInItemPosition() : void { - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($stringVar: String!) { complicatedArgs { stringListArgField(stringListArg: [$stringVar]) } } - '); + ' + ); } /** @@ -173,14 +205,17 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testComplexInputXComplexInput() : void { - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($complexVar: ComplexInput) { complicatedArgs { complexArgField(complexArg: $ComplexInput) } } - '); + ' + ); } /** @@ -188,14 +223,17 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testComplexInputXComplexInputInFieldPosition() : void { - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($boolVar: Boolean = false) { complicatedArgs { complexArgField(complexArg: {requiredArg: $boolVar}) } } - '); + ' + ); } /** @@ -203,12 +241,15 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testBooleanNonNullXBooleanNonNullInDirective() : void { - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($boolVar: Boolean!) { dog @include(if: $boolVar) } - '); + ' + ); } /** @@ -216,12 +257,15 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testBooleanXBooleanNonNullInDirectiveWithDefault() : void { - $this->expectPassesRule(new VariablesInAllowedPosition, ' + $this->expectPassesRule( + new VariablesInAllowedPosition(), + ' query Query($boolVar: Boolean = false) { dog @include(if: $boolVar) } - '); + ' + ); } /** @@ -229,18 +273,22 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testIntXIntNonNull() : void { - $this->expectFailsRule(new VariablesInAllowedPosition, ' + $this->expectFailsRule( + new VariablesInAllowedPosition(), + ' query Query($intArg: Int) { complicatedArgs { nonNullIntArgField(nonNullIntArg: $intArg) } } - ', [ - FormattedError::create( - VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'), - [new SourceLocation(2, 19), new SourceLocation(4, 45)] - ) - ]); + ', + [ + FormattedError::create( + VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'), + [new SourceLocation(2, 19), new SourceLocation(4, 45)] + ), + ] + ); } /** @@ -248,7 +296,9 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testIntXIntNonNullWithinFragment() : void { - $this->expectFailsRule(new VariablesInAllowedPosition, ' + $this->expectFailsRule( + new VariablesInAllowedPosition(), + ' fragment nonNullIntArgFieldFrag on ComplicatedArgs { nonNullIntArgField(nonNullIntArg: $intArg) } @@ -258,12 +308,14 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase ...nonNullIntArgFieldFrag } } - ', [ - FormattedError::create( - VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'), - [new SourceLocation(6, 19), new SourceLocation(3, 43)] - ) - ]); + ', + [ + FormattedError::create( + VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'), + [new SourceLocation(6, 19), new SourceLocation(3, 43)] + ), + ] + ); } /** @@ -272,7 +324,9 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase public function testIntXIntNonNullWithinNestedFragment() : void { // Int => Int! within nested fragment - $this->expectFailsRule(new VariablesInAllowedPosition, ' + $this->expectFailsRule( + new VariablesInAllowedPosition(), + ' fragment outerFrag on ComplicatedArgs { ...nonNullIntArgFieldFrag } @@ -287,12 +341,14 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase ...outerFrag } } - ', [ - FormattedError::create( - VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'), - [new SourceLocation(10, 19), new SourceLocation(7,43)] - ) - ]); + ', + [ + FormattedError::create( + VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'), + [new SourceLocation(10, 19), new SourceLocation(7, 43)] + ), + ] + ); } /** @@ -300,18 +356,22 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testStringOverBoolean() : void { - $this->expectFailsRule(new VariablesInAllowedPosition, ' + $this->expectFailsRule( + new VariablesInAllowedPosition(), + ' query Query($stringVar: String) { complicatedArgs { booleanArgField(booleanArg: $stringVar) } } - ', [ - FormattedError::create( - VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', 'Boolean'), - [new SourceLocation(2,19), new SourceLocation(4,39)] - ) - ]); + ', + [ + FormattedError::create( + VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', 'Boolean'), + [new SourceLocation(2, 19), new SourceLocation(4, 39)] + ), + ] + ); } /** @@ -319,18 +379,22 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testStringXListOfString() : void { - $this->expectFailsRule(new VariablesInAllowedPosition, ' + $this->expectFailsRule( + new VariablesInAllowedPosition(), + ' query Query($stringVar: String) { complicatedArgs { stringListArgField(stringListArg: $stringVar) } } - ', [ - FormattedError::create( - VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', '[String]'), - [new SourceLocation(2, 19), new SourceLocation(4,45)] - ) - ]); + ', + [ + FormattedError::create( + VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', '[String]'), + [new SourceLocation(2, 19), new SourceLocation(4, 45)] + ), + ] + ); } /** @@ -338,16 +402,20 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase */ public function testBooleanXBooleanNonNullInDirective() : void { - $this->expectFailsRule(new VariablesInAllowedPosition, ' + $this->expectFailsRule( + new VariablesInAllowedPosition(), + ' query Query($boolVar: Boolean) { dog @include(if: $boolVar) } - ', [ - FormattedError::create( - VariablesInAllowedPosition::badVarPosMessage('boolVar', 'Boolean', 'Boolean!'), - [new SourceLocation(2, 19), new SourceLocation(3,26)] - ) - ]); + ', + [ + FormattedError::create( + VariablesInAllowedPosition::badVarPosMessage('boolVar', 'Boolean', 'Boolean!'), + [new SourceLocation(2, 19), new SourceLocation(3, 26)] + ), + ] + ); } /** @@ -356,16 +424,20 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase public function testStringXBooleanNonNullInDirective() : void { // String => Boolean! in directive - $this->expectFailsRule(new VariablesInAllowedPosition, ' + $this->expectFailsRule( + new VariablesInAllowedPosition(), + ' query Query($stringVar: String) { dog @include(if: $stringVar) } - ', [ - FormattedError::create( - VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', 'Boolean!'), - [new SourceLocation(2, 19), new SourceLocation(3,26)] - ) - ]); + ', + [ + FormattedError::create( + VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', 'Boolean!'), + [new SourceLocation(2, 19), new SourceLocation(3, 26)] + ), + ] + ); } /** @@ -374,7 +446,7 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase public function testStringArrayXStringNonNullArray() : void { $this->expectFailsRule( - new VariablesInAllowedPosition, + new VariablesInAllowedPosition(), ' query Query($stringListVar: [String]) { @@ -382,11 +454,12 @@ class VariablesInAllowedPositionTest extends ValidatorTestCase stringListNonNullArgField(stringListNonNullArg: $stringListVar) } } - ', [ + ', + [ FormattedError::create( VariablesInAllowedPosition::badVarPosMessage('stringListVar', '[String]', '[String!]'), [new SourceLocation(2, 19), new SourceLocation(5, 59)] - ) + ), ] ); }