Merge pull request #933 from hungthai1401/throws_unless

[2.x] Add `throwsUnless`
This commit is contained in:
Nuno Maduro
2023-10-17 10:03:16 +01:00
committed by GitHub
2 changed files with 52 additions and 0 deletions

View File

@ -105,6 +105,24 @@ final class TestCall
return $this;
}
/**
* Asserts that the test throws the given `$exceptionClass` when called if the given condition is false.
*
* @param (callable(): bool)|bool $condition
*/
public function throwsUnless(callable|bool $condition, string|int $exception, string $exceptionMessage = null, int $exceptionCode = null): self
{
$condition = is_callable($condition)
? $condition
: static fn (): bool => $condition;
if (! $condition()) {
return $this->throws($exception, $exceptionMessage, $exceptionCode);
}
return $this;
}
/**
* Runs the current test multiple times with
* each item of the given `iterable`.