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

82 lines
2.0 KiB
PHP
Raw Normal View History

<?php
2020-03-01 21:52:25 +01:00
declare(strict_types=1);
2020-09-29 15:54:27 +02:00
namespace Psl\Tests\Result;
use Exception;
use PHPUnit\Framework\TestCase;
use Psl\Exception\InvariantViolationException;
use Psl\Fun;
2020-09-29 15:54:27 +02:00
use Psl\Result\Success;
final class SuccessTest extends TestCase
{
public function testIsSucceeded(): void
{
2020-09-29 15:54:27 +02:00
$wrapper = new Success('hello');
static::assertTrue($wrapper->isSucceeded());
}
public function testIsFailed(): void
{
2020-09-29 15:54:27 +02:00
$wrapper = new Success('hello');
static::assertFalse($wrapper->isFailed());
}
public function testGetResult(): void
{
2020-09-29 15:54:27 +02:00
$wrapper = new Success('hello');
static::assertSame('hello', $wrapper->getResult());
}
public function testGetException(): void
{
2020-09-29 15:54:27 +02:00
$wrapper = new Success('hello');
$this->expectException(InvariantViolationException::class);
$this->expectExceptionMessage('No exception thrown');
$wrapper->getException();
}
public function testProceed(): void
{
$wrapper = new Success('hello');
$actual = $wrapper->proceed(
static fn (string $result): int => 200,
static fn (Exception $exception): int => 404
);
static::assertSame(200, $actual);
}
public function testThenToSuccess(): void
{
$wrapper = new Success('hello');
$actual = $wrapper->then(
Fun\identity(),
Fun\rethrow()
);
static::assertNotSame($wrapper, $actual);
static::assertTrue($actual->isSucceeded());
static::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()
);
static::assertFalse($actual->isSucceeded());
static::assertSame($actual->getException(), $exception);
}
}