From 95ab3716d338d272fc71fe29f34e873872547d55 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Tue, 8 Oct 2019 17:41:58 +0200 Subject: [PATCH] Forward compatibility with react/promise 3 --- composer.json | 2 +- src/functions.php | 6 +++--- tests/FunctionTimeoutTest.php | 10 ++++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index cfe99ee..5f99c59 100644 --- a/composer.json +++ b/composer.json @@ -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": "^3.0 || ^2.7.0 || ^1.2.1" }, "require-dev": { "phpunit/phpunit": "^7.0 || ^6.4 || ^5.7 || ^4.8.35" diff --git a/src/functions.php b/src/functions.php index 123a905..27cba65 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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; @@ -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) { // 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) { @@ -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->cancel(); } $promise = null; diff --git a/tests/FunctionTimeoutTest.php b/tests/FunctionTimeoutTest.php index 9652d1a..47a7662 100644 --- a/tests/FunctionTimeoutTest.php +++ b/tests/FunctionTimeoutTest.php @@ -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); @@ -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); @@ -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);