mirror of
https://github.com/danog/amp.git
synced 2025-01-22 05:11:42 +01:00
misc updates + tests
This commit is contained in:
parent
c8bbf8208f
commit
564a81c519
@ -81,9 +81,9 @@ trait Placeholder {
|
||||
$result->when(function($error, $result) {
|
||||
$this->resolve($error, $result);
|
||||
});
|
||||
} elseif (isset($error) && !($error instanceof \Exception || $error instanceof \Throwable)) {
|
||||
} elseif (isset($error) && !($error instanceof \Throwable || $error instanceof \Exception)) {
|
||||
throw new \InvalidArgumentException(
|
||||
"Only exceptions may be used to fail a promise"
|
||||
"Throwable Exception instance required to fail a promise"
|
||||
);
|
||||
} else {
|
||||
$this->isResolved = true;
|
||||
|
@ -26,7 +26,7 @@ trait PublicPromisor {
|
||||
public function update($progress) {
|
||||
if ($this->isResolved) {
|
||||
throw new \LogicException(
|
||||
'Cannot update resolved promise'
|
||||
"Cannot update resolved promise"
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -294,7 +294,7 @@ function map(array $promises, callable $functor) {
|
||||
}
|
||||
$struct->remaining--;
|
||||
try {
|
||||
$struct->results[$key] = call_user_func($struct->functor, $result);
|
||||
$struct->results[$key] = \call_user_func($struct->functor, $result);
|
||||
} catch (\Exception $e) {
|
||||
$struct->remaining = 0;
|
||||
$struct->promisor->fail($e);
|
||||
@ -311,7 +311,7 @@ function map(array $promises, callable $functor) {
|
||||
} else {
|
||||
$struct->remaining--;
|
||||
try {
|
||||
$struct->results[$key] = call_user_func($struct->functor, $promise);
|
||||
$struct->results[$key] = \call_user_func($struct->functor, $promise);
|
||||
} catch (\Exception $e) {
|
||||
$struct->remaining = 0;
|
||||
$struct->promisor->fail($e);
|
||||
@ -360,7 +360,7 @@ function filter(array $promises, callable $functor) {
|
||||
}
|
||||
$struct->remaining--;
|
||||
try {
|
||||
if (call_user_func($struct->functor, $result)) {
|
||||
if (\call_user_func($struct->functor, $result)) {
|
||||
$struct->results[$key] = $result;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
@ -379,7 +379,7 @@ function filter(array $promises, callable $functor) {
|
||||
} else {
|
||||
$struct->remaining--;
|
||||
try {
|
||||
if (call_user_func($struct->functor, $promise)) {
|
||||
if (\call_user_func($struct->functor, $promise)) {
|
||||
$struct->results[$key] = $promise;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
@ -405,7 +405,7 @@ function filter(array $promises, callable $functor) {
|
||||
function pipe($promise, callable $functor) {
|
||||
if (!($promise instanceof Promise)) {
|
||||
try {
|
||||
return new Success(call_user_func($functor, $promise));
|
||||
return new Success(\call_user_func($functor, $promise));
|
||||
} catch (\Exception $e) {
|
||||
return new Failure($e);
|
||||
}
|
||||
@ -418,7 +418,7 @@ function pipe($promise, callable $functor) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
$promisor->succeed(call_user_func($functor, $result));
|
||||
$promisor->succeed(\call_user_func($functor, $result));
|
||||
} catch (\Exception $error) {
|
||||
$promisor->fail($error);
|
||||
}
|
||||
|
@ -76,6 +76,19 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
});
|
||||
}
|
||||
|
||||
public function testAnyReturnsImmediatelyOnEmptyPromiseArray() {
|
||||
$promise = \Amp\any([]);
|
||||
$this->assertInstanceOf("Amp\Success", $promise);
|
||||
$error = null;
|
||||
$result = null;
|
||||
$promise->when(function($e, $r) use (&$error, &$result) {
|
||||
$error = $e;
|
||||
$result = $r;
|
||||
});
|
||||
$this->assertNull($error);
|
||||
$this->assertSame([[], []], $result);
|
||||
}
|
||||
|
||||
public function testAllResolvesWithArrayOfResults() {
|
||||
\Amp\all(['r1' => 42, 'r2' => new Success(41)])->when(function($error, $result) {
|
||||
$expected = ['r1' => 42, 'r2' => 41];
|
||||
@ -83,6 +96,19 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
});
|
||||
}
|
||||
|
||||
public function testAllReturnsImmediatelyOnEmptyPromiseArray() {
|
||||
$promise = \Amp\all([]);
|
||||
$this->assertInstanceOf("Amp\Success", $promise);
|
||||
$error = null;
|
||||
$result = null;
|
||||
$promise->when(function($e, $r) use (&$error, &$result) {
|
||||
$error = $e;
|
||||
$result = $r;
|
||||
});
|
||||
$this->assertNull($error);
|
||||
$this->assertSame([], $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
* @expectedExceptionMessage zanzibar
|
||||
@ -111,6 +137,20 @@ class FunctionsTest extends \PHPUnit_Framework_TestCase {
|
||||
});
|
||||
}
|
||||
|
||||
public function testSomeFailsImmediatelyOnEmptyPromiseArrayInput() {
|
||||
$promise = \Amp\some([]);
|
||||
$this->assertInstanceOf("Amp\Failure", $promise);
|
||||
$error = null;
|
||||
$result = null;
|
||||
$promise->when(function($e, $r) use (&$error, &$result) {
|
||||
$error = $e;
|
||||
$result = $r;
|
||||
});
|
||||
$this->assertNull($result);
|
||||
$this->assertInstanceOf("\LogicException", $error);
|
||||
$this->assertSame("No promises or values provided for resolution", $error->getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
|
@ -120,5 +120,4 @@ abstract class PlaceholderTest {
|
||||
yield $promisor->promise();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,4 +14,22 @@ class PromisorPublicTest extends PromisorTest {
|
||||
$promisor = new PromisorPublicImpl;
|
||||
$this->assertSame($promisor, $promisor->promise());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @expectedExceptionMessage Throwable Exception instance required to fail a promise
|
||||
* @dataProvider provideBadFailureArguments
|
||||
*/
|
||||
public function testResolvingErrorWithNonExceptionThrows($badArg) {
|
||||
$promisor = $this->getPromisor();
|
||||
$promisor->fail($badArg);
|
||||
}
|
||||
|
||||
public function provideBadFailureArguments() {
|
||||
return [
|
||||
[1],
|
||||
[true],
|
||||
[new \StdClass],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@ -139,4 +139,14 @@ abstract class PromisorTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
$this->assertSame($expected, $updates->arr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \LogicException
|
||||
* @expectedExceptionMessage Cannot update resolved promise
|
||||
*/
|
||||
public function testUpdateThrowsIfPromiseAlreadyResolved() {
|
||||
$promisor = $this->getPromisor();
|
||||
$promisor->succeed();
|
||||
$promisor->update(42);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user