2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
2018-09-02 14:08:49 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-04-09 10:36:53 +03:00
|
|
|
namespace GraphQL\Tests\Validator;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\FormattedError;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Validator\Rules\NoUnusedFragments;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class NoUnusedFragmentsTest extends ValidatorTestCase
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
// Validate: No unused fragments
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('all fragment names are used')
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllFragmentNamesAreUsed() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new NoUnusedFragments(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
human(id: 4) {
|
|
|
|
...HumanFields1
|
|
|
|
... on Human {
|
|
|
|
...HumanFields2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment HumanFields1 on Human {
|
|
|
|
name
|
|
|
|
...HumanFields3
|
|
|
|
}
|
|
|
|
fragment HumanFields2 on Human {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
fragment HumanFields3 on Human {
|
|
|
|
name
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('all fragment names are used by multiple operations')
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testAllFragmentNamesAreUsedByMultipleOperations() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectPassesRule(
|
|
|
|
new NoUnusedFragments(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
query Foo {
|
|
|
|
human(id: 4) {
|
|
|
|
...HumanFields1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query Bar {
|
|
|
|
human(id: 4) {
|
|
|
|
...HumanFields2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment HumanFields1 on Human {
|
|
|
|
name
|
|
|
|
...HumanFields3
|
|
|
|
}
|
|
|
|
fragment HumanFields2 on Human {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
fragment HumanFields3 on Human {
|
|
|
|
name
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
'
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('contains unknown fragments')
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testContainsUnknownFragments() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new NoUnusedFragments(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
query Foo {
|
|
|
|
human(id: 4) {
|
|
|
|
...HumanFields1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query Bar {
|
|
|
|
human(id: 4) {
|
|
|
|
...HumanFields2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment HumanFields1 on Human {
|
|
|
|
name
|
|
|
|
...HumanFields3
|
|
|
|
}
|
|
|
|
fragment HumanFields2 on Human {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
fragment HumanFields3 on Human {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
fragment Unused1 on Human {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
fragment Unused2 on Human {
|
|
|
|
name
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[
|
|
|
|
$this->unusedFrag('Unused1', 22, 7),
|
|
|
|
$this->unusedFrag('Unused2', 25, 7),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function unusedFrag($fragName, $line, $column)
|
|
|
|
{
|
|
|
|
return FormattedError::create(
|
|
|
|
NoUnusedFragments::unusedFragMessage($fragName),
|
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('contains unknown fragments with ref cycle')
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testContainsUnknownFragmentsWithRefCycle() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new NoUnusedFragments(),
|
|
|
|
'
|
2015-07-15 20:05:46 +03:00
|
|
|
query Foo {
|
|
|
|
human(id: 4) {
|
|
|
|
...HumanFields1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
query Bar {
|
|
|
|
human(id: 4) {
|
|
|
|
...HumanFields2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment HumanFields1 on Human {
|
|
|
|
name
|
|
|
|
...HumanFields3
|
|
|
|
}
|
|
|
|
fragment HumanFields2 on Human {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
fragment HumanFields3 on Human {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
fragment Unused1 on Human {
|
|
|
|
name
|
|
|
|
...Unused2
|
|
|
|
}
|
|
|
|
fragment Unused2 on Human {
|
|
|
|
name
|
|
|
|
...Unused1
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[
|
|
|
|
$this->unusedFrag('Unused1', 22, 7),
|
|
|
|
$this->unusedFrag('Unused2', 26, 7),
|
|
|
|
]
|
|
|
|
);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('contains unknown and undef fragments')
|
2016-04-25 00:57:09 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testContainsUnknownAndUndefFragments() : void
|
2015-08-17 17:01:55 +03:00
|
|
|
{
|
2018-09-02 14:08:49 +03:00
|
|
|
$this->expectFailsRule(
|
|
|
|
new NoUnusedFragments(),
|
|
|
|
'
|
2015-08-17 17:01:55 +03:00
|
|
|
query Foo {
|
|
|
|
human(id: 4) {
|
|
|
|
...bar
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment foo on Human {
|
|
|
|
name
|
|
|
|
}
|
2018-09-02 14:08:49 +03:00
|
|
|
',
|
|
|
|
[
|
|
|
|
$this->unusedFrag('foo', 7, 7),
|
|
|
|
]
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|