graphql-php/tests/Server/ServerTestCase.php

108 lines
4.1 KiB
PHP
Raw Permalink Normal View History

<?php
2018-09-01 23:00:00 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Deferred;
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-09-01 23:00:00 +03:00
use function trigger_error;
use const E_USER_DEPRECATED;
use const E_USER_NOTICE;
use const E_USER_WARNING;
2018-07-29 18:43:10 +03:00
abstract class ServerTestCase extends TestCase
{
protected function buildSchema()
{
2018-09-26 12:07:23 +03:00
return new Schema([
2018-09-01 23:00:00 +03:00
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
2018-09-01 23:00:00 +03:00
'f1' => [
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function ($root, $args, $context, $info) {
return $info->fieldName;
2018-09-01 23:00:00 +03:00
},
],
2018-09-01 23:00:00 +03:00
'fieldWithPhpError' => [
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function ($root, $args, $context, $info) {
trigger_error('deprecated', E_USER_DEPRECATED);
trigger_error('notice', E_USER_NOTICE);
trigger_error('warning', E_USER_WARNING);
$a = [];
$a['test']; // should produce PHP notice
2018-09-01 23:00:00 +03:00
return $info->fieldName;
2018-09-01 23:00:00 +03:00
},
],
'fieldWithSafeException' => [
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function () {
throw new UserError('This is the exception we want');
2018-09-03 22:43:02 +03:00
},
],
'fieldWithUnsafeException' => [
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function () {
2018-09-03 22:43:02 +03:00
throw new Unsafe('This exception should not be shown to the user');
},
],
'testContextAndRootValue' => [
2018-09-01 23:00:00 +03:00
'type' => Type::string(),
2018-09-26 12:07:23 +03:00
'resolve' => static function ($root, $args, $context, $info) {
$context->testedRootValue = $root;
2018-09-01 23:00:00 +03:00
return $info->fieldName;
2018-09-01 23:00:00 +03:00
},
],
2018-09-01 23:00:00 +03:00
'fieldWithArg' => [
'type' => Type::string(),
'args' => [
'arg' => [
2018-09-01 23:00:00 +03:00
'type' => Type::nonNull(Type::string()),
],
],
2018-09-26 12:07:23 +03:00
'resolve' => static function ($root, $args) {
return $args['arg'];
2018-09-01 23:00:00 +03:00
},
],
2018-09-01 23:00:00 +03:00
'dfd' => [
'type' => Type::string(),
'args' => [
'num' => [
2018-09-01 23:00:00 +03:00
'type' => Type::nonNull(Type::int()),
],
],
2018-09-26 12:07:23 +03:00
'resolve' => static function ($root, $args, $context) {
$context['buffer']($args['num']);
2018-09-26 12:07:23 +03:00
return new Deferred(static function () use ($args, $context) {
return $context['load']($args['num']);
});
2018-09-01 23:00:00 +03:00
},
],
],
]),
'mutation' => new ObjectType([
2018-09-01 23:00:00 +03:00
'name' => 'Mutation',
'fields' => [
'm1' => [
'type' => new ObjectType([
2018-09-01 23:00:00 +03:00
'name' => 'TestMutation',
'fields' => [
2018-09-01 23:00:00 +03:00
'result' => Type::string(),
],
]),
],
],
]),
]);
}
}