graphql-php/tests/Validator/UniqueArgumentNamesTest.php

228 lines
4.8 KiB
PHP
Raw Normal View History

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