Skip to content

Commit c249fa2

Browse files
committed
Promise tests
1 parent f010502 commit c249fa2

File tree

1 file changed

+121
-2
lines changed

1 file changed

+121
-2
lines changed

tests/Executor/NonNullTest.php

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
2+
23
namespace GraphQL\Tests\Executor;
34

4-
use GraphQL\Error\Error;
5+
56
use GraphQL\Executor\Executor;
67
use GraphQL\Error\FormattedError;
78
use GraphQL\Language\Parser;
89
use GraphQL\Language\SourceLocation;
10+
use GraphQL\Promise\Adapter\ReactPromiseAdapter;
911
use GraphQL\Schema;
1012
use GraphQL\Type\Definition\ObjectType;
1113
use GraphQL\Type\Definition\Type;
14+
use React\Promise\Promise;
15+
use React\Promise\PromiseInterface;
1216

1317
class NonNullTest extends \PHPUnit_Framework_TestCase
1418
{
@@ -17,6 +21,13 @@ class NonNullTest extends \PHPUnit_Framework_TestCase
1721

1822
/** @var \Exception */
1923
public $nonNullSyncError;
24+
25+
/** @var \Exception */
26+
public $promiseError;
27+
28+
/** @var \Exception */
29+
public $nonNullPromiseError;
30+
2031
public $throwingData;
2132
public $nullingData;
2233
public $schema;
@@ -25,6 +36,8 @@ public function setUp()
2536
{
2637
$this->syncError = new \Exception('sync');
2738
$this->nonNullSyncError = new \Exception('nonNullSync');
39+
$this->promiseError = new \Exception('promise');
40+
$this->nonNullPromiseError = new \Exception('nonNullPromise');
2841

2942
$this->throwingData = [
3043
'sync' => function () {
@@ -33,12 +46,32 @@ public function setUp()
3346
'nonNullSync' => function () {
3447
throw $this->nonNullSyncError;
3548
},
49+
'promise' => function () {
50+
return new Promise(function () {
51+
throw $this->promiseError;
52+
});
53+
},
54+
'nonNullPromise' => function () {
55+
return new Promise(function () {
56+
throw $this->nonNullPromiseError;
57+
});
58+
},
3659
'nest' => function () {
3760
return $this->throwingData;
3861
},
3962
'nonNullNest' => function () {
4063
return $this->throwingData;
4164
},
65+
'promiseNest' => function () {
66+
return new Promise(function (callable $resolve) {
67+
$resolve($this->throwingData);
68+
});
69+
},
70+
'nonNullPromiseNest' => function () {
71+
return new Promise(function (callable $resolve) {
72+
$resolve($this->throwingData);
73+
});
74+
},
4275
];
4376

4477
$this->nullingData = [
@@ -48,12 +81,32 @@ public function setUp()
4881
'nonNullSync' => function () {
4982
return null;
5083
},
84+
'promise' => function () {
85+
return new Promise(function (callable $resolve) {
86+
return $resolve(null);
87+
});
88+
},
89+
'nonNullPromise' => function () {
90+
return new Promise(function (callable $resolve) {
91+
return $resolve(null);
92+
});
93+
},
5194
'nest' => function () {
5295
return $this->nullingData;
5396
},
5497
'nonNullNest' => function () {
5598
return $this->nullingData;
5699
},
100+
'promiseNest' => function () {
101+
return new Promise(function (callable $resolve) {
102+
$resolve($this->nullingData);
103+
});
104+
},
105+
'nonNullPromiseNest' => function () {
106+
return new Promise(function (callable $resolve) {
107+
$resolve($this->nullingData);
108+
});
109+
},
57110
];
58111

59112
$dataType = new ObjectType([
@@ -62,13 +115,18 @@ public function setUp()
62115
return [
63116
'sync' => ['type' => Type::string()],
64117
'nonNullSync' => ['type' => Type::nonNull(Type::string())],
118+
'promise' => Type::string(),
119+
'nonNullPromise' => Type::nonNull(Type::string()),
65120
'nest' => $dataType,
66-
'nonNullNest' => Type::nonNull($dataType)
121+
'nonNullNest' => Type::nonNull($dataType),
122+
'promiseNest' => $dataType,
123+
'nonNullPromiseNest' => Type::nonNull($dataType),
67124
];
68125
}
69126
]);
70127

71128
$this->schema = new Schema(['query' => $dataType]);
129+
Executor::setPromiseAdapter(null);
72130
}
73131

74132
// Execute: handles non-nullable types
@@ -100,6 +158,32 @@ public function testNullsANullableFieldThatThrowsSynchronously()
100158
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
101159
}
102160

161+
public function testNullsANullableFieldThatThrowsInAPromise()
162+
{
163+
$doc = '
164+
query Q {
165+
promise
166+
}
167+
';
168+
169+
$ast = Parser::parse($doc);
170+
171+
$expected = [
172+
'data' => [
173+
'promise' => null,
174+
],
175+
'errors' => [
176+
FormattedError::create(
177+
$this->promiseError->getMessage(),
178+
[new SourceLocation(3, 9)]
179+
)
180+
]
181+
];
182+
183+
Executor::setPromiseAdapter(new ReactPromiseAdapter());
184+
$this->assertArraySubsetPromise($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q'));
185+
}
186+
103187
public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsSynchronously()
104188
{
105189
// nulls a synchronously returned object that contains a non-nullable field that throws synchronously
@@ -124,6 +208,31 @@ public function testNullsASynchronouslyReturnedObjectThatContainsANonNullableFie
124208
$this->assertArraySubset($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q')->toArray());
125209
}
126210

211+
public function testNullsAsynchronouslyReturnedObjectThatContainsANonNullableFieldThatThrowsInAPromise()
212+
{
213+
$doc = '
214+
query Q {
215+
nest {
216+
nonNullPromise,
217+
}
218+
}
219+
';
220+
221+
$ast = Parser::parse($doc);
222+
223+
$expected = [
224+
'data' => [
225+
'nest' => null
226+
],
227+
'errors' => [
228+
FormattedError::create($this->nonNullPromiseError->getMessage(), [new SourceLocation(4, 11)])
229+
]
230+
];
231+
232+
Executor::setPromiseAdapter(new ReactPromiseAdapter());
233+
$this->assertArraySubsetPromise($expected, Executor::execute($this->schema, $ast, $this->throwingData, null, [], 'Q'));
234+
}
235+
127236
public function testNullsAComplexTreeOfNullableFieldsThatThrow()
128237
{
129238
$doc = '
@@ -262,4 +371,14 @@ public function testNullsTheTopLevelIfSyncNonNullableFieldReturnsNull()
262371
];
263372
$this->assertArraySubset($expected, Executor::execute($this->schema, Parser::parse($doc), $this->nullingData)->toArray());
264373
}
374+
375+
private function assertArraySubsetPromise($subset, PromiseInterface $promise)
376+
{
377+
$array = null;
378+
$promise->then(function ($value) use (&$array) {
379+
$array = $value;
380+
});
381+
382+
$this->assertArraySubset($subset, $array);
383+
}
265384
}

0 commit comments

Comments
 (0)