2020-03-01 11:49:22 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Psl\Tests\Asio;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-03-01 21:52:25 +01:00
|
|
|
use Psl\Asio;
|
2020-03-01 11:49:22 +01:00
|
|
|
use Psl\Exception\InvariantViolationException;
|
|
|
|
|
|
|
|
class WrapTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testWrapException(): void
|
|
|
|
{
|
|
|
|
$exception = new \Exception('foo');
|
2020-03-01 21:52:25 +01:00
|
|
|
$wrapper = Asio\wrap(static function () use ($exception): void {
|
2020-03-01 11:49:22 +01:00
|
|
|
throw $exception;
|
|
|
|
});
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertFalse($wrapper->isSucceeded());
|
|
|
|
self::assertTrue($wrapper->isFailed());
|
|
|
|
self::assertSame($exception, $wrapper->getException());
|
2020-03-01 11:49:22 +01:00
|
|
|
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
|
|
|
|
$wrapper->getResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testWrapResult(): void
|
|
|
|
{
|
2020-03-01 21:52:25 +01:00
|
|
|
$wrapper = Asio\wrap(static function (): string {
|
2020-03-01 11:49:22 +01:00
|
|
|
return 'foo';
|
|
|
|
});
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertTrue($wrapper->isSucceeded());
|
|
|
|
self::assertFalse($wrapper->isFailed());
|
|
|
|
self::assertSame('foo', $wrapper->getResult());
|
2020-03-01 11:49:22 +01:00
|
|
|
|
|
|
|
$this->expectException(InvariantViolationException::class);
|
|
|
|
$this->expectExceptionMessage('No exception thrown');
|
|
|
|
|
|
|
|
$wrapper->getException();
|
|
|
|
}
|
|
|
|
}
|