diff --git a/composer.json b/composer.json index 4545462..2779eb2 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "require": { "php": ">=5.3", "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5", - "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 0fda798..5d31c29 100644 --- a/src/functions.php +++ b/src/functions.php @@ -283,7 +283,7 @@ function awaitAll(array $promises, LoopInterface $loop, $timeout = null) function _cancelAllPromises(array $promises) { foreach ($promises as $promise) { - if ($promise instanceof CancellablePromiseInterface) { + if ($promise instanceof CancellablePromiseInterface || (! \interface_exists('React\\Promise\\CancellablePromiseInterface') && \method_exists($promise, 'cancel'))) { $promise->cancel(); } } diff --git a/tests/FunctionAwaitAllTest.php b/tests/FunctionAwaitAllTest.php index 9532657..de29ec6 100644 --- a/tests/FunctionAwaitAllTest.php +++ b/tests/FunctionAwaitAllTest.php @@ -36,6 +36,8 @@ public function testAwaitAllRejected() public function testAwaitAllRejectedWithFalseWillWrapInUnexpectedValueException() { + $this->skipForPromise3('Promises must reject with an exception, so this case cannot happen.'); + $all = array( $this->createPromiseResolved(1), Promise\reject(false) diff --git a/tests/FunctionAwaitAnyTest.php b/tests/FunctionAwaitAnyTest.php index faded88..61151e9 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) ); @@ -33,7 +33,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); }); @@ -50,8 +50,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 d50736d..8207ff7 100644 --- a/tests/FunctionAwaitTest.php +++ b/tests/FunctionAwaitTest.php @@ -18,6 +18,7 @@ public function testAwaitOneRejected() public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException() { + $this->skipForPromise3('Promises must reject with an exception, so this case cannot happen.'); $promise = Promise\reject(false); $this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool'); @@ -26,6 +27,7 @@ public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException( public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException() { + $this->skipForPromise3('Promises must reject with an exception, so this case cannot happen.'); $promise = Promise\reject(null); $this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL'); @@ -153,6 +155,7 @@ public function testAwaitOneRejectedWithTimeoutShouldNotCreateAnyGarbageReferenc public function testAwaitNullValueShouldNotCreateAnyGarbageReferences() { + $this->skipForPromise3('Promises must reject with an exception, so this case cannot happen.'); if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) { $this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+'); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 6ef25bc..0ae1ec6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -17,6 +17,21 @@ public function setUpLoop() $this->loop = \React\EventLoop\Factory::create(); } + /** + * Skips a test if the test suite is running with react/promise version + * 3.0 or later. + * + * @param string $reason + * + * @return void + */ + protected function skipForPromise3($reason) + { + if (! interface_exists('React\Promise\CancellablePromiseInterface')) { + $this->markTestSkipped('Test is not supported/required by the Promise v3 API: '.$reason); + } + } + protected function createPromiseResolved($value = null, $delay = 0.01) { $deferred = new Deferred();