diff --git a/composer.json b/composer.json index f220f40..669dab1 100644 --- a/composer.json +++ b/composer.json @@ -39,9 +39,6 @@ "react/promise": "^3 || ^2.1 || ^1.2" }, "require-dev": { - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", - "react/promise-timer": "^1.0", - "clue/block-react": "^1.0", "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35" } } diff --git a/tests/BufferTest.php b/tests/BufferTest.php index 45bc06c..1dd7f47 100644 --- a/tests/BufferTest.php +++ b/tests/BufferTest.php @@ -2,8 +2,6 @@ namespace React\Tests\Promise\Stream; -use Clue\React\Block; -use React\EventLoop\Factory; use React\Promise\Stream; use React\Stream\ThroughStream; @@ -92,37 +90,26 @@ public function testCancelPendingStreamWillReject() public function testMaximumSize() { - $loop = Factory::create(); $stream = new ThroughStream(); - $loop->addTimer(0.1, function () use ($stream) { - $stream->write('12345678910111213141516'); - }); - $promise = Stream\buffer($stream, 16); - if (method_exists($this, 'expectException')) { - $this->expectException('OverflowException'); - $this->expectExceptionMessage('Buffer exceeded maximum length'); - } else { - $this->setExpectedException('\OverflowException', 'Buffer exceeded maximum length'); - } - Block\await($promise, $loop, 10); + $stream->write('12345678910111213141516'); + + $promise->then(null, $this->expectCallableOnceWith(new \OverflowException( + 'Buffer exceeded maximum length' + ))); } public function testUnderMaximumSize() { - $loop = Factory::create(); $stream = new ThroughStream(); - $loop->addTimer(0.1, function () use ($stream) { - $stream->write('1234567891011'); - $stream->end(); - }); - $promise = Stream\buffer($stream, 16); - $result = Block\await($promise, $loop, 10); - $this->assertSame('1234567891011', $result); + $stream->write('1234567891011'); + $stream->end(); + + $promise->then($this->expectCallableOnceWith('1234567891011')); } } diff --git a/tests/UnwrapReadableTest.php b/tests/UnwrapReadableTest.php index 59c578f..36c15c6 100644 --- a/tests/UnwrapReadableTest.php +++ b/tests/UnwrapReadableTest.php @@ -2,25 +2,13 @@ namespace React\Tests\Promise\Stream; -use Clue\React\Block; -use React\EventLoop\Factory; use React\Promise; +use React\Promise\Deferred; use React\Promise\Stream; -use React\Promise\Timer; use React\Stream\ThroughStream; class UnwrapReadableTest extends TestCase { - private $loop; - - /** - * @before - */ - public function setUpLoop() - { - $this->loop = Factory::create(); - } - public function testReturnsReadableStreamForPromise() { $promise = new \React\Promise\Promise(function () { }); @@ -45,15 +33,15 @@ public function testClosingStreamMakesItNotReadable() public function testClosingRejectingStreamMakesItNotReadable() { - $promise = Timer\reject(0.001, $this->loop); - $stream = Stream\unwrapReadable($promise); + $deferred = new Deferred(); + + $stream = Stream\unwrapReadable($deferred->promise()); $stream->on('close', $this->expectCallableOnce()); $stream->on('end', $this->expectCallableNever()); $stream->on('error', $this->expectCallableNever()); $stream->close(); - $this->loop->run(); $this->assertFalse($stream->isReadable()); } @@ -70,32 +58,32 @@ public function testClosingStreamWillCancelInputPromiseAndMakeStreamNotReadable( public function testEmitsErrorWhenPromiseRejects() { - $promise = Timer\reject(0.001, $this->loop); + $deferred = new Deferred(); - $stream = Stream\unwrapReadable($promise); + $stream = Stream\unwrapReadable($deferred->promise()); $this->assertTrue($stream->isReadable()); $stream->on('error', $this->expectCallableOnce()); $stream->on('end', $this->expectCallableNever()); - $this->loop->run(); + $deferred->reject(new \RuntimeException()); $this->assertFalse($stream->isReadable()); } public function testEmitsErrorWhenPromiseResolvesWithWrongValue() { - $promise = Timer\resolve(0.001, $this->loop); + $deferred = new Deferred(); - $stream = Stream\unwrapReadable($promise); + $stream = Stream\unwrapReadable($deferred->promise()); $this->assertTrue($stream->isReadable()); $stream->on('error', $this->expectCallableOnce()); $stream->on('end', $this->expectCallableNever()); - $this->loop->run(); + $deferred->resolve(42); $this->assertFalse($stream->isReadable()); } @@ -126,17 +114,15 @@ public function testReturnsStreamThatWillBeClosedWhenPromiseResolvesWithClosedIn $input = new ThroughStream(); $input->close(); - $promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) { - return $input; - }); + $deferred = new Deferred(); - $stream = Stream\unwrapReadable($promise); + $stream = Stream\unwrapReadable($deferred->promise()); $this->assertTrue($stream->isReadable()); $stream->on('close', $this->expectCallableOnce()); - $this->loop->run(); + $deferred->resolve($input); $this->assertFalse($stream->isReadable()); } @@ -284,20 +270,15 @@ public function testClosingStreamWillCloseStreamIfItIgnoredCancellationAndResolv { $input = new ThroughStream(); - $loop = $this->loop; - $promise = new Promise\Promise(function ($resolve) use ($loop, $input) { - $loop->addTimer(0.001, function () use ($resolve, $input) { - $resolve($input); - }); - }); + $deferred = new Deferred(); - $stream = Stream\unwrapReadable($promise); + $stream = Stream\unwrapReadable($deferred->promise()); $stream->on('close', $this->expectCallableOnce()); $stream->close(); - Block\await($promise, $this->loop); + $deferred->resolve($input); $this->assertFalse($input->isReadable()); } diff --git a/tests/UnwrapWritableTest.php b/tests/UnwrapWritableTest.php index ecf003c..5ba47c4 100644 --- a/tests/UnwrapWritableTest.php +++ b/tests/UnwrapWritableTest.php @@ -2,26 +2,13 @@ namespace React\Tests\Promise\Stream; -use Clue\React\Block; -use React\EventLoop\Factory; use React\Promise; use React\Promise\Deferred; use React\Promise\Stream; -use React\Promise\Timer; use React\Stream\ThroughStream; class UnwrapWritableTest extends TestCase { - private $loop; - - /** - * @before - */ - public function setUpLoop() - { - $this->loop = Factory::create(); - } - public function testReturnsWritableStreamForPromise() { $promise = new \React\Promise\Promise(function () { }); @@ -45,14 +32,14 @@ public function testClosingStreamMakesItNotWritable() public function testClosingRejectingStreamMakesItNotWritable() { - $promise = Timer\reject(0.001, $this->loop); - $stream = Stream\unwrapWritable($promise); + $deferred = new Deferred(); + + $stream = Stream\unwrapWritable($deferred->promise()); $stream->on('close', $this->expectCallableOnce()); $stream->on('error', $this->expectCallableNever()); $stream->close(); - $this->loop->run(); $this->assertFalse($stream->isWritable()); } @@ -69,32 +56,32 @@ public function testClosingStreamWillCancelInputPromiseAndMakeStreamNotWritable( public function testEmitsErrorWhenPromiseRejects() { - $promise = Timer\reject(0.001, $this->loop); + $deferred = new Deferred(); - $stream = Stream\unwrapWritable($promise); + $stream = Stream\unwrapWritable($deferred->promise()); $this->assertTrue($stream->isWritable()); $stream->on('error', $this->expectCallableOnce()); $stream->on('close', $this->expectCallableOnce()); - $this->loop->run(); + $deferred->reject(new \RuntimeException()); $this->assertFalse($stream->isWritable()); } public function testEmitsErrorWhenPromiseResolvesWithWrongValue() { - $promise = Timer\resolve(0.001, $this->loop); + $deferred = new Deferred(); - $stream = Stream\unwrapWritable($promise); + $stream = Stream\unwrapWritable($deferred->promise()); $this->assertTrue($stream->isWritable()); $stream->on('error', $this->expectCallableOnce()); $stream->on('close', $this->expectCallableOnce()); - $this->loop->run(); + $deferred->resolve(42); $this->assertFalse($stream->isWritable()); } @@ -125,17 +112,15 @@ public function testReturnsStreamThatWillBeClosedWhenPromiseResolvesWithClosedIn $input = new ThroughStream(); $input->close(); - $promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) { - return $input; - }); + $deferred = new Deferred(); - $stream = Stream\unwrapWritable($promise); + $stream = Stream\unwrapWritable($deferred->promise()); $this->assertTrue($stream->isWritable()); $stream->on('close', $this->expectCallableOnce()); - $this->loop->run(); + $deferred->resolve($input); $this->assertFalse($stream->isWritable()); } @@ -162,14 +147,13 @@ public function testForwardsOriginalDataOncePromiseResolves() $input->expects($this->once())->method('write')->with($data); $input->expects($this->never())->method('end'); - $promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) { - return $input; - }); - $stream = Stream\unwrapWritable($promise); + $deferred = new Deferred(); + + $stream = Stream\unwrapWritable($deferred->promise()); $stream->write($data); - $this->loop->run(); + $deferred->resolve($input); } public function testForwardsDataInOriginalChunksOncePromiseResolves() @@ -179,15 +163,14 @@ public function testForwardsDataInOriginalChunksOncePromiseResolves() $input->expects($this->exactly(2))->method('write')->withConsecutive(array('hello'), array('world')); $input->expects($this->never())->method('end'); - $promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) { - return $input; - }); - $stream = Stream\unwrapWritable($promise); + $deferred = new Deferred(); + + $stream = Stream\unwrapWritable($deferred->promise()); $stream->write('hello'); $stream->write('world'); - $this->loop->run(); + $deferred->resolve($input); } public function testForwardsDataAndEndImmediatelyIfPromiseIsAlreadyResolved() @@ -211,16 +194,15 @@ public function testForwardsDataAndEndOncePromiseResolves() $input->expects($this->exactly(3))->method('write')->withConsecutive(array('hello'), array('world'), array('!')); $input->expects($this->once())->method('end'); - $promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) { - return $input; - }); - $stream = Stream\unwrapWritable($promise); + $deferred = new Deferred(); + + $stream = Stream\unwrapWritable($deferred->promise()); $stream->write('hello'); $stream->write('world'); $stream->end('!'); - $this->loop->run(); + $deferred->resolve($input); } public function testForwardsNoDataWhenWritingAfterEndIfPromiseIsAlreadyResolved() @@ -245,15 +227,14 @@ public function testForwardsNoDataWhenWritingAfterEndOncePromiseResolves() $input->expects($this->never())->method('write'); $input->expects($this->once())->method('end'); - $promise = Timer\resolve(0.001, $this->loop)->then(function () use ($input) { - return $input; - }); - $stream = Stream\unwrapWritable($promise); + $deferred = new Deferred(); + + $stream = Stream\unwrapWritable($deferred->promise()); $stream->end(); $stream->write('nope'); - $this->loop->run(); + $deferred->resolve($input); } public function testWriteReturnsFalseWhenPromiseIsPending()