Skip to content

Commit

Permalink
Forward compatibility with react/promise 3
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Jan 31, 2020
1 parent 0f6e328 commit e5b3030
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require": {
"php": ">=5.3",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
"react/promise": "^2.7.0 || ^1.2.1"
"react/promise": "dev-master as 3.0.0 || ^2.7.0 || ^1.2.1"
},
"require-dev": {
"phpunit/phpunit": "^6.4 || ^5.7 || ^4.8.35"
Expand Down
6 changes: 3 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace React\Promise\Timer;

use React\Promise\CancellablePromiseInterface;
use React\EventLoop\LoopInterface;
use React\Promise\CancellablePromiseInterface;
use React\Promise\PromiseInterface;
use React\Promise\Promise;

Expand All @@ -12,7 +12,7 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop)
// cancelling this promise will only try to cancel the input promise,
// thus leaving responsibility to the input promise.
$canceller = null;
if ($promise instanceof CancellablePromiseInterface) {
if ($promise instanceof CancellablePromiseInterface || (\method_exists($promise, 'cancel') === true && $promise instanceof PromiseInterface)) {
// pass promise by reference to clean reference after cancellation handler
// has been invoked once in order to avoid garbage references in call stack.
$canceller = function () use (&$promise) {
Expand Down Expand Up @@ -48,7 +48,7 @@ function timeout(PromiseInterface $promise, $time, LoopInterface $loop)

// try to invoke cancellation handler of input promise and then clean
// reference in order to avoid garbage references in call stack.
if ($promise instanceof CancellablePromiseInterface) {
if ($promise instanceof CancellablePromiseInterface || (\method_exists($promise, 'cancel') === true && $promise instanceof PromiseInterface)) {
$promise->cancel();
}
$promise = null;
Expand Down
5 changes: 3 additions & 2 deletions tests/FunctionRejectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function testPromiseIsPendingWithoutRunningLoop()

public function testPromiseExpiredIsPendingWithoutRunningLoop()
{
$promise = Timer\reject(-1, $this->loop);
$promise = Timer\reject(0, $this->loop);
$promise->done();

$this->expectPromisePending($promise);
}
Expand All @@ -31,7 +32,7 @@ public function testPromiseWillBeRejectedOnTimeout()

public function testPromiseExpiredWillBeRejectedOnTimeout()
{
$promise = Timer\reject(-1, $this->loop);
$promise = Timer\reject(0, $this->loop);

$this->loop->run();

Expand Down
4 changes: 2 additions & 2 deletions tests/FunctionResolveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function testPromiseIsPendingWithoutRunningLoop()

public function testPromiseExpiredIsPendingWithoutRunningLoop()
{
$promise = Timer\resolve(-1, $this->loop);
$promise = Timer\resolve(0, $this->loop);

$this->expectPromisePending($promise);
}
Expand All @@ -31,7 +31,7 @@ public function testPromiseWillBeResolvedOnTimeout()

public function testPromiseExpiredWillBeResolvedOnTimeout()
{
$promise = Timer\resolve(-1, $this->loop);
$promise = Timer\resolve(0, $this->loop);

$this->loop->run();

Expand Down
10 changes: 6 additions & 4 deletions tests/FunctionTimeoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testResolvedWillNotStartTimer()

public function testRejectedWillRejectRightAway()
{
$promise = Promise\reject();
$promise = Promise\reject(new \Exception('reject'));

$promise = Timer\timeout($promise, 3, $this->loop);

Expand All @@ -49,7 +49,7 @@ public function testRejectedWillRejectRightAway()

public function testRejectedWillNotStartTimer()
{
$promise = Promise\reject();
$promise = Promise\reject(new \Exception('reject'));

Timer\timeout($promise, 3, $this->loop);

Expand All @@ -73,10 +73,12 @@ public function testPendingWillRejectOnTimeout()

public function testPendingCancellableWillBeCancelledThroughFollowerOnTimeout()
{
$cancellable = $this->getMockBuilder('React\Promise\CancellablePromiseInterface')->getMock();
$cancellableInterface = interface_exists('React\Promise\CancellablePromiseInterface') ?
'React\Promise\CancellablePromiseInterface' : 'React\Promise\PromiseInterface';
$cancellable = $this->getMockBuilder($cancellableInterface)->getMock();
$cancellable->expects($this->once())->method('cancel');

$promise = $this->getMockBuilder('React\Promise\CancellablePromiseInterface')->getMock();
$promise = $this->getMockBuilder($cancellableInterface)->getMock();
$promise->expects($this->once())->method('then')->willReturn($cancellable);

Timer\timeout($promise, 0.01, $this->loop);
Expand Down

0 comments on commit e5b3030

Please sign in to comment.