graphql-php/tests/Validator/KnownArgumentNamesTest.php

216 lines
5.2 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\KnownArgumentNames;
2018-07-29 18:43:10 +03:00
class KnownArgumentNamesTest extends ValidatorTestCase
2015-07-15 20:05:46 +03:00
{
// Validate: Known argument names:
/**
* @it single arg is known
*/
2015-07-15 20:05:46 +03:00
public function testSingleArgIsKnown()
{
$this->expectPassesRule(new KnownArgumentNames, '
fragment argOnRequiredArg on Dog {
doesKnowCommand(dogCommand: SIT)
}
');
}
/**
* @it multiple args are known
*/
2015-07-15 20:05:46 +03:00
public function testMultipleArgsAreKnown()
{
$this->expectPassesRule(new KnownArgumentNames, '
fragment multipleArgs on ComplicatedArgs {
multipleReqs(req1: 1, req2: 2)
}
');
}
/**
* @it ignores args of unknown fields
*/
public function testIgnoresArgsOfUnknownFields()
{
$this->expectPassesRule(new KnownArgumentNames, '
fragment argOnUnknownField on Dog {
unknownField(unknownArg: SIT)
}
');
}
/**
* @it multiple args in reverse order are known
*/
2015-07-15 20:05:46 +03:00
public function testMultipleArgsInReverseOrderAreKnown()
{
$this->expectPassesRule(new KnownArgumentNames, '
fragment multipleArgsReverseOrder on ComplicatedArgs {
multipleReqs(req2: 2, req1: 1)
}
');
}
/**
* @it no args on optional arg
*/
2015-07-15 20:05:46 +03:00
public function testNoArgsOnOptionalArg()
{
$this->expectPassesRule(new KnownArgumentNames, '
fragment noArgOnOptionalArg on Dog {
isHousetrained
}
');
}
/**
* @it args are known deeply
*/
2015-07-15 20:05:46 +03:00
public function testArgsAreKnownDeeply()
{
$this->expectPassesRule(new KnownArgumentNames, '
{
dog {
doesKnowCommand(dogCommand: SIT)
}
human {
pet {
... on Dog {
doesKnowCommand(dogCommand: SIT)
}
}
}
}
');
}
/**
* @it directive args are known
*/
public function testDirectiveArgsAreKnown()
{
$this->expectPassesRule(new KnownArgumentNames, '
{
dog @skip(if: true)
}
');
}
/**
* @it undirective args are invalid
*/
public function testUndirectiveArgsAreInvalid()
{
$this->expectFailsRule(new KnownArgumentNames, '
{
dog @skip(unless: true)
}
', [
$this->unknownDirectiveArg('unless', 'skip', [], 3, 19),
]);
}
/**
* @it misspelled directive args are reported
*/
public function testMisspelledDirectiveArgsAreReported()
{
$this->expectFailsRule(new KnownArgumentNames, '
{
dog @skip(iff: true)
}
', [
$this->unknownDirectiveArg('iff', 'skip', ['if'], 3, 19),
]);
}
/**
* @it invalid arg name
*/
2015-07-15 20:05:46 +03:00
public function testInvalidArgName()
{
$this->expectFailsRule(new KnownArgumentNames, '
fragment invalidArgName on Dog {
doesKnowCommand(unknown: true)
}
', [
$this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [],3, 25),
]);
}
/**
* @it misspelled arg name is reported
*/
public function testMisspelledArgNameIsReported()
{
$this->expectFailsRule(new KnownArgumentNames, '
fragment invalidArgName on Dog {
doesKnowCommand(dogcommand: true)
}
', [
$this->unknownArg('dogcommand', 'doesKnowCommand', 'Dog', ['dogCommand'],3, 25),
2015-07-15 20:05:46 +03:00
]);
}
/**
* @it unknown args amongst known args
*/
2015-07-15 20:05:46 +03:00
public function testUnknownArgsAmongstKnownArgs()
{
$this->expectFailsRule(new KnownArgumentNames, '
fragment oneGoodArgOneInvalidArg on Dog {
doesKnowCommand(whoknows: 1, dogCommand: SIT, unknown: true)
}
', [
$this->unknownArg('whoknows', 'doesKnowCommand', 'Dog', [], 3, 25),
$this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 3, 55),
2015-07-15 20:05:46 +03:00
]);
}
/**
* @it unknown args deeply
*/
2015-07-15 20:05:46 +03:00
public function testUnknownArgsDeeply()
{
$this->expectFailsRule(new KnownArgumentNames, '
{
dog {
doesKnowCommand(unknown: true)
}
human {
pet {
... on Dog {
doesKnowCommand(unknown: true)
}
}
}
}
', [
$this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 4, 27),
$this->unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 9, 31),
2015-07-15 20:05:46 +03:00
]);
}
private function unknownArg($argName, $fieldName, $typeName, $suggestedArgs, $line, $column)
2015-07-15 20:05:46 +03:00
{
return FormattedError::create(
KnownArgumentNames::unknownArgMessage($argName, $fieldName, $typeName, $suggestedArgs),
[new SourceLocation($line, $column)]
);
2015-07-15 20:05:46 +03:00
}
private function unknownDirectiveArg($argName, $directiveName, $suggestedArgs, $line, $column)
2015-07-15 20:05:46 +03:00
{
return FormattedError::create(
KnownArgumentNames::unknownDirectiveArgMessage($argName, $directiveName, $suggestedArgs),
2015-07-15 20:05:46 +03:00
[new SourceLocation($line, $column)]
);
}
}