2015-07-15 20:05:46 +03:00
|
|
|
<?php
|
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\NoUnusedVariables;
|
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
class NoUnusedVariablesTest extends ValidatorTestCase
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
// Validate: No unused variables
|
2016-04-25 16:29:17 +03:00
|
|
|
|
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('uses all variables')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testUsesAllVariables() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectPassesRule(new NoUnusedVariables(), '
|
|
|
|
query Foo($a: String, $b: String, $c: String) {
|
|
|
|
field(a: $a, b: $b, c: $c)
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('uses all variables deeply')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testUsesAllVariablesDeeply() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectPassesRule(new NoUnusedVariables, '
|
|
|
|
query Foo($a: String, $b: String, $c: String) {
|
|
|
|
field(a: $a) {
|
|
|
|
field(b: $b) {
|
|
|
|
field(c: $c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('uses all variables deeply in inline fragments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testUsesAllVariablesDeeplyInInlineFragments() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectPassesRule(new NoUnusedVariables, '
|
|
|
|
query Foo($a: String, $b: String, $c: String) {
|
|
|
|
... on Type {
|
|
|
|
field(a: $a) {
|
|
|
|
field(b: $b) {
|
|
|
|
... on Type {
|
|
|
|
field(c: $c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('uses all variables in fragments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testUsesAllVariablesInFragments() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectPassesRule(new NoUnusedVariables, '
|
|
|
|
query Foo($a: String, $b: String, $c: String) {
|
|
|
|
...FragA
|
|
|
|
}
|
|
|
|
fragment FragA on Type {
|
|
|
|
field(a: $a) {
|
|
|
|
...FragB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment FragB on Type {
|
|
|
|
field(b: $b) {
|
|
|
|
...FragC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment FragC on Type {
|
|
|
|
field(c: $c)
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variable used by fragment in multiple operations')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testVariableUsedByFragmentInMultipleOperations() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectPassesRule(new NoUnusedVariables, '
|
|
|
|
query Foo($a: String) {
|
|
|
|
...FragA
|
|
|
|
}
|
|
|
|
query Bar($b: String) {
|
|
|
|
...FragB
|
|
|
|
}
|
|
|
|
fragment FragA on Type {
|
|
|
|
field(a: $a)
|
|
|
|
}
|
|
|
|
fragment FragB on Type {
|
|
|
|
field(b: $b)
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variable used by recursive fragment')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testVariableUsedByRecursiveFragment() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectPassesRule(new NoUnusedVariables, '
|
|
|
|
query Foo($a: String) {
|
|
|
|
...FragA
|
|
|
|
}
|
|
|
|
fragment FragA on Type {
|
|
|
|
field(a: $a) {
|
|
|
|
...FragA
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variable not used')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testVariableNotUsed() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectFailsRule(new NoUnusedVariables, '
|
2016-04-25 16:29:17 +03:00
|
|
|
query ($a: String, $b: String, $c: String) {
|
2015-07-15 20:05:46 +03:00
|
|
|
field(a: $a, b: $b)
|
|
|
|
}
|
|
|
|
', [
|
2016-04-25 16:29:17 +03:00
|
|
|
$this->unusedVar('c', null, 2, 38)
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('multiple variables not used')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMultipleVariablesNotUsed() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectFailsRule(new NoUnusedVariables, '
|
|
|
|
query Foo($a: String, $b: String, $c: String) {
|
|
|
|
field(b: $b)
|
|
|
|
}
|
|
|
|
', [
|
2016-04-25 16:29:17 +03:00
|
|
|
$this->unusedVar('a', 'Foo', 2, 17),
|
|
|
|
$this->unusedVar('c', 'Foo', 2, 41)
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variable not used in fragments')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testVariableNotUsedInFragments() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectFailsRule(new NoUnusedVariables, '
|
|
|
|
query Foo($a: String, $b: String, $c: String) {
|
|
|
|
...FragA
|
|
|
|
}
|
|
|
|
fragment FragA on Type {
|
|
|
|
field(a: $a) {
|
|
|
|
...FragB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment FragB on Type {
|
|
|
|
field(b: $b) {
|
|
|
|
...FragC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment FragC on Type {
|
|
|
|
field
|
|
|
|
}
|
|
|
|
', [
|
2016-04-25 16:29:17 +03:00
|
|
|
$this->unusedVar('c', 'Foo', 2, 41)
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('multiple variables not used')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testMultipleVariablesNotUsed2() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectFailsRule(new NoUnusedVariables, '
|
|
|
|
query Foo($a: String, $b: String, $c: String) {
|
|
|
|
...FragA
|
|
|
|
}
|
|
|
|
fragment FragA on Type {
|
|
|
|
field {
|
|
|
|
...FragB
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment FragB on Type {
|
|
|
|
field(b: $b) {
|
|
|
|
...FragC
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fragment FragC on Type {
|
|
|
|
field
|
|
|
|
}
|
|
|
|
', [
|
2016-04-25 16:29:17 +03:00
|
|
|
$this->unusedVar('a', 'Foo', 2, 17),
|
|
|
|
$this->unusedVar('c', 'Foo', 2, 41)
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variable not used by unreferenced fragment')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testVariableNotUsedByUnreferencedFragment() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectFailsRule(new NoUnusedVariables, '
|
|
|
|
query Foo($b: String) {
|
|
|
|
...FragA
|
|
|
|
}
|
|
|
|
fragment FragA on Type {
|
|
|
|
field(a: $a)
|
|
|
|
}
|
|
|
|
fragment FragB on Type {
|
|
|
|
field(b: $b)
|
|
|
|
}
|
|
|
|
', [
|
2016-04-25 16:29:17 +03:00
|
|
|
$this->unusedVar('b', 'Foo', 2, 17)
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
/**
|
2018-08-31 11:55:14 +03:00
|
|
|
* @see it('variable not used by fragment used by other operation')
|
2016-04-25 16:29:17 +03:00
|
|
|
*/
|
2018-08-31 12:07:29 +03:00
|
|
|
public function testVariableNotUsedByFragmentUsedByOtherOperation() : void
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
|
|
|
$this->expectFailsRule(new NoUnusedVariables, '
|
|
|
|
query Foo($b: String) {
|
|
|
|
...FragA
|
|
|
|
}
|
|
|
|
query Bar($a: String) {
|
|
|
|
...FragB
|
|
|
|
}
|
|
|
|
fragment FragA on Type {
|
|
|
|
field(a: $a)
|
|
|
|
}
|
|
|
|
fragment FragB on Type {
|
|
|
|
field(b: $b)
|
|
|
|
}
|
|
|
|
', [
|
2016-04-25 16:29:17 +03:00
|
|
|
$this->unusedVar('b', 'Foo', 2, 17),
|
|
|
|
$this->unusedVar('a', 'Bar', 5, 17)
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:29:17 +03:00
|
|
|
private function unusedVar($varName, $opName, $line, $column)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
return FormattedError::create(
|
2016-04-25 16:29:17 +03:00
|
|
|
NoUnusedVariables::unusedVariableMessage($varName, $opName),
|
2015-07-15 20:05:46 +03:00
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|