graphql-php/tests/Validator/KnownFragmentNamesTest.php

81 lines
1.8 KiB
PHP
Raw Permalink Normal View History

2015-07-15 23:05:46 +06:00
<?php
2018-09-02 13:08:49 +02:00
declare(strict_types=1);
2016-04-09 09:36:53 +02:00
namespace GraphQL\Tests\Validator;
2015-07-15 23:05:46 +06:00
use GraphQL\Error\FormattedError;
2015-07-15 23:05:46 +06:00
use GraphQL\Language\SourceLocation;
2018-09-02 13:08:49 +02:00
use GraphQL\Validator\Rules\KnownFragmentNames;
2015-07-15 23:05:46 +06:00
2018-07-29 17:43:10 +02:00
class KnownFragmentNamesTest extends ValidatorTestCase
2015-07-15 23:05:46 +06:00
{
// Validate: Known fragment names
/**
* @see it('known fragment names are valid')
*/
public function testKnownFragmentNamesAreValid() : void
2015-07-15 23:05:46 +06:00
{
2018-09-02 13:08:49 +02:00
$this->expectPassesRule(
new KnownFragmentNames(),
'
2015-07-15 23:05:46 +06:00
{
human(id: 4) {
...HumanFields1
... on Human {
...HumanFields2
}
}
}
fragment HumanFields1 on Human {
name
...HumanFields3
}
fragment HumanFields2 on Human {
name
}
fragment HumanFields3 on Human {
name
}
2018-09-02 13:08:49 +02:00
'
);
2015-07-15 23:05:46 +06:00
}
/**
* @see it('unknown fragment names are invalid')
*/
public function testUnknownFragmentNamesAreInvalid() : void
2015-07-15 23:05:46 +06:00
{
2018-09-02 13:08:49 +02:00
$this->expectFailsRule(
new KnownFragmentNames(),
'
2015-07-15 23:05:46 +06:00
{
human(id: 4) {
...UnknownFragment1
... on Human {
...UnknownFragment2
}
}
}
fragment HumanFields on Human {
name
...UnknownFragment3
}
2018-09-02 13:08:49 +02:00
',
[
$this->undefFrag('UnknownFragment1', 4, 14),
$this->undefFrag('UnknownFragment2', 6, 16),
$this->undefFrag('UnknownFragment3', 12, 12),
]
);
2015-07-15 23:05:46 +06:00
}
private function undefFrag($fragName, $line, $column)
{
return FormattedError::create(
KnownFragmentNames::unknownFragmentMessage($fragName),
2015-07-15 23:05:46 +06:00
[new SourceLocation($line, $column)]
);
}
}