mirror of
https://github.com/danog/amp.git
synced 2025-01-22 13:21:16 +01:00
Validate number of required promises
This commit is contained in:
parent
c464e070e4
commit
cb717952f1
@ -445,12 +445,20 @@ namespace Amp\Promise {
|
||||
* @throws \Error If a non-Promise is in the array.
|
||||
*/
|
||||
function some(array $promises, int $required = 1): Promise {
|
||||
if (empty($promises)) {
|
||||
return new Success([[], []]);
|
||||
if ($required < 0) {
|
||||
throw new \Error("Number of promises required must be non-negative");
|
||||
}
|
||||
|
||||
$pending = \count($promises);
|
||||
|
||||
if ($required > $pending) {
|
||||
throw new \Error("Too few promises provided");
|
||||
}
|
||||
|
||||
if (empty($promises)) {
|
||||
return new Success([[], []]);
|
||||
}
|
||||
|
||||
$deferred = new Deferred;
|
||||
$values = [];
|
||||
$exceptions = [];
|
||||
|
@ -11,7 +11,23 @@ use Amp\Loop;
|
||||
|
||||
class SomeTest extends \PHPUnit\Framework\TestCase {
|
||||
public function testEmptyArray() {
|
||||
$this->assertSame([[], []], Promise\wait(Promise\some([])));
|
||||
$this->assertSame([[], []], Promise\wait(Promise\some([], 0)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage Too few promises provided
|
||||
*/
|
||||
public function testEmptyArrayWithNonZeroRequired() {
|
||||
Promise\some([], 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Error
|
||||
* @expectedExceptionMessage non-negative
|
||||
*/
|
||||
public function testInvalidRequiredNumberOfPromises() {
|
||||
Promise\some([], -1);
|
||||
}
|
||||
|
||||
public function testSuccessfulPromisesArray() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user