graphql-php/tests/Validator/VariablesInAllowedPositionTest.php

372 lines
9.4 KiB
PHP
Raw Permalink 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;
class VariablesInAllowedPositionTest extends TestCase
{
// Validate: Variables are in allowed positions
/**
* @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)
}
}
');
}
/**
* @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)
}
');
}
/**
* @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)
}
}
');
}
/**
* @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
}
}
');
}
/**
* @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)
}
}
');
}
/**
* @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)
}
}
');
}
/**
* @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)
}
}
');
}
/**
* @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])
}
}
');
}
/**
* @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])
}
}
');
}
/**
* @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)
}
}
');
}
/**
* @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})
}
}
');
}
/**
* @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
}
');
}
/**
* @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
}
');
}
/**
* @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
)
]);
}
/**
* @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
)
]);
}
/**
* @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
)
]);
}
/**
* @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
)
]);
}
/**
* @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
)
]);
}
/**
* @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
)
]);
}
/**
* @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
)
]);
}
}