graphql-php/tests/Validator/UniqueFragmentNamesTest.php

161 lines
3.1 KiB
PHP
Raw Permalink 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\UniqueFragmentNames;
2018-07-29 18:43:10 +03:00
class UniqueFragmentNamesTest extends ValidatorTestCase
{
// Validate: Unique fragment names
/**
* @see it('no fragments')
*/
public function testNoFragments() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueFragmentNames(),
'
{
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('one fragment')
*/
public function testOneFragment() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueFragmentNames(),
'
{
...fragA
}
fragment fragA on Type {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('many fragments')
*/
public function testManyFragments() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueFragmentNames(),
'
{
...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
'
);
}
/**
* @see it('inline fragments are always unique')
*/
public function testInlineFragmentsAreAlwaysUnique() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueFragmentNames(),
'
{
...on Type {
fieldA
}
...on Type {
fieldB
}
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('fragment and operation named the same')
*/
public function testFragmentAndOperationNamedTheSame() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new UniqueFragmentNames(),
'
query Foo {
...Foo
}
fragment Foo on Type {
field
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('fragments named the same')
*/
public function testFragmentsNamedTheSame() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new UniqueFragmentNames(),
'
{
...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)]
);
}
/**
* @see it('fragments named the same without being referenced')
*/
public function testFragmentsNamedTheSameWithoutBeingReferenced() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new UniqueFragmentNames(),
'
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)]
);
}
}