1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Butler\Graphql\Concerns; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Exception; |
7
|
|
|
use React\EventLoop\Factory as LoopFactory; |
8
|
|
|
use React\EventLoop\LoopInterface; |
9
|
|
|
use React\Promise\PromiseInterface; |
10
|
|
|
|
11
|
|
|
use function React\Promise\all; |
12
|
|
|
|
13
|
|
|
trait AssertsPromises |
14
|
|
|
{ |
15
|
|
|
private $loop; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param \React\Promise\PromiseInterface|array $promise |
19
|
|
|
* @param mixed|Closure $expectedValue |
20
|
|
|
*/ |
21
|
4 |
|
public function assertPromiseFulfills($promise, $expectedValue = null): void |
22
|
|
|
{ |
23
|
4 |
|
$this->addToAssertionCount(1); |
|
|
|
|
24
|
|
|
|
25
|
4 |
|
if (! $promise instanceof PromiseInterface) { |
26
|
2 |
|
$promise = all($promise); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
try { |
30
|
4 |
|
$result = $this->waitForPromise($promise); |
31
|
|
|
} catch (Exception $_) { |
32
|
|
|
$this->fail('Failed asserting that promise fulfills. Promise was rejected.'); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
if ($expectedValue instanceof Closure) { |
36
|
1 |
|
$this->assertTrue($expectedValue($result)); |
|
|
|
|
37
|
1 |
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
3 |
|
if (! is_null($expectedValue)) { |
41
|
3 |
|
$this->assertEquals( |
|
|
|
|
42
|
3 |
|
$expectedValue, |
43
|
|
|
$result, |
44
|
3 |
|
'Failed asserting that promise fulfills with a specified value.' |
45
|
|
|
); |
46
|
2 |
|
return; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
protected function getLoop(): LoopInterface |
51
|
|
|
{ |
52
|
4 |
|
if (! $this->loop) { |
53
|
4 |
|
$this->loop = LoopFactory::create(); |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
return $this->loop; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
private function waitForPromise(PromiseInterface $promise) |
60
|
|
|
{ |
61
|
4 |
|
$wait = true; |
62
|
4 |
|
$resolved = null; |
63
|
4 |
|
$exception = null; |
64
|
4 |
|
$rejected = false; |
65
|
|
|
|
66
|
4 |
|
$promise->then( |
67
|
|
|
function ($c) use (&$resolved, &$wait) { |
68
|
4 |
|
$resolved = $c; |
69
|
4 |
|
$wait = false; |
70
|
4 |
|
$this->getLoop()->stop(); |
71
|
4 |
|
}, |
72
|
|
|
function ($error) use (&$exception, &$rejected, &$wait) { |
73
|
|
|
$exception = $error; |
74
|
|
|
$rejected = true; |
75
|
|
|
$wait = false; |
76
|
|
|
$this->getLoop()->stop(); |
77
|
4 |
|
} |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
// Explicitly overwrite argument with null value. This ensure that this |
81
|
|
|
// argument does not show up in the stack trace in PHP 7+ only. |
82
|
4 |
|
$promise = null; |
|
|
|
|
83
|
|
|
|
84
|
4 |
|
while ($wait) { |
85
|
4 |
|
$this->getLoop()->run(); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
if ($rejected) { |
89
|
|
|
if (! $exception instanceof Exception) { |
90
|
|
|
$type = is_object($exception) ? get_class($exception) : gettype($exception); |
91
|
|
|
$exception = new \UnexpectedValueException( |
92
|
|
|
'Promise rejected with unexpected value of type ' . $type, |
93
|
|
|
0, |
94
|
|
|
$exception instanceof \Throwable ? $exception : null |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
throw $exception; |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
return $resolved; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|