2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 01:50:33 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\LazyPromise;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2020-09-27 05:26:52 +02:00
|
|
|
use Amp\Promise;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Success;
|
2020-09-27 05:26:52 +02:00
|
|
|
use function Amp\await;
|
2016-12-16 01:50:33 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class LazyPromiseTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-27 05:26:52 +02:00
|
|
|
public function testPromisorNotCalledOnConstruct(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = false;
|
2017-01-08 08:02:11 +01:00
|
|
|
$lazy = new LazyPromise(function () use (&$invoked) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
$this->assertFalse($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
public function testPromisorReturningScalar(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
2020-09-27 05:26:52 +02:00
|
|
|
$lazy = new LazyPromise(function () use (&$invoked, $value): int {
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = true;
|
|
|
|
return $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
$this->assertSame($value, await($lazy));
|
2016-12-16 01:50:33 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
public function testPromisorReturningSuccessfulPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$value = 1;
|
|
|
|
$promise = new Success($value);
|
2020-09-27 05:26:52 +02:00
|
|
|
$lazy = new LazyPromise(static fn (): Promise => $promise);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
$this->assertSame($value, await($lazy));
|
2016-12-16 01:50:33 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
public function testPromisorReturningFailedPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = new Failure($exception);
|
2020-09-27 05:26:52 +02:00
|
|
|
$lazy = new LazyPromise(static fn (): Promise => $promise);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
try {
|
|
|
|
await($lazy);
|
|
|
|
} catch (\Exception $reason) {
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
$this->fail("Promise was not failed");
|
2016-12-16 01:50:33 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
public function testPromisorThrowingException(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$exception = new \Exception;
|
2020-09-27 05:26:52 +02:00
|
|
|
$lazy = new LazyPromise(function () use ($exception): void {
|
2016-12-16 01:50:33 +01:00
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
try {
|
|
|
|
await($lazy);
|
|
|
|
} catch (\Exception $reason) {
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
return;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
$this->fail("Promise was not failed");
|
2016-12-16 01:50:33 +01:00
|
|
|
}
|
2017-03-12 21:02:26 +01:00
|
|
|
|
2020-09-27 05:26:52 +02:00
|
|
|
public function testPromisorCallingAwait(): void
|
|
|
|
{
|
|
|
|
$value = 1;
|
|
|
|
$lazy = new LazyPromise(static fn (): int => await(new Delayed(100, $value)));
|
|
|
|
|
|
|
|
$this->assertSame($value, await($lazy));
|
2017-03-28 01:37:55 +02:00
|
|
|
}
|
2016-12-16 01:50:33 +01:00
|
|
|
}
|