graphql-php/tests/Validator/VariablesInAllowedPositionTest.php

394 lines
10 KiB
PHP
Raw Normal View History

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
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')
*/
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)
}
}
');
}
/**
* @see 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)
}
');
}
/**
* @see 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)
}
}
');
}
/**
* @see 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
}
}
');
}
/**
* @see 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)
}
}
');
}
/**
* @see 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)
}
}
');
}
/**
* @see 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)
}
}
');
}
/**
* @see 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])
}
}
');
}
/**
* @see 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])
}
}
');
}
/**
* @see 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)
}
}
');
}
/**
* @see 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})
}
}
');
}
/**
* @see it('Boolean! => Boolean! in directive')
*/
2015-07-15 20:05:46 +03:00
public function testBooleanNonNullXBooleanNonNullInDirective()
{
$this->expectPassesRule(new VariablesInAllowedPosition, '
query Query($boolVar: Boolean!)
{
dog @include(if: $boolVar)
2015-07-15 20:05:46 +03:00
}
');
}
/**
* @see 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)
{
dog @include(if: $boolVar)
2015-07-15 20:05:46 +03:00
}
');
}
/**
* @see it('Int => Int!')
*/
2015-07-15 20:05:46 +03:00
public function testIntXIntNonNull()
{
$this->expectFailsRule(new VariablesInAllowedPosition, '
query Query($intArg: Int) {
2015-07-15 20:05:46 +03:00
complicatedArgs {
nonNullIntArgField(nonNullIntArg: $intArg)
}
}
', [
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')
*/
2015-07-15 20:05:46 +03:00
public function testIntXIntNonNullWithinFragment()
{
$this->expectFailsRule(new VariablesInAllowedPosition, '
fragment nonNullIntArgFieldFrag on ComplicatedArgs {
nonNullIntArgField(nonNullIntArg: $intArg)
}
query Query($intArg: Int) {
2015-07-15 20:05:46 +03:00
complicatedArgs {
...nonNullIntArgFieldFrag
}
}
', [
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')
*/
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
}
}
', [
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')
*/
2015-07-15 20:05:46 +03:00
public function testStringOverBoolean()
{
$this->expectFailsRule(new VariablesInAllowedPosition, '
query Query($stringVar: String) {
2015-07-15 20:05:46 +03:00
complicatedArgs {
booleanArgField(booleanArg: $stringVar)
}
}
', [
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]')
*/
2015-07-15 20:05:46 +03:00
public function testStringXListOfString()
{
$this->expectFailsRule(new VariablesInAllowedPosition, '
query Query($stringVar: String) {
2015-07-15 20:05:46 +03:00
complicatedArgs {
stringListArgField(stringListArg: $stringVar)
}
}
', [
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')
*/
2015-07-15 20:05:46 +03:00
public function testBooleanXBooleanNonNullInDirective()
{
$this->expectFailsRule(new VariablesInAllowedPosition, '
query Query($boolVar: Boolean) {
dog @include(if: $boolVar)
2015-07-15 20:05:46 +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')
*/
2015-07-15 20:05:46 +03:00
public function testStringXBooleanNonNullInDirective()
{
// String => Boolean! in directive
$this->expectFailsRule(new VariablesInAllowedPosition, '
query Query($stringVar: String) {
dog @include(if: $stringVar)
2015-07-15 20:05:46 +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()
{
$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
}