1
0
mirror of https://github.com/danog/amp.git synced 2024-11-27 04:24:42 +01:00
amp/test/LazyPromiseTest.php

83 lines
2.2 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
use Amp;
2017-01-08 08:02:11 +01:00
use Amp\{ Failure, LazyPromise, Success };
2017-01-08 08:02:11 +01:00
class LazyPromiseTest extends \PHPUnit_Framework_TestCase {
public function testPromisorNotCalledOnConstruct() {
$invoked = false;
2017-01-08 08:02:11 +01:00
$lazy = new LazyPromise(function () use (&$invoked) {
$invoked = true;
});
$this->assertFalse($invoked);
}
public function testPromisorReturningScalar() {
$invoked = false;
$value = 1;
2017-01-08 08:02:11 +01:00
$lazy = new LazyPromise(function () use (&$invoked, $value) {
$invoked = true;
return $value;
});
$lazy->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
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) {
$invoked = true;
return $promise;
});
$lazy->when(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
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) {
$invoked = true;
return $promise;
});
$lazy->when(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}
public function testPromisorThrowingException() {
$invoked = false;
$exception = new \Exception;
2017-01-08 08:02:11 +01:00
$lazy = new LazyPromise(function () use (&$invoked, $exception) {
$invoked = true;
throw $exception;
});
$lazy->when(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}
}