8
8
9
9
class AwaitTest extends TestCase
10
10
{
11
- public function testAwaitThrowsExceptionWhenPromiseIsRejectedWithException ()
11
+ /**
12
+ * @dataProvider provideAwaiters
13
+ */
14
+ public function testAwaitThrowsExceptionWhenPromiseIsRejectedWithException (callable $ await )
12
15
{
13
16
$ promise = new Promise (function () {
14
17
throw new \Exception ('test ' );
15
18
});
16
19
17
20
$ this ->expectException (\Exception::class);
18
21
$ this ->expectExceptionMessage ('test ' );
19
- React \ Async \ await ($ promise );
22
+ $ await ($ promise );
20
23
}
21
24
22
- public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithFalse ()
25
+ /**
26
+ * @dataProvider provideAwaiters
27
+ */
28
+ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithFalse (callable $ await )
23
29
{
24
30
if (!interface_exists ('React\Promise\CancellablePromiseInterface ' )) {
25
31
$ this ->markTestSkipped ('Promises must be rejected with a \Throwable instance since Promise v3 ' );
@@ -31,10 +37,13 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
31
37
32
38
$ this ->expectException (\UnexpectedValueException::class);
33
39
$ this ->expectExceptionMessage ('Promise rejected with unexpected value of type bool ' );
34
- React \ Async \ await ($ promise );
40
+ $ await ($ promise );
35
41
}
36
42
37
- public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithNull ()
43
+ /**
44
+ * @dataProvider provideAwaiters
45
+ */
46
+ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithNull (callable $ await )
38
47
{
39
48
if (!interface_exists ('React\Promise\CancellablePromiseInterface ' )) {
40
49
$ this ->markTestSkipped ('Promises must be rejected with a \Throwable instance since Promise v3 ' );
@@ -46,10 +55,13 @@ public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWith
46
55
47
56
$ this ->expectException (\UnexpectedValueException::class);
48
57
$ this ->expectExceptionMessage ('Promise rejected with unexpected value of type NULL ' );
49
- React \ Async \ await ($ promise );
58
+ $ await ($ promise );
50
59
}
51
60
52
- public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError ()
61
+ /**
62
+ * @dataProvider provideAwaiters
63
+ */
64
+ public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError (callable $ await )
53
65
{
54
66
$ promise = new Promise (function ($ _ , $ reject ) {
55
67
throw new \Error ('Test ' , 42 );
@@ -58,19 +70,25 @@ public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError()
58
70
$ this ->expectException (\Error::class);
59
71
$ this ->expectExceptionMessage ('Test ' );
60
72
$ this ->expectExceptionCode (42 );
61
- React \ Async \ await ($ promise );
73
+ $ await ($ promise );
62
74
}
63
75
64
- public function testAwaitReturnsValueWhenPromiseIsFullfilled ()
76
+ /**
77
+ * @dataProvider provideAwaiters
78
+ */
79
+ public function testAwaitReturnsValueWhenPromiseIsFullfilled (callable $ await )
65
80
{
66
81
$ promise = new Promise (function ($ resolve ) {
67
82
$ resolve (42 );
68
83
});
69
84
70
- $ this ->assertEquals (42 , React \ Async \ await ($ promise ));
85
+ $ this ->assertEquals (42 , $ await ($ promise ));
71
86
}
72
87
73
- public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerStopsLoop ()
88
+ /**
89
+ * @dataProvider provideAwaiters
90
+ */
91
+ public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerStopsLoop (callable $ await )
74
92
{
75
93
$ this ->markTestIncomplete ();
76
94
@@ -83,10 +101,13 @@ public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerSto
83
101
Loop::stop ();
84
102
});
85
103
86
- $ this ->assertEquals (2 , React \ Async \ await ($ promise ));
104
+ $ this ->assertEquals (2 , $ await ($ promise ));
87
105
}
88
106
89
- public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise ()
107
+ /**
108
+ * @dataProvider provideAwaiters
109
+ */
110
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise (callable $ await )
90
111
{
91
112
if (class_exists ('React\Promise\When ' )) {
92
113
$ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
@@ -97,13 +118,16 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise()
97
118
$ promise = new Promise (function ($ resolve ) {
98
119
$ resolve (42 );
99
120
});
100
- React \ Async \ await ($ promise );
121
+ $ await ($ promise );
101
122
unset($ promise );
102
123
103
124
$ this ->assertEquals (0 , gc_collect_cycles ());
104
125
}
105
126
106
- public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise ()
127
+ /**
128
+ * @dataProvider provideAwaiters
129
+ */
130
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise (callable $ await )
107
131
{
108
132
if (class_exists ('React\Promise\When ' )) {
109
133
$ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
@@ -115,7 +139,7 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise()
115
139
throw new \RuntimeException ();
116
140
});
117
141
try {
118
- React \ Async \ await ($ promise );
142
+ $ await ($ promise );
119
143
} catch (\Exception $ e ) {
120
144
// no-op
121
145
}
@@ -124,7 +148,10 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise()
124
148
$ this ->assertEquals (0 , gc_collect_cycles ());
125
149
}
126
150
127
- public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue ()
151
+ /**
152
+ * @dataProvider provideAwaiters
153
+ */
154
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue (callable $ await )
128
155
{
129
156
if (!interface_exists ('React\Promise\CancellablePromiseInterface ' )) {
130
157
$ this ->markTestSkipped ('Promises must be rejected with a \Throwable instance since Promise v3 ' );
@@ -140,12 +167,18 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
140
167
$ reject (null );
141
168
});
142
169
try {
143
- React \ Async \ await ($ promise );
170
+ $ await ($ promise );
144
171
} catch (\Exception $ e ) {
145
172
// no-op
146
173
}
147
174
unset($ promise , $ e );
148
175
149
176
$ this ->assertEquals (0 , gc_collect_cycles ());
150
177
}
178
+
179
+ public function provideAwaiters (): iterable
180
+ {
181
+ yield 'await ' => [static fn (React \Promise \PromiseInterface $ promise ): mixed => React \Async \await ($ promise )];
182
+ yield 'async ' => [static fn (React \Promise \PromiseInterface $ promise ): mixed => React \Async \await (React \Async \async (static fn (): mixed => $ promise ))];
183
+ }
151
184
}
0 commit comments