graphql-php/tests/Validator/NoUndefinedVariablesTest.php

430 lines
9.7 KiB
PHP
Raw Permalink 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\NoUndefinedVariables;
2018-07-29 18:43:10 +03:00
class NoUndefinedVariablesTest extends ValidatorTestCase
2015-07-15 20:05:46 +03:00
{
// Validate: No undefined variables
/**
* @see it('all variables defined')
*/
public function testAllVariablesDefined() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUndefinedVariables(),
'
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('all variables deeply defined')
*/
public function testAllVariablesDeeplyDefined() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUndefinedVariables(),
'
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('all variables deeply in inline fragments defined')
*/
public function testAllVariablesDeeplyInInlineFragmentsDefined() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUndefinedVariables(),
'
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('all variables in fragments deeply defined')
*/
public function testAllVariablesInFragmentsDeeplyDefined() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUndefinedVariables(),
'
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 within single fragment defined in multiple operations')
*/
public function testVariableWithinSingleFragmentDefinedInMultipleOperations() : void
2015-07-15 20:05:46 +03:00
{
// variable within single fragment defined in multiple operations
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String) {
...FragA
}
query Bar($a: String) {
...FragA
}
fragment FragA on Type {
field(a: $a)
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variable within fragments defined in operations')
*/
public function testVariableWithinFragmentsDefinedInOperations() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUndefinedVariables(),
'
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 within recursive fragment defined')
*/
public function testVariableWithinRecursiveFragmentDefined() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new NoUndefinedVariables(),
'
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 defined')
*/
public function testVariableNotDefined() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String, $b: String, $c: String) {
field(a: $a, b: $b, c: $c, d: $d)
}
2018-09-02 14:08:49 +03:00
',
[
$this->undefVar('d', 3, 39, 'Foo', 2, 7),
]
);
}
private function undefVar($varName, $line, $column, $opName = null, $l2 = null, $c2 = null)
{
$locs = [new SourceLocation($line, $column)];
if ($l2 && $c2) {
$locs[] = new SourceLocation($l2, $c2);
}
return FormattedError::create(
NoUndefinedVariables::undefinedVarMessage($varName, $opName),
$locs
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variable not defined by un-named query')
*/
public function testVariableNotDefinedByUnNamedQuery() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
{
field(a: $a)
}
2018-09-02 14:08:49 +03:00
',
[
$this->undefVar('a', 3, 18, '', 2, 7),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('multiple variables not defined')
*/
public function testMultipleVariablesNotDefined() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($b: String) {
field(a: $a, b: $b, c: $c)
}
2018-09-02 14:08:49 +03:00
',
[
$this->undefVar('a', 3, 18, 'Foo', 2, 7),
$this->undefVar('c', 3, 32, 'Foo', 2, 7),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variable in fragment not defined by un-named query')
*/
public function testVariableInFragmentNotDefinedByUnNamedQuery() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
{
...FragA
}
fragment FragA on Type {
field(a: $a)
}
2018-09-02 14:08:49 +03:00
',
[
$this->undefVar('a', 6, 18, '', 2, 7),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variable in fragment not defined by operation')
*/
public function testVariableInFragmentNotDefinedByOperation() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String, $b: 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
',
[
$this->undefVar('c', 16, 18, 'Foo', 2, 7),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('multiple variables in fragments not defined')
*/
public function testMultipleVariablesInFragmentsNotDefined() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($b: 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
',
[
$this->undefVar('a', 6, 18, 'Foo', 2, 7),
$this->undefVar('c', 16, 18, 'Foo', 2, 7),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('single variable in fragment not defined by multiple operations')
*/
public function testSingleVariableInFragmentNotDefinedByMultipleOperations() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($a: String) {
...FragAB
}
query Bar($a: String) {
...FragAB
}
fragment FragAB on Type {
field(a: $a, b: $b)
}
2018-09-02 14:08:49 +03:00
',
[
$this->undefVar('b', 9, 25, 'Foo', 2, 7),
$this->undefVar('b', 9, 25, 'Bar', 5, 7),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variables in fragment not defined by multiple operations')
*/
public function testVariablesInFragmentNotDefinedByMultipleOperations() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($b: String) {
...FragAB
}
query Bar($a: String) {
...FragAB
}
fragment FragAB on Type {
field(a: $a, b: $b)
}
2018-09-02 14:08:49 +03:00
',
[
$this->undefVar('a', 9, 18, 'Foo', 2, 7),
$this->undefVar('b', 9, 25, 'Bar', 5, 7),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('variable in fragment used by other operation')
*/
public function testVariableInFragmentUsedByOtherOperation() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
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->undefVar('a', 9, 18, 'Foo', 2, 7),
$this->undefVar('b', 12, 18, 'Bar', 5, 7),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('multiple undefined variables produce multiple errors')
*/
public function testMultipleUndefinedVariablesProduceMultipleErrors() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new NoUndefinedVariables(),
'
2015-07-15 20:05:46 +03:00
query Foo($b: String) {
...FragAB
}
query Bar($a: String) {
...FragAB
}
fragment FragAB on Type {
field1(a: $a, b: $b)
...FragC
field3(a: $a, b: $b)
}
fragment FragC on Type {
field2(c: $c)
}
2018-09-02 14:08:49 +03:00
',
[
$this->undefVar('a', 9, 19, 'Foo', 2, 7),
$this->undefVar('a', 11, 19, 'Foo', 2, 7),
$this->undefVar('c', 14, 19, 'Foo', 2, 7),
$this->undefVar('b', 9, 26, 'Bar', 5, 7),
$this->undefVar('b', 11, 26, 'Bar', 5, 7),
$this->undefVar('c', 14, 19, 'Bar', 5, 7),
]
2015-07-15 20:05:46 +03:00
);
}
}