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

126 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
use Amp\Delayed;
use Amp\Failure;
use Amp\LazyPromise;
2020-09-28 05:19:52 +02:00
use Amp\PHPUnit\AsyncTestCase;
use Amp\Promise;
use Amp\Success;
use React\Promise\PromiseInterface as ReactPromise;
use function Amp\await;
use function React\Promise\reject;
use function React\Promise\resolve;
2020-09-28 05:19:52 +02:00
class LazyPromiseTest extends AsyncTestCase
2018-06-18 20:00:01 +02:00
{
public function testPromisorNotCalledOnConstruct(): void
2018-06-18 20:00:01 +02:00
{
$invoked = false;
2017-01-08 08:02:11 +01:00
$lazy = new LazyPromise(function () use (&$invoked) {
$invoked = true;
});
$this->assertFalse($invoked);
}
public function testPromisorReturningScalar(): void
2018-06-18 20:00:01 +02:00
{
$invoked = false;
$value = 1;
$lazy = new LazyPromise(function () use (&$invoked, $value): int {
$invoked = true;
return $value;
});
$this->assertSame($value, await($lazy));
$this->assertTrue($invoked);
}
public function testPromisorReturningSuccessfulPromise(): void
2018-06-18 20:00:01 +02:00
{
$value = 1;
$promise = new Success($value);
$lazy = new LazyPromise(static fn (): Promise => $promise);
$this->assertSame($value, await($lazy));
}
public function testPromisorReturningFailedPromise(): void
2018-06-18 20:00:01 +02:00
{
$exception = new \Exception;
$promise = new Failure($exception);
$lazy = new LazyPromise(static fn (): Promise => $promise);
try {
await($lazy);
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
return;
}
$this->fail("Promise was not failed");
}
public function testPromisorThrowingException(): void
2018-06-18 20:00:01 +02:00
{
$exception = new \Exception;
$lazy = new LazyPromise(function () use ($exception): void {
throw $exception;
});
try {
await($lazy);
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
return;
}
$this->fail("Promise was not failed");
}
public function testPromisorReturningSuccessfulReactPromise(): void
2018-06-18 20:00:01 +02:00
{
$value = 1;
$promise = resolve($value);
$lazy = new LazyPromise(static fn (): ReactPromise => $promise);
$this->assertSame($value, await($lazy));
}
public function testPromisorReturningFailedReactPromise(): void
2018-06-18 20:00:01 +02:00
{
$exception = new \Exception;
$promise = reject($exception);
$lazy = new LazyPromise(static fn (): ReactPromise => $promise);
try {
await($lazy);
} catch (\Exception $reason) {
$this->assertSame($exception, $reason);
return;
}
$this->fail("Promise was not failed");
}
public function testPromisorReturningGenerator(): void
2018-06-18 20:00:01 +02:00
{
$value = 1;
$lazy = new LazyPromise(function () use ($value): \Generator {
return $value;
yield; // Unreachable, but makes function a generator.
});
$this->assertSame($value, await($lazy));
}
public function testPromisorCallingAwait(): void
{
$value = 1;
$lazy = new LazyPromise(static fn (): int => await(new Delayed(100, $value)));
$this->assertSame($value, await($lazy));
}
}