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\UniqueFragmentNames;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class UniqueFragmentNamesTest extends ValidatorTestCase
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
// Validate: Unique fragment names
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('no fragments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testNoFragments() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueFragmentNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
field
|
|
|
|
}
|
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('one fragment')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testOneFragment() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueFragmentNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
...fragA
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment fragA on Type {
|
|
|
|
field
|
|
|
|
}
|
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('many fragments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testManyFragments() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueFragmentNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
...fragA
|
|
|
|
...fragB
|
|
|
|
...fragC
|
|
|
|
}
|
|
|
|
fragment fragA on Type {
|
|
|
|
fieldA
|
|
|
|
}
|
|
|
|
fragment fragB on Type {
|
|
|
|
fieldB
|
|
|
|
}
|
|
|
|
fragment fragC on Type {
|
|
|
|
fieldC
|
|
|
|
}
|
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('inline fragments are always unique')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testInlineFragmentsAreAlwaysUnique() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueFragmentNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
...on Type {
|
|
|
|
fieldA
|
|
|
|
}
|
|
|
|
...on Type {
|
|
|
|
fieldB
|
|
|
|
}
|
|
|
|
}
|
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('fragment and operation named the same')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFragmentAndOperationNamedTheSame() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new UniqueFragmentNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
query Foo {
|
|
|
|
...Foo
|
|
|
|
}
|
|
|
|
fragment Foo on Type {
|
|
|
|
field
|
|
|
|
}
|
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('fragments named the same')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFragmentsNamedTheSame() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new UniqueFragmentNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
|
|
|
...fragA
|
|
|
|
}
|
|
|
|
fragment fragA on Type {
|
|
|
|
fieldA
|
|
|
|
}
|
|
|
|
fragment fragA on Type {
|
|
|
|
fieldB
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[$this->duplicateFrag('fragA', 5, 16, 8, 16)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function duplicateFrag($fragName, $l1, $c1, $l2, $c2)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
UniqueFragmentNames::duplicateFragmentNameMessage($fragName),
|
|
|
|
[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('fragments named the same without being referenced')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testFragmentsNamedTheSameWithoutBeingReferenced() : void
|
2016-04-25 16:29:17 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new UniqueFragmentNames(),
|
|
|
|
'
|
2016-04-25 16:29:17 +03:00
|
|
|
fragment fragA on Type {
|
|
|
|
fieldA
|
|
|
|
}
|
|
|
|
fragment fragA on Type {
|
|
|
|
fieldB
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[$this->duplicateFrag('fragA', 2, 16, 5, 16)]
|
2016-04-25 16:29:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|