2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 01:50:33 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp;
|
2017-01-08 08:02:11 +01:00
|
|
|
use Amp\{ Failure, LazyPromise, Success };
|
2016-12-16 01:50:33 +01:00
|
|
|
|
2017-01-08 08:02:11 +01:00
|
|
|
class LazyPromiseTest extends \PHPUnit_Framework_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
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
$lazy->when(function ($exception, $value) use (&$result) {
|
|
|
|
$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
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
$lazy->when(function ($exception, $value) use (&$result) {
|
|
|
|
$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
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
$lazy->when(function ($exception, $value) use (&$reason) {
|
|
|
|
$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
|
|
|
|
2016-12-16 01:50:33 +01:00
|
|
|
$lazy->when(function ($exception, $value) use (&$reason) {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|