2020-03-01 11:49:22 +01:00
|
|
|
<?php
|
|
|
|
|
2020-03-01 21:52:25 +01:00
|
|
|
declare(strict_types=1);
|
2020-03-01 11:49:22 +01:00
|
|
|
|
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\Success;
|
2020-03-01 11:49:22 +01:00
|
|
|
use Psl\Exception\InvariantViolationException;
|
2020-10-01 13:19:52 +02:00
|
|
|
use stdClass;
|
2020-03-01 11:49:22 +01:00
|
|
|
|
2020-09-29 15:54:27 +02:00
|
|
|
class SuccessTest extends TestCase
|
2020-03-01 11:49:22 +01:00
|
|
|
{
|
|
|
|
public function testIsSucceeded(): void
|
|
|
|
{
|
2020-09-29 15:54:27 +02:00
|
|
|
$wrapper = new Success('hello');
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertTrue($wrapper->isSucceeded());
|
2020-03-01 11:49:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsFailed(): void
|
|
|
|
{
|
2020-09-29 15:54:27 +02:00
|
|
|
$wrapper = new Success('hello');
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertFalse($wrapper->isFailed());
|
2020-03-01 11:49:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetResult(): void
|
|
|
|
{
|
2020-09-29 15:54:27 +02:00
|
|
|
$wrapper = new Success('hello');
|
2020-07-12 16:51:18 +02:00
|
|
|
self::assertSame('hello', $wrapper->getResult());
|
2020-03-01 11:49:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetException(): void
|
|
|
|
{
|
2020-09-29 15:54:27 +02:00
|
|
|
$wrapper = new Success('hello');
|
2020-03-01 11:49:22 +01:00
|
|
|
|
|
|
|
$this->expectException(InvariantViolationException::class);
|
|
|
|
$this->expectExceptionMessage('No exception thrown');
|
|
|
|
|
|
|
|
$wrapper->getException();
|
|
|
|
}
|
2020-10-01 13:19:52 +02:00
|
|
|
|
|
|
|
public function testProceed(): void
|
|
|
|
{
|
|
|
|
$wrapper = new Success('hello');
|
|
|
|
$actual = $wrapper->proceed(
|
|
|
|
static fn (string $result): int => 200,
|
|
|
|
static fn (Exception $exception): int => 404
|
|
|
|
);
|
|
|
|
|
|
|
|
self::assertSame(200, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThenToSuccess(): void
|
|
|
|
{
|
|
|
|
$wrapper = new Success('hello');
|
|
|
|
$actual = $wrapper->then(
|
|
|
|
Fun\passthrough(),
|
|
|
|
Fun\rethrow()
|
|
|
|
);
|
|
|
|
|
|
|
|
self::assertNotSame($wrapper, $actual);
|
|
|
|
self::assertTrue($actual->isSucceeded());
|
|
|
|
self::assertSame('hello', $actual->getResult());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThenToFailure(): void
|
|
|
|
{
|
|
|
|
$exception = new Exception('bar');
|
|
|
|
$wrapper = new Success('hello');
|
|
|
|
$actual = $wrapper->then(
|
|
|
|
static function () use ($exception) {
|
|
|
|
throw $exception;
|
|
|
|
},
|
|
|
|
Fun\rethrow()
|
|
|
|
);
|
|
|
|
|
|
|
|
self::assertFalse($actual->isSucceeded());
|
|
|
|
self::assertSame($actual->getException(), $exception);
|
|
|
|
}
|
2020-03-01 11:49:22 +01:00
|
|
|
}
|