2017-08-20 21:16:55 +03:00
|
|
|
<?php
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace GraphQL\Tests\Server;
|
2017-08-20 21:16:55 +03:00
|
|
|
|
|
|
|
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;
|
2017-08-20 21:16:55 +03:00
|
|
|
|
2018-07-29 18:43:10 +03:00
|
|
|
abstract class ServerTestCase extends TestCase
|
2017-08-20 21:16:55 +03:00
|
|
|
{
|
|
|
|
protected function buildSchema()
|
|
|
|
{
|
|
|
|
$schema = new Schema([
|
2018-09-01 23:00:00 +03:00
|
|
|
'query' => new ObjectType([
|
|
|
|
'name' => 'Query',
|
2017-08-20 21:16:55 +03:00
|
|
|
'fields' => [
|
2018-09-01 23:00:00 +03:00
|
|
|
'f1' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'resolve' => function ($root, $args, $context, $info) {
|
2017-08-20 21:16:55 +03:00
|
|
|
return $info->fieldName;
|
2018-09-01 23:00:00 +03:00
|
|
|
},
|
2017-08-20 21:16:55 +03:00
|
|
|
],
|
2018-09-01 23:00:00 +03:00
|
|
|
'fieldWithPhpError' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'resolve' => function ($root, $args, $context, $info) {
|
2017-08-20 21:16:55 +03:00
|
|
|
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
|
|
|
|
2017-08-20 21:16:55 +03:00
|
|
|
return $info->fieldName;
|
2018-09-01 23:00:00 +03:00
|
|
|
},
|
2017-08-20 21:16:55 +03:00
|
|
|
],
|
2018-09-01 23:00:00 +03:00
|
|
|
'fieldWithException' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'resolve' => function ($root, $args, $context, $info) {
|
|
|
|
throw new UserError('This is the exception we want');
|
|
|
|
},
|
2017-08-20 21:16:55 +03:00
|
|
|
],
|
|
|
|
'testContextAndRootValue' => [
|
2018-09-01 23:00:00 +03:00
|
|
|
'type' => Type::string(),
|
|
|
|
'resolve' => function ($root, $args, $context, $info) {
|
2017-08-20 21:16:55 +03:00
|
|
|
$context->testedRootValue = $root;
|
2018-09-01 23:00:00 +03:00
|
|
|
|
2017-08-20 21:16:55 +03:00
|
|
|
return $info->fieldName;
|
2018-09-01 23:00:00 +03:00
|
|
|
},
|
2017-08-20 21:16:55 +03:00
|
|
|
],
|
2018-09-01 23:00:00 +03:00
|
|
|
'fieldWithArg' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [
|
2017-08-20 21:16:55 +03:00
|
|
|
'arg' => [
|
2018-09-01 23:00:00 +03:00
|
|
|
'type' => Type::nonNull(Type::string()),
|
2017-08-20 21:16:55 +03:00
|
|
|
],
|
|
|
|
],
|
2018-09-01 23:00:00 +03:00
|
|
|
'resolve' => function ($root, $args) {
|
2017-08-20 21:16:55 +03:00
|
|
|
return $args['arg'];
|
2018-09-01 23:00:00 +03:00
|
|
|
},
|
2017-08-20 21:16:55 +03:00
|
|
|
],
|
2018-09-01 23:00:00 +03:00
|
|
|
'dfd' => [
|
|
|
|
'type' => Type::string(),
|
|
|
|
'args' => [
|
2017-08-20 21:16:55 +03:00
|
|
|
'num' => [
|
2018-09-01 23:00:00 +03:00
|
|
|
'type' => Type::nonNull(Type::int()),
|
2017-08-20 21:16:55 +03:00
|
|
|
],
|
|
|
|
],
|
2018-09-01 23:00:00 +03:00
|
|
|
'resolve' => function ($root, $args, $context) {
|
2017-08-20 21:16:55 +03:00
|
|
|
$context['buffer']($args['num']);
|
|
|
|
|
2018-09-01 23:00:00 +03:00
|
|
|
return new Deferred(function () use ($args, $context) {
|
2017-08-20 21:16:55 +03:00
|
|
|
return $context['load']($args['num']);
|
|
|
|
});
|
2018-09-01 23:00:00 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2017-08-20 21:16:55 +03:00
|
|
|
]),
|
|
|
|
'mutation' => new ObjectType([
|
2018-09-01 23:00:00 +03:00
|
|
|
'name' => 'Mutation',
|
2017-08-20 21:16:55 +03:00
|
|
|
'fields' => [
|
|
|
|
'm1' => [
|
|
|
|
'type' => new ObjectType([
|
2018-09-01 23:00:00 +03:00
|
|
|
'name' => 'TestMutation',
|
2017-08-20 21:16:55 +03:00
|
|
|
'fields' => [
|
2018-09-01 23:00:00 +03:00
|
|
|
'result' => Type::string(),
|
|
|
|
],
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]),
|
2017-08-20 21:16:55 +03:00
|
|
|
]);
|
2018-09-01 23:00:00 +03:00
|
|
|
|
2017-08-20 21:16:55 +03:00
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
}
|