graphql-php/tests/Validator/NoUnusedVariablesTest.php

259 lines
5.7 KiB
PHP
Raw Permalink 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;
class NoUnusedVariablesTest extends TestCase
{
// Validate: No unused variables
/**
* @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)
}
');
}
/**
* @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)
}
}
}
');
}
/**
* @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)
}
}
}
}
}
');
}
/**
* @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)
}
');
}
/**
* @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)
}
');
}
/**
* @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
}
}
');
}
/**
* @it variable not used
*/
2015-07-15 20:05:46 +03:00
public function testVariableNotUsed()
{
$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
]);
}
/**
* @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)
}
', [
$this->unusedVar('a', 'Foo', 2, 17),
$this->unusedVar('c', 'Foo', 2, 41)
2015-07-15 20:05:46 +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
}
', [
$this->unusedVar('c', 'Foo', 2, 41)
2015-07-15 20:05:46 +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
}
', [
$this->unusedVar('a', 'Foo', 2, 17),
$this->unusedVar('c', 'Foo', 2, 41)
2015-07-15 20:05:46 +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)
}
', [
$this->unusedVar('b', 'Foo', 2, 17)
2015-07-15 20:05:46 +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)
}
', [
$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)]
);
}
}