graphql-php/tests/Validator/KnownDirectivesTest.php

263 lines
6.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\KnownDirectives;
2018-07-29 18:43:10 +03:00
class KnownDirectivesTest extends ValidatorTestCase
2015-07-15 20:05:46 +03:00
{
// Validate: Known directives
/**
* @see it('with no directives')
*/
public function testWithNoDirectives() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new KnownDirectives(),
'
2015-07-15 20:05:46 +03:00
query Foo {
name
...Frag
}
fragment Frag on Dog {
name
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('with known directives')
*/
public function testWithKnownDirectives() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new KnownDirectives(),
'
2015-07-15 20:05:46 +03:00
{
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
}
}
2018-09-02 14:08:49 +03:00
'
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('with unknown directive')
*/
public function testWithUnknownDirective() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new KnownDirectives(),
'
2015-07-15 20:05:46 +03:00
{
dog @unknown(directive: "value") {
2015-07-15 20:05:46 +03:00
name
}
}
2018-09-02 14:08:49 +03:00
',
[$this->unknownDirective('unknown', 3, 13)]
);
}
private function unknownDirective($directiveName, $line, $column)
{
return FormattedError::create(
KnownDirectives::unknownDirectiveMessage($directiveName),
[new SourceLocation($line, $column)]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('with many unknown directives')
*/
public function testWithManyUnknownDirectives() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new KnownDirectives(),
'
2015-07-15 20:05:46 +03:00
{
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
}
}
}
2018-09-02 14:08:49 +03:00
',
[
$this->unknownDirective('unknown', 3, 13),
$this->unknownDirective('unknown', 6, 15),
$this->unknownDirective('unknown', 8, 16),
]
);
2015-07-15 20:05:46 +03:00
}
/**
* @see it('with well placed directives')
*/
public function testWithWellPlacedDirectives() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new KnownDirectives(),
'
query Foo @onQuery {
name @include(if: true)
...Frag @include(if: true)
skippedField @skip(if: true)
...SkippedFrag @skip(if: true)
}
mutation Bar @onMutation {
someField
}
2018-09-02 14:08:49 +03:00
'
);
}
2018-09-02 14:08:49 +03:00
// within schema language
/**
* @see it('with misplaced directives')
*/
public function testWithMisplacedDirectives() : void
2015-07-15 20:05:46 +03:00
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new KnownDirectives(),
'
query Foo @include(if: true) {
name @onQuery
...Frag @onQuery
}
mutation Bar @onQuery {
someField
2015-07-15 20:05:46 +03:00
}
2018-09-02 14:08:49 +03:00
',
[
$this->misplacedDirective('include', 'QUERY', 2, 17),
$this->misplacedDirective('onQuery', 'FIELD', 3, 14),
$this->misplacedDirective('onQuery', 'FRAGMENT_SPREAD', 4, 17),
$this->misplacedDirective('onQuery', 'MUTATION', 7, 20),
]
);
2015-07-15 20:05:46 +03:00
}
2018-09-02 14:08:49 +03:00
private function misplacedDirective($directiveName, $placement, $line, $column)
{
return FormattedError::create(
KnownDirectives::misplacedDirectiveMessage($directiveName, $placement),
[new SourceLocation($line, $column)]
);
}
/**
* @see it('with well placed directives')
*/
public function testWSLWithWellPlacedDirectives() : void
{
2018-09-02 14:08:49 +03:00
$this->expectPassesRule(
new KnownDirectives(),
'
type MyObj implements MyInterface @onObject {
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
}
extend type MyObj @onObject
scalar MyScalar @onScalar
extend scalar MyScalar @onScalar
interface MyInterface @onInterface {
myField(myArg: Int @onArgumentDefinition): String @onFieldDefinition
}
extend interface MyInterface @onInterface
union MyUnion @onUnion = MyObj | Other
extend union MyUnion @onUnion
enum MyEnum @onEnum {
MY_VALUE @onEnumValue
}
extend enum MyEnum @onEnum
input MyInput @onInputObject {
myField: Int @onInputFieldDefinition
}
extend input MyInput @onInputObject
schema @onSchema {
query: MyQuery
}
2018-09-02 14:08:49 +03:00
'
);
}
/**
* @see it('with misplaced directives')
*/
public function testWSLWithMisplacedDirectives() : void
{
2018-09-02 14:08:49 +03:00
$this->expectFailsRule(
new KnownDirectives(),
'
type MyObj implements MyInterface @onInterface {
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
}
scalar MyScalar @onEnum
interface MyInterface @onObject {
myField(myArg: Int @onInputFieldDefinition): String @onInputFieldDefinition
}
union MyUnion @onEnumValue = MyObj | Other
enum MyEnum @onScalar {
MY_VALUE @onUnion
}
input MyInput @onEnum {
myField: Int @onArgumentDefinition
}
schema @onObject {
query: MyQuery
}
',
[
$this->misplacedDirective('onInterface', 'OBJECT', 2, 43),
$this->misplacedDirective('onInputFieldDefinition', 'ARGUMENT_DEFINITION', 3, 30),
$this->misplacedDirective('onInputFieldDefinition', 'FIELD_DEFINITION', 3, 63),
$this->misplacedDirective('onEnum', 'SCALAR', 6, 25),
$this->misplacedDirective('onObject', 'INTERFACE', 8, 31),
$this->misplacedDirective('onInputFieldDefinition', 'ARGUMENT_DEFINITION', 9, 30),
$this->misplacedDirective('onInputFieldDefinition', 'FIELD_DEFINITION', 9, 63),
$this->misplacedDirective('onEnumValue', 'UNION', 12, 23),
$this->misplacedDirective('onScalar', 'ENUM', 14, 21),
$this->misplacedDirective('onUnion', 'ENUM_VALUE', 15, 20),
$this->misplacedDirective('onEnum', 'INPUT_OBJECT', 18, 23),
$this->misplacedDirective('onArgumentDefinition', 'INPUT_FIELD_DEFINITION', 19, 24),
$this->misplacedDirective('onObject', 'SCHEMA', 22, 16),
]
);
}
2015-07-15 20:05:46 +03:00
}