2017-02-22 22:52:30 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Coroutine;
|
2017-05-03 15:21:49 +02:00
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\PHPUnit\TestException;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Success;
|
2017-03-14 22:15:36 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use React\Promise\FulfilledPromise as FulfilledReactPromise;
|
2017-02-22 22:52:30 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
class CallTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testCallWithFunctionReturningPromise()
|
|
|
|
{
|
2017-02-22 22:52:30 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = Amp\call(function ($value) {
|
|
|
|
return new Success($value);
|
|
|
|
}, $value);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-22 22:52:30 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCallWithFunctionReturningValue()
|
|
|
|
{
|
2017-02-22 22:52:30 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = Amp\call(function ($value) {
|
|
|
|
return $value;
|
|
|
|
}, $value);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-22 22:52:30 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCallWithThrowingFunction()
|
|
|
|
{
|
2017-02-22 22:52:30 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = Amp\call(function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-22 22:52:30 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
$this->assertNull($result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCallWithFunctionReturningReactPromise()
|
|
|
|
{
|
2017-03-14 22:15:36 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = Amp\call(function ($value) {
|
|
|
|
return new FulfilledReactPromise($value);
|
|
|
|
}, $value);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-03-14 22:15:36 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testCallWithGeneratorFunction()
|
|
|
|
{
|
2017-02-22 22:52:30 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = Amp\call(function ($value) {
|
|
|
|
return yield new Success($value);
|
|
|
|
}, $value);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(Coroutine::class, $promise);
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$reason, &$result) {
|
2017-02-22 22:52:30 +01:00
|
|
|
$reason = $exception;
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNull($reason);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-05-03 15:21:49 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testAsyncCallFunctionWithFailure()
|
|
|
|
{
|
2017-05-03 15:21:49 +02:00
|
|
|
\Amp\asyncCall(function ($value) {
|
|
|
|
return new Failure(new TestException);
|
|
|
|
}, 42);
|
|
|
|
|
|
|
|
$this->expectException(TestException::class);
|
|
|
|
|
|
|
|
Loop::run();
|
|
|
|
}
|
2017-02-22 22:52:30 +01:00
|
|
|
}
|