1
0
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:
Aaron Piotrowski 2017-03-27 11:42:11 -05:00
parent c464e070e4
commit cb717952f1
2 changed files with 27 additions and 3 deletions

View File

@ -445,12 +445,20 @@ namespace Amp\Promise {
* @throws \Error If a non-Promise is in the array. * @throws \Error If a non-Promise is in the array.
*/ */
function some(array $promises, int $required = 1): Promise { function some(array $promises, int $required = 1): Promise {
if (empty($promises)) { if ($required < 0) {
return new Success([[], []]); throw new \Error("Number of promises required must be non-negative");
} }
$pending = \count($promises); $pending = \count($promises);
if ($required > $pending) {
throw new \Error("Too few promises provided");
}
if (empty($promises)) {
return new Success([[], []]);
}
$deferred = new Deferred; $deferred = new Deferred;
$values = []; $values = [];
$exceptions = []; $exceptions = [];

View File

@ -11,7 +11,23 @@ use Amp\Loop;
class SomeTest extends \PHPUnit\Framework\TestCase { class SomeTest extends \PHPUnit\Framework\TestCase {
public function testEmptyArray() { 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() { public function testSuccessfulPromisesArray() {