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..32de9fc 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 || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($promise, 'cancel'))) { // 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 || (!\interface_exists('React\Promise\CancellablePromiseInterface') && \method_exists($promise, 'cancel'))) { $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);