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