2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 01:50:33 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\LazyPromise;
|
|
|
|
use Amp\Success;
|
2017-03-12 21:02:26 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use React\Promise\FulfilledPromise as FulfilledReactPromise;
|
|
|
|
use React\Promise\RejectedPromise as RejectedReactPromise;
|
2016-12-16 01:50:33 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
class LazyPromiseTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testPromisorNotCalledOnConstruct()
|
|
|
|
{
|
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
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromisorReturningScalar()
|
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
2017-01-08 08:02:11 +01:00
|
|
|
$lazy = new LazyPromise(function () use (&$invoked, $value) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = true;
|
|
|
|
return $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$lazy->onResolve(function ($exception, $value) use (&$result) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromisorReturningSuccessfulPromise()
|
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
|
|
|
$promise = new Success($value);
|
2017-01-08 08:02:11 +01:00
|
|
|
$lazy = new LazyPromise(function () use (&$invoked, $promise) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = true;
|
|
|
|
return $promise;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$lazy->onResolve(function ($exception, $value) use (&$result) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromisorReturningFailedPromise()
|
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = false;
|
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = new Failure($exception);
|
2017-01-08 08:02:11 +01:00
|
|
|
$lazy = new LazyPromise(function () use (&$invoked, $promise) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = true;
|
|
|
|
return $promise;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$lazy->onResolve(function ($exception, $value) use (&$reason) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromisorThrowingException()
|
|
|
|
{
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = false;
|
|
|
|
$exception = new \Exception;
|
2017-01-08 08:02:11 +01:00
|
|
|
$lazy = new LazyPromise(function () use (&$invoked, $exception) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$invoked = true;
|
|
|
|
throw $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$lazy->onResolve(function ($exception, $value) use (&$reason) {
|
2016-12-16 01:50:33 +01:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-03-12 21:02:26 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromisorReturningSuccessfulReactPromise()
|
|
|
|
{
|
2017-03-12 21:02:26 +01:00
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
|
|
|
$promise = new FulfilledReactPromise($value);
|
|
|
|
$lazy = new LazyPromise(function () use (&$invoked, $promise) {
|
|
|
|
$invoked = true;
|
|
|
|
return $promise;
|
|
|
|
});
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$lazy->onResolve(function ($exception, $value) use (&$result) {
|
2017-03-12 21:02:26 +01:00
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromisorReturningFailedReactPromise()
|
|
|
|
{
|
2017-03-12 21:02:26 +01:00
|
|
|
$invoked = false;
|
|
|
|
$exception = new \Exception;
|
|
|
|
$promise = new RejectedReactPromise($exception);
|
|
|
|
$lazy = new LazyPromise(function () use (&$invoked, $promise) {
|
|
|
|
$invoked = true;
|
|
|
|
return $promise;
|
|
|
|
});
|
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$lazy->onResolve(function ($exception, $value) use (&$reason) {
|
2017-03-12 21:02:26 +01:00
|
|
|
$reason = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($exception, $reason);
|
|
|
|
}
|
2017-03-28 01:37:55 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testPromisorReturningGenerator()
|
|
|
|
{
|
2017-03-28 01:37:55 +02:00
|
|
|
$invoked = false;
|
|
|
|
$value = 1;
|
|
|
|
$lazy = new LazyPromise(function () use (&$invoked, $value) {
|
|
|
|
$invoked = true;
|
|
|
|
return $value;
|
|
|
|
yield; // Unreachable, but makes function a generator.
|
|
|
|
});
|
|
|
|
|
|
|
|
$lazy->onResolve(function ($exception, $value) use (&$result) {
|
|
|
|
$result = $value;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2016-12-16 01:50:33 +01:00
|
|
|
}
|