1
0
mirror of https://github.com/danog/amp.git synced 2025-01-21 21:01:16 +01:00

Update Amp\coroutine() tests

This commit is contained in:
Aaron Piotrowski 2016-08-12 16:56:03 -05:00
parent 2ff32c2d6b
commit 496152282e

View File

@ -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 {
@ -420,15 +421,66 @@ class CoroutineTest extends \PHPUnit_Framework_TestCase {
$this->assertInstanceOf(Coroutine::class, $callable());
}
/**
* @depends testCoroutineFunction
*/
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
* @expectedException \LogicException
*/
public function testCoroutineFunctionWithNonGeneratorCallback() {
$callable = Amp\coroutine(function () {});
$this->assertInstanceOf(Coroutine::class, $callable());
$value = 1;
$callable = Amp\coroutine(function ($value) {
return $value;
});
$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);
}