88use function React \Async \async ;
99use function React \Async \await ;
1010use function React \Promise \all ;
11+ use function React \Promise \reject ;
12+ use function React \Promise \resolve ;
1113
1214class AsyncTest extends TestCase
1315{
14- public function testAsyncReturnsPendingPromise ()
16+ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsValue ()
1517 {
1618 $ promise = async (function () {
1719 return 42 ;
1820 })();
1921
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 );
2128 }
2229
23- public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturns ()
30+ public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsPromiseThatFulfillsWithValue ()
2431 {
2532 $ promise = async (function () {
26- return 42 ;
33+ return resolve ( 42 ) ;
2734 })();
2835
29- $ value = await ($ promise );
36+ $ value = null ;
37+ $ promise ->then (function ($ v ) use (&$ value ) {
38+ $ value = $ v ;
39+ });
3040
3141 $ this ->assertEquals (42 , $ value );
3242 }
@@ -37,10 +47,41 @@ public function testAsyncReturnsPromiseThatRejectsWithExceptionWhenCallbackThrow
3747 throw new \RuntimeException ('Foo ' , 42 );
3848 })();
3949
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 ());
4485 }
4586
4687 public function testAsyncReturnsPromiseThatFulfillsWithValueWhenCallbackReturnsAfterAwaitingPromise ()
0 commit comments