-
-
Notifications
You must be signed in to change notification settings - Fork 574
Expand file tree
/
Copy pathRequestValidationTest.php
More file actions
143 lines (120 loc) · 3.99 KB
/
RequestValidationTest.php
File metadata and controls
143 lines (120 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Server;
use GraphQL\Server\Helper;
use GraphQL\Server\OperationParams;
use PHPUnit\Framework\TestCase;
class RequestValidationTest extends TestCase
{
public function testSimpleRequestShouldValidate(): void
{
$query = '{my q}';
$variables = ['a' => 'b', 'c' => 'd'];
$operation = 'op';
$parsedBody = OperationParams::create([
'query' => $query,
'variables' => $variables,
'operationName' => $operation,
]);
self::assertValid($parsedBody);
}
private static function assertValid($parsedRequest): void
{
$helper = new Helper();
$errors = $helper->parseOperationParams($parsedRequest);
self::assertEmpty($errors, isset($errors[0]) ? $errors[0]->getMessage() : '');
}
public function testRequestWithQueryIdShouldValidate(): void
{
$queryId = 'some-query-id';
$variables = ['a' => 'b', 'c' => 'd'];
$operation = 'op';
$parsedBody = OperationParams::create([
'queryId' => $queryId,
'variables' => $variables,
'operationName' => $operation,
]);
self::assertValid($parsedBody);
}
public function testRequiresQueryOrQueryId(): void
{
$parsedBody = OperationParams::create([
'variables' => ['foo' => 'bar'],
'operationName' => 'op',
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request must include at least one of those two parameters: "query" or "queryId"'
);
}
private function assertInputError($parsedRequest, $expectedMessage): void
{
$helper = new Helper();
$errors = $helper->parseOperationParams($parsedRequest);
if (isset($errors[0])) {
self::assertEquals($expectedMessage, $errors[0]->getMessage());
} else {
self::fail('Expected error not returned');
}
}
public function testFailsWhenQueryParameterIsNotString(): void
{
$parsedBody = OperationParams::create([
'query' => ['t' => '{my query}'],
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameter "query" must be string, but got {"t":"{my query}"}'
);
}
public function testFailsWhenQueryIdParameterIsNotString(): void
{
$parsedBody = OperationParams::create([
'queryId' => ['t' => '{my query}'],
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameter "queryId" must be string, but got {"t":"{my query}"}'
);
}
public function testFailsWhenOperationParameterIsNotString(): void
{
$parsedBody = OperationParams::create([
'query' => '{my query}',
'operationName' => [],
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameter "operation" must be string, but got []'
);
}
/**
* @see https://github.com/webonyx/graphql-php/issues/156
*/
public function testIgnoresNullAndEmptyStringVariables(): void
{
$query = '{my q}';
$parsedBody = OperationParams::create([
'query' => $query,
'variables' => null,
]);
self::assertValid($parsedBody);
$variables = '';
$parsedBody = OperationParams::create([
'query' => $query,
'variables' => $variables,
]);
self::assertValid($parsedBody);
}
public function testFailsWhenVariablesParameterIsNotObject(): void
{
$parsedBody = OperationParams::create([
'query' => '{my query}',
'variables' => 0,
]);
$this->assertInputError(
$parsedBody,
'GraphQL Request parameter "variables" must be object or JSON string parsed to object, but got 0'
);
}
}