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-18 21:34:46 +03:00
|
|
|
use GraphQL\GraphQL;
|
2016-11-11 01:20:19 +03:00
|
|
|
use GraphQL\Language\Lexer;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Language\Parser;
|
|
|
|
use GraphQL\Schema;
|
2016-04-25 00:57:09 +03:00
|
|
|
use GraphQL\Type\Definition\Directive;
|
2015-07-15 20:05:46 +03:00
|
|
|
use GraphQL\Type\Definition\EnumType;
|
|
|
|
use GraphQL\Type\Definition\InputObjectType;
|
|
|
|
use GraphQL\Type\Definition\InterfaceType;
|
|
|
|
use GraphQL\Type\Definition\ListOfType;
|
|
|
|
use GraphQL\Type\Definition\ObjectType;
|
|
|
|
use GraphQL\Type\Definition\Type;
|
|
|
|
use GraphQL\Type\Definition\UnionType;
|
2016-04-09 10:36:53 +03:00
|
|
|
use GraphQL\Validator\DocumentValidator;
|
2015-07-15 20:05:46 +03:00
|
|
|
|
|
|
|
abstract class TestCase extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return Schema
|
|
|
|
*/
|
2016-04-30 17:39:06 +03:00
|
|
|
public static function getDefaultSchema()
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2015-08-17 17:01:55 +03:00
|
|
|
$FurColor = null;
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
$Being = new InterfaceType([
|
|
|
|
'name' => 'Being',
|
|
|
|
'fields' => [
|
2015-08-17 17:01:55 +03:00
|
|
|
'name' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
|
|
|
|
]
|
2015-07-15 20:05:46 +03:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$Pet = new InterfaceType([
|
|
|
|
'name' => 'Pet',
|
|
|
|
'fields' => [
|
2015-08-17 17:01:55 +03:00
|
|
|
'name' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
|
|
|
|
]
|
2015-07-15 20:05:46 +03:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
$Canine = new InterfaceType([
|
|
|
|
'name' => 'Canine',
|
|
|
|
'fields' => function() {
|
|
|
|
return [
|
|
|
|
'name' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => ['surname' => ['type' => Type::boolean()]]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
$DogCommand = new EnumType([
|
|
|
|
'name' => 'DogCommand',
|
|
|
|
'values' => [
|
|
|
|
'SIT' => ['value' => 0],
|
|
|
|
'HEEL' => ['value' => 1],
|
2016-04-25 00:57:09 +03:00
|
|
|
'DOWN' => ['value' => 2]
|
2015-07-15 20:05:46 +03:00
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$Dog = new ObjectType([
|
|
|
|
'name' => 'Dog',
|
|
|
|
'fields' => [
|
2015-08-17 17:01:55 +03:00
|
|
|
'name' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
|
|
|
|
],
|
2015-07-15 20:05:46 +03:00
|
|
|
'nickname' => ['type' => Type::string()],
|
|
|
|
'barkVolume' => ['type' => Type::int()],
|
|
|
|
'barks' => ['type' => Type::boolean()],
|
|
|
|
'doesKnowCommand' => [
|
|
|
|
'type' => Type::boolean(),
|
|
|
|
'args' => ['dogCommand' => ['type' => $DogCommand]]
|
|
|
|
],
|
|
|
|
'isHousetrained' => [
|
|
|
|
'type' => Type::boolean(),
|
|
|
|
'args' => ['atOtherHomes' => ['type' => Type::boolean(), 'defaultValue' => true]]
|
|
|
|
],
|
|
|
|
'isAtLocation' => [
|
|
|
|
'type' => Type::boolean(),
|
|
|
|
'args' => ['x' => ['type' => Type::int()], 'y' => ['type' => Type::int()]]
|
|
|
|
]
|
|
|
|
],
|
2016-04-25 00:57:09 +03:00
|
|
|
'interfaces' => [$Being, $Pet, $Canine]
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$Cat = new ObjectType([
|
|
|
|
'name' => 'Cat',
|
2016-10-22 20:49:25 +03:00
|
|
|
'fields' => function() use (&$FurColor) {
|
|
|
|
return [
|
|
|
|
'name' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'surname' => [ 'type' => Type::boolean() ] ]
|
|
|
|
],
|
|
|
|
'nickname' => ['type' => Type::string()],
|
|
|
|
'meows' => ['type' => Type::boolean()],
|
|
|
|
'meowVolume' => ['type' => Type::int()],
|
|
|
|
'furColor' => $FurColor
|
|
|
|
];
|
|
|
|
},
|
2015-07-15 20:05:46 +03:00
|
|
|
'interfaces' => [$Being, $Pet]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$CatOrDog = new UnionType([
|
|
|
|
'name' => 'CatOrDog',
|
|
|
|
'types' => [$Dog, $Cat],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$Intelligent = new InterfaceType([
|
|
|
|
'name' => 'Intelligent',
|
|
|
|
'fields' => [
|
|
|
|
'iq' => ['type' => Type::int()]
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2016-04-30 17:39:06 +03:00
|
|
|
$Human = null;
|
|
|
|
$Human = new ObjectType([
|
2015-07-15 20:05:46 +03:00
|
|
|
'name' => 'Human',
|
|
|
|
'interfaces' => [$Being, $Intelligent],
|
2016-10-22 20:49:25 +03:00
|
|
|
'fields' => function() use (&$Human, $Pet) {
|
|
|
|
return [
|
|
|
|
'name' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => ['surname' => ['type' => Type::boolean()]]
|
|
|
|
],
|
|
|
|
'pets' => ['type' => Type::listOf($Pet)],
|
|
|
|
'relatives' => ['type' => Type::listOf($Human)],
|
|
|
|
'iq' => ['type' => Type::int()]
|
|
|
|
];
|
|
|
|
}
|
2015-07-15 20:05:46 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
$Alien = new ObjectType([
|
|
|
|
'name' => 'Alien',
|
|
|
|
'interfaces' => [$Being, $Intelligent],
|
|
|
|
'fields' => [
|
|
|
|
'iq' => ['type' => Type::int()],
|
2015-08-17 17:01:55 +03:00
|
|
|
'name' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => ['surname' => ['type' => Type::boolean()]]
|
|
|
|
],
|
2015-07-15 20:05:46 +03:00
|
|
|
'numEyes' => ['type' => Type::int()]
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$DogOrHuman = new UnionType([
|
|
|
|
'name' => 'DogOrHuman',
|
|
|
|
'types' => [$Dog, $Human],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$HumanOrAlien = new UnionType([
|
|
|
|
'name' => 'HumanOrAlien',
|
|
|
|
'types' => [$Human, $Alien],
|
|
|
|
]);
|
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
$FurColor = new EnumType([
|
|
|
|
'name' => 'FurColor',
|
|
|
|
'values' => [
|
|
|
|
'BROWN' => [ 'value' => 0 ],
|
|
|
|
'BLACK' => [ 'value' => 1 ],
|
|
|
|
'TAN' => [ 'value' => 2 ],
|
|
|
|
'SPOTTED' => [ 'value' => 3 ],
|
2017-07-04 16:19:52 +03:00
|
|
|
'NO_FUR' => [ 'value' => null ],
|
2015-08-17 17:01:55 +03:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2015-07-15 20:05:46 +03:00
|
|
|
$ComplexInput = new InputObjectType([
|
|
|
|
'name' => 'ComplexInput',
|
|
|
|
'fields' => [
|
|
|
|
'requiredField' => ['type' => Type::nonNull(Type::boolean())],
|
|
|
|
'intField' => ['type' => Type::int()],
|
|
|
|
'stringField' => ['type' => Type::string()],
|
|
|
|
'booleanField' => ['type' => Type::boolean()],
|
|
|
|
'stringListField' => ['type' => Type::listOf(Type::string())]
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$ComplicatedArgs = new ObjectType([
|
|
|
|
'name' => 'ComplicatedArgs',
|
|
|
|
// TODO List
|
|
|
|
// TODO Coercion
|
|
|
|
// TODO NotNulls
|
|
|
|
'fields' => [
|
|
|
|
'intArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => ['intArg' => ['type' => Type::int()]],
|
|
|
|
],
|
|
|
|
'nonNullIntArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'nonNullIntArg' => [ 'type' => Type::nonNull(Type::int())]],
|
|
|
|
],
|
|
|
|
'stringArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'stringArg' => [ 'type' => Type::string()]],
|
|
|
|
],
|
|
|
|
'booleanArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => ['booleanArg' => [ 'type' => Type::boolean() ]],
|
|
|
|
],
|
|
|
|
'enumArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'enumArg' => ['type' => $FurColor ]],
|
|
|
|
],
|
|
|
|
'floatArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'floatArg' => [ 'type' => Type::float()]],
|
|
|
|
],
|
|
|
|
'idArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'idArg' => [ 'type' => Type::id() ]],
|
|
|
|
],
|
|
|
|
'stringListArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'stringListArg' => [ 'type' => Type::listOf(Type::string())]],
|
|
|
|
],
|
|
|
|
'complexArgField' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [ 'complexArg' => [ 'type' => $ComplexInput ]],
|
|
|
|
],
|
|
|
|
'multipleReqs' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [
|
|
|
|
'req1' => [ 'type' => Type::nonNull(Type::int())],
|
|
|
|
'req2' => [ 'type' => Type::nonNull(Type::int())],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'multipleOpts' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [
|
|
|
|
'opt1' => [
|
|
|
|
'type' => Type::int(),
|
|
|
|
'defaultValue' => 0,
|
|
|
|
],
|
|
|
|
'opt2' => [
|
|
|
|
'type' => Type::int(),
|
|
|
|
'defaultValue' => 0,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'multipleOptAndReq' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [
|
|
|
|
'req1' => [ 'type' => Type::nonNull(Type::int())],
|
|
|
|
'req2' => [ 'type' => Type::nonNull(Type::int())],
|
|
|
|
'opt1' => [
|
|
|
|
'type' => Type::int(),
|
|
|
|
'defaultValue' => 0,
|
|
|
|
],
|
|
|
|
'opt2' => [
|
|
|
|
'type' => Type::int(),
|
|
|
|
'defaultValue' => 0,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
|
|
|
$queryRoot = new ObjectType([
|
|
|
|
'name' => 'QueryRoot',
|
|
|
|
'fields' => [
|
|
|
|
'human' => [
|
|
|
|
'args' => ['id' => ['type' => Type::id()]],
|
|
|
|
'type' => $Human
|
|
|
|
],
|
|
|
|
'alien' => ['type' => $Alien],
|
|
|
|
'dog' => ['type' => $Dog],
|
|
|
|
'cat' => ['type' => $Cat],
|
|
|
|
'pet' => ['type' => $Pet],
|
|
|
|
'catOrDog' => ['type' => $CatOrDog],
|
|
|
|
'dogOrHuman' => ['type' => $DogOrHuman],
|
|
|
|
'humanOrAlien' => ['type' => $HumanOrAlien],
|
|
|
|
'complicatedArgs' => ['type' => $ComplicatedArgs]
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
|
2016-04-25 00:57:09 +03:00
|
|
|
$defaultSchema = new Schema([
|
|
|
|
'query' => $queryRoot,
|
2016-10-18 21:34:46 +03:00
|
|
|
'directives' => array_merge(GraphQL::getInternalDirectives(), [
|
2016-04-25 00:57:09 +03:00
|
|
|
new Directive([
|
|
|
|
'name' => 'operationOnly',
|
|
|
|
'locations' => [ 'QUERY' ],
|
|
|
|
])
|
2016-10-18 21:34:46 +03:00
|
|
|
])
|
2016-04-25 00:57:09 +03:00
|
|
|
]);
|
2015-07-15 20:05:46 +03:00
|
|
|
return $defaultSchema;
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectValid($schema, $rules, $queryString)
|
|
|
|
{
|
|
|
|
$this->assertEquals(
|
2015-08-17 17:01:55 +03:00
|
|
|
[],
|
2016-11-16 14:37:35 +03:00
|
|
|
DocumentValidator::validate($schema, Parser::parse($queryString), $rules),
|
2015-08-17 17:01:55 +03:00
|
|
|
'Should validate'
|
2015-07-15 20:05:46 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
function expectInvalid($schema, $rules, $queryString, $expectedErrors)
|
2015-07-15 20:05:46 +03:00
|
|
|
{
|
2016-11-16 14:37:35 +03:00
|
|
|
$errors = DocumentValidator::validate($schema, Parser::parse($queryString), $rules);
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
$this->assertNotEmpty($errors, 'GraphQL should not validate');
|
2016-10-21 12:39:57 +03:00
|
|
|
$this->assertEquals($expectedErrors, array_map(['GraphQL\Error\Error', 'formatError'], $errors));
|
2015-07-15 20:05:46 +03:00
|
|
|
|
2015-08-17 17:01:55 +03:00
|
|
|
return $errors;
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function expectPassesRule($rule, $queryString)
|
|
|
|
{
|
|
|
|
$this->expectValid($this->getDefaultSchema(), [$rule], $queryString);
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectFailsRule($rule, $queryString, $errors)
|
|
|
|
{
|
|
|
|
return $this->expectInvalid($this->getDefaultSchema(), [$rule], $queryString, $errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectPassesRuleWithSchema($schema, $rule, $queryString)
|
|
|
|
{
|
|
|
|
$this->expectValid($schema, [$rule], $queryString);
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectFailsRuleWithSchema($schema, $rule, $queryString, $errors)
|
|
|
|
{
|
|
|
|
$this->expectInvalid($schema, [$rule], $queryString, $errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
function expectPassesCompleteValidation($queryString)
|
|
|
|
{
|
2016-04-25 16:50:03 +03:00
|
|
|
$this->expectValid($this->getDefaultSchema(), DocumentValidator::allRules(), $queryString);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function expectFailsCompleteValidation($queryString, $errors)
|
|
|
|
{
|
2016-04-25 16:50:03 +03:00
|
|
|
$this->expectInvalid($this->getDefaultSchema(), DocumentValidator::allRules(), $queryString, $errors);
|
2015-07-15 20:05:46 +03:00
|
|
|
}
|
|
|
|
}
|