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;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\Pause;
|
|
|
|
use Amp\Success;
|
|
|
|
use Amp\Loop;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class WaitTest extends \PHPUnit\Framework\TestCase {
|
2017-03-10 21:31:57 +01:00
|
|
|
public function testWaitOnSuccessfulPromise() {
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = 1;
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Success($value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$result = Amp\wait($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
public function testWaitOnFailedPromise() {
|
2016-07-12 18:20:06 +02:00
|
|
|
$exception = new \Exception();
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Failure($exception);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
try {
|
2016-11-14 20:59:21 +01:00
|
|
|
$result = Amp\wait($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertSame($exception, $e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fail('Rejection exception should be thrown from wait().');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* @depends testWaitOnSuccessfulPromise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2017-03-10 21:31:57 +01:00
|
|
|
public function testWaitOnPendingPromise() {
|
|
|
|
Loop::run(function () {
|
2016-07-12 18:20:06 +02:00
|
|
|
$value = 1;
|
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Pause(100, $value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 20:59:21 +01:00
|
|
|
$result = Amp\wait($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 21:35:58 +02:00
|
|
|
* @expectedException \Error
|
2016-12-11 16:39:58 +01:00
|
|
|
* @expectedExceptionMessage Loop stopped without resolving promise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2017-03-10 21:31:57 +01:00
|
|
|
public function testPromiseWithNoResolutionPathThrowsException() {
|
2016-11-14 20:59:21 +01:00
|
|
|
$promise = new Deferred;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2016-11-14 23:29:49 +01:00
|
|
|
$result = Amp\wait($promise->promise());
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
|
|
|
}
|