Skip to content

Commit ee21147

Browse files
authored
Merge pull request #61 from SimonFrings/promisev3
Added support for react/promise v3
2 parents 4c6a2ea + d243c1d commit ee21147

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require": {
2020
"php": ">=5.3",
2121
"react/event-loop": "^1.2",
22-
"react/promise": "^2.7 || ^1.2.1",
22+
"react/promise": "^3.0 || ^2.7 || ^1.2.1",
2323
"react/promise-timer": "^1.5"
2424
},
2525
"require-dev": {

src/functions.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ function ($error) use (&$exception, &$rejected, &$wait, $loop) {
196196
* value, it will still start a timer and will thus trigger at the earliest
197197
* possible time in the future.
198198
*
199-
* @param array $promises
200-
* @param ?LoopInterface $loop
201-
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
199+
* @param PromiseInterface[] $promises
200+
* @param ?LoopInterface $loop
201+
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
202202
* @return mixed returns whatever the first promise resolves to
203203
* @throws Exception if ALL promises are rejected
204204
* @throws TimeoutException if the $timeout is given and triggers
@@ -287,9 +287,9 @@ function awaitAny(array $promises, LoopInterface $loop = null, $timeout = null)
287287
* value, it will still start a timer and will thus trigger at the earliest
288288
* possible time in the future.
289289
*
290-
* @param array $promises
291-
* @param ?LoopInterface $loop
292-
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
290+
* @param PromiseInterface[] $promises
291+
* @param ?LoopInterface $loop
292+
* @param ?float $timeout [deprecated] (optional) maximum timeout in seconds or null=wait forever
293293
* @return array returns an array with whatever each promise resolves to
294294
* @throws Exception when ANY promise is rejected
295295
* @throws TimeoutException if the $timeout is given and triggers
@@ -322,7 +322,7 @@ function awaitAll(array $promises, LoopInterface $loop = null, $timeout = null)
322322
function _cancelAllPromises(array $promises)
323323
{
324324
foreach ($promises as $promise) {
325-
if ($promise instanceof CancellablePromiseInterface) {
325+
if ($promise instanceof PromiseInterface && ($promise instanceof CancellablePromiseInterface || !\interface_exists('React\Promise\CancellablePromiseInterface'))) {
326326
$promise->cancel();
327327
}
328328
}

tests/FunctionAwaitAllTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function testAwaitAllRejected()
4343

4444
public function testAwaitAllRejectedWithFalseWillWrapInUnexpectedValueException()
4545
{
46+
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
47+
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
48+
}
49+
4650
$all = array(
4751
$this->createPromiseResolved(1),
4852
Promise\reject(false)

tests/FunctionAwaitAnyTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function testAwaitAnyEmpty()
1818
public function testAwaitAnyFirstResolved()
1919
{
2020
$all = array(
21-
$this->createPromiseRejected(1),
21+
$this->createPromiseRejected(new \Exception('1')),
2222
$this->createPromiseResolved(2, 0.01),
2323
$this->createPromiseResolved(3, 0.02)
2424
);
@@ -40,7 +40,7 @@ public function testAwaitAnyFirstResolvedConcurrently()
4040
$d3 = new Deferred();
4141

4242
$this->loop->addTimer(0.01, function() use ($d1, $d2, $d3) {
43-
$d1->reject(1);
43+
$d1->reject(new \Exception('1'));
4444
$d2->resolve(2);
4545
$d3->resolve(3);
4646
});
@@ -57,8 +57,8 @@ public function testAwaitAnyFirstResolvedConcurrently()
5757
public function testAwaitAnyAllRejected()
5858
{
5959
$all = array(
60-
$this->createPromiseRejected(1),
61-
$this->createPromiseRejected(2)
60+
$this->createPromiseRejected(new \Exception('1')),
61+
$this->createPromiseRejected(new \Exception('2'))
6262
);
6363

6464
$this->setExpectedException('UnderflowException');

tests/FunctionAwaitTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public function testAwaitOneRejected()
1818

1919
public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException()
2020
{
21+
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
22+
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
23+
}
24+
2125
$promise = Promise\reject(false);
2226

2327
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
@@ -26,6 +30,10 @@ public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException(
2630

2731
public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
2832
{
33+
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
34+
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
35+
}
36+
2937
$promise = Promise\reject(null);
3038

3139
$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
@@ -162,6 +170,10 @@ public function testAwaitOneRejectedWithTimeoutShouldNotCreateAnyGarbageReferenc
162170

163171
public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
164172
{
173+
if (!interface_exists('React\Promise\CancellablePromiseInterface')) {
174+
$this->markTestSkipped('Promises must be rejected with a \Throwable instance since Promise v3');
175+
}
176+
165177
if (class_exists('React\Promise\When') && PHP_VERSION_ID >= 50400) {
166178
$this->markTestSkipped('Not supported on legacy Promise v1 API with PHP 5.4+');
167179
}

0 commit comments

Comments
 (0)