diff --git a/composer.json b/composer.json index 6102250..ddfc6c8 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": ">=5.3", "react/event-loop": "^1.2", - "react/promise": "^2.7 || ^1.2.1", + "react/promise": "^3.0 || ^2.7 || ^1.2.1", "react/promise-timer": "^1.5" }, "require-dev": { diff --git a/src/functions.php b/src/functions.php index ae98981..cf23fc0 100644 --- a/src/functions.php +++ b/src/functions.php @@ -196,9 +196,9 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) { * value, it will still start a timer and will thus trigger at the earliest * possible time in the future. * - * @param array $promises - * @param ?LoopInterface $loop - * @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever + * @param PromiseInterface[] $promises + * @param ?LoopInterface $loop + * @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever * @return mixed returns whatever the first promise resolves to * @throws Exception if ALL promises are rejected * @throws TimeoutException if the $timeout is given and triggers @@ -287,9 +287,9 @@ function awaitAny(array $promises, LoopInterface $loop = null, $timeout = null) * value, it will still start a timer and will thus trigger at the earliest * possible time in the future. * - * @param array $promises - * @param ?LoopInterface $loop - * @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever + * @param PromiseInterface[] $promises + * @param ?LoopInterface $loop + * @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever * @return array returns an array with whatever each promise resolves to * @throws Exception when ANY promise is rejected * @throws TimeoutException if the $timeout is given and triggers @@ -322,7 +322,7 @@ function awaitAll(array $promises, LoopInterface $loop = null, $timeout = null) function _cancelAllPromises(array $promises) { foreach ($promises as $promise) { - if ($promise instanceof CancellablePromiseInterface) { + if ($promise instanceof PromiseInterface && ($promise instanceof CancellablePromiseInterface || !\interface_exists('React\Promise\CancellablePromiseInterface'))) { $promise->cancel(); } } diff --git a/tests/FunctionAwaitAllTest.php b/tests/FunctionAwaitAllTest.php index cc6e645..282046b 100644 --- a/tests/FunctionAwaitAllTest.php +++ b/tests/FunctionAwaitAllTest.php @@ -43,6 +43,10 @@ public function testAwaitAllRejected() public function testAwaitAllRejectedWithFalseWillWrapInUnexpectedValueException() { + if (!interface_exists('React\Promise\CancellablePromiseInterface')) { + $this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3'); + } + $all = array( $this->createPromiseResolved(1), Promise\reject(false) diff --git a/tests/FunctionAwaitAnyTest.php b/tests/FunctionAwaitAnyTest.php index 58539dd..d6d0b16 100644 --- a/tests/FunctionAwaitAnyTest.php +++ b/tests/FunctionAwaitAnyTest.php @@ -18,7 +18,7 @@ public function testAwaitAnyEmpty() public function testAwaitAnyFirstResolved() { $all = array( - $this->createPromiseRejected(1), + $this->createPromiseRejected(new \Exception('1')), $this->createPromiseResolved(2, 0.01), $this->createPromiseResolved(3, 0.02) ); @@ -40,7 +40,7 @@ public function testAwaitAnyFirstResolvedConcurrently() $d3 = new Deferred(); $this->loop->addTimer(0.01, function() use ($d1, $d2, $d3) { - $d1->reject(1); + $d1->reject(new \Exception('1')); $d2->resolve(2); $d3->resolve(3); }); @@ -57,8 +57,8 @@ public function testAwaitAnyFirstResolvedConcurrently() public function testAwaitAnyAllRejected() { $all = array( - $this->createPromiseRejected(1), - $this->createPromiseRejected(2) + $this->createPromiseRejected(new \Exception('1')), + $this->createPromiseRejected(new \Exception('2')) ); $this->setExpectedException('UnderflowException'); diff --git a/tests/FunctionAwaitTest.php b/tests/FunctionAwaitTest.php index 41c95f5..67227b7 100644 --- a/tests/FunctionAwaitTest.php +++ b/tests/FunctionAwaitTest.php @@ -18,6 +18,10 @@ public function testAwaitOneRejected() public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException() { + if (!interface_exists('React\Promise\CancellablePromiseInterface')) { + $this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3'); + } + $promise = Promise\reject(false); $this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool'); @@ -26,6 +30,10 @@ public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException( public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException() { + if (!interface_exists('React\Promise\CancellablePromiseInterface')) { + $this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3'); + } + $promise = Promise\reject(null); $this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL'); @@ -162,6 +170,10 @@ public function testAwaitOneRejectedWithTimeoutShouldNotCreateAnyGarbageReferenc public function testAwaitNullValueShouldNotCreateAnyGarbageReferences() { + if (!interface_exists('React\Promise\CancellablePromiseInterface')) { + $this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3'); + } + if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) { $this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+'); }