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