mirror of
https://github.com/danog/endtoend-test-psl.git
synced 2025-01-21 21:01:17 +01:00
[Result] Rename classes
This commit is contained in:
parent
40a4574e9d
commit
26581d1ecd
@ -1,27 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Asio;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Wrap the given callable result in a `WrappedResult`, or `WrappedException` if the callable throws
|
||||
* an `Exception`.
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @psalm-param (callable(): T) $fun
|
||||
*
|
||||
* @psalm-return IResultOrExceptionWrapper<T>
|
||||
*/
|
||||
function wrap(callable $fun): IResultOrExceptionWrapper
|
||||
{
|
||||
try {
|
||||
$result = $fun();
|
||||
return new WrappedResult($result);
|
||||
} catch (Exception $e) {
|
||||
return new WrappedException($e);
|
||||
}
|
||||
}
|
@ -97,7 +97,6 @@ final class Loader
|
||||
'Psl\Arr\map_with_key',
|
||||
'Psl\Fun\after',
|
||||
'Psl\Fun\pipe',
|
||||
'Psl\Asio\wrap',
|
||||
'Psl\Internal\boolean',
|
||||
'Psl\Internal\type',
|
||||
'Psl\Internal\validate_offset',
|
||||
@ -179,6 +178,7 @@ final class Loader
|
||||
'Psl\Math\sum_floats',
|
||||
'Psl\Math\tan',
|
||||
'Psl\Math\to_base',
|
||||
'Psl\Result\wrap',
|
||||
'Psl\SecureRandom\bytes',
|
||||
'Psl\SecureRandom\float',
|
||||
'Psl\SecureRandom\int',
|
||||
@ -327,7 +327,6 @@ final class Loader
|
||||
|
||||
public const INTERFACES = [
|
||||
'Psl\Exception\ExceptionInterface',
|
||||
'Psl\Asio\IResultOrExceptionWrapper',
|
||||
'Psl\Collection\CollectionInterface',
|
||||
'Psl\Collection\IndexAccessInterface',
|
||||
'Psl\Collection\MutableCollectionInterface',
|
||||
@ -340,6 +339,7 @@ final class Loader
|
||||
'Psl\Collection\MutableMapInterface',
|
||||
'Psl\Observer\SubjectInterface',
|
||||
'Psl\Observer\ObserverInterface',
|
||||
'Psl\Result\ResultInterface',
|
||||
];
|
||||
|
||||
public const TRAITS = [
|
||||
@ -353,11 +353,11 @@ final class Loader
|
||||
'Psl\Collection\MutableVector',
|
||||
'Psl\Collection\Map',
|
||||
'Psl\Collection\MutableMap',
|
||||
'Psl\Asio\WrappedException',
|
||||
'Psl\Asio\WrappedResult',
|
||||
'Psl\Exception\InvalidArgumentException',
|
||||
'Psl\Exception\RuntimeException',
|
||||
'Psl\Exception\InvariantViolationException',
|
||||
'Psl\Result\Failure',
|
||||
'Psl\Result\Success',
|
||||
'Psl\Type\Internal\ArrayKeyType',
|
||||
'Psl\Type\Internal\ArrayType',
|
||||
'Psl\Type\Internal\BoolType',
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Asio;
|
||||
namespace Psl\Result;
|
||||
|
||||
use Exception;
|
||||
use Psl;
|
||||
@ -13,9 +13,9 @@ use Psl;
|
||||
* @template T
|
||||
* @template Te of Exception
|
||||
*
|
||||
* @implements IResultOrExceptionWrapper<T>
|
||||
* @implements ResultInterface<T>
|
||||
*/
|
||||
final class WrappedException implements IResultOrExceptionWrapper
|
||||
final class Failure implements ResultInterface
|
||||
{
|
||||
/**
|
||||
* @psalm-var Te
|
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Asio;
|
||||
namespace Psl\Result;
|
||||
|
||||
use Exception;
|
||||
use Psl;
|
||||
@ -11,12 +11,12 @@ use Psl;
|
||||
* Represents a result of operation that either has a successful result or the exception object if
|
||||
* that operation failed.
|
||||
*
|
||||
* This is an interface. You get generally `IResultOrExceptionWrapper<T>` by calling `wrap<T>()`, passing in
|
||||
* the `callable(): T`, and a `WrappedResult<T>` or `WrappedException<Te>` is returned.
|
||||
* This is an interface. You get generally `ResultInterface<T>` by calling `tryResultFrom<T>()`, passing in
|
||||
* the `callable(): T`, and a `Success<T>` or `Failure<Te>` is returned.
|
||||
*
|
||||
* @template T
|
||||
*/
|
||||
interface IResultOrExceptionWrapper
|
||||
interface ResultInterface
|
||||
{
|
||||
/**
|
||||
* Return the result of the operation, or throw underlying exception.
|
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Asio;
|
||||
namespace Psl\Result;
|
||||
|
||||
use Exception;
|
||||
use Psl;
|
||||
@ -12,9 +12,9 @@ use Psl;
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @implements IResultOrExceptionWrapper<T>
|
||||
* @implements ResultInterface<T>
|
||||
*/
|
||||
final class WrappedResult implements IResultOrExceptionWrapper
|
||||
final class Success implements ResultInterface
|
||||
{
|
||||
/**
|
||||
* @psalm-var T
|
27
src/Psl/Result/wrap.php
Normal file
27
src/Psl/Result/wrap.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Result;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Wrap the given callable result in a `Success`, or `Failure` if the callable throws
|
||||
* an `Exception`.
|
||||
*
|
||||
* @template T
|
||||
*
|
||||
* @psalm-param (callable(): T) $fun
|
||||
*
|
||||
* @psalm-return ResultInterface<T>
|
||||
*/
|
||||
function wrap(callable $fun): ResultInterface
|
||||
{
|
||||
try {
|
||||
$result = $fun();
|
||||
return new Success($result);
|
||||
} catch (Exception $e) {
|
||||
return new Failure($e);
|
||||
}
|
||||
}
|
@ -2,29 +2,29 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Tests\Asio;
|
||||
namespace Psl\Tests\Result;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Asio\WrappedException;
|
||||
use Psl\Result\Failure;
|
||||
|
||||
class WrappedExceptionTest extends TestCase
|
||||
class FailureTest extends TestCase
|
||||
{
|
||||
public function testIsSucceeded(): void
|
||||
{
|
||||
$wrapper = new WrappedException(new \Exception('foo'));
|
||||
$wrapper = new Failure(new \Exception('foo'));
|
||||
self::assertFalse($wrapper->isSucceeded());
|
||||
}
|
||||
|
||||
public function testIsFailed(): void
|
||||
{
|
||||
$wrapper = new WrappedException(new \Exception('foo'));
|
||||
$wrapper = new Failure(new \Exception('foo'));
|
||||
self::assertTrue($wrapper->isFailed());
|
||||
}
|
||||
|
||||
public function testGetResult(): void
|
||||
{
|
||||
$exception = new \Exception('bar');
|
||||
$wrapper = new WrappedException($exception);
|
||||
$wrapper = new Failure($exception);
|
||||
|
||||
$this->expectExceptionObject($exception);
|
||||
$wrapper->getResult();
|
||||
@ -33,7 +33,7 @@ class WrappedExceptionTest extends TestCase
|
||||
public function testGetException(): void
|
||||
{
|
||||
$exception = new \Exception('bar');
|
||||
$wrapper = new WrappedException($exception);
|
||||
$wrapper = new Failure($exception);
|
||||
$e = $wrapper->getException();
|
||||
self::assertSame($exception, $e);
|
||||
}
|
@ -2,35 +2,35 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Tests\Asio;
|
||||
namespace Psl\Tests\Result;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Asio\WrappedResult;
|
||||
use Psl\Result\Success;
|
||||
use Psl\Exception\InvariantViolationException;
|
||||
|
||||
class WrappedResultTest extends TestCase
|
||||
class SuccessTest extends TestCase
|
||||
{
|
||||
public function testIsSucceeded(): void
|
||||
{
|
||||
$wrapper = new WrappedResult('hello');
|
||||
$wrapper = new Success('hello');
|
||||
self::assertTrue($wrapper->isSucceeded());
|
||||
}
|
||||
|
||||
public function testIsFailed(): void
|
||||
{
|
||||
$wrapper = new WrappedResult('hello');
|
||||
$wrapper = new Success('hello');
|
||||
self::assertFalse($wrapper->isFailed());
|
||||
}
|
||||
|
||||
public function testGetResult(): void
|
||||
{
|
||||
$wrapper = new WrappedResult('hello');
|
||||
$wrapper = new Success('hello');
|
||||
self::assertSame('hello', $wrapper->getResult());
|
||||
}
|
||||
|
||||
public function testGetException(): void
|
||||
{
|
||||
$wrapper = new WrappedResult('hello');
|
||||
$wrapper = new Success('hello');
|
||||
|
||||
$this->expectException(InvariantViolationException::class);
|
||||
$this->expectExceptionMessage('No exception thrown');
|
@ -2,10 +2,10 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Psl\Tests\Asio;
|
||||
namespace Psl\Tests\Result;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psl\Asio;
|
||||
use Psl\Result;
|
||||
use Psl\Exception\InvariantViolationException;
|
||||
|
||||
class WrapTest extends TestCase
|
||||
@ -13,7 +13,7 @@ class WrapTest extends TestCase
|
||||
public function testWrapException(): void
|
||||
{
|
||||
$exception = new \Exception('foo');
|
||||
$wrapper = Asio\wrap(static function () use ($exception): void {
|
||||
$wrapper = Result\wrap(static function () use ($exception): void {
|
||||
throw $exception;
|
||||
});
|
||||
self::assertFalse($wrapper->isSucceeded());
|
||||
@ -27,7 +27,7 @@ class WrapTest extends TestCase
|
||||
|
||||
public function testWrapResult(): void
|
||||
{
|
||||
$wrapper = Asio\wrap(static function (): string {
|
||||
$wrapper = Result\wrap(static function (): string {
|
||||
return 'foo';
|
||||
});
|
||||
self::assertTrue($wrapper->isSucceeded());
|
Loading…
x
Reference in New Issue
Block a user