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
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-09-29 15:54:27 +02:00
|
|
|
use Psl\Result;
|
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-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-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-09-29 15:54:27 +02:00
|
|
|
$wrapper = Result\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();
|
|
|
|
}
|
|
|
|
}
|