1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Tests\Server; |
6
|
|
|
|
7
|
|
|
use GraphQL\Deferred; |
8
|
|
|
use GraphQL\Error\UserError; |
9
|
|
|
use GraphQL\Type\Definition\ObjectType; |
10
|
|
|
use GraphQL\Type\Definition\Type; |
11
|
|
|
use GraphQL\Type\Schema; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use function trigger_error; |
14
|
|
|
use const E_USER_DEPRECATED; |
15
|
|
|
use const E_USER_NOTICE; |
16
|
|
|
use const E_USER_WARNING; |
17
|
|
|
|
18
|
|
|
abstract class ServerTestCase extends TestCase |
19
|
|
|
{ |
20
|
|
|
protected function buildSchema() |
21
|
|
|
{ |
22
|
|
|
return new Schema([ |
23
|
|
|
'query' => new ObjectType([ |
24
|
|
|
'name' => 'Query', |
25
|
|
|
'fields' => [ |
26
|
|
|
'f1' => [ |
27
|
|
|
'type' => Type::string(), |
28
|
|
|
'resolve' => static function ($root, $args, $context, $info) { |
29
|
|
|
return $info->fieldName; |
30
|
|
|
}, |
31
|
|
|
], |
32
|
|
|
'fieldWithPhpError' => [ |
33
|
|
|
'type' => Type::string(), |
34
|
|
|
'resolve' => static function ($root, $args, $context, $info) { |
35
|
|
|
trigger_error('deprecated', E_USER_DEPRECATED); |
36
|
|
|
trigger_error('notice', E_USER_NOTICE); |
37
|
|
|
trigger_error('warning', E_USER_WARNING); |
38
|
|
|
$a = []; |
39
|
|
|
$a['test']; // should produce PHP notice |
40
|
|
|
|
41
|
|
|
return $info->fieldName; |
42
|
|
|
}, |
43
|
|
|
], |
44
|
|
|
'fieldWithSafeException' => [ |
45
|
|
|
'type' => Type::string(), |
46
|
|
|
'resolve' => static function () { |
47
|
|
|
throw new UserError('This is the exception we want'); |
48
|
|
|
}, |
49
|
|
|
], |
50
|
|
|
'fieldWithUnsafeException' => [ |
51
|
|
|
'type' => Type::string(), |
52
|
|
|
'resolve' => static function () { |
53
|
|
|
throw new Unsafe('This exception should not be shown to the user'); |
54
|
|
|
}, |
55
|
|
|
], |
56
|
|
|
'testContextAndRootValue' => [ |
57
|
|
|
'type' => Type::string(), |
58
|
|
|
'resolve' => static function ($root, $args, $context, $info) { |
59
|
|
|
$context->testedRootValue = $root; |
60
|
|
|
|
61
|
|
|
return $info->fieldName; |
62
|
|
|
}, |
63
|
|
|
], |
64
|
|
|
'fieldWithArg' => [ |
65
|
|
|
'type' => Type::string(), |
66
|
|
|
'args' => [ |
67
|
|
|
'arg' => [ |
68
|
|
|
'type' => Type::nonNull(Type::string()), |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
'resolve' => static function ($root, $args) { |
72
|
|
|
return $args['arg']; |
73
|
|
|
}, |
74
|
|
|
], |
75
|
|
|
'dfd' => [ |
76
|
|
|
'type' => Type::string(), |
77
|
|
|
'args' => [ |
78
|
|
|
'num' => [ |
79
|
|
|
'type' => Type::nonNull(Type::int()), |
80
|
|
|
], |
81
|
|
|
], |
82
|
|
|
'resolve' => static function ($root, $args, $context) { |
83
|
|
|
$context['buffer']($args['num']); |
84
|
|
|
|
85
|
|
|
return new Deferred(static function () use ($args, $context) { |
86
|
|
|
return $context['load']($args['num']); |
87
|
|
|
}); |
88
|
|
|
}, |
89
|
|
|
], |
90
|
|
|
], |
91
|
|
|
]), |
92
|
|
|
'mutation' => new ObjectType([ |
93
|
|
|
'name' => 'Mutation', |
94
|
|
|
'fields' => [ |
95
|
|
|
'm1' => [ |
96
|
|
|
'type' => new ObjectType([ |
97
|
|
|
'name' => 'TestMutation', |
98
|
|
|
'fields' => [ |
99
|
|
|
'result' => Type::string(), |
100
|
|
|
], |
101
|
|
|
]), |
102
|
|
|
], |
103
|
|
|
], |
104
|
|
|
]), |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|