graphql-php/tests/Validator/VariablesInAllowedPositionTest.php

467 lines
11 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2018-09-02 14:08:49 +03:00
declare(strict_types=1);
2016-04-09 10:36:53 +03:00
namespace GraphQL\Tests\Validator;
2015-07-15 20:05:46 +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
/**
* @see it('Boolean => Boolean')
*/
public function testBooleanXBoolean() : void
2015-07-15 20:05:46 +03:00
{
// Boolean => Boolean
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($booleanArg: Boolean)
{
complicatedArgs {
booleanArgField(booleanArg: $booleanArg)
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Boolean => Boolean within fragment')
*/
public function testBooleanXBooleanWithinFragment() : void
2015-07-15 20:05:46 +03:00
{
// Boolean => Boolean within fragment
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
fragment booleanArgFrag on ComplicatedArgs {
booleanArgField(booleanArg: $booleanArg)
}
query Query($booleanArg: Boolean)
{
complicatedArgs {
...booleanArgFrag
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($booleanArg: Boolean)
{
complicatedArgs {
...booleanArgFrag
}
}
fragment booleanArgFrag on ComplicatedArgs {
booleanArgField(booleanArg: $booleanArg)
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Boolean! => Boolean')
*/
public function testBooleanNonNullXBoolean() : void
2015-07-15 20:05:46 +03:00
{
// Boolean! => Boolean
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($nonNullBooleanArg: Boolean!)
{
complicatedArgs {
booleanArgField(booleanArg: $nonNullBooleanArg)
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Boolean! => Boolean within fragment')
*/
public function testBooleanNonNullXBooleanWithinFragment() : void
2015-07-15 20:05:46 +03:00
{
// Boolean! => Boolean within fragment
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
fragment booleanArgFrag on ComplicatedArgs {
booleanArgField(booleanArg: $nonNullBooleanArg)
}
query Query($nonNullBooleanArg: Boolean!)
{
complicatedArgs {
...booleanArgFrag
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Int => Int! with default')
*/
public function testIntXIntNonNullWithDefault() : void
2015-07-15 20:05:46 +03:00
{
// Int => Int! with default
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($intArg: Int = 1)
{
complicatedArgs {
nonNullIntArgField(nonNullIntArg: $intArg)
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('[String] => [String]')
*/
public function testListOfStringXListOfString() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($stringListVar: [String])
{
complicatedArgs {
stringListArgField(stringListArg: $stringListVar)
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('[String!] => [String]')
*/
public function testListOfStringNonNullXListOfString() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($stringListVar: [String!])
{
complicatedArgs {
stringListArgField(stringListArg: $stringListVar)
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('String => [String] in item position')
*/
public function testStringXListOfStringInItemPosition() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($stringVar: String)
{
complicatedArgs {
stringListArgField(stringListArg: [$stringVar])
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('String! => [String] in item position')
*/
public function testStringNonNullXListOfStringInItemPosition() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($stringVar: String!)
{
complicatedArgs {
stringListArgField(stringListArg: [$stringVar])
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('ComplexInput => ComplexInput')
*/
public function testComplexInputXComplexInput() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($complexVar: ComplexInput)
{
complicatedArgs {
complexArgField(complexArg: $ComplexInput)
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('ComplexInput => ComplexInput in field position')
*/
public function testComplexInputXComplexInputInFieldPosition() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($boolVar: Boolean = false)
{
complicatedArgs {
complexArgField(complexArg: {requiredArg: $boolVar})
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Boolean! => Boolean! in directive')
*/
public function testBooleanNonNullXBooleanNonNullInDirective() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($boolVar: Boolean!)
{
dog @include(if: $boolVar)
2015-07-15 20:05:46 +03:00
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Boolean => Boolean! in directive with default')
*/
public function testBooleanXBooleanNonNullInDirectiveWithDefault() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
query Query($boolVar: Boolean = false)
{
dog @include(if: $boolVar)
2015-07-15 20:05:46 +03:00
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Int => Int!')
*/
public function testIntXIntNonNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new VariablesInAllowedPosition(),
'
query Query($intArg: Int) {
2015-07-15 20:05:46 +03:00
complicatedArgs {
nonNullIntArgField(nonNullIntArg: $intArg)
}
}
2018-09-02 14:08:49 +03:00
',
[
FormattedError::create(
VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'),
[new SourceLocation(2, 19), new SourceLocation(4, 45)]
),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Int => Int! within fragment')
*/
public function testIntXIntNonNullWithinFragment() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
fragment nonNullIntArgFieldFrag on ComplicatedArgs {
nonNullIntArgField(nonNullIntArg: $intArg)
}
query Query($intArg: Int) {
2015-07-15 20:05:46 +03:00
complicatedArgs {
...nonNullIntArgFieldFrag
}
}
2018-09-02 14:08:49 +03:00
',
[
FormattedError::create(
VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'),
[new SourceLocation(6, 19), new SourceLocation(3, 43)]
),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Int => Int! within nested fragment')
*/
public function testIntXIntNonNullWithinNestedFragment() : void
2015-07-15 20:05:46 +03:00
{
// Int => Int! within nested fragment
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new VariablesInAllowedPosition(),
'
2015-07-15 20:05:46 +03:00
fragment outerFrag on ComplicatedArgs {
...nonNullIntArgFieldFrag
}
fragment nonNullIntArgFieldFrag on ComplicatedArgs {
nonNullIntArgField(nonNullIntArg: $intArg)
}
query Query($intArg: Int)
{
complicatedArgs {
...outerFrag
}
}
2018-09-02 14:08:49 +03:00
',
[
FormattedError::create(
VariablesInAllowedPosition::badVarPosMessage('intArg', 'Int', 'Int!'),
[new SourceLocation(10, 19), new SourceLocation(7, 43)]
),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('String over Boolean')
*/
public function testStringOverBoolean() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new VariablesInAllowedPosition(),
'
query Query($stringVar: String) {
2015-07-15 20:05:46 +03:00
complicatedArgs {
booleanArgField(booleanArg: $stringVar)
}
}
2018-09-02 14:08:49 +03:00
',
[
FormattedError::create(
VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', 'Boolean'),
[new SourceLocation(2, 19), new SourceLocation(4, 39)]
),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('String => [String]')
*/
public function testStringXListOfString() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new VariablesInAllowedPosition(),
'
query Query($stringVar: String) {
2015-07-15 20:05:46 +03:00
complicatedArgs {
stringListArgField(stringListArg: $stringVar)
}
}
2018-09-02 14:08:49 +03:00
',
[
FormattedError::create(
VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', '[String]'),
[new SourceLocation(2, 19), new SourceLocation(4, 45)]
),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('Boolean => Boolean! in directive')
*/
public function testBooleanXBooleanNonNullInDirective() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new VariablesInAllowedPosition(),
'
query Query($boolVar: Boolean) {
dog @include(if: $boolVar)
2015-07-15 20:05:46 +03:00
}
2018-09-02 14:08:49 +03:00
',
[
FormattedError::create(
VariablesInAllowedPosition::badVarPosMessage('boolVar', 'Boolean', 'Boolean!'),
[new SourceLocation(2, 19), new SourceLocation(3, 26)]
),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('String => Boolean! in directive')
*/
public function testStringXBooleanNonNullInDirective() : void
2015-07-15 20:05:46 +03:00
{
// String => Boolean! in directive
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new VariablesInAllowedPosition(),
'
query Query($stringVar: String) {
dog @include(if: $stringVar)
2015-07-15 20:05:46 +03:00
}
2018-09-02 14:08:49 +03:00
',
[
FormattedError::create(
VariablesInAllowedPosition::badVarPosMessage('stringVar', 'String', 'Boolean!'),
[new SourceLocation(2, 19), new SourceLocation(3, 26)]
),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('[String] => [String!]')
*/
public function testStringArrayXStringNonNullArray() : void
{
$this->expectFailsRule(
2018-09-02 14:08:49 +03:00
new VariablesInAllowedPosition(),
'
query Query($stringListVar: [String])
{
complicatedArgs {
stringListNonNullArgField(stringListNonNullArg: $stringListVar)
}
}
2018-09-02 14:08:49 +03:00
',
[
FormattedError::create(
VariablesInAllowedPosition::badVarPosMessage('stringListVar', '[String]', '[String!]'),
[new SourceLocation(2, 19), new SourceLocation(5, 59)]
2018-09-02 14:08:49 +03:00
),
]
);
}
2015-07-15 20:05:46 +03:00
}