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
|
6 |
|
public function assertPromiseFulfills($promise, $expectedValue = null): void |
22
|
|
|
{ |
23
|
6 |
|
$this->addToAssertionCount(1); |
|
|
|
|
24
|
|
|
|
25
|
6 |
|
if (! $promise instanceof PromiseInterface) { |
26
|
2 |
|
$promise = all($promise); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
try { |
30
|
6 |
|
$result = $this->waitForPromise($promise); |
31
|
2 |
|
} catch (Exception $_) { |
32
|
2 |
|
$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
|
|
|
} |
47
|
2 |
|
} |
48
|
|
|
|
49
|
6 |
|
protected function getLoop(): LoopInterface |
50
|
|
|
{ |
51
|
6 |
|
if (! $this->loop) { |
52
|
6 |
|
$this->loop = LoopFactory::create(); |
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
return $this->loop; |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
private function waitForPromise(PromiseInterface $promise) |
59
|
|
|
{ |
60
|
6 |
|
$wait = true; |
61
|
6 |
|
$resolved = null; |
62
|
6 |
|
$exception = null; |
63
|
6 |
|
$rejected = false; |
64
|
|
|
|
65
|
6 |
|
$promise->then( |
66
|
|
|
function ($c) use (&$resolved, &$wait) { |
67
|
4 |
|
$resolved = $c; |
68
|
4 |
|
$wait = false; |
69
|
4 |
|
$this->getLoop()->stop(); |
70
|
6 |
|
}, |
71
|
|
|
function ($error) use (&$exception, &$rejected, &$wait) { |
72
|
1 |
|
$exception = $error; |
73
|
1 |
|
$rejected = true; |
74
|
1 |
|
$wait = false; |
75
|
1 |
|
$this->getLoop()->stop(); |
76
|
6 |
|
} |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
// Explicitly overwrite argument with null value. This ensure that this |
80
|
|
|
// argument does not show up in the stack trace in PHP 7+ only. |
81
|
6 |
|
$promise = null; |
|
|
|
|
82
|
|
|
|
83
|
6 |
|
while ($wait) { |
84
|
6 |
|
$this->getLoop()->run(); |
85
|
|
|
} |
86
|
|
|
|
87
|
5 |
|
if ($rejected) { |
88
|
1 |
|
if (! $exception instanceof Exception) { |
89
|
1 |
|
$type = is_object($exception) ? get_class($exception) : gettype($exception); |
90
|
1 |
|
$exception = new \UnexpectedValueException( |
91
|
1 |
|
'Promise rejected with unexpected value of type ' . $type, |
92
|
1 |
|
0, |
93
|
1 |
|
$exception instanceof \Throwable ? $exception : null |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
throw $exception; |
98
|
|
|
} |
99
|
|
|
|
100
|
4 |
|
return $resolved; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|