1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 09:29:45 +01:00
amp/test/CallTest.php

98 lines
2.7 KiB
PHP
Raw Normal View History

2017-02-22 22:52:30 +01:00
<?php
namespace Amp\Test;
use Amp;
use Amp\Coroutine;
use Amp\Success;
use Amp\Promise;
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
2017-03-14 22:15:36 +01:00
class CallTest extends TestCase {
2017-02-22 22:52:30 +01:00
public function testCallWithFunctionReturningPromise() {
$value = 1;
$promise = Amp\call(function ($value) {
return new Success($value);
}, $value);
$this->assertInstanceOf(Promise::class, $promise);
$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);
}
public function testCallWithFunctionReturningValue() {
$value = 1;
$promise = Amp\call(function ($value) {
return $value;
}, $value);
$this->assertInstanceOf(Promise::class, $promise);
$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);
}
public function testCallWithThrowingFunction() {
$exception = new \Exception;
$promise = Amp\call(function () use ($exception) {
throw $exception;
});
$this->assertInstanceOf(Promise::class, $promise);
$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);
}
2017-03-14 22:15:36 +01:00
public function testCallWithFunctionReturningReactPromise() {
$value = 1;
$promise = Amp\call(function ($value) {
return new FulfilledReactPromise($value);
}, $value);
$this->assertInstanceOf(Promise::class, $promise);
$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);
}
2017-02-22 22:52:30 +01:00
public function testCallWithGeneratorFunction() {
$value = 1;
$promise = Amp\call(function ($value) {
return yield new Success($value);
}, $value);
$this->assertInstanceOf(Coroutine::class, $promise);
$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);
}
}