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