2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 23:39:25 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp\Deferred;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2020-09-28 05:19:52 +02:00
|
|
|
use function Amp\sleep;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class DeferredTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
private Deferred $deferred;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function setUp(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->deferred = new Deferred;
|
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testGetPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = $this->deferred->promise();
|
|
|
|
$this->assertInstanceOf(Promise::class, $promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testGetPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testResolve(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = "Resolution value";
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = $this->deferred->promise();
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2020-07-16 20:50:38 +02:00
|
|
|
$this->assertFalse($this->deferred->isResolved());
|
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$invoked = false;
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$invoked, &$result) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$invoked = true;
|
|
|
|
$result = $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->deferred->resolve($value);
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertFalse($invoked); // Resolution should be async.
|
|
|
|
|
|
|
|
sleep(0); // Force loop to tick once.
|
|
|
|
|
2020-07-16 20:50:38 +02:00
|
|
|
$this->assertTrue($this->deferred->isResolved());
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testGetPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testFail(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception;
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = $this->deferred->promise();
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$invoked = false;
|
2017-03-21 17:23:37 +01:00
|
|
|
$promise->onResolve(function ($exception, $value) use (&$invoked, &$result) {
|
2016-07-12 18:20:06 +02:00
|
|
|
$invoked = true;
|
|
|
|
$result = $exception;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->deferred->fail($exception);
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertFalse($invoked); // Resolution should be async.
|
|
|
|
|
|
|
|
sleep(0); // Force loop to tick once.
|
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
}
|