2016-04-25 16:29:17 +03:00
|
|
|
<?php
|
2018-09-02 14:08:49 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
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\UniqueInputFieldNames;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class UniqueInputFieldNamesTest extends ValidatorTestCase
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
// Validate: Unique input field names
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('input object with fields')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testInputObjectWithFields() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueInputFieldNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
field(arg: { f: true })
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2016-04-25 16:29:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('same input object within two args')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testSameInputObjectWithinTwoArgs() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueInputFieldNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
field(arg1: { f: true }, arg2: { f: true })
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2016-04-25 16:29:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('multiple input object fields')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMultipleInputObjectFields() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueInputFieldNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
field(arg: { f1: "value", f2: "value", f3: "value" })
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2016-04-25 16:29:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('allows for nested input objects with similar fields')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllowsForNestedInputObjectsWithSimilarFields() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueInputFieldNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
field(arg: {
|
|
|
|
deep: {
|
|
|
|
deep: {
|
|
|
|
id: 1
|
|
|
|
}
|
|
|
|
id: 1
|
|
|
|
}
|
|
|
|
id: 1
|
|
|
|
})
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2016-04-25 16:29:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('duplicate input object fields')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testDuplicateInputObjectFields() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new UniqueInputFieldNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
field(arg: { f1: "value", f1: "value" })
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[$this->duplicateField('f1', 3, 22, 3, 35)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function duplicateField($name, $l1, $c1, $l2, $c2)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
UniqueInputFieldNames::duplicateInputFieldMessage($name),
|
|
|
|
[new SourceLocation($l1, $c1), new SourceLocation($l2, $c2)]
|
|
|
|
);
|
2016-04-25 16:29:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('many duplicate input object fields')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testManyDuplicateInputObjectFields() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new UniqueInputFieldNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
field(arg: { f1: "value", f1: "value", f1: "value" })
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[
|
|
|
|
$this->duplicateField('f1', 3, 22, 3, 35),
|
|
|
|
$this->duplicateField('f1', 3, 22, 3, 48),
|
|
|
|
]
|
2016-04-25 16:29:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|