4
4
5
5
use React ;
6
6
use React \EventLoop \Loop ;
7
- use React \Promise ;
8
- use React \Promise \Deferred ;
7
+ use React \Promise \Promise ;
9
8
10
9
class AwaitTest extends TestCase
11
10
{
12
- public function testAwaitOneRejected ()
11
+ public function testAwaitThrowsExceptionWhenPromiseIsRejectedWithException ()
13
12
{
14
- $ promise = $ this ->createPromiseRejected (new \Exception ('test ' ));
13
+ $ promise = new Promise (function () {
14
+ throw new \Exception ('test ' );
15
+ });
15
16
16
17
$ this ->setExpectedException ('Exception ' , 'test ' );
17
18
React \Async \await ($ promise );
18
19
}
19
20
20
- public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException ()
21
+ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithFalse ()
21
22
{
22
23
if (!interface_exists ('React\Promise\CancellablePromiseInterface ' )) {
23
24
$ this ->markTestSkipped ('Promises must be rejected with a \Throwable instance since Promise v3 ' );
24
25
}
25
26
26
- $ promise = Promise \reject (false );
27
+ $ promise = new Promise (function ($ _ , $ reject ) {
28
+ $ reject (false );
29
+ });
27
30
28
31
$ this ->setExpectedException ('UnexpectedValueException ' , 'Promise rejected with unexpected value of type bool ' );
29
32
React \Async \await ($ promise );
30
33
}
31
34
32
- public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException ()
35
+ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithNull ()
33
36
{
34
37
if (!interface_exists ('React\Promise\CancellablePromiseInterface ' )) {
35
38
$ this ->markTestSkipped ('Promises must be rejected with a \Throwable instance since Promise v3 ' );
36
39
}
37
40
38
- $ promise = Promise \reject (null );
41
+ $ promise = new Promise (function ($ _ , $ reject ) {
42
+ $ reject (null );
43
+ });
39
44
40
45
$ this ->setExpectedException ('UnexpectedValueException ' , 'Promise rejected with unexpected value of type NULL ' );
41
46
React \Async \await ($ promise );
@@ -44,60 +49,67 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
44
49
/**
45
50
* @requires PHP 7
46
51
*/
47
- public function testAwaitRejectedWithPhp7ErrorWillThrowOriginalError ()
52
+ public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError ()
48
53
{
49
- $ promise = Promise \reject (new \Error ('Test ' , 42 ));
54
+ $ promise = new Promise (function ($ _ , $ reject ) {
55
+ throw new \Error ('Test ' , 42 );
56
+ });
50
57
51
58
$ this ->setExpectedException ('Error ' , 'Test ' , 42 );
52
59
React \Async \await ($ promise );
53
60
}
54
61
55
- public function testAwaitOneResolved ()
56
- {
57
- $ promise = $ this ->createPromiseResolved (2 );
58
-
59
- $ this ->assertEquals (2 , React \Async \await ($ promise ));
60
- }
61
-
62
- public function testAwaitReturnsFulfilledValueWithoutGivingLoop ()
62
+ public function testAwaitReturnsValueWhenPromiseIsFullfilled ()
63
63
{
64
- $ promise = Promise \resolve (42 );
64
+ $ promise = new Promise (function ($ resolve ) {
65
+ $ resolve (42 );
66
+ });
65
67
66
68
$ this ->assertEquals (42 , React \Async \await ($ promise ));
67
69
}
68
70
69
- public function testAwaitOneInterrupted ()
71
+ public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerStopsLoop ()
70
72
{
71
- $ promise = $ this ->createPromiseResolved (2 , 0.02 );
72
- $ this ->createTimerInterrupt (0.01 );
73
+ $ promise = new Promise (function ($ resolve ) {
74
+ Loop::addTimer (0.02 , function () use ($ resolve ) {
75
+ $ resolve (2 );
76
+ });
77
+ });
78
+ Loop::addTimer (0.01 , function () {
79
+ Loop::stop ();
80
+ });
73
81
74
82
$ this ->assertEquals (2 , React \Async \await ($ promise ));
75
83
}
76
84
77
- public function testAwaitOneResolvesShouldNotCreateAnyGarbageReferences ()
85
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise ()
78
86
{
79
87
if (class_exists ('React\Promise\When ' ) && PHP_VERSION_ID >= 50400 ) {
80
88
$ this ->markTestSkipped ('Not supported on legacy Promise v1 API with PHP 5.4+ ' );
81
89
}
82
90
83
91
gc_collect_cycles ();
84
92
85
- $ promise = Promise \resolve (1 );
93
+ $ promise = new Promise (function ($ resolve ) {
94
+ $ resolve (42 );
95
+ });
86
96
React \Async \await ($ promise );
87
97
unset($ promise );
88
98
89
99
$ this ->assertEquals (0 , gc_collect_cycles ());
90
100
}
91
101
92
- public function testAwaitOneRejectedShouldNotCreateAnyGarbageReferences ()
102
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise ()
93
103
{
94
- if (class_exists ('React\Promise\When ' ) && PHP_VERSION_ID >= 50400 ) {
95
- $ this ->markTestSkipped ('Not supported on legacy Promise v1 API with PHP 5.4+ ' );
104
+ if (class_exists ('React\Promise\When ' )) {
105
+ $ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
96
106
}
97
107
98
108
gc_collect_cycles ();
99
109
100
- $ promise = Promise \reject (new \RuntimeException ());
110
+ $ promise = new Promise (function () {
111
+ throw new \RuntimeException ();
112
+ });
101
113
try {
102
114
React \Async \await ($ promise );
103
115
} catch (\Exception $ e ) {
@@ -108,7 +120,7 @@ public function testAwaitOneRejectedShouldNotCreateAnyGarbageReferences()
108
120
$ this ->assertEquals (0 , gc_collect_cycles ());
109
121
}
110
122
111
- public function testAwaitNullValueShouldNotCreateAnyGarbageReferences ()
123
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue ()
112
124
{
113
125
if (!interface_exists ('React\Promise\CancellablePromiseInterface ' )) {
114
126
$ this ->markTestSkipped ('Promises must be rejected with a \Throwable instance since Promise v3 ' );
@@ -120,7 +132,9 @@ public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
120
132
121
133
gc_collect_cycles ();
122
134
123
- $ promise = Promise \reject (null );
135
+ $ promise = new Promise (function ($ _ , $ reject ) {
136
+ $ reject (null );
137
+ });
124
138
try {
125
139
React \Async \await ($ promise );
126
140
} catch (\Exception $ e ) {
@@ -131,35 +145,6 @@ public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
131
145
$ this ->assertEquals (0 , gc_collect_cycles ());
132
146
}
133
147
134
- protected function createPromiseResolved ($ value = null , $ delay = 0.01 )
135
- {
136
- $ deferred = new Deferred ();
137
-
138
- Loop::addTimer ($ delay , function () use ($ deferred , $ value ) {
139
- $ deferred ->resolve ($ value );
140
- });
141
-
142
- return $ deferred ->promise ();
143
- }
144
-
145
- protected function createPromiseRejected ($ value = null , $ delay = 0.01 )
146
- {
147
- $ deferred = new Deferred ();
148
-
149
- Loop::addTimer ($ delay , function () use ($ deferred , $ value ) {
150
- $ deferred ->reject ($ value );
151
- });
152
-
153
- return $ deferred ->promise ();
154
- }
155
-
156
- protected function createTimerInterrupt ($ delay = 0.01 )
157
- {
158
- Loop::addTimer ($ delay , function () {
159
- Loop::stop ();
160
- });
161
- }
162
-
163
148
public function setExpectedException ($ exception , $ exceptionMessage = '' , $ exceptionCode = null )
164
149
{
165
150
if (method_exists ($ this , 'expectException ' )) {
0 commit comments