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;
|
2017-03-10 21:58:46 +01:00
|
|
|
use Amp\Promise;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
class DeferredTest extends \PHPUnit\Framework\TestCase
|
|
|
|
{
|
2016-08-18 05:25:54 +02:00
|
|
|
/** @var \Amp\Deferred */
|
2016-07-12 18:20:06 +02:00
|
|
|
private $deferred;
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function setUp()
|
|
|
|
{
|
2016-07-12 18:20:06 +02:00
|
|
|
$this->deferred = new Deferred;
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testGetPromise()
|
|
|
|
{
|
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
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testResolve()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
$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);
|
|
|
|
|
|
|
|
$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
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function testFail()
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
$this->assertTrue($invoked);
|
|
|
|
$this->assertSame($exception, $result);
|
|
|
|
}
|
|
|
|
}
|