Skip to content

Commit

Permalink
Merge pull request #32 from clue-labs/tests
Browse files Browse the repository at this point in the history
Update test suite to avoid using deprecated functions
  • Loading branch information
WyriHaximus committed Aug 10, 2022
2 parents b314b5e + c429b8c commit 0df894e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 107 deletions.
3 changes: 0 additions & 3 deletions composer.json
Expand Up @@ -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"
}
}
31 changes: 9 additions & 22 deletions tests/BufferTest.php
Expand Up @@ -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;

Expand Down Expand Up @@ -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'));
}
}
51 changes: 16 additions & 35 deletions tests/UnwrapReadableTest.php
Expand Up @@ -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 () { });
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down
75 changes: 28 additions & 47 deletions tests/UnwrapWritableTest.php
Expand Up @@ -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 () { });
Expand All @@ -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());
}
Expand All @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 0df894e

Please sign in to comment.