Skip to content

Commit 0df894e

Browse files
authored
Merge pull request #32 from clue-labs/tests
Update test suite to avoid using deprecated functions
2 parents b314b5e + c429b8c commit 0df894e

File tree

4 files changed

+53
-107
lines changed

4 files changed

+53
-107
lines changed

composer.json

-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939
"react/promise": "^3 || ^2.1 || ^1.2"
4040
},
4141
"require-dev": {
42-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
43-
"react/promise-timer": "^1.0",
44-
"clue/block-react": "^1.0",
4542
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35"
4643
}
4744
}

tests/BufferTest.php

+9-22
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace React\Tests\Promise\Stream;
44

5-
use Clue\React\Block;
6-
use React\EventLoop\Factory;
75
use React\Promise\Stream;
86
use React\Stream\ThroughStream;
97

@@ -92,37 +90,26 @@ public function testCancelPendingStreamWillReject()
9290

9391
public function testMaximumSize()
9492
{
95-
$loop = Factory::create();
9693
$stream = new ThroughStream();
9794

98-
$loop->addTimer(0.1, function () use ($stream) {
99-
$stream->write('12345678910111213141516');
100-
});
101-
10295
$promise = Stream\buffer($stream, 16);
10396

104-
if (method_exists($this, 'expectException')) {
105-
$this->expectException('OverflowException');
106-
$this->expectExceptionMessage('Buffer exceeded maximum length');
107-
} else {
108-
$this->setExpectedException('\OverflowException', 'Buffer exceeded maximum length');
109-
}
110-
Block\await($promise, $loop, 10);
97+
$stream->write('12345678910111213141516');
98+
99+
$promise->then(null, $this->expectCallableOnceWith(new \OverflowException(
100+
'Buffer exceeded maximum length'
101+
)));
111102
}
112103

113104
public function testUnderMaximumSize()
114105
{
115-
$loop = Factory::create();
116106
$stream = new ThroughStream();
117107

118-
$loop->addTimer(0.1, function () use ($stream) {
119-
$stream->write('1234567891011');
120-
$stream->end();
121-
});
122-
123108
$promise = Stream\buffer($stream, 16);
124109

125-
$result = Block\await($promise, $loop, 10);
126-
$this->assertSame('1234567891011', $result);
110+
$stream->write('1234567891011');
111+
$stream->end();
112+
113+
$promise->then($this->expectCallableOnceWith('1234567891011'));
127114
}
128115
}

tests/UnwrapReadableTest.php

+16-35
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,13 @@
22

33
namespace React\Tests\Promise\Stream;
44

5-
use Clue\React\Block;
6-
use React\EventLoop\Factory;
75
use React\Promise;
6+
use React\Promise\Deferred;
87
use React\Promise\Stream;
9-
use React\Promise\Timer;
108
use React\Stream\ThroughStream;
119

