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
|
|
|
|
2016-10-21 12:39:57 +03:00
|
|
|
use GraphQL\Error\FormattedError;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\SourceLocation;
|
|
|
|
use GraphQL\Validator\Rules\KnownDirectives;
|
|
|
|
|
|
|
|
class KnownDirectivesTest extends TestCase
|
|
|
|
{
|
|
|
|
// Validate: Known directives
|
2016-04-25 00:57:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @it with no directives
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testWithNoDirectives()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new KnownDirectives, '
|
|
|
|
query Foo {
|
|
|
|
name
|
|
|
|
...Frag
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment Frag on Dog {
|
|
|
|
name
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @it with known directives
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testWithKnownDirectives()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new KnownDirectives, '
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
dog @include(if: true) {
|
2015-07-15 20:05:46 +03:00
|
|
|
name
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
human @skip(if: true) {
|
2015-07-15 20:05:46 +03:00
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @it with unknown directive
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testWithUnknownDirective()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new KnownDirectives, '
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
dog @unknown(directive: "value") {
|
2015-07-15 20:05:46 +03:00
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
', [
|
|
|
|
$this->unknownDirective('unknown', 3, 13)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @it with many unknown directives
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testWithManyUnknownDirectives()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new KnownDirectives, '
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
dog @unknown(directive: "value") {
|
2015-07-15 20:05:46 +03:00
|
|
|
name
|
|
|
|
}
|
2015-08-17 17:01:55 +03:00
|
|
|
human @unknown(directive: "value") {
|
2015-07-15 20:05:46 +03:00
|
|
|
name
|
2015-08-17 17:01:55 +03:00
|
|
|
pets @unknown(directive: "value") {
|
2015-07-15 20:05:46 +03:00
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
', [
|
|
|
|
$this->unknownDirective('unknown', 3, 13),
|
|
|
|
$this->unknownDirective('unknown', 6, 15),
|
|
|
|
$this->unknownDirective('unknown', 8, 16)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @it with well placed directives
|
|
|
|
*/
|
2015-08-17 17:01:55 +03:00
|
|
|
public function testWithWellPlacedDirectives()
|
|
|
|
{
|
|
|
|
$this->expectPassesRule(new KnownDirectives, '
|
|
|
|
query Foo {
|
|
|
|
name @include(if: true)
|
|
|
|
...Frag @include(if: true)
|
|
|
|
skippedField @skip(if: true)
|
|
|
|
...SkippedFrag @skip(if: true)
|
|
|
|
}
|
|
|
|
');
|
|
|
|
}
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
/**
|
|
|
|
* @it with misplaced directives
|
|
|
|
*/
|
2015-07-15 20:05:46 +03:00
|
|
|
public function testWithMisplacedDirectives()
|
|
|
|
{
|
|
|
|
$this->expectFailsRule(new KnownDirectives, '
|
2015-08-17 17:01:55 +03:00
|
|
|
query Foo @include(if: true) {
|
2016-04-25 00:57:09 +03:00
|
|
|
name @operationOnly
|
|
|
|
...Frag @operationOnly
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
', [
|
2016-04-25 00:57:09 +03:00
|
|
|
$this->misplacedDirective('include', 'QUERY', 2, 17),
|
|
|
|
$this->misplacedDirective('operationOnly', 'FIELD', 3, 14),
|
|
|
|
$this->misplacedDirective('operationOnly', 'FRAGMENT_SPREAD', 4, 17),
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function unknownDirective($directiveName, $line, $column)
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
return FormattedError::create(
|
|
|
|
KnownDirectives::unknownDirectiveMessage($directiveName),
|
2015-07-15 20:05:46 +03:00
|
|
|
[ new SourceLocation($line, $column) ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function misplacedDirective($directiveName, $placement, $line, $column)
|
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
return FormattedError::create(
|
|
|
|
KnownDirectives::misplacedDirectiveMessage($directiveName, $placement),
|
2015-07-15 20:05:46 +03:00
|
|
|
[new SourceLocation($line, $column)]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|