2018-02-15 23:29:14 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Tests\Validator;
|
|
|
|
|
|
|
|
use GraphQL\Error\FormattedError;
|
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Validator\Rules\VariablesDefaultValueAllowed;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class VariablesDefaultValueAllowedTest extends ValidatorTestCase
|
2018-02-15 23:29:14 +03:00
|
|
|
{
|
|
|
|
private function defaultForRequiredVar($varName, $typeName, $guessTypeName, $line, $column)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
VariablesDefaultValueAllowed::defaultForRequiredVarMessage(
|
|
|
|
$varName,
|
|
|
|
$typeName,
|
|
|
|
$guessTypeName
|
|
|
|
),
|
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// DESCRIBE: Validate: Variable default value is allowed
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variables with no default values')
|
2018-02-15 23:29:14 +03:00
|
|
|
*/
|
|
|
|
public function testVariablesWithNoDefaultValues()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesDefaultValueAllowed(), '
|
|
|
|
query NullableValues($a: Int, $b: String, $c: ComplexInput) {
|
|
|
|
dog { name }
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('required variables without default values')
|
2018-02-15 23:29:14 +03:00
|
|
|
*/
|
|
|
|
public function testRequiredVariablesWithoutDefaultValues()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesDefaultValueAllowed(), '
|
|
|
|
query RequiredValues($a: Int!, $b: String!) {
|
|
|
|
dog { name }
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variables with valid default values')
|
2018-02-15 23:29:14 +03:00
|
|
|
*/
|
|
|
|
public function testVariablesWithValidDefaultValues()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesDefaultValueAllowed(), '
|
|
|
|
query WithDefaultValues(
|
|
|
|
$a: Int = 1,
|
|
|
|
$b: String = "ok",
|
|
|
|
$c: ComplexInput = { requiredField: true, intField: 3 }
|
|
|
|
) {
|
|
|
|
dog { name }
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variables with valid default null values')
|
2018-02-15 23:29:14 +03:00
|
|
|
*/
|
|
|
|
public function testVariablesWithValidDefaultNullValues()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new VariablesDefaultValueAllowed(), '
|
|
|
|
query WithDefaultValues(
|
|
|
|
$a: Int = null,
|
|
|
|
$b: String = null,
|
|
|
|
$c: ComplexInput = { requiredField: true, intField: null }
|
|
|
|
) {
|
|
|
|
dog { name }
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('no required variables with default values')
|
2018-02-15 23:29:14 +03:00
|
|
|
*/
|
|
|
|
public function testNoRequiredVariablesWithDefaultValues()
|
|
|
|
{
|
|
|
|
$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),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variables with invalid default null values')
|
2018-02-15 23:29:14 +03:00
|
|
|
*/
|
|
|
|
public function testNullIntoNullableType()
|
|
|
|
{
|
|
|
|
$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),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|