graphql-php/tests/Validator/NoUnusedVariablesTest.php

259 lines
5.8 KiB
PHP
Raw Normal View History

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
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
/**
* @see it('uses all variables')
*/
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)
}
');
}
/**
* @see it('uses all variables deeply')
*/
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)
}
}
}
');
}
/**
* @see it('uses all variables deeply in inline fragments')
*/
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)
}
}
}
}
}
');
}
/**
* @see it('uses all variables in fragments')
*/
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)
}
');
}
/**
* @see it('variable used by fragment in multiple operations')
*/
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)
}
');
}
/**
* @see it('variable used by recursive fragment')
*/
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
}
}
');
}
/**
* @see it('variable not used')
*/
public function testVariableNotUsed() : void
2015-07-15 20:05:46 +03:00
{
$this->expectFailsRule(new NoUnusedVariables, '
query ($a: String, $b: String, $c: String) {
2015-07-15 20:05:46 +03:00
field(a: $a, b: $b)
}
', [
$this->unusedVar('c', null, 2, 38)
2015-07-15 20:05:46 +03:00
]);
}
/**
* @see it('multiple variables not used')
*/
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)
}
', [
$this->unusedVar('a', 'Foo', 2, 17),
$this->unusedVar('c', 'Foo', 2, 41)
2015-07-15 20:05:46 +03:00
]);
}
/**
* @see it('variable not used in fragments')
*/
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
}
', [
$this->unusedVar('c', 'Foo', 2, 41)
2015-07-15 20:05:46 +03:00
]);
}
/**
* @see it('multiple variables not used')
*/
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
}
', [
$this->unusedVar('a', 'Foo', 2, 17),
$this->unusedVar('c', 'Foo', 2, 41)
2015-07-15 20:05:46 +03:00
]);
}
/**
* @see it('variable not used by unreferenced fragment')
*/
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)
}
', [
$this->unusedVar('b', 'Foo', 2, 17)
2015-07-15 20:05:46 +03:00
]);
}
/**
* @see it('variable not used by fragment used by other operation')
*/
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)
}
', [
$this->unusedVar('b', 'Foo', 2, 17),
$this->unusedVar('a', 'Bar', 5, 17)
2015-07-15 20:05:46 +03:00
]);
}
private function unusedVar($varName, $opName, $line, $column)
2015-07-15 20:05:46 +03:00
{
return FormattedError::create(
NoUnusedVariables::unusedVariableMessage($varName, $opName),
2015-07-15 20:05:46 +03:00
[new SourceLocation($line, $column)]
);
}
}