Skip to content

Commit

Permalink
Merge pull request #61 from SimonFrings/promisev3
Browse files Browse the repository at this point in the history
Added support for react/promise v3
  • Loading branch information
clue authored Oct 19, 2021
2 parents 4c6a2ea + d243c1d commit ee21147
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 7 additions & 7 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/FunctionAwaitAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions tests/FunctionAwaitAnyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
Expand All @@ -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);
});
Expand All @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions tests/FunctionAwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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+');
}
Expand Down

0 comments on commit ee21147

Please sign in to comment.