1210
class UnwrapReadableTest extends TestCase
1311
{
14-
private $loop;
15-
16-
/**
17-
* @before
18-
*/
19-
public function setUpLoop()
20-
{
21-
$this->loop = Factory::create();
22-
}
23-
2412
public function testReturnsReadableStreamForPromise()
2513
{
2614
$promise = new \React\Promise\Promise(function () { });
@@ -45,15 +33,15 @@ public function testClosingStreamMakesItNotReadable()
4533

4634
public function testClosingRejectingStreamMakesItNotReadable()
4735
{
48-
$promise = Timer\reject(0.001, $this->loop);
49-
$stream = Stream\unwrapReadable($promise);
36+
$deferred = new Deferred();
37+
38+
$stream = Stream\unwrapReadable($deferred->promise());
5039

5140
$stream->on('close', $this->expectCallableOnce());
5241
$stream->on('end', $this->expectCallableNever());
5342
$stream->on('error', $this->expectCallableNever());
5443

5544
$stream->close();
56-
$this->loop->run();
5745

5846
$this->assertFalse($stream->isReadable());
5947
}
@@ -70,32 +58,32 @@ public function testClosingStreamWillCancelInputPromiseAndMakeStreamNotReadable(
7058

7159
public function testEmitsErrorWhenPromiseRejects()
7260
{
73-
$promise = Timer\reject(0.001, $this->loop);
61+
$deferred = new Deferred();
7462

75-
$stream = Stream\unwrapReadable($promise);
63+
$stream = Stream\unwrapReadable($deferred->promise());
7664

7765
$this->assertTrue($stream->isReadable());
7866

7967
$stream->on('error', $this->expectCallableOnce());
8068
$stream->on('end', $this->expectCallableNever());
8169

82-
$this->loop->run();
70+
$deferred->reject(new \RuntimeException());
8371

8472
$this->assertFalse($stream->isReadable());
8573
}
8674

8775
public function testEmitsErrorWhenPromiseResolvesWithWrongValue()
8876
{
89-
$promise = Timer\resolve(0.001, $this->loop);
77+
$deferred = new Deferred();
9078

91-
$stream = Stream\unwrapReadable($promise);
79+
$stream = Stream\unwrapReadable($deferred->promise());
9280

9381
$this->assertTrue($stream->isReadable());
9482

9583
$stream->on('error', $this->expectCallableOnce());
9684
$stream->on('end', $this->expectCallableNever());
9785

98-
$this->loop->run();
86+
$deferred->resolve(42);
9987

10088
$this->assertFalse($stream->isReadable());
10189
}
@@ -126,17 +114,15 @@ public function testReturnsStreamThatWillBeClosedWhenPromiseResolvesWithClosedIn
126114
$input = new ThroughStream();
127115
$input->close();
128116

129-
$promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) {
130-
return $input;
131-
});
117+
$deferred = new Deferred();
132118

133-
$stream = Stream\unwrapReadable($promise);
119+
$stream = Stream\unwrapReadable($deferred->promise());
134120

135121
$this->assertTrue($stream->isReadable());
136122

137123
$stream->on('close', $this->expectCallableOnce());
138124

139-
$this->loop->run();
125+
$deferred->resolve($input);
140126

141127
$this->assertFalse($stream->isReadable());
142128
}
@@ -284,20 +270,15 @@ public function testClosingStreamWillCloseStreamIfItIgnoredCancellationAndResolv
284270
{
285271
$input = new ThroughStream();
286272

287-
$loop = $this->loop;
288-
$promise = new Promise\Promise(function ($resolve) use ($loop, $input) {
289-
$loop->addTimer(0.001, function () use ($resolve, $input) {
290-
$resolve($input);
291-
});
292-
});
273+
$deferred = new Deferred();
293274

294-
$stream = Stream\unwrapReadable($promise);
275+
$stream = Stream\unwrapReadable($deferred->promise());
295276

296277
$stream->on('close', $this->expectCallableOnce());
297278

298279
$stream->close();
299280

300-
Block\await($promise, $this->loop);
281+
$deferred->resolve($input);
301282

302283
$this->assertFalse($input->isReadable());
303284
}

tests/UnwrapWritableTest.php

+28-47
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,13 @@
22

33
namespace React\Tests\Promise\Stream;
44

5-
use Clue\React\Block;
6-
use React\EventLoop\Factory;
75
use React\Promise;
86
use React\Promise\Deferred;
97
use React\Promise\Stream;
10-
use React\Promise\Timer;
118
use React\Stream\ThroughStream;
129

