mirror of
https://github.com/danog/amp.git
synced 2025-01-22 05:11:42 +01:00
Update Amp\coroutine() tests
This commit is contained in:
parent
2ff32c2d6b
commit
496152282e
@ -8,6 +8,7 @@ use Amp\Failure;
|
||||
use Amp\InvalidYieldError;
|
||||
use Amp\Pause;
|
||||
use Amp\Success;
|
||||
use Interop\Async\Awaitable;
|
||||
use Interop\Async\Loop;
|
||||
|
||||
class CoroutineTest extends \PHPUnit_Framework_TestCase {
|
||||
@ -423,12 +424,63 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
|
||||
|
||||
/**
|
||||
* @depends testCoroutineFunction
|
||||
* @expectedException \LogicException
|
||||
*/
|
||||
public function testCoroutineFunctionWithCallbackReturningAwaitable() {
|
||||
$value = 1;
|
||||
$awaitable = new Success($value);
|
||||
$callable = Amp\coroutine(function ($value) {
|
||||
return $value;
|
||||
});
|
||||
|
||||
$awaitable = $callable($awaitable);
|
||||
|
||||
$this->assertInstanceOf(Awaitable::class, $awaitable);
|
||||
|
||||
$awaitable->when(function ($exception, $value) use (&$result) {
|
||||
$result = $value;
|
||||
});
|
||||
|
||||
$this->assertSame($value, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCoroutineFunction
|
||||
*/
|
||||
public function testCoroutineFunctionWithNonGeneratorCallback() {
|
||||
$callable = Amp\coroutine(function () {});
|
||||
$value = 1;
|
||||
$callable = Amp\coroutine(function ($value) {
|
||||
return $value;
|
||||
});
|
||||
|
||||
$this->assertInstanceOf(Coroutine::class, $callable());
|
||||
$awaitable = $callable($value);
|
||||
|
||||
$this->assertInstanceOf(Awaitable::class, $awaitable);
|
||||
|
||||
$awaitable->when(function ($exception, $value) use (&$result) {
|
||||
$result = $value;
|
||||
});
|
||||
|
||||
$this->assertSame($value, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testCoroutineFunction
|
||||
*/
|
||||
public function testCoroutineFunctionWithThrowingCallback() {
|
||||
$exception = new \Exception;
|
||||
$callable = Amp\coroutine(function () use ($exception) {
|
||||
throw $exception;
|
||||
});
|
||||
|
||||
$awaitable = $callable();
|
||||
|
||||
$this->assertInstanceOf(Awaitable::class, $awaitable);
|
||||
|
||||
$awaitable->when(function ($exception, $value) use (&$reason) {
|
||||
$reason = $exception;
|
||||
});
|
||||
|
||||
$this->assertSame($exception, $reason);
|
||||
}
|
||||
|
||||
public function testCoroutineResolvedWithReturn() {
|
||||
@ -445,7 +497,6 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
|
||||
$result = $value;
|
||||
});
|
||||
|
||||
|
||||
$this->assertSame($value, $result);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user