mirror of
https://github.com/danog/amp.git
synced 2024-12-02 17:37:50 +01:00
7612ef3f1e
React promises are still supported using Amp\Promise\adapt().
85 lines
1.7 KiB
PHP
85 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Amp\Test;
|
|
|
|
use Amp\Delayed;
|
|
use Amp\Failure;
|
|
use Amp\PHPUnit\AsyncTestCase;
|
|
use Amp\Promise;
|
|
use Amp\Success;
|
|
use function Amp\call;
|
|
|
|
class WaitTest extends AsyncTestCase
|
|
{
|
|
public function testWaitOnSuccessfulPromise(): void
|
|
{
|
|
$value = 1;
|
|
|
|
$promise = new Success($value);
|
|
|
|
$result = Promise\wait($promise);
|
|
|
|
$this->assertSame($value, $result);
|
|
}
|
|
|
|
public function testWaitOnFailedPromise(): void
|
|
{
|
|
$exception = new \Exception();
|
|
|
|
$promise = new Failure($exception);
|
|
|
|
try {
|
|
$result = Promise\wait($promise);
|
|
} catch (\Exception $e) {
|
|
$this->assertSame($exception, $e);
|
|
return;
|
|
}
|
|
|
|
$this->fail('Rejection exception should be thrown from wait().');
|
|
}
|
|
|
|
/**
|
|
* @depends testWaitOnSuccessfulPromise
|
|
*/
|
|
public function testWaitOnPendingPromise(): void
|
|
{
|
|
$value = 1;
|
|
|
|
$promise = new Delayed(100, $value);
|
|
|
|
$result = Promise\wait($promise);
|
|
|
|
$this->assertSame($value, $result);
|
|
}
|
|
|
|
public function testWaitNested(): void
|
|
{
|
|
$promise = call(static function () {
|
|
yield new Delayed(10);
|
|
|
|
return Promise\wait(new Delayed(10, 1));
|
|
});
|
|
|
|
$result = Promise\wait($promise);
|
|
|
|
$this->assertSame(1, $result);
|
|
}
|
|
|
|
public function testWaitNestedDelayed(): void
|
|
{
|
|
$promise = call(static function () {
|
|
yield new Delayed(10);
|
|
|
|
$result = Promise\wait(new Delayed(10, 1));
|
|
|
|
yield new Delayed(0);
|
|
|
|
return $result;
|
|
});
|
|
|
|
$result = Promise\wait($promise);
|
|
|
|
$this->assertSame(1, $result);
|
|
}
|
|
}
|