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
|
|
|
|
2017-03-12 21:02:26 +01:00
|
|
|
class LazyPromiseTest extends TestCase {
|
2016-12-16 01:50:33 +01:00
|
|
|
public function testPromisorNotCalledOnConstruct() {
|
|
|
|
$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
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
public function testPromisorReturningScalar() {
|
|
|
|
$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
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
public function testPromisorReturningSuccessfulPromise() {
|
|
|
|
$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
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
public function testPromisorReturningFailedPromise() {
|
|
|
|
$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
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
public function testPromisorThrowingException() {
|
|
|
|
$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
|
|
|
|
|
|
|
public function testPromisorReturningSuccessfulReactPromise() {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPromisorReturningFailedReactPromise() {
|
|
|
|
$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
|
|
|
|
|
|
|
public function testPromisorReturningGenerator() {
|
|
|
|
$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
|
|
|
}
|