2016-04-25 16:29:17 +03:00
|
|
|
<?php
|
|
|
|
namespace GraphQL\Tests\Validator;
|
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\FormattedError;
|
2016-04-25 16:29:17 +03:00
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Validator\Rules\UniqueArgumentNames;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class UniqueArgumentNamesTest extends ValidatorTestCase
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
// Validate: Unique argument names
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('no arguments on field')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testNoArgumentsOnField()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames(), '
|
|
|
|
{
|
|
|
|
field
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('no arguments on directive')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testNoArgumentsOnDirective()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field @directive
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('argument on field')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testArgumentOnField()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field(arg: "value")
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('argument on directive')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testArgumentOnDirective()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field @directive(arg: "value")
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('same argument on two fields')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testSameArgumentOnTwoFields()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
one: field(arg: "value")
|
|
|
|
two: field(arg: "value")
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('same argument on field and directive')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testSameArgumentOnFieldAndDirective()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field(arg: "value") @directive(arg: "value")
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('same argument on two directives')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testSameArgumentOnTwoDirectives()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field @directive1(arg: "value") @directive2(arg: "value")
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('multiple field arguments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testMultipleFieldArguments()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field(arg1: "value", arg2: "value", arg3: "value")
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('multiple directive arguments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testMultipleDirectiveArguments()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field @directive(arg1: "value", arg2: "value", arg3: "value")
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('duplicate field arguments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testDuplicateFieldArguments()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field(arg1: "value", arg1: "value")
|
|
|
|
}
|
|
|
|
', [
|
|
|
|
$this->duplicateArg('arg1', 3, 15, 3, 30)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('many duplicate field arguments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testManyDuplicateFieldArguments()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field(arg1: "value", arg1: "value", arg1: "value")
|
|
|
|
}
|
|
|
|
', [
|
|
|
|
$this->duplicateArg('arg1', 3, 15, 3, 30),
|
|
|
|
$this->duplicateArg('arg1', 3, 15, 3, 45)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('duplicate directive arguments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testDuplicateDirectiveArguments()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field @directive(arg1: "value", arg1: "value")
|
|
|
|
}
|
|
|
|
', [
|
|
|
|
$this->duplicateArg('arg1', 3, 26, 3, 41)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('many duplicate directive arguments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testManyDuplicateDirectiveArguments()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new UniqueArgumentNames, '
|
|
|
|
{
|
|
|
|
field @directive(arg1: "value", arg1: "value", arg1: "value")
|
|
|
|
}
|
|
|
|
', [
|
|
|
|
$this->duplicateArg('arg1', 3, 26, 3, 41),
|
|
|
|
$this->duplicateArg('arg1', 3, 26, 3, 56)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function duplicateArg($argName, $l1, $c1, $l2, $c2)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
UniqueArgumentNames::duplicateArgMessage($argName),
|
|
|
|
[new SourceLocation($l1, $c1), new SourceLocation($l2, $c2)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|