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()
|
|
|
|
{
|
2018-09-26 12:07:23 +03:00
|
|
|
return 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(),
|
2019-06-30 21:54:56 +03:00
|
|
|
'resolve' => static function ($rootValue, $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(),
|
2019-06-30 21:54:56 +03:00
|
|
|
'resolve' => static function ($rootValue, $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-03 22:18:04 +03:00
|
|
|
'fieldWithSafeException' => [
|
2017-08-20 21:16:55 +03:00
|
|
|
'type' => Type::string(),
|
2018-09-26 12:07:23 +03:00
|
|
|
'resolve' => static function () {
|
2018-09-03 22:18:04 +03:00
|
|
|
throw new UserError('This is the exception we want');
|
2018-09-03 22:43:02 +03:00
|
|
|
},
|
2018-09-03 22:18:04 +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');
|
|
|
|
},
|
2017-08-20 21:16:55 +03:00
|
|
|
],
|
|
|
|
'testContextAndRootValue' => [
|
2018-09-01 23:00:00 +03:00
|
|
|
'type' => Type::string(),
|
2019-06-30 21:54:56 +03:00
|
|
|
'resolve' => static function ($rootValue, $args, $context, $info) {
|
|
|
|
$context->testedRootValue = $rootValue;
|
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
|
|
|
],
|
|
|
|
],
|
2019-06-30 21:54:56 +03:00
|
|
|
'resolve' => static function ($rootValue, $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
|
|
|
],
|
|
|
|
],
|
2019-06-30 21:54:56 +03:00
|
|
|
'resolve' => static function ($rootValue, $args, $context) {
|
2017-08-20 21:16:55 +03:00
|
|
|
$context['buffer']($args['num']);
|
|
|
|
|
2018-09-26 12:07:23 +03:00
|
|
|
return new Deferred(static 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
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|