graphql-php/tests/Validator/ProvidedNonNullArgumentsTest.php

339 lines
7.3 KiB
PHP
Raw Permalink Normal View History

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