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\UniqueVariableNames;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class UniqueVariableNamesTest extends ValidatorTestCase
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
// Validate: Unique variable names
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('unique variable names')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testUniqueVariableNames()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new UniqueVariableNames(), '
|
|
|
|
query A($x: Int, $y: String) { __typename }
|
|
|
|
query B($x: String, $y: Int) { __typename }
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('duplicate variable names')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
|
|
|
public function testDuplicateVariableNames()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new UniqueVariableNames, '
|
|
|
|
query A($x: Int, $x: Int, $x: String) { __typename }
|
|
|
|
query B($x: String, $x: Int) { __typename }
|
|
|
|
query C($x: Int, $x: Int) { __typename }
|
|
|
|
', [
|
|
|
|
$this->duplicateVariable('x', 2, 16, 2, 25),
|
|
|
|
$this->duplicateVariable('x', 2, 16, 2, 34),
|
|
|
|
$this->duplicateVariable('x', 3, 16, 3, 28),
|
|
|
|
$this->duplicateVariable('x', 4, 16, 4, 25)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function duplicateVariable($name, $l1, $c1, $l2, $c2)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
UniqueVariableNames::duplicateVariableMessage($name),
|
|
|
|
[new SourceLocation($l1, $c1), new SourceLocation($l2, $c2)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|