Skip to content

Commit

Permalink
Cover latest methods
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaudo committed Nov 14, 2023
1 parent 7040bc7 commit 0640c91
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function isOk(): bool
}

/**
* @param Closure(E):void $action
* @param Closure(E):mixed $action
*
* @return $this
*
Expand All @@ -104,7 +104,7 @@ public function onFailure(Closure $action): static
}

/**
* @param Closure(mixed):void $action
* @param Closure(mixed):mixed $action
*
* @return $this
*/
Expand Down Expand Up @@ -134,7 +134,7 @@ public function or(mixed $value): mixed

public function orDie(int|string $status = null): never
{
if (isset($status)) {
if ($status !== null) {
exit($status);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/Resultlike.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function isOk(): bool;
* Performs the given `action` on the encapsulated value if this
* instance is an error. Returns the original instance unchanged.
*
* @param Closure(mixed):void $action
* @param Closure(mixed):mixed $action
*
* @return $this
*/
Expand All @@ -52,7 +52,7 @@ public function onFailure(Closure $action): static;
* Performs the given `action` on the encapsulated value if this
* instance is a success. Returns the original instance unchanged.
*
* @param Closure(mixed):void $action
* @param Closure(mixed):mixed $action
*
* @return $this
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Ok.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function isOk(): bool
}

/**
* @param Closure(Throwable):void $action
* @param Closure(Throwable):mixed $action
*
* @return $this
*/
Expand All @@ -109,7 +109,7 @@ public function onFailure(Closure $action): static
}

/**
* @param Closure(T):void $action
* @param Closure(T):mixed $action
*
* @return $this
*
Expand Down
12 changes: 12 additions & 0 deletions tests/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ function (): void {
$this->emptyError->onFailure(fn() => throw new Exception());
}
);
self::assertSame(
$this->emptyError,
$this->emptyError->onFailure(function (): void {})
);

self::assertSame(
$this->errorWithException,
Expand All @@ -234,6 +238,10 @@ function (): void {
$this->errorWithException->onFailure(fn() => throw new Exception());
}
);
self::assertSame(
$this->errorWithException,
$this->errorWithException->onFailure(fn() => 42)
);

self::assertSame(
$this->errorWithMessage,
Expand All @@ -245,5 +253,9 @@ function (): void {
$this->errorWithMessage->onFailure(fn() => throw new Exception());
}
);
self::assertSame(
$this->errorWithMessage,
$this->errorWithMessage->onFailure(fn() => 'Smeagol')
);
}
}
11 changes: 11 additions & 0 deletions tests/OkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public function testBooleanOperations(): void
self::assertNull($this->emptyOk->orFail());
self::assertSame(42, $this->okWithValue->orFail());

self::assertNull($this->emptyOk->orDie());
self::assertSame(42, $this->okWithValue->orDie(1));

self::assertNull($this->emptyOk->orThrow(new UnexpectedValueException()));
self::assertSame(42, $this->okWithValue->orThrow(new UnexpectedValueException()));

Expand Down Expand Up @@ -149,6 +152,10 @@ function (): void {
$this->emptyOk,
$this->emptyOk->onFailure(fn() => throw new Exception())
);
self::assertSame(
$this->emptyOk,
$this->emptyOk->onSuccess(function (): void {})
);

self::assertException(
Exception::class,
Expand All @@ -160,5 +167,9 @@ function (): void {
$this->okWithValue,
$this->okWithValue->onFailure(fn() => throw new Exception())
);
self::assertSame(
$this->okWithValue,
$this->okWithValue->onSuccess(fn() => 42)
);
}
}

0 comments on commit 0640c91

Please sign in to comment.