2015-08-17 17:01:55 +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-08-17 17:01:55 +03:00
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\FormattedError;
|
2015-08-17 17:01:55 +03:00
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Validator\Rules\ProvidedNonNullArguments;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class ProvidedNonNullArgumentsTest extends ValidatorTestCase
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
// Validate: Provided required arguments
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('ignores unknown arguments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testIgnoresUnknownArguments() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
// ignores unknown arguments
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
dog {
|
|
|
|
isHousetrained(unknownArgument: true)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Valid non-nullable value:
|
2016-04-25 16:29:17 +03:00
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Arg on optional arg')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testArgOnOptionalArg() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
dog {
|
|
|
|
isHousetrained(atOtherHomes: true)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('No Arg on optional arg')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testNoArgOnOptionalArg() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
dog {
|
|
|
|
isHousetrained
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2016-04-25 16:29:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Multiple args')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMultipleArgs() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleReqs(req1: 1, req2: 2)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Multiple args reverse order')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMultipleArgsReverseOrder() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleReqs(req2: 2, req1: 1)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('No args on multiple optional')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testNoArgsOnMultipleOptional() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleOpts
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('One arg on multiple optional')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testOneArgOnMultipleOptional() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleOpts(opt1: 1)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Second arg on multiple optional')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testSecondArgOnMultipleOptional() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleOpts(opt2: 1)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Multiple reqs on mixedList')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMultipleReqsOnMixedList() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleOptAndReq(req1: 3, req2: 4)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Multiple reqs and one opt on mixedList')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMultipleReqsAndOneOptOnMixedList() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleOptAndReq(req1: 3, req2: 4, opt1: 5)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('All reqs and opts on mixedList')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllReqsAndOptsOnMixedList() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleOptAndReq(req1: 3, req2: 4, opt1: 5, opt2: 6)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid non-nullable value
|
2016-04-25 16:29:17 +03:00
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Missing one non-nullable argument')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMissingOneNonNullableArgument() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleReqs(req2: 2)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[$this->missingFieldArg('multipleReqs', 'req1', 'Int!', 4, 13)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function missingFieldArg($fieldName, $argName, $typeName, $line, $column)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
ProvidedNonNullArguments::missingFieldArgMessage($fieldName, $argName, $typeName),
|
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Missing multiple non-nullable arguments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMissingMultipleNonNullableArguments() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleReqs
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[
|
|
|
|
$this->missingFieldArg('multipleReqs', 'req1', 'Int!', 4, 13),
|
|
|
|
$this->missingFieldArg('multipleReqs', 'req2', 'Int!', 4, 13),
|
|
|
|
]
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2018-09-02 14:08:49 +03:00
|
|
|
// Describe: Directive arguments
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('Incorrect value and missing argument')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testIncorrectValueAndMissingArgument() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
complicatedArgs {
|
|
|
|
multipleReqs(req1: "one")
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[
|
|
|
|
$this->missingFieldArg('multipleReqs', 'req2', 'Int!', 4, 13),
|
|
|
|
]
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('ignores unknown directives')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testIgnoresUnknownDirectives() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
dog @unknown
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('with directives of valid types')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testWithDirectivesOfValidTypes() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
dog @include(if: true) {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
human @skip(if: false) {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-08-17 17:01:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('with directive with missing types')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testWithDirectiveWithMissingTypes() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new ProvidedNonNullArguments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
|
|
|
dog @include {
|
|
|
|
name @skip
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[
|
|
|
|
$this->missingDirectiveArg('include', 'if', 'Boolean!', 3, 15),
|
|
|
|
$this->missingDirectiveArg('skip', 'if', 'Boolean!', 4, 18),
|
|
|
|
]
|
2015-08-17 17:01:55 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function missingDirectiveArg($directiveName, $argName, $typeName, $line, $column)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
ProvidedNonNullArguments::missingDirectiveArgMessage($directiveName, $argName, $typeName),
|
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|