1
0
mirror of https://github.com/danog/amp.git synced 2024-12-02 17:37:50 +01:00
amp/test/Future/AnyTest.php
Aaron Piotrowski 7e30ee0c2c
Import Future
Co-authored-by: Niklas Keller <me@kelunik.com>
2021-08-29 12:18:05 -05:00

51 lines
1.4 KiB
PHP

<?php
namespace Amp\Test\Future;
use Amp\CompositeException;
use Amp\Deferred;
use Amp\Future;
use PHPUnit\Framework\TestCase;
use function Amp\Future\any;
class AnyTest extends TestCase
{
public function testSingleComplete(): void
{
self::assertSame(42, any([Future::complete(42)]));
}
public function testTwoComplete(): void
{
self::assertSame(1, any([Future::complete(1), Future::complete(2)]));
}
public function testTwoFirstPending(): void
{
$deferred = new Deferred();
self::assertSame(2, any([$deferred->getFuture(), Future::complete(2)]));
}
public function testTwoFirstThrowing(): void
{
self::assertSame(2, any([Future::error(new \Exception('foo')), Future::complete(2)]));
}
public function testTwoBothThrowing(): void
{
$this->expectException(CompositeException::class);
$this->expectExceptionMessage('Multiple errors encountered (2); use "Amp\CompositeException::getReasons()" to retrieve the array of exceptions thrown:');
Future\any([Future::error(new \Exception('foo')), Future::error(new \RuntimeException('bar'))]);
}
public function testTwoGeneratorThrows(): void
{
self::assertSame(2, any((static function () {
yield Future::error(new \Exception('foo'));
yield Future::complete(2);
})()));
}
}