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;
|
|
|
|
|
|
|
|
class NoUnusedVariablesTest extends TestCase
|
|
|
|
{
|
|
|
|
// Validate: No unused variables
|
2016-04-25 16:29:17 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @it uses all variables
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testUsesAllVariables()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it uses all variables deeply
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testUsesAllVariablesDeeply()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it uses all variables deeply in inline fragments
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testUsesAllVariablesDeeplyInInlineFragments()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it uses all variables in fragments
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testUsesAllVariablesInFragments()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it variable used by fragment in multiple operations
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testVariableUsedByFragmentInMultipleOperations()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it variable used by recursive fragment
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testVariableUsedByRecursiveFragment()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it variable not used
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testVariableNotUsed()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it multiple variables not used
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testMultipleVariablesNotUsed()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it variable not used in fragments
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testVariableNotUsedInFragments()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it multiple variables not used
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testMultipleVariablesNotUsed2()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it variable not used by unreferenced fragment
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testVariableNotUsedByUnreferencedFragment()
|
|
|
|
{
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @it variable not used by fragment used by other operation
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testVariableNotUsedByFragmentUsedByOtherOperation()
|
|
|
|
{
|
|
|
|
$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)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|