2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-16 00:28:22 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\MultiReasonException;
|
2020-09-28 05:19:52 +02:00
|
|
|
use Amp\PHPUnit\AsyncTestCase;
|
2017-03-15 17:12:49 +01:00
|
|
|
use Amp\Promise;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Success;
|
2020-09-28 05:19:52 +02:00
|
|
|
use function Amp\await;
|
|
|
|
use function React\Promise\resolve;
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
class FirstTest extends AsyncTestCase
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testEmptyArray(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->expectException(\Error::class);
|
|
|
|
$this->expectExceptionMessage("No promises provided");
|
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
Promise\first([]);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testSuccessfulPromisesArray(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 00:28:22 +01:00
|
|
|
$promises = [new Success(1), new Success(2), new Success(3)];
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertSame(1, await(Promise\first($promises)));
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testFailedPromisesArray(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 00:28:22 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
try {
|
|
|
|
await(Promise\first($promises));
|
|
|
|
} catch (MultiReasonException $reason) {
|
|
|
|
$this->assertSame([$exception, $exception, $exception], $reason->getReasons());
|
|
|
|
return;
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->fail("Promise was not failed");
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testMixedPromisesArray(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-12-16 00:28:22 +01:00
|
|
|
$exception = new \Exception;
|
|
|
|
$promises = [new Failure($exception), new Failure($exception), new Success(3)];
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertSame(3, await(Promise\first($promises)));
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testReactPromiseArray(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$promises = [resolve(1), resolve(2), new Success(3)];
|
2017-05-02 18:59:52 +02:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertSame(1, await(Promise\first($promises)));
|
2017-05-02 18:59:52 +02:00
|
|
|
}
|
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testPendingPromiseArray(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$promises = [
|
|
|
|
new Delayed(20, 1),
|
|
|
|
new Delayed(30, 2),
|
|
|
|
new Delayed(10, 3),
|
|
|
|
];
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->assertSame(3, await(Promise\first($promises)));
|
2016-12-16 00:28:22 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
await(Promise\all($promises)); // Clear event loop.
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2020-09-28 05:19:52 +02:00
|
|
|
public function testNonPromise(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-09-28 05:19:52 +02:00
|
|
|
$this->expectException(\TypeError::class);
|
|
|
|
|
2017-03-15 17:12:49 +01:00
|
|
|
Promise\first([1]);
|
2016-12-16 00:28:22 +01:00
|
|
|
}
|
|
|
|
}
|