1
0
mirror of https://github.com/danog/amp.git synced 2024-11-30 04:29:08 +01:00
amp/test/WaitTest.php

101 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?php
2016-07-12 18:20:06 +02:00
namespace Amp\Test;
use Amp\Deferred;
use Amp\Delayed;
use Amp\Failure;
2017-04-23 14:39:19 +02:00
use Amp\Loop;
use Amp\PHPUnit\TestException;
use Amp\Promise;
use Amp\Success;
use function React\Promise\resolve;
2016-07-12 18:20:06 +02:00
class WaitTest extends BaseTest
2018-06-18 20:00:01 +02: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
$result = Promise\wait($promise);
2016-07-12 18:20:06 +02:00
$this->assertSame($value, $result);
}
2018-06-18 20:00:01 +02: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 {
$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
*/
2018-06-18 20:00:01 +02:00
public function testWaitOnPendingPromise()
{
Loop::run(function () {
2016-07-12 18:20:06 +02:00
$value = 1;
$promise = new Delayed(100, $value);
2016-07-12 18:20:06 +02: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
*/
2018-06-18 20:00:01 +02: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
}
/**
* @expectedException \Error
* @expectedExceptionMessage Loop exceptionally stopped without resolving the promise
*/
2018-06-18 20:00:01 +02:00
public function testPromiseWithErrorBeforeResolutionThrowsException()
{
Loop::defer(function () {
throw new TestException;
});
Promise\wait((new Deferred)->promise());
}
/**
* @depends testWaitOnSuccessfulPromise
*/
2018-06-18 20:00:01 +02:00
public function testReactPromise()
{
$value = 1;
$promise = resolve($value);
$result = Promise\wait($promise);
$this->assertSame($value, $result);
}
2017-03-14 22:15:36 +01:00
2018-06-18 20:00:01 +02:00
public function testNonPromise()
{
2017-04-23 19:08:40 +02:00
$this->expectException(\TypeError::class);
Promise\wait(42);
2017-03-14 22:15:36 +01:00
}
2016-07-12 18:20:06 +02:00
}