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;
|
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Deferred;
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Loop;
|
2017-03-15 17:12:49 +01:00
|
|
|
use Amp\Promise;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Success;
|
2017-03-14 22:15:36 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-03-13 05:27:43 +01:00
|
|
|
use function React\Promise\resolve;
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-14 22:15:36 +01:00
|
|
|
class WaitTest extends 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
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$result = Promise\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 {
|
2017-03-15 17:12:49 +01:00
|
|
|
$result = Promise\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;
|
|
|
|
|
2017-04-28 14:42:02 +02:00
|
|
|
$promise = new Delayed(100, $value);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$result = Promise\wait($promise);
|
2016-07-12 18:20:06 +02:00
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-11 21:35:58 +02:00
|
|
|
* @expectedException \Error
|
2017-04-23 19:08:40 +02:00
|
|
|
* @expectedExceptionMessage Loop stopped without resolving the promise
|
2016-07-12 18:20:06 +02:00
|
|
|
*/
|
2017-03-10 21:31:57 +01:00
|
|
|
public function testPromiseWithNoResolutionPathThrowsException() {
|
2017-04-23 19:08:40 +02:00
|
|
|
Promise\wait((new Deferred)->promise());
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|
2017-03-13 05:27:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testWaitOnSuccessfulPromise
|
|
|
|
*/
|
|
|
|
public function testReactPromise() {
|
|
|
|
$value = 1;
|
|
|
|
|
|
|
|
$promise = resolve($value);
|
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
$result = Promise\wait($promise);
|
2017-03-13 05:27:43 +01:00
|
|
|
|
|
|
|
$this->assertSame($value, $result);
|
|
|
|
}
|
2017-03-14 22:15:36 +01:00
|
|
|
|
|
|
|
public function testNonPromise() {
|
2017-04-23 19:08:40 +02:00
|
|
|
$this->expectException(\TypeError::class);
|
2017-03-15 17:12:49 +01:00
|
|
|
Promise\wait(42);
|
2017-03-14 22:15:36 +01:00
|
|
|
}
|
2016-07-12 18:20:06 +02:00
|
|
|
}
|