-
-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathSyncTest.php
More file actions
218 lines (194 loc) · 7.23 KB
/
Copy pathSyncTest.php
File metadata and controls
218 lines (194 loc) · 7.23 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php declare(strict_types=1);
namespace GraphQL\Tests\Executor;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use GraphQL\Deferred;
use GraphQL\Error\FormattedError;
use GraphQL\Error\InvariantViolation;
use GraphQL\Executor\ExecutionResult;
use GraphQL\Executor\Executor;
use GraphQL\Executor\Promise\Adapter\SyncPromise;
use GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter;
use GraphQL\Executor\Promise\Promise;
use GraphQL\GraphQL;
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Validator\DocumentValidator;
use PHPUnit\Framework\TestCase;
final class SyncTest extends TestCase
{
use ArraySubsetAsserts;
private Schema $schema;
private SyncPromiseAdapter $promiseAdapter;
protected function setUp(): void
{
$this->schema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'syncField' => [
'type' => Type::string(),
'resolve' => static fn ($rootValue) => $rootValue,
],
'asyncField' => [
'type' => Type::string(),
'resolve' => static fn ($rootValue): Deferred => new Deferred(
static fn () => $rootValue
),
],
],
]),
'mutation' => new ObjectType([
'name' => 'Mutation',
'fields' => [
'syncMutationField' => [
'type' => Type::string(),
'resolve' => static fn ($rootValue) => $rootValue,
],
],
]),
]);
$this->promiseAdapter = new SyncPromiseAdapter();
}
// Describe: Execute: synchronously when possible
/** @see it('does not return a Promise for initial errors') */
public function testDoesNotReturnAPromiseForInitialErrors(): void
{
$doc = 'fragment Example on Query { syncField }';
$result = $this->execute(
$this->schema,
Parser::parse($doc),
'rootValue'
);
self::assertSync(['errors' => [['message' => 'Must provide an operation.']]], $result);
}
/** @param mixed $rootValue */
private function execute(Schema $schema, DocumentNode $doc, $rootValue = null): Promise
{
return Executor::promiseToExecute($this->promiseAdapter, $schema, $doc, $rootValue);
}
/**
* @param array<string, mixed> $expectedFinalArray
*
* @throws \Exception
*/
private static function assertSync(array $expectedFinalArray, Promise $actualResult): void
{
$message = 'Failed assertion that execution was synchronous';
$adoptedPromise = $actualResult->adoptedPromise;
self::assertInstanceOf(SyncPromise::class, $adoptedPromise, $message);
self::assertSame(SyncPromise::FULFILLED, $adoptedPromise->state, $message);
$result = $adoptedPromise->result;
self::assertInstanceOf(ExecutionResult::class, $result);
self::assertArraySubset($expectedFinalArray, $result->toArray());
}
/** @see it('does not return a Promise if fields are all synchronous') */
public function testDoesNotReturnAPromiseIfFieldsAreAllSynchronous(): void
{
$doc = 'query Example { syncField }';
$result = $this->execute(
$this->schema,
Parser::parse($doc),
'rootValue'
);
self::assertSync(['data' => ['syncField' => 'rootValue']], $result);
}
// Describe: graphqlSync
/** @see it('does not return a Promise if mutation fields are all synchronous') */
public function testDoesNotReturnAPromiseIfMutationFieldsAreAllSynchronous(): void
{
$doc = 'mutation Example { syncMutationField }';
$result = $this->execute(
$this->schema,
Parser::parse($doc),
'rootValue'
);
self::assertSync(['data' => ['syncMutationField' => 'rootValue']], $result);
}
/** @see it('returns a Promise if any field is asynchronous') */
public function testReturnsAPromiseIfAnyFieldIsAsynchronous(): void
{
$doc = 'query Example { syncField, asyncField }';
$result = $this->execute(
$this->schema,
Parser::parse($doc),
'rootValue'
);
$this->assertAsync(['data' => ['syncField' => 'rootValue', 'asyncField' => 'rootValue']], $result);
}
/**
* @param array<string, mixed> $expectedFinalArray
*
* @throws \Exception
* @throws InvariantViolation
*/
private function assertAsync(array $expectedFinalArray, Promise $actualResult): void
{
$message = 'Failed assertion that execution was asynchronous';
$adoptedPromise = $actualResult->adoptedPromise;
self::assertInstanceOf(SyncPromise::class, $adoptedPromise, $message);
self::assertSame(SyncPromise::PENDING, $adoptedPromise->state, $message);
$resolvedResult = $this->promiseAdapter->wait($actualResult);
self::assertInstanceOf(ExecutionResult::class, $resolvedResult);
self::assertArraySubset($expectedFinalArray, $resolvedResult->toArray());
}
/** @see it('does not return a Promise for syntax errors') */
public function testDoesNotReturnAPromiseForSyntaxErrors(): void
{
$result = $this->graphqlSync(
$this->schema,
'fragment Example on Query { { { syncField }'
);
self::assertSync(
[
'errors' => [
[
'message' => 'Syntax Error: Expected Name, found {',
'locations' => [['line' => 1, 'column' => 29]],
],
],
],
$result
);
}
/**
* @param mixed $rootValue
*
* @throws \Exception
* @throws InvariantViolation
*/
private function graphqlSync(Schema $schema, string $doc, $rootValue = null): Promise
{
return GraphQL::promiseToExecute($this->promiseAdapter, $schema, $doc, $rootValue);
}
/** @see it('does not return a Promise for validation errors') */
public function testDoesNotReturnAPromiseForValidationErrors(): void
{
$doc = 'fragment Example on Query { unknownField }';
$validationErrors = DocumentValidator::validate($this->schema, Parser::parse($doc));
$result = $this->graphqlSync(
$this->schema,
$doc
);
$expected = [
'errors' => array_map(
[FormattedError::class, 'createFromException'],
$validationErrors
),
];
self::assertSync($expected, $result);
}
/** @see it('does not return a Promise for sync execution') */
public function testDoesNotReturnAPromiseForSyncExecution(): void
{
$doc = 'query Example { syncField }';
$result = $this->graphqlSync(
$this->schema,
$doc,
'rootValue'
);
self::assertSync(['data' => ['syncField' => 'rootValue']], $result);
}
}