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