endtoend-test-psl/tests/Psl/Result/WrapTest.php

44 lines
1.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-09-29 15:54:27 +02:00
namespace Psl\Tests\Result;
use Exception;
use PHPUnit\Framework\TestCase;
use Psl\Exception\InvariantViolationException;
use Psl\Result;
final class WrapTest extends TestCase
{
public function testWrapException(): void
{
$exception = new Exception('foo');
2020-09-29 15:54:27 +02:00
$wrapper = Result\wrap(static function () use ($exception): void {
throw $exception;
});
static::assertFalse($wrapper->isSucceeded());
static::assertTrue($wrapper->isFailed());
static::assertSame($exception, $wrapper->getException());
$this->expectExceptionObject($exception);
$wrapper->getResult();
}
public function testWrapResult(): void
{
2020-09-29 15:54:27 +02:00
$wrapper = Result\wrap(static function (): string {
return 'foo';
});
static::assertTrue($wrapper->isSucceeded());
static::assertFalse($wrapper->isFailed());
static::assertSame('foo', $wrapper->getResult());
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('No exception thrown');
$wrapper->getException();
}
}