1310
class UnwrapWritableTest extends TestCase
1411
{
15-
private $loop;
16-
17-
/**
18-
* @before
19-
*/
20-
public function setUpLoop()
21-
{
22-
$this->loop = Factory::create();
23-
}
24-
2512
public function testReturnsWritableStreamForPromise()
2613
{
2714
$promise = new \React\Promise\Promise(function () { });
@@ -45,14 +32,14 @@ public function testClosingStreamMakesItNotWritable()
4532

4633
public function testClosingRejectingStreamMakesItNotWritable()
4734
{
48-
$promise = Timer\reject(0.001, $this->loop);
49-
$stream = Stream\unwrapWritable($promise);
35+
$deferred = new Deferred();
36+
37+
$stream = Stream\unwrapWritable($deferred->promise());
5038

5139
$stream->on('close', $this->expectCallableOnce());
5240
$stream->on('error', $this->expectCallableNever());
5341

5442
$stream->close();
55-
$this->loop->run();
5643

5744
$this->assertFalse($stream->isWritable());
5845
}
@@ -69,32 +56,32 @@ public function testClosingStreamWillCancelInputPromiseAndMakeStreamNotWritable(
6956

7057
public function testEmitsErrorWhenPromiseRejects()
7158
{
72-
$promise = Timer\reject(0.001, $this->loop);
59+
$deferred = new Deferred();
7360

74-
$stream = Stream\unwrapWritable($promise);
61+
$stream = Stream\unwrapWritable($deferred->promise());
7562

7663
$this->assertTrue($stream->isWritable());
7764

7865
$stream->on('error', $this->expectCallableOnce());
7966
$stream->on('close', $this->expectCallableOnce());
8067

81-
$this->loop->run();
68+
$deferred->reject(new \RuntimeException());
8269

8370
$this->assertFalse($stream->isWritable());
8471
}
8572

8673
public function testEmitsErrorWhenPromiseResolvesWithWrongValue()
8774
{
88-
$promise = Timer\resolve(0.001, $this->loop);
75+
$deferred = new Deferred();
8976

90-
$stream = Stream\unwrapWritable($promise);
77+
$stream = Stream\unwrapWritable($deferred->promise());
9178

9279
$this->assertTrue($stream->isWritable());
9380

9481
$stream->on('error', $this->expectCallableOnce());
9582
$stream->on('close', $this->expectCallableOnce());
9683

97-
$this->loop->run();
84+
$deferred->resolve(42);
9885

9986
$this->assertFalse($stream->isWritable());
10087
}
@@ -125,17 +112,15 @@ public function testReturnsStreamThatWillBeClosedWhenPromiseResolvesWithClosedIn
125112
$input = new ThroughStream();
126113
$input->close();
127114

128-
$promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) {
129-
return $input;
130-
});
115+
$deferred = new Deferred();
131116

132-
$stream = Stream\unwrapWritable($promise);
117+
$stream = Stream\unwrapWritable($deferred->promise());
133118

134119
$this->assertTrue($stream->isWritable());
135120

136121
$stream->on('close', $this->expectCallableOnce());
137122

138-
$this->loop->run();
123+
$deferred->resolve($input);
139124

140125
$this->assertFalse($stream->isWritable());
141126
}
@@ -162,14 +147,13 @@ public function testForwardsOriginalDataOncePromiseResolves()
162147
$input->expects($this->once())->method('write')->with($data);
163148
$input->expects($this->never())->method('end');
164149

165-
$promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) {
166-
return $input;
167-
});
168-
$stream = Stream\unwrapWritable($promise);
150+
$deferred = new Deferred();
151+
152+
$stream = Stream\unwrapWritable($deferred->promise());
169153

170154
$stream->write($data);
171155

172-
$this->loop->run();
156+
$deferred->resolve($input);
173157
}
174158

175159
public function testForwardsDataInOriginalChunksOncePromiseResolves()
@@ -179,15 +163,14 @@ public function testForwardsDataInOriginalChunksOncePromiseResolves()
179163
$input->expects($this->exactly(2))->method('write')->withConsecutive(array('hello'), array('world'));
180164
$input->expects($this->never())->method('end');
181165

182-
$promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) {
183-
return $input;
184-
});
185-
$stream = Stream\unwrapWritable($promise);
166+
$deferred = new Deferred();
167+
168+
$stream = Stream\unwrapWritable($deferred->promise());
186169

187170
$stream->write('hello');
188171
$stream->write('world');
189172

190-
$this->loop->run();
173+
$deferred->resolve($input);
191174
}
192175

193176
public function testForwardsDataAndEndImmediatelyIfPromiseIsAlreadyResolved()
@@ -211,16 +194,15 @@ public function testForwardsDataAndEndOncePromiseResolves()
211194
$input->expects($this->exactly(3))->method('write')->withConsecutive(array('hello'), array('world'), array('!'));
212195
$input->expects($this->once())->method('end');
213196

214-
$promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) {
215-
return $input;
216-
});
217-
$stream = Stream\unwrapWritable($promise);
197+
$deferred = new Deferred();
198+
199+
$stream = Stream\unwrapWritable($deferred->promise());
218200

219201
$stream->write('hello');
220202
$stream->write('world');
221203
$stream->end('!');
222204

223-
$this->loop->run();
205+
$deferred->resolve($input);
224206
}
225207

226208
public function testForwardsNoDataWhenWritingAfterEndIfPromiseIsAlreadyResolved()
@@ -245,15 +227,14 @@ public function testForwardsNoDataWhenWritingAfterEndOncePromiseResolves()
245227
$input->expects($this->never())->method('write');
246228
$input->expects($this->once())->method('end');
247229

248-
$promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) {
249-
return $input;
250-
});
251-
$stream = Stream\unwrapWritable($promise);
230+
$deferred = new Deferred();
231+
232+
$stream = Stream\unwrapWritable($deferred->promise());
252233

253234
$stream->end();
254235
$stream->write('nope');
255236

256-
$this->loop->run();
237+
$deferred->resolve($input);
257238
}
258239

259240
public function testWriteReturnsFalseWhenPromiseIsPending()

0 commit comments

Comments
 (0)