Skip to content
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.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": {
Expand Down
4 changes: 2 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ function awaitAll(array $promises, LoopInterface $loop, $timeout = null)
function _cancelAllPromises(array $promises)
{
foreach ($promises as $promise) {
if ($promise instanceof CancellablePromiseInterface) {
$promise->cancel();
if ($promise instanceof PromiseInterface) {
Promise\resolve($promise)->cancel();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! I see why this was done, but it looks like static analysis complains about this because resolve() has a PromiseInterface return type for Promise v2 and Promise v1.

Perhaps use logic similar to https://github.com/reactphp/promise-timer/pull/37/files#diff-fe65dcdace9cc44252b537bee79dd574edd1bccf6cee646cc860006a6ec50e8bR15? /cc @WyriHaximus

What do you think about this?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps use logic similar to https://github.com/reactphp/promise-timer/pull/37/files#diff-fe65dcdace9cc44252b537bee79dd574edd1bccf6cee646cc860006a6ec50e8bR15? /cc @WyriHaximus

That is the preferred way, making it more explicit, and more performant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I thought so, had this initially but changed it due to this comment. I'll change it back as it's more lightweight.

}
}
}
4 changes: 3 additions & 1 deletion tests/FunctionAwaitAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public function testAwaitAllRejected()
Block\awaitAll($all, $this->loop);
}

public function testAwaitAllRejectedWithFalseWillWrapInUnexpectedValueException()
public function testAwaitAllRejectedWithExceptionWillWrapInUnexpectedValueException()
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the name should not have been changed as the logic didn't actually change?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops - accident!

{
$this->skipForPromise3();
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see why you've decided to add a helper method for this, but I wonder if it could be more descriptive if we added a reason each time? What do you think? See also all following occurrences.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a reason parameter for the function to pass through when skipping the test


$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)),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit only: First argument is a string instead of an int. See also all following occurrences.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming you mean about the first argument of Exception taking a string? I've changed these to strings now.

$this->createPromiseResolved(2, 0.01),
$this->createPromiseResolved(3, 0.02)
);
Expand All @@ -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);
});
Expand All @@ -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');
Expand Down
3 changes: 3 additions & 0 deletions tests/FunctionAwaitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function testAwaitOneRejected()

public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException()
{
$this->skipForPromise3();
$promise = Promise\reject(false);

$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type bool');
Expand All @@ -26,6 +27,7 @@ public function testAwaitOneRejectedWithFalseWillWrapInUnexpectedValueException(

public function testAwaitOneRejectedWithNullWillWrapInUnexpectedValueException()
{
$this->skipForPromise3();
$promise = Promise\reject(null);

$this->setExpectedException('UnexpectedValueException', 'Promise rejected with unexpected value of type NULL');
Expand Down Expand Up @@ -153,6 +155,7 @@ public function testAwaitOneRejectedWithTimeoutShouldNotCreateAnyGarbageReferenc

public function testAwaitNullValueShouldNotCreateAnyGarbageReferences()
{
$this->skipForPromise3();
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
13 changes: 13 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ 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.
*
* @return void
*/
protected function skipForPromise3()
{
if (! interface_exists('React\Promise\CancellablePromiseInterface')) {
$this->markTestSkipped('Test is not supported/required by the Promise v3 API.');
}
}

protected function createPromiseResolved($value = null, $delay = 0.01)
{
$deferred = new Deferred();
Expand Down