Skip to content

Commit eed69a5

Browse files
committed
Clean up test suite for await() function
1 parent 5f7e7d3 commit eed69a5

File tree

1 file changed

+44
-59
lines changed

1 file changed

+44
-59
lines changed

tests/AwaitTest.php

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,43 @@
44

55
use React;
66
use React\EventLoop\Loop;
7-
use React\Promise;
8-
use React\Promise\Deferred;
7+
use React\Promise\Promise;
98

109
class AwaitTest extends TestCase
1110
{
12-
public function testAwaitOneRejected()
11+
public function testAwaitThrowsExceptionWhenPromiseIsRejectedWithException()
1312
{
14-
$promise = $this->createPromiseRejected(new \Exception('test'));
13+
$promise = new Promise(function () {
14+
throw new \Exception('test');
15+
});
1516

1617
$this->setExpectedException('Exception', 'test');
1718
React\Async\await($promise);
1819
}
1920

20-
public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException()
21+
public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithFalse()
2122
{
2223
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
2324
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
2425
}
2526

26-
$promise = Promise\reject(false);
27+
$promise = new Promise(function ($_, $reject) {
28+
$reject(false);
29+
});
2730

2831
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
2932
React\Async\await($promise);
3033
}
3134

32-
public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
35+
public function testAwaitThrowsUnexpectedValueExceptionWhenPromiseIsRejectedWithNull()
3336
{
3437
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
3538
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
3639
}
3740

38-
$promise = Promise\reject(null);
41+
$promise = new Promise(function ($_, $reject) {
42+
$reject(null);
43+
});
3944

4045
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
4146
React\Async\await($promise);
@@ -44,60 +49,67 @@ public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
4449
/**
4550
* @requires PHP 7
4651
*/
47-
public function testAwaitRejectedWithPhp7ErrorWillThrowOriginalError()
52+
public function testAwaitThrowsErrorWhenPromiseIsRejectedWithError()
4853
{
49-
$promise = Promise\reject(new \Error('Test', 42));
54+
$promise = new Promise(function ($_, $reject) {
55+
throw new \Error('Test', 42);
56+
});
5057

5158
$this->setExpectedException('Error', 'Test', 42);
5259
React\Async\await($promise);
5360
}
5461

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()
6363
{
64-
$promise = Promise\resolve(42);
64+
$promise = new Promise(function ($resolve) {
65+
$resolve(42);
66+
});
6567

6668
$this->assertEquals(42, React\Async\await($promise));
6769
}
6870

69-
public function testAwaitOneInterrupted()
71+
public function testAwaitReturnsValueWhenPromiseIsFulfilledEvenWhenOtherTimerStopsLoop()
7072
{
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+
});
7381

7482
$this->assertEquals(2, React\Async\await($promise));
7583
}
7684

77-
public function testAwaitOneResolvesShouldNotCreateAnyGarbageReferences()
85+
public function testAwaitShouldNotCreateAnyGarbageReferencesForResolvedPromise()
7886
{
7987
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
8088
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
8189
}
8290

8391
gc_collect_cycles();
8492

85-
$promise = Promise\resolve(1);
93+
$promise = new Promise(function ($resolve) {
94+
$resolve(42);
95+
});
8696
React\Async\await($promise);
8797
unset($promise);
8898

8999
$this->assertEquals(0, gc_collect_cycles());
90100
}
91101

92-
public function testAwaitOneRejectedShouldNotCreateAnyGarbageReferences()
102+
public function testAwaitShouldNotCreateAnyGarbageReferencesForRejectedPromise()
93103
{
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');
96106
}
97107

98108
gc_collect_cycles();
99109

100-
$promise = Promise\reject(new \RuntimeException());
110+
$promise = new Promise(function () {
111+
throw new \RuntimeException();
112+
});
101113
try {
102114
React\Async\await($promise);
103115
} catch (\Exception $e) {
@@ -108,7 +120,7 @@ public function testAwaitOneRejectedShouldNotCreateAnyGarbageReferences()
108120
$this->assertEquals(0, gc_collect_cycles());
109121
}
110122

111-
public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
123+
public function testAwaitShouldNotCreateAnyGarbageReferencesForPromiseRejectedWithNullValue()
112124
{
113125
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
114126
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
@@ -120,7 +132,9 @@ public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
120132

121133
gc_collect_cycles();
122134

123-
$promise = Promise\reject(null);
135+
$promise = new Promise(function ($_, $reject) {
136+
$reject(null);
137+
});
124138
try {
125139
React\Async\await($promise);
126140
} catch (\Exception $e) {
@@ -131,35 +145,6 @@ public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
131145
$this->assertEquals(0, gc_collect_cycles());
132146
}
133147

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-
163148
public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null)
164149
{
165150
if (method_exists($this, 'expectException')) {

0 commit comments

Comments
 (0)