graphql-php/tests/Validator/KnownDirectivesTest.php

113 lines
2.6 KiB
PHP
Raw 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\FormattedError;
use GraphQL\Language\SourceLocation;
use GraphQL\Validator\Rules\KnownDirectives;
class KnownDirectivesTest extends TestCase
{
// Validate: Known directives
public function testWithNoDirectives()
{
$this->expectPassesRule(new KnownDirectives, '
query Foo {
name
...Frag
}
fragment Frag on Dog {
name
}
');
}
public function testWithKnownDirectives()
{
$this->expectPassesRule(new KnownDirectives, '
{
dog @include(if: true) {
2015-07-15 20:05:46 +03:00
name
}
human @skip(if: true) {
2015-07-15 20:05:46 +03:00
name
}
}
');
}
public function testWithUnknownDirective()
{
$this->expectFailsRule(new KnownDirectives, '
{
dog @unknown(directive: "value") {
2015-07-15 20:05:46 +03:00
name
}
}
', [
$this->unknownDirective('unknown', 3, 13)
]);
}
public function testWithManyUnknownDirectives()
{
$this->expectFailsRule(new KnownDirectives, '
{
dog @unknown(directive: "value") {
2015-07-15 20:05:46 +03:00
name
}
human @unknown(directive: "value") {
2015-07-15 20:05:46 +03:00
name
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)
]);
}
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)
}
');
}
2015-07-15 20:05:46 +03:00
public function testWithMisplacedDirectives()
{
$this->expectFailsRule(new KnownDirectives, '
query Foo @include(if: true) {
2015-07-15 20:05:46 +03:00
name
...Frag
}
', [
$this->misplacedDirective('include', 'operation', 2, 17)
2015-07-15 20:05:46 +03:00
]);
}
private function unknownDirective($directiveName, $line, $column)
{
return FormattedError::create(
KnownDirectives::unknownDirectiveMessage($directiveName),
2015-07-15 20:05:46 +03:00
[ new SourceLocation($line, $column) ]
);
}
function misplacedDirective($directiveName, $placement, $line, $column)
{
return FormattedError::create(
KnownDirectives::misplacedDirectiveMessage($directiveName, $placement),
2015-07-15 20:05:46 +03:00
[new SourceLocation($line, $column)]
);
}
}