1
0
mirror of https://github.com/danog/amp.git synced 2024-11-26 20:15:00 +01:00
amp/test/LazyPromiseTest.php

147 lines
3.9 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Test;
use Amp\Failure;
use Amp\LazyPromise;
use Amp\Success;
use PHPUnit\Framework\TestCase;
use React\Promise\FulfilledPromise as FulfilledReactPromise;
use React\Promise\RejectedPromise as RejectedReactPromise;
2018-06-18 20:00:01 +02:00
class LazyPromiseTest extends 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);
}
2018-06-18 20:00:01 +02:00
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->onResolve(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
2018-06-18 20:00:01 +02: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) {
$invoked = true;
return $promise;
});
$lazy->onResolve(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
2018-06-18 20:00:01 +02: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) {
$invoked = true;
return $promise;
});
$lazy->onResolve(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}
2018-06-18 20:00:01 +02:00
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->onResolve(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}
2018-06-18 20:00:01 +02:00
public function testPromisorReturningSuccessfulReactPromise()
{
$invoked = false;
$value = 1;
$promise = new FulfilledReactPromise($value);
$lazy = new LazyPromise(function () use (&$invoked, $promise) {
$invoked = true;
return $promise;
});
$lazy->onResolve(function ($exception, $value) use (&$result) {
$result = $value;
});
$this->assertTrue($invoked);
$this->assertSame($value, $result);
}
2018-06-18 20:00:01 +02:00
public function testPromisorReturningFailedReactPromise()
{
$invoked = false;
$exception = new \Exception;
$promise = new RejectedReactPromise($exception);
$lazy = new LazyPromise(function () use (&$invoked, $promise) {
$invoked = true;
return $promise;
});
$lazy->onResolve(function ($exception, $value) use (&$reason) {
$reason = $exception;
});
$this->assertTrue($invoked);
$this->assertSame($exception, $reason);
}
2018-06-18 20:00:01 +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);
}
}