1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/test/FirstTest.php
Aaron Piotrowski 7612ef3f1e
Drop direct conversion of React promises
React promises are still supported using Amp\Promise\adapt().
2020-10-30 10:19:12 -05:00

73 lines
1.8 KiB
PHP

<?php
namespace Amp\Test;
use Amp\Delayed;
use Amp\Failure;
use Amp\MultiReasonException;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Promise;
use Amp\Success;
use function Amp\await;
class FirstTest extends AsyncTestCase
{
public function testEmptyArray(): void
{
$this->expectException(\Error::class);
$this->expectExceptionMessage("No promises provided");
Promise\first([]);
}
public function testSuccessfulPromisesArray(): void
{
$promises = [new Success(1), new Success(2), new Success(3)];
$this->assertSame(1, await(Promise\first($promises)));
}
public function testFailedPromisesArray(): void
{
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Failure($exception)];
try {
await(Promise\first($promises));
} catch (MultiReasonException $reason) {
$this->assertSame([$exception, $exception, $exception], $reason->getReasons());
return;
}
$this->fail("Promise was not failed");
}
public function testMixedPromisesArray(): void
{
$exception = new \Exception;
$promises = [new Failure($exception), new Failure($exception), new Success(3)];
$this->assertSame(3, await(Promise\first($promises)));
}
public function testPendingPromiseArray(): void
{
$promises = [
new Delayed(20, 1),
new Delayed(30, 2),
new Delayed(10, 3),
];
$this->assertSame(3, await(Promise\first($promises)));
await(Promise\all($promises)); // Clear event loop.
}
public function testNonPromise(): void
{
$this->expectException(\TypeError::class);
Promise\first([1]);
}
}