graphql-php/tests/Validator/NoUnusedVariablesTest.php

303 lines
6.4 KiB
PHP
Raw Normal View History

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
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
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String, $b: String, $c: String) {
field(a: $a, b: $b, c: $c)
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('uses all variables deeply')
*/
public function testUsesAllVariablesDeeply() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String, $b: String, $c: String) {
field(a: $a) {
field(b: $b) {
field(c: $c)
}
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('uses all variables deeply in inline fragments')
*/
public function testUsesAllVariablesDeeplyInInlineFragments() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String, $b: String, $c: String) {
... on Type {
field(a: $a) {
field(b: $b) {
... on Type {
field(c: $c)
}
}
}
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('uses all variables in fragments')
*/
public function testUsesAllVariablesInFragments() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
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)
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variable used by fragment in multiple operations')
*/
public function testVariableUsedByFragmentInMultipleOperations() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String) {
...FragA
}
query Bar($b: String) {
...FragB
}
fragment FragA on Type {
field(a: $a)
}
fragment FragB on Type {
field(b: $b)
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variable used by recursive fragment')
*/
public function testVariableUsedByRecursiveFragment() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String) {
...FragA
}
fragment FragA on Type {
field(a: $a) {
...FragA
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variable not used')
*/
public function testVariableNotUsed() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +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)
}
2018-09-02 14:08:49 +03:00
',
[
$this->unusedVar('c', null, 2, 38),
]
);
}
private function unusedVar($varName, $opName, $line, $column)
{
return FormattedError::create(
NoUnusedVariables::unusedVariableMessage($varName, $opName),
[new SourceLocation($line, $column)]
);
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
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String, $b: String, $c: String) {
field(b: $b)
}
2018-09-02 14:08:49 +03:00
',
[
$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
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
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
}
2018-09-02 14:08:49 +03:00
',
[
$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
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
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
}
2018-09-02 14:08:49 +03:00
',
[
$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
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($b: String) {
...FragA
}
fragment FragA on Type {
field(a: $a)
}
fragment FragB on Type {
field(b: $b)
}
2018-09-02 14:08:49 +03:00
',
[
$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
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUnusedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($b: String) {
...FragA
}
query Bar($a: String) {
...FragB
}
fragment FragA on Type {
field(a: $a)
}
fragment FragB on Type {
field(b: $b)
}
2018-09-02 14:08:49 +03:00
',
[
$this->unusedVar('b', 'Foo', 2, 17),
$this->unusedVar('a', 'Bar', 5, 17),
]
2015-07-15 20:05:46 +03:00
);
}
}