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 testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise ()
88
+ /**
89
+ * @dataProvider provideAwaiters
90
+ */
91
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise (callable $ await )
74
92
{
75
93
if (class_exists ('React\Promise\When ' )) {
76
94
$ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
@@ -81,13 +99,16 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise()
81
99
$ promise = new Promise (function ($ resolve ) {
82
100
$ resolve (42 );
83
101
});
84
- React \ Async \ await ($ promise );
102
+ $ await ($ promise );
85
103
unset($ promise );
86
104
87
105
$ this ->assertEquals (0 , gc_collect_cycles ());
88
106
}
89
107
90
- public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise ()
108
+ /**
109
+ * @dataProvider provideAwaiters
110
+ */
111
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise (callable $ await )
91
112
{
92
113
if (class_exists ('React\Promise\When ' )) {
93
114
$ this ->markTestSkipped ('Not supported on legacy Promise v1 API ' );
@@ -99,7 +120,7 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise()
99
120
throw new \RuntimeException ();
100
121
});
101
122
try {
102
- React \ Async \ await ($ promise );
123
+ $ await ($ promise );
103
124
} catch (\Exception $ e ) {
104
125
// no-op
105
126
}
@@ -108,7 +129,10 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise()
108
129
$ this ->assertEquals (0 , gc_collect_cycles ());
109
130
}
110
131
111
- public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue ()
132
+ /**
133
+ * @dataProvider provideAwaiters
134
+ */
135
+ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue (callable $ await )
112
136
{
113
137
if (!interface_exists ('React\Promise\CancellablePromiseInterface ' )) {
114
138
$ this ->markTestSkipped ('Promises must be rejected with a \Throwable instance since Promise v3 ' );
@@ -124,12 +148,18 @@ public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWi
124
148
$ reject (null );
125
149
});
126
150
try {
127
- React \ Async \ await ($ promise );
151
+ $ await ($ promise );
128
152
} catch (\Exception $ e ) {
129
153
// no-op
130
154
}
131
155
unset($ promise , $ e );
132
156
133
157
$ this ->assertEquals (0 , gc_collect_cycles ());
134
158
}
159
+
160
+ public function provideAwaiters (): iterable
161
+ {
162
+ yield 'await ' => [static fn (React \Promise \PromiseInterface $ promise ): mixed => React \Async \await ($ promise )];
163
+ yield 'async ' => [static fn (React \Promise \PromiseInterface $ promise ): mixed => React \Async \await (React \Async \async (static fn (): mixed => $ promise ))];
164
+ }
135
165
}
0 commit comments