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-01 13:19:52 +02:00
|
|
|
use Exception;
|
2020-03-01 11:49:22 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2020-10-01 13:19:52 +02:00
|
|
|
use Psl\Fun;
|
2020-09-29 15:54:27 +02:00
|
|
|
use Psl\Result\Failure;
|
2020-03-01 11:49:22 +01:00
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
final class FailureTest extends TestCase
|
2020-03-01 11:49:22 +01:00
|
|
|
{
|
|
|
|
public function testIsSucceeded(): void
|
|
|
|
{
|
2020-10-01 13:19:52 +02:00
|
|
|
$wrapper = new Failure(new Exception('foo'));
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertFalse($wrapper->isSucceeded());
|
2020-03-01 11:49:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsFailed(): void
|
|
|
|
{
|
2020-10-01 13:19:52 +02:00
|
|
|
$wrapper = new Failure(new Exception('foo'));
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertTrue($wrapper->isFailed());
|
2020-03-01 11:49:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetResult(): void
|
|
|
|
{
|
2020-10-01 13:19:52 +02:00
|
|
|
$exception = new Exception('bar');
|
2020-09-29 15:54:27 +02:00
|
|
|
$wrapper = new Failure($exception);
|
2020-03-01 11:49:22 +01:00
|
|
|
|
|
|
|
$this->expectExceptionObject($exception);
|
|
|
|
$wrapper->getResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetException(): void
|
|
|
|
{
|
2020-10-01 13:19:52 +02:00
|
|
|
$exception = new Exception('bar');
|
2020-09-29 15:54:27 +02:00
|
|
|
$wrapper = new Failure($exception);
|
2020-09-05 17:23:37 +02:00
|
|
|
$e = $wrapper->getException();
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame($exception, $e);
|
2020-03-01 11:49:22 +01:00
|
|
|
}
|
2020-10-01 13:19:52 +02:00
|
|
|
|
|
|
|
public function testProceed(): void
|
|
|
|
{
|
|
|
|
$exception = new Exception('bar');
|
|
|
|
$wrapper = new Failure($exception);
|
|
|
|
$actual = $wrapper->proceed(
|
|
|
|
static fn (string $result): int => 200,
|
|
|
|
static fn (Exception $exception): int => 404
|
|
|
|
);
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertSame(404, $actual);
|
2020-10-01 13:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testThenToSuccess(): void
|
|
|
|
{
|
|
|
|
$exception = new Exception('bar');
|
|
|
|
$wrapper = new Failure($exception);
|
|
|
|
$actual = $wrapper->then(
|
|
|
|
static function () {
|
2020-10-15 10:18:03 +02:00
|
|
|
throw new Exception('Dont call us, we\'ll call you!');
|
2020-10-01 13:19:52 +02:00
|
|
|
},
|
|
|
|
static fn (Exception $exception): string => $exception->getMessage()
|
|
|
|
);
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertTrue($actual->isSucceeded());
|
|
|
|
static::assertSame($actual->getResult(), 'bar');
|
2020-10-01 13:19:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testThenToFailure(): void
|
|
|
|
{
|
|
|
|
$exception = new Exception('bar');
|
|
|
|
$wrapper = new Failure($exception);
|
|
|
|
$actual = $wrapper->then(
|
|
|
|
static function () {
|
2020-10-15 10:18:03 +02:00
|
|
|
throw new Exception('Dont call us, we\'ll call you!');
|
2020-10-01 13:19:52 +02:00
|
|
|
},
|
|
|
|
Fun\rethrow()
|
|
|
|
);
|
|
|
|
|
2020-10-15 10:18:03 +02:00
|
|
|
static::assertFalse($actual->isSucceeded());
|
|
|
|
static::assertSame($actual->getException(), $exception);
|
2020-10-01 13:19:52 +02:00
|
|
|
}
|
2020-03-01 11:49:22 +01:00
|
|
|
}
|