2016-08-17 22:25:54 -05:00
|
|
|
<?php declare(strict_types = 1);
|
2016-08-16 16:39:25 -05:00
|
|
|
|
2016-07-12 11:20:06 -05:00
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp;
|
2016-11-14 13:59:21 -06:00
|
|
|
use Amp\{ Deferred, Failure, Pause, Success };
|
2016-07-12 11:20:06 -05:00
|
|
|
use Interop\Async\Loop;
|
|
|
|
|
|
|
|
class WaitTest extends \PHPUnit_Framework_TestCase {
|
2016-11-14 13:59:21 -06:00
|
|
|
public function testWaitOnSuccessfulPromise()
|
2016-07-12 11:20:06 -05:00
|
|
|
{
|
|
|
|
$value = 1;
|
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$promise = new Success($value);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$result = Amp\wait($promise);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
public function testWaitOnFailedPromise()
|
2016-07-12 11:20:06 -05:00
|
|
|
{
|
|
|
|
$exception = new \Exception();
|
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$promise = new Failure($exception);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
|
|
|
try {
|
2016-11-14 13:59:21 -06:00
|
|
|
$result = Amp\wait($promise);
|
2016-07-12 11:20:06 -05:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertSame($exception, $e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail('Rejection exception should be thrown from wait().');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 13:59:21 -06:00
|
|
|
* @depends testWaitOnSuccessfulPromise
|
2016-07-12 11:20:06 -05:00
|
|
|
*/
|
2016-11-14 13:59:21 -06:00
|
|
|
public function testWaitOnPendingPromise()
|
2016-07-12 11:20:06 -05:00
|
|
|
{
|
|
|
|
Loop::execute(function () {
|
|
|
|
$value = 1;
|
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$promise = new Pause(100, $value);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$result = Amp\wait($promise);
|
2016-07-12 11:20:06 -05:00
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 14:35:58 -05:00
|
|
|
* @expectedException \Error
|
2016-07-12 11:20:06 -05:00
|
|
|
*/
|
2016-11-14 13:59:21 -06:00
|
|
|
public function testPromiseWithNoResolutionPathThrowsException()
|
2016-07-12 11:20:06 -05:00
|
|
|
{
|
2016-11-14 13:59:21 -06:00
|
|
|
$promise = new Deferred;
|
2016-07-12 11:20:06 -05:00
|
|
|
|
2016-11-14 13:59:21 -06:00
|
|
|
$result = Amp\wait($promise->getPromise());
|
2016-07-12 11:20:06 -05:00
|
|
|
}
|
|
|
|
}
|