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

43 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 PHPUnit\Framework\TestCase;
2020-09-29 15:54:27 +02:00
use Psl\Result;
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 {
throw $exception;
});
self::assertFalse($wrapper->isSucceeded());
self::assertTrue($wrapper->isFailed());
self::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';
});
self::assertTrue($wrapper->isSucceeded());
self::assertFalse($wrapper->isFailed());
self::assertSame('foo', $wrapper->getResult());
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('No exception thrown');
$wrapper->getException();
}
}