2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2016-04-09 10:36:53 +03:00
|
|
|
namespace GraphQL\Tests\Validator;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\FormattedError;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Validator\Rules\VariablesInAllowedPosition;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class VariablesInAllowedPositionTest extends ValidatorTestCase
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
// Validate: Variables are in allowed positions
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Boolean => Boolean
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testBooleanXBoolean()
|
|
|
|
{
|
|
|
|
// Boolean => Boolean
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition(), '
|
|
|
|
query Query($booleanArg: Boolean)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
booleanArgField(booleanArg: $booleanArg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Boolean => Boolean within fragment
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testBooleanXBooleanWithinFragment()
|
|
|
|
{
|
|
|
|
// Boolean => Boolean within fragment
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
fragment booleanArgFrag on ComplicatedArgs {
|
|
|
|
booleanArgField(booleanArg: $booleanArg)
|
|
|
|
}
|
|
|
|
query Query($booleanArg: Boolean)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
...booleanArgFrag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($booleanArg: Boolean)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
...booleanArgFrag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment booleanArgFrag on ComplicatedArgs {
|
|
|
|
booleanArgField(booleanArg: $booleanArg)
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Boolean! => Boolean
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testBooleanNonNullXBoolean()
|
|
|
|
{
|
|
|
|
// Boolean! => Boolean
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($nonNullBooleanArg: Boolean!)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
booleanArgField(booleanArg: $nonNullBooleanArg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Boolean! => Boolean within fragment
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testBooleanNonNullXBooleanWithinFragment()
|
|
|
|
{
|
|
|
|
// Boolean! => Boolean within fragment
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
fragment booleanArgFrag on ComplicatedArgs {
|
|
|
|
booleanArgField(booleanArg: $nonNullBooleanArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
query Query($nonNullBooleanArg: Boolean!)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
...booleanArgFrag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Int => Int! with default
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testIntXIntNonNullWithDefault()
|
|
|
|
{
|
|
|
|
// Int => Int! with default
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($intArg: Int = 1)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
nonNullIntArgField(nonNullIntArg: $intArg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it [String] => [String]
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testListOfStringXListOfString()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($stringListVar: [String])
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
stringListArgField(stringListArg: $stringListVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it [String!] => [String]
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testListOfStringNonNullXListOfString()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($stringListVar: [String!])
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
stringListArgField(stringListArg: $stringListVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it String => [String] in item position
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testStringXListOfStringInItemPosition()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($stringVar: String)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
stringListArgField(stringListArg: [$stringVar])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it String! => [String] in item position
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testStringNonNullXListOfStringInItemPosition()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($stringVar: String!)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
stringListArgField(stringListArg: [$stringVar])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it ComplexInput => ComplexInput
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testComplexInputXComplexInput()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($complexVar: ComplexInput)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
complexArgField(complexArg: $ComplexInput)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it ComplexInput => ComplexInput in field position
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testComplexInputXComplexInputInFieldPosition()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($boolVar: Boolean = false)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
complexArgField(complexArg: {requiredArg: $boolVar})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Boolean! => Boolean! in directive
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testBooleanNonNullXBooleanNonNullInDirective()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($boolVar: Boolean!)
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
dog @include(if: $boolVar)
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Boolean => Boolean! in directive with default
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testBooleanXBooleanNonNullInDirectiveWithDefault()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesInAllowedPosition, '
|
|
|
|
query Query($boolVar: Boolean = false)
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
dog @include(if: $boolVar)
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Int => Int!
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testIntXIntNonNull()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new VariablesInAllowedPosition, '
|
2016-04-25 16:29:17 +03:00
|
|
|
query Query($intArg: Int) {
|
2015-07-15 20:05:46 +03:00
|
|
|
complicatedArgs {
|
|
|
|
nonNullIntArgField(nonNullIntArg: $intArg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
', [
|
2015-08-17 17:01:55 +03:00
|
|
|
FormattedError::create(
|
2016-04-25 16:29:17 +03:00
|
|
|
VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'),
|
|
|
|
[new SourceLocation(2, 19), new SourceLocation(4, 45)]
|
2015-07-15 20:05:46 +03:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Int => Int! within fragment
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testIntXIntNonNullWithinFragment()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new VariablesInAllowedPosition, '
|
|
|
|
fragment nonNullIntArgFieldFrag on ComplicatedArgs {
|
|
|
|
nonNullIntArgField(nonNullIntArg: $intArg)
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
query Query($intArg: Int) {
|
2015-07-15 20:05:46 +03:00
|
|
|
complicatedArgs {
|
|
|
|
...nonNullIntArgFieldFrag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
', [
|
2015-08-17 17:01:55 +03:00
|
|
|
FormattedError::create(
|
2016-04-25 16:29:17 +03:00
|
|
|
VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'),
|
|
|
|
[new SourceLocation(6, 19), new SourceLocation(3, 43)]
|
2015-07-15 20:05:46 +03:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Int => Int! within nested fragment
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testIntXIntNonNullWithinNestedFragment()
|
|
|
|
{
|
|
|
|
// Int => Int! within nested fragment
|
|
|
|
$this->expectFailsRule(new VariablesInAllowedPosition, '
|
|
|
|
fragment outerFrag on ComplicatedArgs {
|
|
|
|
...nonNullIntArgFieldFrag
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment nonNullIntArgFieldFrag on ComplicatedArgs {
|
|
|
|
nonNullIntArgField(nonNullIntArg: $intArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
query Query($intArg: Int)
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
...outerFrag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
', [
|
2015-08-17 17:01:55 +03:00
|
|
|
FormattedError::create(
|
2016-04-25 16:29:17 +03:00
|
|
|
VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'),
|
|
|
|
[new SourceLocation(10, 19), new SourceLocation(7,43)]
|
2015-07-15 20:05:46 +03:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it String over Boolean
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testStringOverBoolean()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new VariablesInAllowedPosition, '
|
2016-04-25 16:29:17 +03:00
|
|
|
query Query($stringVar: String) {
|
2015-07-15 20:05:46 +03:00
|
|
|
complicatedArgs {
|
|
|
|
booleanArgField(booleanArg: $stringVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
', [
|
2015-08-17 17:01:55 +03:00
|
|
|
FormattedError::create(
|
2016-04-25 16:29:17 +03:00
|
|
|
VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', 'Boolean'),
|
|
|
|
[new SourceLocation(2,19), new SourceLocation(4,39)]
|
2015-07-15 20:05:46 +03:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it String => [String]
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testStringXListOfString()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new VariablesInAllowedPosition, '
|
2016-04-25 16:29:17 +03:00
|
|
|
query Query($stringVar: String) {
|
2015-07-15 20:05:46 +03:00
|
|
|
complicatedArgs {
|
|
|
|
stringListArgField(stringListArg: $stringVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
', [
|
2015-08-17 17:01:55 +03:00
|
|
|
FormattedError::create(
|
2016-04-25 16:29:17 +03:00
|
|
|
VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', '[String]'),
|
|
|
|
[new SourceLocation(2, 19), new SourceLocation(4,45)]
|
2015-07-15 20:05:46 +03:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it Boolean => Boolean! in directive
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testBooleanXBooleanNonNullInDirective()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new VariablesInAllowedPosition, '
|
2016-04-25 16:29:17 +03:00
|
|
|
query Query($boolVar: Boolean) {
|
2015-08-17 17:01:55 +03:00
|
|
|
dog @include(if: $boolVar)
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
', [
|
2015-08-17 17:01:55 +03:00
|
|
|
FormattedError::create(
|
2016-04-25 16:29:17 +03:00
|
|
|
VariablesInAllowedPosition::badVarPosMessage('boolVar', 'Boolean', 'Boolean!'),
|
|
|
|
[new SourceLocation(2, 19), new SourceLocation(3,26)]
|
2015-07-15 20:05:46 +03:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
|
|
|
* @it String => Boolean! in directive
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testStringXBooleanNonNullInDirective()
|
|
|
|
{
|
|
|
|
// String => Boolean! in directive
|
|
|
|
$this->expectFailsRule(new VariablesInAllowedPosition, '
|
2016-04-25 16:29:17 +03:00
|
|
|
query Query($stringVar: String) {
|
2015-08-17 17:01:55 +03:00
|
|
|
dog @include(if: $stringVar)
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
', [
|
2015-08-17 17:01:55 +03:00
|
|
|
FormattedError::create(
|
2016-04-25 16:29:17 +03:00
|
|
|
VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', 'Boolean!'),
|
|
|
|
[new SourceLocation(2, 19), new SourceLocation(3,26)]
|
2015-07-15 20:05:46 +03:00
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-08-22 13:55:42 +03:00
|
|
|
/**
|
|
|
|
* @it [String] => [String!]
|
|
|
|
*/
|
|
|
|
public function testStringArrayXStringNonNullArray()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(
|
|
|
|
new VariablesInAllowedPosition,
|
|
|
|
'
|
|
|
|
query Query($stringListVar: [String])
|
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
stringListNonNullArgField(stringListNonNullArg: $stringListVar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
', [
|
|
|
|
FormattedError::create(
|
|
|
|
VariablesInAllowedPosition::badVarPosMessage('stringListVar', '[String]', '[String!]'),
|
|
|
|
[new SourceLocation(2, 19), new SourceLocation(5, 59)]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|