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
|
|
|
|
2017-03-11 14:57:03 +01: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;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
$this->deferred = new Deferred;
|
|
|
|
}
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
public function testGetPromise() {
|
|
|
|
$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
|
|
|
*/
|
|
|
|
public function testResolve() {
|
|
|
|
$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
|
|
|
*/
|
|
|
|
public function testFail() {
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
}
|