8
8
use function React \Async \async ;
9
9
use function React \Async \await ;
10
10
use function React \Promise \all ;
11
+ use function React \Promise \reject ;
12
+ use function React \Promise \resolve ;
11
13
12
14
class AsyncTest extends TestCase
13
15
{
14
- public function testAsyncReturnsPendingPromise ()
16
+ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsValue ()
15
17
{
16
18
$ promise = async (function () {
17
19
return 42 ;
18
20
})();
19
21
20
- $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableNever ());
22
+ $ value = null ;
23
+ $ promise ->then (function ($ v ) use (&$ value ) {
24
+ $ value = $ v ;
25
+ });
26
+
27
+ $ this ->assertEquals (42 , $ value );
21
28
}
22
29
23
- public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns ()
30
+ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsPromiseThatFulfillsWithValue ()
24
31
{
25
32
$ promise = async (function () {
26
- return 42 ;
33
+ return resolve ( 42 ) ;
27
34
})();
28
35
29
- $ value = await ($ promise );
36
+ $ value = null ;
37
+ $ promise ->then (function ($ v ) use (&$ value ) {
38
+ $ value = $ v ;
39
+ });
30
40
31
41
$ this ->assertEquals (42 , $ value );
32
42
}
@@ -37,10 +47,41 @@ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrow
37
47
throw new \RuntimeException ('Foo ' , 42 );
38
48
})();
39
49
40
- $ this ->expectException (\RuntimeException::class);
41
- $ this ->expectExceptionMessage ('Foo ' );
42
- $ this ->expectExceptionCode (42 );
43
- await ($ promise );
50
+ $ exception = null ;
51
+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
52
+ $ exception = $ reason ;
53
+ });
54
+
55
+ assert ($ exception instanceof \RuntimeException);
56
+ $ this ->assertInstanceOf (\RuntimeException::class, $ exception );
57
+ $ this ->assertEquals ('Foo ' , $ exception ->getMessage ());
58
+ $ this ->assertEquals (42 , $ exception ->getCode ());
59
+ }
60
+
61
+ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackReturnsPromiseThatRejectsWithException ()
62
+ {
63
+ $ promise = async (function () {
64
+ return reject (new \RuntimeException ('Foo ' , 42 ));
65
+ })();
66
+
67
+ $ exception = null ;
68
+ $ promise ->then (null , function ($ reason ) use (&$ exception ) {
69
+ $ exception = $ reason ;
70
+ });
71
+
72
+ assert ($ exception instanceof \RuntimeException);
73
+ $ this ->assertInstanceOf (\RuntimeException::class, $ exception );
74
+ $ this ->assertEquals ('Foo ' , $ exception ->getMessage ());
75
+ $ this ->assertEquals (42 , $ exception ->getCode ());
76
+ }
77
+
78
+ public function testAsyncReturnsPendingPromiseWhenCallbackReturnsPendingPromise ()
79
+ {
80
+ $ promise = async (function () {
81
+ return new Promise (function () { });
82
+ })();
83
+
84
+ $ promise ->then ($ this ->expectCallableNever (), $ this ->expectCallableNever ());
44
85
}
45
86
46
87
public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsAfterAwaitingPromise ()
0 commit comments